Here is one solution that seems to work. To simplify Z I use
Z.replace(Abs, MyAbs) with
def MyAbs(x):
x1=symbols('x1',real=True,positive=True)
x1 = x.evalf(subs={a:0.573})
if x1 < 0.0:
return S(-1)*x
else:
return x
Is this a reasonable way to go, or are there gotchas that I should be aware
of?
On Sunday, March 28, 2021 at 11:14:57 AM UTC+2 B A wrote:
> I am a sympy beginner, and fairly new to python, so I suspect that my
> question has a simple answer, but have not been able to figure it out
> myself.
>
> I have sympy expressions containing the built-in Abs function. The
> arguments of Abs() are polynomials in a=symbol('a',
> real=True,positive=True) . Here are a three examples:
>
> Abs(a**2 + 2)
> sqrt(2)*Abs(2*a**2 - 1)/3
> sqrt(2)*(a + 1)*Abs(a - 1)/3
>
> Here's the point: I know that 'a' is approximately 0.6. So in cases where
> the argument of Abs has an unambiguous sign (within floating-point
> precision) I would like to simplify the absolute value. For example in the
> three cases above I would like:
>
> Abs(a**2 + 2) -> a**2 + 2
> sqrt(2)*Abs(2*a**2 - 1)/3 -> sqrt(2)*(1- 2*a**2)/3
> sqrt(2)*(a + 1)*Abs(a - 1)/3 -> sqrt(2)*(a + 1)*(1-a)/3
>
> where the right arrow '->' indicates replacement or simplification. Note
> the change of sign in the second and third examples, because (for example)
> I know that (a-1) is negative.
>
> Is there a (simple) way to implement this?
>
> Thank you!
> Bruce
>
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/5c6a7de4-e709-4a8c-88f1-ce3f44ee277an%40googlegroups.com.