Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multivariable linearization in python: 'Pow' object has no attribute 'sqrt'

Tags:

python

numpy

As a newcomer to Python world, I'm just simply about to linearize the following two variable function:

function

enter image description here

using the fairly routine Newton method:

linearization method

enter image description here

Here is what I've tried so far:

import numpy as np
import math
from sympy import symbols, diff

d = 1.7

def f(arg1, arg2):
    return (arg1 - arg2)/(np.power(np.linalg.norm(arg1 - arg2),2) - np.power(d,2))

def linearize_f(f, arg1, arg2, equi_arg1, equi_arg2):
    arg1, arg2 = symbols('arg1 arg2', real=True)
    der_1 = diff(f(arg1,arg2), arg1)
    der_2 = diff(f(arg1,arg2), arg2)
    constant_term = f(equi_arg1, equi_arg2)

    vars = sympy.symbols('arg1, arg2')
    par_term_1 = sympy.evalf(der_1, subs = dict(zip(vars,[equi_arg1, equi_arg2])))
    par_term_2 = sympy.evalf(der_2, subs = dict(zip(vars,[equi_arg1, equi_arg2])))

    result = constant_term + par_term_1*(arg1-equi_arg1) + par_term_2*(arg2-equi_arg2)

    return result

q0, q1 = symbols('q0 q1', real=True)
result = linearize_f(f,q0,q1,0,0)
print(result)

The interpreter returns a 'Pow' object has no attribute 'sqrt'. However, I've never used any sqrt in my code.

Would you please help me to resolve the case?

like image 283
Pinton Avatar asked Nov 23 '18 14:11

Pinton


1 Answers

You have not called sqrt but np.linalg.norm has. The arg1, arg2 arguments are of type sympy.Symbol. The function expects to get an array-like argument. However, it gets a sympy symbol, which it does not know how to handle.

I looked in the np.linalg source code, and it seems that it checks for some known types and tries to find the square root. Otherwise, it relies on the argument itself to know its own square root. sympy.Symbol has no such thing, and hence the error.

There is no way to avoid this. numpy works with numbers, sympy works with (its own) symbols. You are not supposed to mix them. Most likely sympy will have its own functions for handling its own symbols, but, if not, you are out of luck, unless you add them yourself.

like image 146
blue_note Avatar answered Oct 14 '22 04:10

blue_note