Yesterday I had the need for a matrix type in Python.
Apparently, a trivial answer to this need would be to use numpy.matrix()
, but the additional issue I have is that I would like a matrix to store arbitrary values with mixed types, similarly to a list. numpy.matrix
does not perform this. An example is
>>> numpy.matrix([[1,2,3],[4,"5",6]])
matrix([['1', '2', '3'],
['4', '5', '6']],
dtype='|S4')
>>> numpy.matrix([[1,2,3],[4,5,6]])
matrix([[1, 2, 3],
[4, 5, 6]])
As you can see, the numpy.matrix
must be homogeneous in content. If a string value is present in my initialization, every value gets implicitly stored as a string. This is also confirmed by accessing the single values
>>> numpy.matrix([[1,2,3],[4,"5",6]])[1,1]
'5'
>>> numpy.matrix([[1,2,3],[4,"5",6]])[1,2]
'6'
Now, the Python list type can instead accept mixed types. You can have a list containing an integer and a string, both conserving their type. What I would need is something similar to a list, but operating in a matrix-like behavior.
Therefore, I had to implement my own type. I had two choices for the internal implementation: list containing lists, and dictionaries. Both solutions have shortcomings:
Edit: clarification. The concrete reason on why I need this functionality is because I am reading CSV files. Once I collect the values from a CSV file (values that can be string, integers, floats) I would like to perform swapping, removal, insertion and other operations alike. For this reason I need a "matrix list".
My curiosities are:
You can have inhomogeneous types if your dtype
is object
:
In [1]: m = numpy.matrix([[1, 2, 3], [4, '5', 6]], dtype=numpy.object)
In [2]: m
Out[2]:
matrix([[1, 2, 3],
[4, 5, 6]], dtype=object)
In [3]: m[1, 1]
Out[3]: '5'
In [4]: m[1, 2]
Out[4]: 6
I have no idea what good this does you other than fancy indexing, because, as Don pointed out, you can't do math with this matrix.
I'm curious why you want this functionality; as I understand it, the reason for having matrices (in numpy), is primarily for doing linear math (matrix transformations and so on).
I'm not sure what the mathematical definition would be for the product of a decimal and a String.
Internally, you'll probably want to look at sparse matrix implementations (http://www.inf.ethz.ch/personal/arbenz/pycon03_contrib.pdf). There are lots of ways to do this (hash, list, linked list), and each has its own advantages and drawbacks. If your matrix isn't going to have a lot of nulls or zeroes, then you can ditch the sparse implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With