Eval in R

9 views
Skip to first unread message

Vincent MAILLE

unread,
Dec 18, 2011, 2:49:06 PM12/18/11
to sy...@googlegroups.com
Hello,

I want to eval an expression in R, e.g for E=sqrt(x-1)*sqrt(x-3), E.sub(x,0).is_real is true :( 
Is there a way ton force calculus in R sqrt(-1)=nan for exemple ?

Thanks,
Vincent

Chris Smith

unread,
Dec 18, 2011, 9:16:41 PM12/18/11
to sy...@googlegroups.com

Only by doing the substitution by hand, I guess. You write your own
function to pull out the sqrt's:

>>> def presub(expr, reps):
... sr = [p for p in expr.atoms(Pow) if p.exp is S.Half]
... for s in sr:
... ssub = s.subs(reps)
... if not ssub.is_real:
... return S.NaN
... return expr.subs(reps)
...
>>> presub(x,{x:0})
0
>>> presub(sqrt(x-1)*sqrt(x-3),{x:0})
nan

Aaron Meurer

unread,
Dec 18, 2011, 10:20:08 PM12/18/11
to sy...@googlegroups.com
So what you really want is sqrt(-1*-3), not sqrt(-1)*sqrt(-3). The
powsimp() function with the force argument will let you combine square
roots:

In [20]: powsimp(E, force=True)
Out[20]:
_________________
╲╱ (x - 3)⋅(x - 1)

In [21]: powsimp(E, force=True).subs(x, 0)
Out[21]:
___
╲╱ 3

Making it return nan instead is more difficult. Something like
Chris's solution should work (depending on what kinds of expressions
you want it to work on). You could probably hack the core to make I
give nan, but that would (obviously) break a lot of stuff.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Vincent MAILLE

unread,
Dec 22, 2011, 4:15:58 AM12/22/11
to sy...@googlegroups.com
Thanks, yes, I think doing the substitution by hand is the best way for my problem.

Thanks,

Vincent

Reply all
Reply to author
Forward
0 new messages