I / O shape mismatch when using scipy.optimize.fsolve on two dimensional anonymous function array variable
Here is the source code:
def lambdatest():
F=lambda y: y-np.array([[1,2],[3,4]])
y0=np.array([[3,4],[8,7]])
Y=scipy.optimize.fsolve(F,y0)
return Y
And I am getting the error:
raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument '<lambda>'.
I looked around but didn't seem to get it.
0
source to share
1 answer
F
(argument func
fsolve
) must return either a scalar or a one-dimensional array. fsolve
does not handle large arrays.
What you can do is flatten the 2d array in the 1d array using the method ravel()
, and then reshape the solution returned fsolve
in the 2d array:
def lambdatest():
F = lambda y: y - np.array([[1,2],[3,4]]).ravel()
y0 = np.array([[3,4],[8,7]])
Y = scipy.optimize.fsolve(F, y0.ravel()).reshape(y0.shape)
return Y
Here's the result:
>>> lambdatest()
array([[ 1., 2.],
[ 3., 4.]])
0
source to share