Skip to content
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

matrix vstack/hstack can fail with immutable matrix as first argument #11944

Closed
cbm755 opened this issue Dec 14, 2016 · 7 comments · Fixed by #11951
Closed

matrix vstack/hstack can fail with immutable matrix as first argument #11944

cbm755 opened this issue Dec 14, 2016 · 7 comments · Fixed by #11951
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. matrices

Comments

@cbm755
Copy link
Contributor

cbm755 commented Dec 14, 2016

>>> A = Matrix([[1, 2], [pi, 4]])
>>> AIm = sympify(A)

>>> type(A)
sympy.matrices.dense.MutableDenseMatrix

>>> type(AIm)
sympy.matrices.immutable.ImmutableMatrix

>>> Matrix.vstack(A, AIm)
⎡1  2⎤
⎢    ⎥
⎢π  4⎥
⎢    ⎥
⎢1  2⎥
⎢    ⎥
⎣π  4⎦

>>> Matrix.vstack(AIm, A)
('hello vstack, cls=', <class 'sympy.matrices.dense.MutableDenseMatrix'>)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-aec335eeafd0> in <module>()
----> 1 Matrix.vstack(AIm, A)

/home/cbm/work/octsympy/octsympy.git/inst/sympy/matrices/matrices.py in vstack(cls, *args)
   4541         """
-> 4542         return reduce(cls.col_join, args)
   4543 
   4544     def xreplace(self, rule):  # should mirror core.basic.xreplace

TypeError: unbound method col_join() must be called with MutableDenseMatrix instance as first argument (got ImmutableMatrix instance instead)

Note it will work if one uses MatrixBase.vstack.

I don't think this is really about immutable matrices; its about cls.col_join not being the right method, depending on what is in args.

One way to fix this would be to take cls from the type of args[0] instead:

kls = type(args[0])
reduce(kls.col_join, args)

Although I'm not sure that's quite right.

Another option is to avoid reduce and use:

M = args.pop(0)
for i = range(len(args)):
    M = M.col_join(args.pop(0))

(or more pythonic version thereof).

Whatever we do hstack needs the same fix.

@Abdullahjavednesar
Copy link
Member

I want to try this.

@Abdullahjavednesar
Copy link
Member

@cbm755 this the output in my system and seems to be fine.

In [36]: A=Matrix([[1,2],[pi,4]])

In [37]: AIm=sympify(A)

In [38]: type(A)
Out[38]: sympy.matrices.dense.MutableDenseMatrix

In [39]: type(AIm)
Out[39]: sympy.matrices.immutable.ImmutableMatrix

In [40]: Matrix.vstack(AIm,A)
Out[40]:
?1  2?
?    ?
?π  4?
?    ?
?1  2?
?    ?
?π  4?

In [41]: Matrix.hstack(AIm,A)
Out[41]:
?1  2  1  2?
?          ?
?π  4  π  4?

@cbm755
Copy link
Contributor Author

cbm755 commented Dec 15, 2016

Thanks for checking. I've tried it and I can reproduce with Python 2 on both Sympy 1.0.1 and the current git master. But on Python 3, it works as you show: nice catch.

@cbm755 cbm755 added the Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. label Dec 15, 2016
@cbm755
Copy link
Contributor Author

cbm755 commented Dec 15, 2016

I want to try this.

Go for it. It should be an easy fix, I'm just not enough of a Python expert to know which fix is the best. Please add a test and read the https://github.com/sympy/sympy/wiki/Development-workflow.

@mamakancha
Copy link
Contributor

Hi @Abdullahjavednesar . Let me know in case you're not working on it :)

@siefkenj
Copy link
Contributor

This is fixed in #11592 , for what it's worth :-)

@cbm755
Copy link
Contributor Author

cbm755 commented Dec 17, 2016

@siefkenj that's great! I wondered when I saw your refactor but I haven't had a chance to look yet. Thanks.

@Abdullahjavednesar if that PR goes in first, we'll need to update your PR #11951 to just add tests---this issue will still need tests even if its fixed elsewhere.

skirpichev pushed a commit to skirpichev/diofant that referenced this issue Feb 17, 2019
// edited by skirpichev

Closes sympy/sympy#11944
skirpichev pushed a commit to skirpichev/diofant that referenced this issue Feb 23, 2019
Closes sympy/sympy#11944

// edited by skirpichev

Based on sympy/sympy#11951

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this issue Feb 24, 2019
Closes sympy/sympy#11944

// edited by skirpichev

Based on sympy/sympy#11951

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. matrices
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants