New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integral(v,t).doit() differs from integrate(v,t) #10567

Closed
hercidav opened this Issue Feb 11, 2016 · 7 comments

Comments

Projects
None yet
4 participants
@hercidav
Copy link

hercidav commented Feb 11, 2016

Integrating a vector (v=sympy.Matrix([a*t,b,c]) shall give again a vector as a result. Sympy.integrate(v,t) gives the correct answer, however sympy.Integral(v,t) gives a completely different result:

>>> a,b,c = symbols('a b c')
>>> t = symbols('t')
>>> vt = Matrix([a*t,b,c])
>>> integrate(vt,t)
Matrix([
[a*t**2/2],
[     b*t],
[     c*t]])
>>> Integral(vt,t).doit()
a*t**4/3 + b*t**2/2 + c*t

See it also in the sympy online shell:
http://live.sympy.org/?evaluate=a%2Cb%2Cc%3Dsymbols%28%27a%20b%20c%27%29%0A%23--%0Av%3DMatrix%28[a%2Cb%2Cc]%29%0A%23--%0Avt%3DMatrix%28[a*t%2Cb%2Cc]%29%0A%23--%0Aintegrate%28vt%2Ct%29%0A%23--%0AIntegral%28vt%2Ct%29.doit%28%29%0A%23--%

@ashutoshsaboo

This comment has been minimized.

Copy link
Contributor

ashutoshsaboo commented Feb 11, 2016

In which file of the source code or equivalently which module is executed in the 4th and 5th line of the above code? Could you help on that?

@ashutoshsaboo

This comment has been minimized.

Copy link
Contributor

ashutoshsaboo commented Feb 11, 2016

Hi. I managed to find something, that maybe of use for this.

If you see Line No 746 in sympy/integrals.py -
poly = f.as_poly(x)
Now if you edit the code, and try returning that poly variable, this is what is the output for line 4 and 5 of your code, respectively - :

Matrix([[Poly(a*t, t, domain='ZZ[a]')], [b*t], [c*t]])
Poly(a*t*t**2 + b*t + c, t, domain='ZZ[t,a,b,c]')

Now, If you notice the above output, which actually explains the output you get for the 4th and 5th line of your code. This happens because, in the source code, the argument function that is passed to integrate, is only checked if it's a polynomial or not, and it isn't checked for Matrix.

Although I don't understand how the the 1st line of my output (that is above) came, even though when the same functions are executed in case of integral() or Integrate()?

So, could someone help me build on this?

@Shekharrajak Could you help on this? @hercidav @jksuom

@hercidav

This comment has been minimized.

Copy link

hercidav commented Feb 11, 2016

Hi @ashutoshsaboo, thanks for looking into this. It seems, that the Integral.doit() function is taking the input Matrix as a set of coefficients of a polynomial. But why it can be different for Integral and integrate(), if integrate (line 1230 of integrals.py) just calls Integral.doit():

if isinstance(integral, Integral):
        return integral.doit(deep=False, meijerg=meijerg, conds=conds,
                             risch=risch, manual=manual)

David

@asmeurer

This comment has been minimized.

Copy link
Member

asmeurer commented Feb 11, 2016

The correct behavior should be the one with the matrix output. integrate shouldn't consider an iterable to be a list of coefficients for a polynomial.

@jksuom

This comment has been minimized.

Copy link
Member

jksuom commented Feb 11, 2016

integrate is defined with the decorator xthreaded ''intended to make it uniformly possible to apply a function to all elements of composite objects, e.g. matrices, lists, tuples and other iterable containers, or just expressions."

doit has no decorator.

@asmeurer

This comment has been minimized.

Copy link
Member

asmeurer commented Feb 11, 2016

Ugh, I don't like that decorator. We should get rid of it.

skirpichev added a commit to diofant/diofant that referenced this issue Feb 12, 2016

@ashutoshsaboo

This comment has been minimized.

Copy link
Contributor

ashutoshsaboo commented Feb 12, 2016

Thanks @asmeurer @jksuom @hercidav . I managed to solve the issue that is happening here.

@skirpichev The position where you have placed the isinstance of Matrix is I feel wrong, because it must always first check for the zero condition and the deep condition as well, and then only proceed to the condition of Matrix, otherwise in some special case your code, will give a problem.

I looked into the problem that I noticed yesterday, which I pointed above more, and as @jksuom said, the xthreaded thing is the one which is causing the problem.

Henceforth, I am opening a new pull request with the new code, that solves this issue. Please look into it @asmeurer @jksuom . Suggest in case if any other edits in the code are required, and if it can be merged into the master?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment