0

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']])
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • A traceback and some partial results would be nice. It looks like there's enough code to run, but the only reason I'd try it would be curiosity. – hpaulj Nov 16 '20 at 16:57
  • I think your understanding about multdimensional functions is wrong. I see mention of a one dimensional function and an example using a list of lambdas. But nothing involving `mpmath.matrix`. The error arises when it tries to convert `x` to a scalar `mpf`. – hpaulj Nov 16 '20 at 20:53
  • @hpaulj I interpret it as a function returning multidimensional values when given multidimensional values. So the return should be a list/array of mpf values instead? – Leon Schmitz Nov 17 '20 at 09:07
  • I haven't used `mpmath` much, so all I know is from reading the docs. – hpaulj Nov 17 '20 at 16:49

0 Answers0