SciSharp/Numpy.NET

np.roots() not available yet?

DecemberDream opened this issue · 3 comments

I need to find the roots of a few polynomials, however np.roots() doesn't seem to exist (yet). Could it be that I just can't find it or will it still be implemented?

If it is just focus on calculate answer, you could use a array or list or any other container, then use Math library to do it.
If above prerequisite don't stand, u could do the same way then convert it to NDarray. May use the code following:

var l=new List<doulbe>{3.2, 1.3, 4, 8};
What? That is a method named sqrt inhabit in np class in Numpy namespace. So what you mean np.roots in fact is another name for np.sqrt.

Well, according to the NumPy documentation, np.sqrt returns the non-negative square-root of an array, element-wise while np.roots returns the roots of a polynomial.

An example is this:

import numpy as np

a = np.array([1, 2, -2, -4, 0])

np.sqrt(np.array([1,2,3,4,5,9,16]))
# returns array([1., 1.41421356, 1.73205081, 2., 2.23606798, 3., 4.])

np.sqrt(a)
# returns array([1., 1.41421356, nan, nan, 0. ])

np.roots(a)
# returns array([ 1.41421356, -2., -1.41421356, 0.])
henon commented

I just updated numpy to version 1.26, it now contains np.roots:

        [TestMethod]
        public void IssueByDecemberDream()
        {
            //a = np.array([1, 2, -2, -4, 0])
            //np.roots(a)
            //# returns array([ 1.41421356, -2., -1.41421356, 0.])            
            var a = np.array(new[] { 1, 2, -2, -4, 0 });
            var b = np.roots(a);
            Assert.AreEqual("array([ 1.41421356, -2.        , -1.41421356,  0.        ])", b.repr);
        }