Skip to content

Conversation

@bCosgrovePayne
Copy link

Added function to FindRoots class for polynomials with complex coefficients, based on existing .Roots() function in Polynomial class.

I hope I did this right!

…cients, based on existing .Roots() function in Polynomial class
/// <summary>
/// Find all roots of a polynomial with complex coefficients by calculating the characteristic polynomial of the companion matrix
/// </summary>
/// <param name="coefficients">The coefficients of the polynomial in ascending order, e.g. new Complex[] {new (5, 0), new (0, 2) new (2,3)} = "5 + 2i x^1 + (2 + 3i) x^2"</param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new (0, 2) new (2,3) -> new (0, 2), new (2,3) (a comma is missing)

int n = coefficients.Length;
if (n < 2)
{
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to return an empty array/list instead of null?

Copy link
Member

@jkalias jkalias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add some unit tests covering various tests cases?

@bCosgrovePayne
Copy link
Author

Can you please add some unit tests covering various tests cases?

I'll get on that today and try my best! Sorry for any inconvenience, this is my first real pull request.

…tead of null. Added unit test for the function.
@jkalias
Copy link
Member

jkalias commented Dec 25, 2025

Can you please add some unit tests covering various tests cases?

I'll get on that today and try my best! Sorry for any inconvenience, this is my first real pull request.

No worries, we all start somewhere :)

DenseMatrix A = new DenseMatrix(n);

A.SetSubMatrix(1, 0, A0);
A.SetRow(0, p.Reverse().ToArray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the last column, based on this wiki article?
https://en.wikipedia.org/wiki/Companion_matrix

Unless you decide to set the submatrix from the second column and first row. Or maybe I am confused :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to be entirely honest, I don't know the theory. This is based entirely on the existing .Roots() property in the Polynomial class, which uses Evd to approximate the roots when the polynomial is higher than degree 3, if I'm not mistaken. I monkeyed around with the complex version of the DenseMatrix class and found out everything still works.

I'll triple check that this is producing accurate roots, but it seemed to pass the unit test w/o issue.

var r1 = FindRoots.Roots( new Complex[] { c1[0] });
Assert.AreEqual(Array.Empty<Complex>(), r1);

var r2 = FindRoots.Roots(new Complex[] { c1[0], c1[1] });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is supposed to be a first order polynomial, right? So we should have a single root, shouldn't we?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So all I know is that when the Polynomial.Roots() property is called and it uses Evd to approximate, it doesn't accept first order polynomials. I wanted to keep everything as close to existing code as possible, but I can definitely add a case for first order polynomials that bypasses the Evd approximation. I could also include quadratic and cubic cases if that's desired.

}

[Test]
public void RootsTest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate the test cases in separate test functions. This way it's much easier to follow what the edge cases are and what the normal/expected behavior is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I'll look around the existing test code more for good examples of what to emulate. I have an idea of what you mean, but it doesn't hurt to keep things as similar to the existing library as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants