numpy arrays are ... arrays, if you want * to do matmul, use
numpy.matrix (which is soft deprecated since now we have @), and will
do what you expect with square matrix times vector.
broadcasting is the natural extension of array `op` scalar on... arrays.
Say you have an array Pressure with 3 coord : longitude , latitude ,
time, and a typical Mean value M with coord: latitude , longitude
Then you can "just" do `Pressure - M`,
if actually your Mean is just function of time then you can same, do P
- M and it broadcast on the other axes. So yes, extremely useful.
If you use things like xarray then it will not match dimension base
on their neame, and not on their size, so it's less "magical" and more
accurate when some dimensions have the same size.
But of course that's for arrays, not matrices.
--
M
On Thu, 13 Aug 2020 at 18:17, Steven D'Aprano
On Thu, Aug 13, 2020 at 11:17:00PM +0100, Stefano Borini wrote:
The math module has plenty of mathematical functions that are very interesting, but no Matrix object.
Funny you mention this, I have been working on a Matrix object for precisely the use-case you discuss (secondary school maths), where performance is not critical and the dimensions of the matrix is typically single digits.
I, um, got distracted with some over-engineering and then distracted further by work and life, but perhaps this is a good opportunity for me to get it back on track.
Generally, when a Matrix object is needed, numpy is the point of reference, but numpy requires understanding pip, installing modules, maybe creating a virtual environment and finally understanding numpy.
And numpy also offers a surprising interface that doesn't match matrices. (Well, surprising to those who aren't heavy users of numpy :-)
py> A = numpy.array([[1, 2], [3, 4]]) py> A*A array([[ 1, 4], [ 9, 16]])
To get *matrix multiplication* you have to use the `@` multiplication operator from PEP 465:
py> A@A array([[ 7, 10], [15, 22]])
https://www.python.org/dev/peps/pep-0465/
But what's really surprising about numpy arrays is broadcasting:
py> A = numpy.array([[1, 2], [3, 4]]) py> B = numpy.array([[10, 20]]) py> A + B array([[11, 22], [13, 24]])
I don't know if broadcasting is actually useful or not, but it's not what you want when doing matrix arithmetic at a secondary school level.
-- Steven _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/FARJRN... Code of Conduct: http://python.org/psf/codeofconduct/