I guess you'll want to check if the base is_Number and is_negative and
do the custom logic, otherwise run the superclass eval. Something like
(untested):
@classmethod
def eval(cls, base, exp):
if base.is_Number and base.is_negative:
# Custom logic here
else:
return super().eval(base, exp)
Aaron Meurer
On Fri, Nov 6, 2015 at 2:32 PM, Paul Royik <[email protected]> wrote:
> Thank you.
> How to override behaviour, so that above expression, i.e.(-1)**(2/3).is_real
> outputs True?
>
> On Friday, November 6, 2015 at 9:51:39 PM UTC+2, Aaron Meurer wrote:
>>
>> You want to override eval. Take a look at
>> http://docs.sympy.org/latest/guide.html#functions for an example.
>>
>> Aaron Meurer
>>
>> On Fri, Nov 6, 2015 at 1:42 PM, Paul Royik <[email protected]> wrote:
>> > I decided to use "cleaner" solution: create a custom Pow subclass that
>> > evaluates. What method should I override?
>> >
>> > On Wednesday, November 4, 2015 at 9:25:17 PM UTC+2, Aaron Meurer wrote:
>> >>
>> >> You can generally do this sort of thing using replace().
>> >> Unfortunately, the pattern matcher doesn't recognize rational numbers
>> >> as a/b, so you have to do a more manual check. This should work:
>> >>
>> >> e.replace(lambda i: i.is_Pow and i.base == x and i.exp.is_Rational,
>> >> lambda i: real_root(-1,
>> >> i.exp.as_numer_denom()[1])**i.exp.as_numer_denom()[0])
>> >>
>> >> (replace -1 with whatever value you want to replace). A "cleaner"
>> >> solution would be to create a custom Pow subclass that evaluates like
>> >> you want, and replace instances of Pow with your class before doing a
>> >> substitution.
>> >>
>> >> Aaron Meurer
>> >>
>> >> On Wed, Nov 4, 2015 at 1:19 PM, Paul Royik <[email protected]> wrote:
>> >> > So, there is no way to do it using subs and/or some manipulations?
>> >> >
>> >> > real_root(-1, 3) is of no help, because I can have arbitrary
>> >> > expression.
>> >> >
>> >> > On Wednesday, November 4, 2015 at 6:29:29 PM UTC+2, Aaron Meurer
>> >> > wrote:
>> >> >>
>> >> >> You need to use real_root, like
>> >> >>
>> >> >> In [3]: real_root(-1, 3)
>> >> >> Out[3]: -1
>> >> >>
>> >> >> In [4]: real_root(-1, 3)**2
>> >> >> Out[4]: 1
>> >> >>
>> >> >> SymPy, like most math libraries, uses complex roots (i.e., principal
>> >> >> roots) because they have nicer mathematical properties.
>> >> >>
>> >> >> Aaron Meurer
>> >> >>
>> >> >> On Wed, Nov 4, 2015 at 3:30 AM, Paul Royik <[email protected]>
>> >> >> wrote:
>> >> >> > I have the following expresssion:
>> >> >> >
>> >> >> > f=x**(Rational(2,3))
>> >> >> >
>> >> >> > How can I get 1, when substituting (-1) instead of complex number?
>> >> >> > For now, I got complex number when run f.subs(x,-1).evalf()
>> >> >> >
>> >> >> > --
>> >> >> > 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 http://groups.google.com/group/sympy.
>> >> >> > To view this discussion on the web visit
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> > https://groups.google.com/d/msgid/sympy/662a2085-1030-42d5-bbde-feb524fd246c%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 http://groups.google.com/group/sympy.
>> >> > To view this discussion on the web visit
>> >> >
>> >> >
>> >> > https://groups.google.com/d/msgid/sympy/c88261c0-3449-4d7a-abf8-8d28ebdca59d%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 http://groups.google.com/group/sympy.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/sympy/503d6140-3541-42a5-95d4-9c202f7d8e4e%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 http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/2a8b8984-c262-4b64-8c05-80deb5d74700%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 http://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BZeys9iOsWGtcTc6wL22Tr6bxek0zBWNeMvWGRVm5dFA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.