This is a case where you know the magnitude of a is less than 1 so for all 
the cases where you are working with constants in the integer range, 
replaceing `a` with a symbolic `1/2` will do the work for you. (Even 
stating that `a` is positive will work for several cases.)
```
>>> a = var('a',positive=True);eqs=Abs(a**2 + 2),Abs(2*a**2 - 1),Abs(a - 1)
>>> eqs
(a**2 + 2, Abs(2*a**2 - 1), Abs(a - 1))
>>> t = var('two',even=True,prime=True)
>>> [i.subs(a,1/two).subs(two,1/a) for i in eqs]
[a**2 + 2, 1 - 2*a**2, 1 - a]

>>> eqs = (Abs(a       - 1),
... Abs(a       - 2),
... Abs(2*a     - 3),
... Abs(a**2    - 2),
... Abs(a**2    - 3),
... Abs(a**2    - 11),
... Abs(2*a**2  - 1),
... Abs(2*a**2  - 3),
... Abs(3*a**2  - 2),
... Abs(3*a**2  - 16),
... Abs(4*a**2  - 3),
... Abs(5*a**2  - 4),
... Abs(5*a**2  - 7),
... Abs(5*a**2  - 16))
>>> eqs
(Abs(a - 1), Abs(a - 2), Abs(2*a - 3), Abs(a**2 - 2), Abs(a**2 - 3), 
Abs(a**2 -
11), Abs(2*a**2 - 1), Abs(2*a**2 - 3), Abs(3*a**2 - 2), Abs(3*a**2 - 16), 
Abs(4*
a**2 - 3), Abs(5*a**2 - 4), Abs(5*a**2 - 7), Abs(5*a**2 - 16))
>>> [i.subs(a,1/two).subs(two,1/a) for i in eqs]
[1 - a, 2 - a, 3 - 2*a, 2 - a**2, 3 - a**2, 11 - a**2, 1 - 2*a**2, 3 - 
2*a**2, 2
 - 3*a**2, 16 - 3*a**2, 3 - 4*a**2, 4 - 5*a**2, 7 - 5*a**2, 16 - 5*a**2]
```

If you want to target `Abs` in an arbitrary expression, the using `replace` 
would be a good way to go: `expr.replace(lambda x: isinstance(x, Abs), 
lambda x: x.subs(a,1/two).subs(two,1/a)`

/c

On Tuesday, March 30, 2021 at 9:44:51 AM UTC-5 balle...@googlemail.com 
wrote:

> 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 sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/c01fc8c0-adf2-4460-b00b-61297b6e2557n%40googlegroups.com.

Reply via email to