The following code gives the error:
"TypeError: cannot create mpf from matrix(...)"
where the matrix is the initialized vector. I don't understand why it wants to convert this matrix in the first place. To my understanding mpmath
should be able to calculate roots for multi-dimensional functions.
Code:
import mpmath
import numpy as np
def Lgrad2(x,d,n):
a=x[:d]
nij=x[d:]
pi=mpmath.pi
return mpmath.matrix(np.array([Lag(2*k-1,n/2-1,0)+np.sum([a[j]*Lag(2*k-1,n/2-1,4*pi*nij[j]) for j in range(d)]) for k in range(1,2*d+1)]))
def Lag(k,alpha,x):
return mpmath.laguerre(k,alpha,x)
d=2
n=2
nij=mpmath.randmatrix(d,1)*10000
a=mpmath.matrix(np.ones(d))
x=mpmath.matrix(np.hstack([a,nij]))
newt=mpmath.findroot(lambda y: Lgrad2(y,d,n), x0=x)
Traceback:
Traceback (most recent call last):
File "C:/Users/leons/PycharmProjects/pythonProject/venv/Lib/site-packages/mpmath/testquestion.py", line 16, in <module>
newt=mpmath.findroot(lambda y: Lgrad2(y,d,n), x0=x)
File "C:\Users\leons\PycharmProjects\pythonProject\venv\lib\site-packages\mpmath\calculus\optimization.py", line 911, in findroot
x0 = [ctx.convert(x0)]
File "C:\Users\leons\PycharmProjects\pythonProject\venv\lib\site-packages\mpmath\ctx_mp_python.py", line 669, in convert
return ctx._convert_fallback(x, strings)
File "C:\Users\leons\PycharmProjects\pythonProject\venv\lib\site-packages\mpmath\ctx_mp.py", line 634, in _convert_fallback
raise TypeError("cannot create mpf from " + repr(x))
TypeError: cannot create mpf from matrix(
[['1.0'],
['1.0'],
['3575.61862149697117275'],
['4773.95129018594252557']])