>>> meter + second
meter + second
>>> length + time
length + time
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))
3+y+A+x(((3+y)+A)+x)
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?
How would it work as a set? How do you know which function to call?
class Add:
_check_postprocess = False
@classmethod
def flatten(cls, seq):
postprocess = set([])
if cls._check_postprocess:
for i in seq:
if hasattr(i, "_postprocess_function_Add"):
postprocess.add(i._postprocess_function_Add)
[ ... body of flatten ... ]
[ call `expr` the returning expression ]
for i in postprocess:
expr = i(expr)
return expr
3*A + 2*B
An expression like x*A + B would be problematic, but 2*A + B should
actually be fine, because the flatten algorithm splits off numeric
coefficients (so that they can be combined).
Aaron Meurer
On Thu, Mar 23, 2017 at 11:59 AM, Francesco Bonazzi
<franz....@gmail.com> wrote:
> There's another complication. Consider the expression:
>
> 3*A + 2*B
>
> Suppose A and B require a flatten-postprocessor at the Add level. The Add
> object will not detect them, because its parameters are just two Mul
> objects.
>
> --
> 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+unsubscribe@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> 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/e1823603-00b8-4424-8ccc-52d08053f2ea%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 sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
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%3D6JXzSmPiZxCYBSnQwd7URE9_3vieRVA%2B_y2aAZV_gmcHA%40mail.gmail.com.
Would multiple dispatch be best for implementing Add/Mul/Etc for these different types?
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
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/1770ca8c-a772-4e77-a3b2-5147708ea944%40googlegroups.com.
Le 18/04/17 à 21:28, Francesco Bonazzi a écrit :
> I'm gonna merge this PR soon:
>
> https://github.com/sympy/sympy/pull/12508
Wow, that's horrifying! Good luck maintaining it!
Performance is a valid concern. We should definitely check that.
The __slots__ part I don't know. That could probably be done differently.
Regarding the implementation itself, how would you do it? The ability
to make Mul or Add do custom stuff for custom objects is a very
commonly requested feature. Would you keep it unimplemented, or do it
some other way?
On Thu, Apr 20, 2017 at 6:12 AM, Francesco Bonazzi <franz....@gmail.com> wrote:
>
>
> On Wednesday, 19 April 2017 21:51:08 UTC+2, Aaron Meurer wrote:
>>
>> Performance is a valid concern. We should definitely check that.
>>
>> The __slots__ part I don't know. That could probably be done differently.
>>
>
> I didn't like that part too. Has anyone a better idea?
>
>> Regarding the implementation itself, how would you do it? The ability
>> to make Mul or Add do custom stuff for custom objects is a very
>> commonly requested feature. Would you keep it unimplemented, or do it
>> some other way?
>
>
> Yes, we really need this feature.
>
> Ideally we want to extend such mechanism to functions as well.For functions, dispatch works nicely. In fact, most functions are single-argument, so you only need single dispatch, which is basically SymPy's _eval_thing mechanism. We could extend Function so that it dispatches before eval() is called on the subclass (say, sin(x) could call x._eval_function(sin)). At least, that should work when nargs = 1. For nargs > 1, you need something more complicated.
Francesco's solution maybe isn't the most general, but it seems simple, and capable of handling the use-cases I know of.
On Thursday, 20 April 2017 21:23:54 UTC+2, Aaron Meurer wrote:
On Thu, Apr 20, 2017 at 6:12 AM, Francesco Bonazzi <franz....@gmail.com> wrote:
>
>
> On Wednesday, 19 April 2017 21:51:08 UTC+2, Aaron Meurer wrote:
>>
>> Performance is a valid concern. We should definitely check that.
>>
>> The __slots__ part I don't know. That could probably be done differently.
>>
>
> I didn't like that part too. Has anyone a better idea?
>
>> Regarding the implementation itself, how would you do it? The ability
>> to make Mul or Add do custom stuff for custom objects is a very
>> commonly requested feature. Would you keep it unimplemented, or do it
>> some other way?
>
>
> Yes, we really need this feature.
>
> Ideally we want to extend such mechanism to functions as well.For functions, dispatch works nicely. In fact, most functions are single-argument, so you only need single dispatch, which is basically SymPy's _eval_thing mechanism. We could extend Function so that it dispatches before eval() is called on the subclass (say, sin(x) could call x._eval_function(sin)). At least, that should work when nargs = 1. For nargs > 1, you need something more complicated.
Suppose we have the exponential of a MatrixSymbol, how do we transmit the information that the result of the exponential function is a matrix?
Suppose you have the expression:
exp(MatrixSymbol("M", 3, 3)) + MatrixSymbol("A", 2, 2)
It would be desirable to raise a ShapeError.
Francesco's solution maybe isn't the most general, but it seems simple, and capable of handling the use-cases I know of.
I had a thought about using the arguments instead of a new __slots__. That is, add an extra argument to the args that is hidden when printing, but if Mul or Add meet it, they should post-process the other args and pass it further. But this could also have its cons.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
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/c4dbda7f-1798-4f2e-9103-b9a6f310f52d%40googlegroups.com.