Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error solving Matrix equation with numpy

I'm writing my own Newton-Raphson algorithm in Python using sympy and numpy.

The code is below but you can ignore this and skip on to the error:

CODE

def newtonRhapson(fncList, varz, x0):

    jacob = []

    for fnc in fncList:
        vec = []
        for var in varz:
            res = fnc.diff(var)
            for i in range(len(varz)):
                res = res.subs(varz[i], x0[i])
            vec.append(res)
        jacob.append(numpy.array(vec, dtype='float64'))

    fx0=[]

    for fnc in fncList:
        res2 = fnc
        for i in range(len(varz)):
            res2 = res2.subs(varz[i], x0[i])
        fx0.append(res2)

    j = jacob
    f = fx0

    print j
    print ''
    print f

    print numpy.linalg.solve(j,f).tolist()

The arguments to the function are:

fncList - a python list of functions using Sympy symbols

varz - a list containing those symbols (variables)

x0 - initial guess

ERROR

Up until the point where we print j and f it works fine and prints the following:

[array([-9.13378682, -5.91269838]), array([ 4.84401379,  1.01980286])]

[-5.15598620617611, 5.13378681611922]

when I run:

newtonRhapson([5*cos(a)+6*cos(a+b)-10, 5*sin(a)+6*sin(a+b)-4], [a,b], [0.7,0.7])

But on running the line:

print numpy.linalg.solve(j,f).tolist()

I get the error:

File "/Users/me/anaconda/lib/python2.7/site-  packages/numpy/linalg/linalg.py", line 384, in solve
r = gufunc(a, b, signature=signature, extobj=extobj)
TypeError: No loop matching the specified signature and casting was found for ufunc solve1
like image 258
Greg Peckory Avatar asked Apr 18 '16 08:04

Greg Peckory


People also ask

How do you solve a matrix equation in Python?

To solve a linear matrix equation, use the numpy. linalg. solve() method in Python. The method computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.

Can we use NumPy for manipulating matrices?

NumPy can be used to perform a wide variety of mathematical operations on arrays. It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high-level mathematical functions that operate on these arrays and matrices.

What method does NumPy Linalg solve use?

From the numpy docs: solve is a wrapper for the LAPACK routines dgesv and zgesv, the former being used if a is real-valued, the latter if it is complex-valued. The solution to the system of linear equations is computed using an LU decomposition [R40] with partial pivoting and row interchanges.


1 Answers

Your issue is in your second for loop.

for fnc in fncList:
    res2 = fnc
    for i in range(len(varz)):
        res2 = res2.subs(varz[i], x0[i])
    fx0.append(res2)

When you append to fx0, you need to ensure that you are appending the same type (float64) such that NumPy can compute the determinant of your system with LAPACK (see this answer for more info). You are currently appending <class 'sympy.core.numbers.Float'> - your errror message is telling you that you have an incorrect type signature for usage.

To correct this issue, you can simply append numpy.array with a dtype specification for float64 as you did above

for fnc in fncList:
    res2 = fnc
    for i in range(len(varz)):
        res2 = res2.subs(varz[i], x0[i])
    fx0.append(numpy.array(res2, dtype='float'))
like image 70
miradulo Avatar answered Sep 21 '22 16:09

miradulo