upiterbarg / mpmath

Automatically exported from code.google.com/p/mpmath
Other
0 stars 0 forks source link

matrix slicing broken #227

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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?

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~~

Original issue reported on code.google.com by archeryguru2000 on 10 Apr 2012 at 2:25

GoogleCodeExporter commented 9 years ago
By the way, 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.  I am open to any suggestions, but hopefully there is a far more 
elegant (and efficient) means of tackling this.

Thanks,
~~archery~~

Original comment by archeryguru2000 on 10 Apr 2012 at 6:59

GoogleCodeExporter commented 9 years ago
I think this is a bug. The error means that the key of the element you access 
(usually something like (0, 0) for instance) is not hashable. In this case, 
"key" is a slice, which is apparently not hashable. I did not implement matrix 
slicing, so I would have to look further into this to understand the problem. 
It used to work however.

Which version of Python are you using?

Original comment by Vinzent.Steinberg@gmail.com on 14 Apr 2012 at 9:59

GoogleCodeExporter commented 9 years ago
Thanks for the response.

$: python --version
Python 2.7.1+

If there's anything else you need, just ask.

~~archery~~

Original comment by archeryguru2000 on 16 Apr 2012 at 1:58