user42961
user42961

Reputation: 93

Sympy: Lambdify function with large array input

I have a problem lambdifing a function that expects a large array. A simplified code replicating the same problem is:

from sympy import *
def fun(x):
    f = []
    for i,x_i in enumerate(x):
        f.append(x_i**i)
    return Matrix(f)
N = 256
x = Matrix([symbols("x_%s"%i) for i in range(N)])
fun_lam = lambdify((x,),fun(x))

which gives the following error:

Traceback (most recent call last):
  File "bin/problem-lambdify.py", line 13, in <module>
    fun_lam = lambdify((x,),fun(x))
  File ".../env/lib/python3.4/site-packages/sympy-1.1rc1-py3.4.egg/sympy/utilities/lambdify.py", line 434, in lambdify
    func = eval(lstr, namespace)
  File "<string>", line 1
SyntaxError: more than 255 arguments

I suppose sympy at some point flattens the arguments and therefore causes this problem.

I can't think of a good way around it.

Upvotes: 1

Views: 474

Answers (2)

Evgeny
Evgeny

Reputation: 11

I suffered a lot, but I found solution. Here is simple example.

import sympy as sym
import numpy as np

A = sym.MatrixSymbol('A', 1, 260)
f = np.prod(A)
func = sym.lambdify(A, f)
aa = np.ones((1, 260))
print(func(aa))

The trick is that you do not create array of symbols, but create a matrix symbol. But I am so looking forward for implementation into Sympy of passing to lambdify just normal array.

Upvotes: 1

asmeurer
asmeurer

Reputation: 91620

Apparently Python 3.7 (which will be released in 2018), will be removing this limitation. I know this doesn't help you right now (unless you want to run on a dev build of Python), but at least there's hope for the future.

Upvotes: 1

Related Questions