What is the difference between Basic and Expr?

90 views
Skip to first unread message

JS S

unread,
Dec 1, 2019, 8:59:08 PM12/1/19
to sympy
Sorry for duplicate question -  my previous one didn't get any answer so I cannot but ask again.

Now, I know that Basic object must be immutable, and Expr object must be able to be subject to arithmetic operation.
(As far as I know, that is why some classes, e.g. FiniteSet, is instance of Basic but not Expr).

But what confuses me is this:

- ImmutableDenseMatrix is subclass of Expr
- ImmutableSparseMatrix is subclass of Basic, but not Expr.
- ImmutableDenseNDimArray is, unlike its Matrix counterpart, subclass of Basic but not Expr.

This is confusing for me: What makes the difference between these three?

Oscar Benjamin

unread,
Dec 1, 2019, 9:42:30 PM12/1/19
to sympy
On Mon, 2 Dec 2019 at 01:59, JS S <jeeso...@gmail.com> wrote:
>
> But what confuses me is this:
>
> - ImmutableDenseMatrix is subclass of Expr
> - ImmutableSparseMatrix is subclass of Basic, but not Expr.
> - ImmutableDenseNDimArray is, unlike its Matrix counterpart, subclass of Basic but not Expr.

I don't think there is a good reason for the discrepancy here apart
from the fact that there is some confusion about what should subclass
from what. Another example is that Relational is a subclass of Expr
and that makes no sense at all.

Probably all of these should be Expr or none of them should.

--
Oscar

JS S

unread,
Dec 1, 2019, 11:25:54 PM12/1/19
to sympy
The best explanation I could make was this:

There exists MatAdd class, which can have ImmutableDenseMatrix as arguments, thus representing unevaluated matrix addition.
Therefore, ImmutableDenseMatrix subclasses Expr since it can form matrix expression.

However, ImmutableSparseMatrix isn't designed to be argument of MatAdd class.
(When I put it as argument of MatAdd, pretty printing raises error, saying they don't have `as_coeff_mmul` attribute).
This makes ImmutableSparseMatrix not to subclass Expr. Why this can't be argument of MatAdd I don't know.

Likewise, there exists no class such as 'NDimArrayAdd' class, making NDimArray subclass of Basic only.


But, seeing that Relational subclasses Expr as you exemplified, I belive that obviously there have been some confusion subclassing Basic and Expr.


2019년 12월 2일 월요일 오전 11시 42분 30초 UTC+9, Oscar 님의 말:

David Bailey

unread,
Dec 2, 2019, 4:58:55 AM12/2/19
to sy...@googlegroups.com

As a software developer (mainly in C) It seems to me that all big software projects have arbitrary inconsistencies buried deep in the code, but of course projects built with Python are transparent - you can see everything, warts and all. C++ lets a class inherit from more than one parent, but I sincerely hope Python will not do likewise!

David

Aaron Meurer

unread,
Dec 2, 2019, 4:32:58 PM12/2/19
to sympy
If the array classes are supposed to work just like Matrix where they
can be added together, and there are array expressions, then it
probably should as well. I'm not too familiar with the module, but it
doesn't look like array expressions exist yet, so that might be the
reason. ImmutableMatrix is Expr because it can be part of a MatAdd or
MatMul matrix expression, which subclass from Expr.

There has also been some discussion in the past whether classes that
behave like Expr should actually subclass from it or not. For
instance, there was some debate about this in the quantum module a
while back. I think the conclusion was that they should, because it's
too hard to make things work when not doing so.

Relational being an Expr is another can of worms. I agree it shouldn't
be, at least as it is currently used. IMO there should be separate
Expr and Boolean versions of relationals. See
https://github.com/sympy/sympy/issues/5031.

Ultimately, we still don't really have the best design for custom Expr
and Expr-like things. Especially when it comes to things like
dispatching inside of Add and Mul, there are some APIs that are there,
but we aren't necessarily happy with them. So I wouldn't assume that
just because something is done a certain way in SymPy that it is the
best way it can be done, particularly when it comes to the class
hierarchy and the ways of extending SymPy.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/401e29e2-9a02-41ee-b549-f6d300c9d7cf%40googlegroups.com.

Francesco Bonazzi

unread,
Dec 3, 2019, 5:18:34 PM12/3/19
to sympy


On Monday, 2 December 2019 05:25:54 UTC+1, JS S wrote:

Likewise, there exists no class such as 'NDimArrayAdd' class, making NDimArray subclass of Basic only.

Technically there is CodegenArrayElementwiseAdd defined in sympy.codegen.array_utils which can be used to create an addition of N-dim arrays.
 
The reason why NDimArray is a subclass of Basic only is that someone suggested to make it an instance of Basic instead of Expr in a PR review, IIRC. Don't expect any deep thought behind this decision.

JS S

unread,
Dec 4, 2019, 8:59:44 PM12/4/19
to sympy
Then, shouldn't it be changed by now?

I am not a 'real' developer and I hold no knowledge of backward compatibility. (And I am kinda sorry for meddling in like this)
But what I know is that these kind of inconsistencies will cause much more confusion and complexity in the future.

What I suggest is:
1. Make ImmutableDenseNDimArray and ImmutableSparseNDimArray subclass of Expr.
2. Make ImmutableSparseMatrix subclass of Expr.
3. Make Relational subclass of Basic, not Expr.

Perhaps deprecating these and building new classes will be suitable?


2019년 12월 4일 수요일 오전 7시 18분 34초 UTC+9, Francesco Bonazzi 님의 말:

Aaron Meurer

unread,
Dec 9, 2019, 1:32:55 PM12/9/19
to sympy
Going from Basic to Expr doesn't break compatibility, since Expr is a
subclass of Basic. I think the matrix and array classes could be fixed
without major issues. Fixing Relational is not very straightforward. I
would look through the discussion on the issue I posted first. Really
we need to decide what to do with it first.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/7521dcef-b035-4d7d-aa7c-08b100697bcd%40googlegroups.com.

S.Y. Lee

unread,
Dec 12, 2019, 2:15:32 PM12/12/19
to sympy
I see ImmutableSparseMatrix is lacking some properties that ImmutableDenseMatrix has, like is_ZeroMatrix, ... and it's causing some problems in MatMul.
Reply all
Reply to author
Forward
0 new messages