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

On Sun, Dec 18, 2011 at 7:16 PM, Chris Smith <[email protected]> wrote:
> On Mon, Dec 19, 2011 at 1:34 AM, Vincent MAILLE <[email protected]> wrote:
>> 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 ?
>>
>
> 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
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>

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

Reply via email to