Stopping the automatic evaluation of (10**2) to 100 and (10*x)**2 to 100*x_**2 in Sympy expression

32 views
Skip to first unread message

Ankit

unread,
Mar 8, 2017, 11:06:26 PM3/8/17
to sympy
Hello ! I have this question !

Python automatically evaluates 10**2 to 100 or say (10*10) to 100

 Same is the case where our expression is , for example: (2*log(x))**2, it will become 4*log(x_)**2  
 
or 

(10*x)**2 becomes 100*x_**2                  (Also, how is x different from x_ ?) 

Is there anyway,we can stop this evaluation and write these expressions in non-evaluated form.

What I thought of was to take them as Strings, and use Evaluate= False, with sympify . But, if this expression is written in Sympy, like expr= (10*x)**2, 
it will automatically get evaluated and expr = 100*x_**2.

Please let me know the possible solution ! 

Thanks,
Ankit

Aaron Meurer

unread,
Mar 8, 2017, 11:24:21 PM3/8/17
to sy...@googlegroups.com
You can use Pow(10*x, 2, evaluate=False).

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 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/0fe38ea6-3e74-4326-9c78-119b9466e457%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ankit

unread,
Mar 9, 2017, 12:10:57 AM3/9/17
to sympy
Hi Aaron !

I was thinking in context with the following problem,where I was looking into the pattern matching. I found this :
>>> pattern = (a+b*sin(c+d*x))**n
>>> term = (1 + 3 * sin(5 + 4 * y))** 10
>>> pattern.matches(term)
{a_: 1, n_: 10, d_: 4, c_: 5, b_: 3, x_: y}

>>> pattern = (a*sin(b+c*x))**n
>>> term = (3*sin(5+4*y))**3
>>> pattern.matches(term)
>>>

Same goes, with :
>>> pattern2 = (a+ b*log(x))**n
>>> term2 = (3 + 2*log(x))**2
>>> pattern2.matches(term2)
{a_: 3, n_: 2, b_: 2}

But,
>>> pattern3 = (b*log(x))**n
>>> term3 = (3*log(x))**2
>>>

This is due to the fact,that the term, (3*log(x))**2 is now 9*log(x_)**2.
(Is it a bug? or an improper implementation of pattern matching? If, yes, how can this be tackled?)

Due to this, I think special cases have to be considered(while implementing integration rules) where such case is occurring, during pattern matching. Please let me know your views on it.
Thanks,
Ankit

Aaron Meurer

unread,
Mar 9, 2017, 12:46:46 AM3/9/17
to sy...@googlegroups.com
This is a general problem. The pattern matcher is going to have to be
smart enough to recognize things like this, especially for cases that
simplify automatically. We should think about how to tackle this.

One idea is to have a set of rules where automatic simplification is
known to take place for certain inputs, for instance, (a*b)**x ->
a**x*b**x (happens if a and b are numbers, maybe in other cases as
well I'm not sure). Then each pattern should run through the rules and
generate a new pattern. For instance, (b*log(x))**n would generate
b**n*log(x)**n. The rules are then run recursively on this pattern,
and so on. I think the sympy.strategies module would be a good fit for
this. Then instead of a single pattern you have a list of patterns,
and if any match an expression the expression matches. This way, the
expression matching itself can be naive and deal only with the
structural form of the expression, and be separated from the
mathematical rules.

That's just one idea. There may be other ways to handle this as well.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/cbf027df-b0b8-468d-a2de-97d2bb4099d7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages