mpmath.matrix unhashable?

22 views
Skip to first unread message

archeryguru2000

unread,
Apr 13, 2012, 9:37:36 AM4/13/12
to mpm...@googlegroups.com
I apologize for double posting.  I suppose this is where I should have originally place this message.

When trying to manipulate matrices using mpmath, I'm confronted with an obstacle.  Using scipy/numpy/etc. one can call (and thus modify/alter/etc.) a "section" of a matrix, i.e., slicing the matrix.  However, with mpmath.matrix I'm unable to do so.  Is there an acceptable workaround to this.

What steps will reproduce the problem?
>>> from sympy import Matrix as sm_matrix
>>> from scipy import matrix as sp_matrix
>>> from mpmath import matrix as mp_matrix
>>> 
>>> my = sm_matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> mc = sp_matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> mp = mp_matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> 
>>> my[0:2,0:2]
[1, 2]
[4, 5]
>>> mc[0:2,0:2]
matrix([[1, 2],
        [4, 5]])
>>> mp[0:2,0:2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/mpmath/matrices/matrices.py", line 412, in __getitem__
    if key in self.__data:
TypeError: unhashable type


What is the expected output? What do you see instead?
I would have expected a similar response to the prior two commands... a submatrix consisting of the elements [[1,2],[4,5]].  Am I wrong in assuming?

Also, I have found a slight workaround for this with the following.  I basically, convert the mpmath matrix to a list, then convert that list into a sympy or scipy matrix, perform the slicing operation(s), and them convert back into an mpmath matrix.
>>> mp_matrix(sp_matrix(mp.tolist())[0:2,0:2])
>>> mp_matrix(sm_matrix(mp.tolist())[0:2,0:2])

Both of these do get the job done.  But they are extremely inefficient ways of doing so.  And I suspect that some lose of precision may occur in multiple conversions anyway (something I would like to avoid).  I am open to any suggestions, but hopefully there is a far more elegant (and efficient) means of tackling this.

What version of the product are you using? On what operating system? mpmath.__version__ = 0.14 & 0.15 (two different machines) Both operating Ubuntu Linux. Thanks for any assistance you can give. ~~archery~~

Vinzent Steinberg

unread,
Apr 14, 2012, 6:00:14 PM4/14/12
to mpmath
On Apr 13, 3:37 pm, archeryguru2000 <archeryguru2...@gmail.com> wrote:
> I apologize for double posting <http://code.google.com/p/mpmath/issues/detail?id=227>.  I suppose this is where I should have originally place this message.
>
> When trying to manipulate matrices using mpmath, I'm confronted with an obstacle.  Using scipy/numpy/etc. one can call (and thus modify/alter/etc.) a "section" of a matrix, i.e., slicing the matrix.  However, with mpmath.matrix I'm unable to do so.  Is there an acceptable workaround to this.
> *What steps will reproduce the problem?*>>> from sympy import Matrix as sm_matrix
> >>> from scipy import matrix as sp_matrix
> >>> from mpmath import matrix as mp_matrix
>
> >>> my = sm_matrix([[1,2,3],[4,5,6],[7,8,9]])
> >>> mc = sp_matrix([[1,2,3],[4,5,6],[7,8,9]])
> >>> mp = mp_matrix([[1,2,3],[4,5,6],[7,8,9]])
>
> >>> my[0:2,0:2]
>
> [1, 2]
> [4, 5]>>> mc[0:2,0:2]
>
> matrix([[1, 2],
>         [4, 5]])>>> mp[0:2,0:2]
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/pymodules/python2.7/mpmath/matrices/matrices.py", line 412, in __getitem__
>     if key in self.__data:
> TypeError: unhashable type
>
> *What is the expected output? What do you see instead?*
> I would have expected a similar response to the prior two commands... a submatrix consisting of the elements [[1,2],[4,5]].  Am I wrong in assuming?
>
> Also, I have found a *slight* workaround for this with the following.  I basically, convert the mpmath matrix to a list, then convert that list into a sympy or scipy matrix, perform the slicing operation(s), and them convert back into an mpmath matrix.
>
> >>> mp_matrix(sp_matrix(mp.tolist())[0:2,0:2])
> >>> mp_matrix(sm_matrix(mp.tolist())[0:2,0:2])
>
> Both of these *do* get the job done.  But they are extremely inefficient ways of doing so.  And I suspect that some lose of precision may occur in multiple conversions anyway (something I would like to avoid).  I am open to any suggestions, but hopefully there is a far more elegant (and efficient) means of tackling this.
>
> *What version of the product are you using? On what operating system?*mpmath.__version__ = 0.14 & 0.15 (two different machines) Both operating
> Ubuntu Linux. Thanks for any assistance you can give. ~~archery~~
>
> Delete comment <http://code.google.com/p/mpmath/issues/detail?id=227#>

As I stated in the issue, I think this is a bug, thanks for reporting.
The issue tracker is indeed the correct place.

Vinzent
Reply all
Reply to author
Forward
0 new messages