Sympy function as Matrix

Viewed 57

is there a easy way to define a sympy function with multiple outputs. I want to have something like this (currently I am construction it with a loop but it seems stupid):

f1 = Function('f1')(x)
f2 = Function('f2')(x)
...
fn = Function('fn')(x)

f = Matrix([f1,f2 ..., fn])

If there is an implementation to construct this I would be glad to hear.

2 Answers

To do as you have done requires a loop somehow. Here is one way:

>>> f = [Function('f%i' % i)(x) for i in range(5)]; f
[f0(x), f1(x), f2(x), f3(x), f4(x)]

And here is another:

>>> f = [i(x) for i in symbols('f:5', cls=Function)]; f
[f0(x), f1(x), f2(x), f3(x), f4(x)]

(Wrap f with Matrix if you want...but what do you want to do with this list of functions?)

You might be looking for FunctionMatrix, which works just like Function but maps matrix coordinates to a matrix.

See its documentation. Example from the docstring:

>>> from sympy import Function
>>> f = Function('f')
>>> X = FunctionMatrix(3, 3, f)
>>> X.as_explicit()
Matrix([
[f(0, 0), f(0, 1), f(0, 2)],
[f(1, 0), f(1, 1), f(1, 2)],
[f(2, 0), f(2, 1), f(2, 2)]])
Related