On Mon, 17 Aug 2020 at 07:14, Stephen J. Turnbull
<turnbull.stephen...@u.tsukuba.ac.jp> wrote:
>
> Christopher Barker writes:
>
>  > Anyway, I would like to see a nice linear algebra lib -- but not 'cause I'd
>  > use it, only because I find it interesting.
>
> SymPy.
>
> Except that in this conversation, "linear algebra" is likely neither
> restricted to linearity nor so much algebraic as computational, so
> SymPy likely won't do. :-/

SymPy is exactly what I would recommend for the OP use case of high
school students doing basic linear algebra (although perhaps as
maintainer I would say that...). SymPy's matrix class doesn't have the
complicated broadcasting rules of numpy's ndarray and uses exact
calculations rather than floating point (or limited precision
integers). I'm not sure why it would be considered unsuitable for
this.

Here's a demonstration:

>>> from sympy import Matrix

>>> M = Matrix([[1, 2], [3, 4]])

>>> M
Matrix([
[1, 2],
[3, 4]])

>>> M * M
Matrix([
[ 7, 10],
[15, 22]])

>>> 3 * M
Matrix([
[3,  6],
[9, 12]])

>>> M ** -1
Matrix([
[ -2,    1],
[3/2, -1/2]])

>>> M.rank()
2

>>> M.det()
-2

>>> M.eigenvals()
{5/2 - sqrt(33)/2: 1, 5/2 + sqrt(33)/2: 1}

There are no funny broadcasting rules. You can multiply two matrices
with * (or @) and if the shapes are compatible in the standard
mathematical rules of matrix multiplication then you will get the
matrix product. You can also multiply a matrix and a scalar as you
would in an equation. No other kind of multiplication is permitted.
You can add two matrices iff they have the same shape. Using ** gives
matrix powers so M**2 or M**-1 are as you would expect when seeing
superscripts in a mathematical equation.

If you don't put floats in the matrix then all of the calculations
will be exact with no overflow and no rounding error. The only thing
to be careful of is Python's division so 1/2 should be sympy's
Rational(1, 2).

If you enter the above in a jupyter notebook (after calling sympy's
init_printing()) then the matrices will all display nicely using
latex->Mathjax under the hood. The same also works in a Qt console
e.g. you can see a nice mathematical display when using a "sympy
console" in the spyder editor.

Of course as the matrices get larger then sympy will be noticeably
slower than numpy. Even the algorithms for computing the eigenvalues
symbolically can breakdown for matrices larger than 4x4 so there are
good reasons why numerical libraries are more commonly used in many
areas. Simple calculations are still faster than the blink of an eye
though and I'm sure some of the high-school students would benefit
from being able to do calculations with things like sqrt(2) or symbols
like x, y, z at some point.

If your high-school students are okay with using Python then I would
certainly recommend SymPy for this kind of calculation.

On the other hand adding something like this to the stdlib is a very
slippery slope. SymPy's matrices package has 30k lines of code and
counting even though it leverages much of the rest of the sympy
codebase to do the calculations it does. There are a lot of different
features you can add once you have a matrix class.


Oscar
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/CUELRJNK23FNXT3T3MDLGBMWWLSYJRMM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to