I like the idea, although we've have to make sure to keep things fast,
especially for common objects.
Maybe each class could have a registry, and at the top of each class,
it could have a function that checks each argument against the
registry. So for instance, for Add:
class Add:
preprocess_functions = {}
@classmethod
def flatten(cls, seq):
for i in seq:
if type(i) in cls.preprocess_functions: # really should
check for subclass
return cls(*cls.preprocess_functions(seq))
Then the units would register Add.preprocess_functions[Unit] =
<function that checks unit consistency>.
There are a lot of details to work out here, but is that the basic
idea you are suggesting?
Aaron Meurer
On Tue, Mar 21, 2017 at 7:06 AM, Francesco Bonazzi
<[email protected]> wrote:
> I am trying to fix the unit system module in SymPy, and I got into a
> problem:
>
>>>> meter + second
> meter + second
>>>> length + time
> length + time
>
> Such expressions create an Add object. Technically, they such raise an
> exception, given that the summation involves incompatible dimensions.
>
> Obviously, Add does not know about all of this, and just creates an Add
> object.
>
> The point is, what about adding some Python magic (or similar tricks) to
> make it possible, when some submodules are loaded, to extend the
> constructors for Add, Mul, Pow, and possibly also for SymPy functions?
>
> We also have a related problem concerning matrix symbols, as operations on
> them need to be consistent with their dimensions. The problem is currently
> solved by ad-hoc MatMul, MatAdd classes. This causes them to lack a lot of
> features/operations from Add, Mul. Subclassing also appears to cause
> problems. Other instances are tensor indices in implicit summation mode.
>
> One problem that might arise is the inability of adding methods specific for
> those kind of operations (such as matrix symbol related methods implemented
> in MatAdd, MatMul). This could be avoided by using non-class functinos
> though.
>
> What do you think?
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/0b0a8187-6b23-47eb-a4e5-bbb4609e5375%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAKgW%3D6Kwp9kT0G9ZnDf34u7i%3Dey70VhJan7c-ZWN%2BCxLizr2Kg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.