Skip to content

Implement IRootFunctions for Complex #113569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hjrb opened this issue Mar 15, 2025 · 3 comments
Closed

Implement IRootFunctions for Complex #113569

hjrb opened this issue Mar 15, 2025 · 3 comments

Comments

@hjrb
Copy link

hjrb commented Mar 15, 2025

System.Numerics.Complex does not implement the IRootFunctions
The n-th root of a complex number is well defined.
see e http://mathonline.wikidot.com/nth-roots-of-complex-numbers
This would allow to solve a quadratic equation with complex factors a, b, c using generic math.

public static OneOf<(T, T), T, All, None> SolveQuadraticEquation<T>(T a, T b, T c)
		where T: ISignedNumber<T>,  IRootFunctions<T>
	{
		if (T.IsZero(a))
		{
			if (T.IsZero(b)) {
				if (T.IsZero(c)) return new All();
				return new None();
			}
			return -c / b;
		}	
		var d = b * b - T.CreateChecked(4.0) * a * c;
		var a2 = T.CreateChecked(2.0) * a;
		var t = T.Sqrt(d);
		return ((-b+t)/a2,(-b-t)/a2);
	}

As a matter of fact the IRootFunctions is IMHO badly designed. Compute the square root of a double value yields 2 results - not one.
The n-th root of a complex number is a list of n complex numbers.
It would be better to define a new mathematically correct interface IRoots:

public interface IRoots]<TSelf>
        : IFloatingPointConstants<TSelf>
        where TSelf : IRoots<TSelf>?
    {
        /// <summary> Computes the three cube-root of a value.</summary>
        /// <param name="x">The value whose cube-root is to be computed.</param>
        /// <returns>The cube-roots of <paramref name="x" />.</returns>
        static abstract (TSelf,TSelf,TSelf)Cbrt(TSelf x);
 
        /// <summary>Computes the n-th root of a value.</summary>
        /// <param name="x">The value whose <paramref name="n" />-th root is to be computed.</param>
        /// <param name="n">The degree of the root to be computed.</param>
        /// <returns>The <paramref name="n" />-th root of <paramref name="x" />.</returns>
        static abstract IEnumerable<TSelf> RootN(TSelf x, int n);
 
        /// <summary>Computes the square-root of a value.</summary>
        /// <param name="x">The value whose square-root is to be computed.</param>
        /// <returns>The square-root of <paramref name="x" />.</returns>
        static abstract (TSelf,TSelf) Sqrt(TSelf x);
    }
}

I removed the hypotenuse thing - that is not generic at all.

@ghost ghost added the area-System.Numerics label Mar 15, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 15, 2025
@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Mar 16, 2025

Which types would implement IRoots<TSelf>? Would it be only System.Numerics.Complex?

If Double implemented IRoots<Double>, then the implementation of static abstract (TSelf,TSelf,TSelf)Cbrt(TSelf x) there would have to return (Double, Double, Double) rather than (Complex, Complex, Complex), so Cbrt(8.0) would presumably be (2.0, 2.0, 2.0), which doesn't seem right. So I don't think Double could implement it.

@hjrb
Copy link
Author

hjrb commented Mar 17, 2025

double, float, decimal, rationals, all integer types, complex - even defined for symmetric matrix https://en.wikipedia.org/wiki/Square_root_of_a_matrix

@tannergooding
Copy link
Member

There are no plans to implement individual interfaces on Complex at this time.

This should rather be part of a more comprehensive proposal covering Complex<T> where T : IFloatingPointIeee754<T> and a new IComplexNumber<T> where T : IFloatingPointIeee754<T>.

If someone would like to open such a more comprehensive proposal, I'd be happy to take it to API review; otherwise, I expect I'll get to this in .NET 11

@tannergooding tannergooding closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 28, 2025
@github-actions github-actions bot locked and limited conversation to collaborators May 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants