Integrate SymPy Array

Viewed 151

Not sure where is the best place to ask this question. I am trying to integrate a SymPy Array, but if

sympy.integrate(x, (x, 0, 1))

returns

1/2

as expected,

sympy.integrate(sympy.Array([x]), (x, 0, 1))

returns

1

which is quite troubling. Any hint? Thanks a lot!

1 Answers

I would assume Array is a bit finicky with calculus operations since it seems that inherits from tensors. You would have to do some digging to find out why. The Matrix class however tends to do a little better. When I was doing a bit of vector calculus, I found that using Matrix helped a lot.

For your question we have:

from sympy import *

x = symbols('x')
print(integrate(Matrix([[x]]), (x, 0, 1)))

Which produces as expected:

Matrix([[1/2]])

It also works for larger matrices:

from sympy import *

x = symbols('x')
A = Matrix([[x],
            [x+1],
            [x**2]])  # column vector
print(integrate(A, x))

B = Matrix([[1, x, x**2],
            [-x, 2, 0],
            [sin(x), cos(x), tan(x)]])
print(integrate(B, x))

Producing:

Matrix([[x**2/2], [x**2/2 + x], [x**3/3]])
Matrix([[x, x**2/2, x**3/3], [-x**2/2, 2*x, 0], [-cos(x), sin(x), -log(cos(x))]])

If you really need to integrate tensors, then that math is far out of my range but it seems you can at least differentiate them.

Related