January 4th, 2013, 11:44 PM
Why no 2d array system in python?
I recently started studying python and I am wondering if there is a way to have a 2d array.
As far as I know there is only the concept of list in python.
I am surprised that being a high level language why is it so difficult to do things like matrix multiplication etc(which require 2d arrays)?
January 5th, 2013, 12:50 AM
There are only a few programming languages that have nontrivial matrix operations as built-in functionality, and those that do (like Matlab and Mathematica) tend to be awkward for general-purpose (i.e., non-mathematical) programming. The preferred approach in general-purpose languages is to leave matrices and matrix arithmetic to third-party libraries.
In Python, you have multiple options:
- use a list of lists. You'll have to code your own matrix operations, and it won't be very efficient.
- use NumPy, which I highly recommend. This gives you not only two-dimensional arrays, but arrays of arbitrary dimensionality. You also get all the standard matrix operations, including multiplication. The newest version of NumPy also includes the very useful einsum function, which is a generalization of matrix multiplication. (For example, this allows you to matrix-multiply one array of matrices elementwise against another array of matrices in one line of code.)
- use another third-party library that implements some matrix functionality. I don't know of any that are as powerful as NumPy.
January 5th, 2013, 04:00 AM
Numpy seems powerful. Thanks for telling