Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 4th, 2013, 11:44 PM
 Avichal
Contributing User

Join Date: Dec 2012
Posts: 70
Time spent in forums: 8 h 24 m 25 sec
Reputation Power: 2
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)?

#2
January 5th, 2013, 12:50 AM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
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.

#3
January 5th, 2013, 04:00 AM
 Avichal
Contributing User

Join Date: Dec 2012
Posts: 70
Time spent in forums: 8 h 24 m 25 sec
Reputation Power: 2
Numpy seems powerful. Thanks for telling

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Why no 2d array system in python?