Could anyone suggest a solution for this?  I can make a list of 
substitutions by hand (as below) and pass that to the .subs() method, but 
surely there is a better way.

s_list={
    Abs(a       - 1)  : S(1)  - a,
    Abs(a       - 2)  : S(2)  - a,
    Abs(2*a     - 3)  : S(3)  - S(2)*a,
    Abs(a**2    - 2)  : S(2)  - a**2,
    Abs(a**2    - 3)  : S(3)  - a**2,
    Abs(a**2    - 11) : S(11) - a**2,
    Abs(2*a**2  - 1)  : S(1)  - S(2)*a**2,
    Abs(2*a**2  - 3)  : S(3)  - S(2)*a**2,
    Abs(3*a**2  - 2)  : S(2)  - S(3)*a**2,
    Abs(3*a**2  - 16) : S(16) - S(3)*a**2,
    Abs(4*a**2  - 3)  : S(3)  - S(4)*a**2,
    Abs(5*a**2  - 4)  : S(4)  - S(5)*a**2,
    Abs(5*a**2  - 7)  : S(7)  - S(5)*a**2,
    Abs(5*a**2  - 16) : S(16) - S(5)*a**2,
...
}

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/253c3643-71e4-4073-9134-ac53f2b7f605n%40googlegroups.com.

Reply via email to