I have a system of equations expressed by sympy:
def test1a(A):
t, tt = sym.symbols('t tt')
return sym.cos(t+tt+A)*A
def test1b(B):
t, tt = sym.symbols('t tt')
return sym.sin(t-tt+B)*B
That I want to convert to a numpy expression before evaluating the result with fsolve()
:
def testprep(variables, A, B):
t, tt = sym.symbols('t tt')
fA = lambdify([(t, tt)], test1a(A))
fB = lambdify([(t, tt)], test1b(B))
t, tt = variables
return [fA,fB]
def testsolve(A, B):
print(so.fsolve(testprep, [-1, 1], args = (A, B)))
return
When ran, I get the following result:
import scipy.optimize as so
import sympy as sym
from sympy.utilities.lambdify import lambdify as lambdify
import numpy as np
def main():
A = 1
B = 2
testsolve(A,B)
return
if __name__ == '__main__':
main()
Output:
error: Result from function call is not a proper array of floats.
As a sanity check, I drafted the same system in terms of numpy expressions and solved it:
def standard(variables, A, B):
t, tt = variables
eq1 = np.cos(t+tt+A)*A
eq2 = np.sin(t-tt+B)*B
return [eq1, eq2]
def solvestandard(A, B):
print(so.fsolve(standard, np.array([-1, 1]), args = (A,B)))
return
With output:
[-0.71460184 1.28539816]
I'm new to lambdify( )
and am not too familiar with the process of converting from sympy to numpy. What would I need to change in order to make the test-case work?