Reputation: 3256
For problem simplify(abs(1-b)+abs(1+b))
, I want maple to take out the abs
and get results for different ranges of b
.
Upvotes: 0
Views: 410
Reputation: 7246
Conversion of your expression to Maple's piecewise
structure gets you started.
restart;
expr1 := abs(1-b)+abs(1+b);
expr1 := |b - 1| + |1 + b|
convert(expr1, piecewise, b);
/ -2 b b < -1
|
< 2 b < 1
|
\ 2 b 1 <= b
And you can even turn that into a list.
PiecewiseTools:-ToList(%);
[[b < -1, -2 b], [b < 1, 2], [1 <= b, 2 b]]
But for either of the above representations you can notice that some of the information is implicit. The expr1
is identically equal to 2 when b<1
AND the prior condition(s) don't hold. It's implicit that you also need the negation of the first condition in order to get to the second. Maple handles that by proceeding through the conditions until it gets one that's satisfied, when it evaluates a piecewise
at a particular point (ie, at a value for b
).
We can write a procedure to obtain a more explicit (if somewhat redundant) representation.
F1 := proc(ee, x::name)
local T;
T := PiecewiseTools:-ToList(convert(ee,piecewise,x));
piecewise(seq([`and`(solve(`and`(seq(`not`(T[j,1]),j=1..i-1),
T[i,1]),{x})[]),
T[i,2]][],i=1..nops(T)));
end proc:
And, using that,
F1(expr1, b);
/ -2 b b < -1
|
< 2 -1 <= b and b < 1
|
\ 2 b 1 <= b
PiecewiseTools:-ToList(%);
[[b < -1, -2 b], [-1 <= b and b < 1, 2], [1 <= b, 2 b]]
Observe the difference between that and what we got before. Now the middle condition explicitly expresses the negation of the first.
We could write another procedure to express b
in terms of real ranges.
F2 := proc(ee, x::name)
local T;
T := PiecewiseTools:-ToList(convert(ee,piecewise,x));
piecewise(seq([x::solve(`and`(seq(`not`(T[j,1]),j=1..i-1),
T[i,1]),x),
T[i,2]][],i=1..nops(T)));
end proc:
And, using that,
F2(expr1, b);
/ -2 b b::(RealRange(-infinity, Open(-1)))
|
< 2 b::(RealRange(-1, Open(1)))
|
\ 2 b b::(RealRange(1, infinity))
PiecewiseTools:-ToList(%);
[[b::(RealRange(-infinity, Open(-1))), -2 b],
[b::(RealRange(-1, Open(1))), 2], [b::(RealRange(1, infinity)), 2 b]]
Note that those may not each be the widest ranges. We could have alternatively used solve
to find b::RealRange(-1,1)
for the second condition, since 2 = 2*b
when b=1
. The expression is continuous at b=1
where the second and third conditions meet. And similarly for the other conditions.
solve(expr1 = -2*b, b);
RealRange(-infinity, -1)
solve(expr1 = 2, b);
RealRange(-1, 1)
solve(expr1 = 2*b, b);
RealRange(1, infinity)
A similar thing holds for the inequality results from F1
. Obtaining these wider results with intervals closed at some ends (or non-strict inequalities) could be done using modified versions of the procedures, to use solve
instead of just negating all prior conditions at each stage. I haven't done that here.
Let's look at another example, for fun,
expr2 := abs(1-c)+abs(1+c)-abs(3-c)-abs(7+c);
expr2 := |c - 1| + |1 + c| - |c - 3| - |7 + c|
convert(expr2, piecewise, c);
/ 4 c < -7
|
| -2 c - 10 c < -1
|
< -8 c < 1
|
| 2 c - 10 c < 3
|
\ -4 3 <= c
F1(expr2, c);
/ 4 c < -7
|
| -2 c - 10 -7 <= c and c < -1
|
< -8 -1 <= c and c < 1
|
| 2 c - 10 1 <= c and c < 3
|
\ -4 3 <= c
PiecewiseTools:-ToList(%);
[[c < -7, 4], [-7 <= c and c < -1, -2 c - 10],
[-1 <= c and c < 1, -8],
[1 <= c and c < 3, 2 c - 10], [3 <= c, -4]]
F2(expr2, c);
/ 4 c::(RealRange(-infinity, Open(-7)))
|
| -2 c - 10 c::(RealRange(-7, Open(-1)))
|
< -8 c::(RealRange(-1, Open(1)))
|
| 2 c - 10 c::(RealRange(1, Open(3)))
|
\ -4 c::(RealRange(3, infinity))
PiecewiseTools:-ToList(%);
[[c::(RealRange(-infinity, Open(-7))), 4],
[c::(RealRange(-7, Open(-1))), -2 c - 10],
[c::(RealRange(-1, Open(1))), -8],
[c::(RealRange(1, Open(3))), 2 c - 10],
[c::(RealRange(3, infinity)), -4]]
We could plot these results (which I won't inline here).
plots:-display(Array([[plot(expr2,c=-8..4),
plot(convert(expr2,piecewise,c),c=-8..4)],
[plot(F1(expr2,c),c=-8..4),
plot(F2(expr2,c),c=-8..4)]]));
We could even handle other unknowns, although conversion to piecewise
might require assumptions.
expr3 := abs(1-c) + abs(1+c) - abs(3-c) - abs(K-c):
convert(expr3, piecewise, c) assuming K>1, K<3;
/ -3 - K c < -1
|
| -1 + 2 c - K c < 1
|
< 4 c - 3 - K c <= K
|
| 2 c - 3 + K c < 3
|
\ 3 + K 3 <= c
F1(expr3, c) assuming K>1, K<3;
/ -3 - K c < -1
|
| -1 + 2 c - K -1 <= c and c < 1
|
< 4 c - 3 - K 1 <= c and c <= K
|
| 2 c - 3 + K K < c and c < 3
|
\ 3 + K 3 <= c
Upvotes: 2