0

I've asked this question in MaplePrime, but wonder if any folks here can answer it as well.

I would like to find a way to represent each piece of a piecewise function in Maple.

For example, for a piecewise function:

f:=x->piecewise(1<=x<=2,c[1]*x+c[2],2<=x<=3,c[3]*x+c[4],3<=x<=4,c[5]*x+c[6]);

I want to have a way to retrieve each piece, e.g. c[3]*x+c[4], but f(x)[2] doesn't work.

Is it archievable?

Therkel
  • 1,332
dellair
  • 99
  • 2
    If you got an Answer at MaplePrime, you should explain what shortcomings you found in that answer so that Readers can help with the specific difficulty you are running into. It may be due to a misunderstanding of some proprietary details of Maple that are best explained by those with product support responsibility. – hardmath Apr 11 '17 at 01:28
  • 1
    MaplePrime suggest to use convert(f,pwlist,x), which is exactly what I've been looking for, the function op() also works as suggested in the answer. – dellair Apr 11 '17 at 14:36

2 Answers2

1

I have not used Maple in a while, but I believe your function should be defined as follows:

f:=x-piecewise(1<=x<=2,c[1]*x+c[2],x<=3,c[3]*x+c[4],x<=4,c[5]*x+c[6]);

You have defined $f(2)$ two different ways:

  1. $f(2)= 2c_1+c_2$ and also as

  2. $f(2)=2c_3+c_4$

I believe that the piecewise function assumes that the sequence of values of $x$ where the function changes is an increasing sequence. So when you give the first interval as $1\le x\le2$ and the second as $x\le3$ it automatically assumes that the second interval begins where the last interval ended, so in this case it assume that the second interval is $2<x\le3$. Certainly one should take care that the same value of $x$ (in this case $x=2$) is not defined in two different intervals.

  • Maple's piecewise function makes no assumptions that the bounds in the piecewise conditions are increasing: it simply proceeds from left to right and returns the first expression whose condition has been met. For this reason, your modified 'f' is not quite the same as the original one provided by @dellair: if you pass in x=0, your implementation will return c[4], while dellair's will fall off the end into the implicit "otherwise" clause which gives a result of 0. Yours is the better approach though, as it avoids checking the bounds twice for each branch. – saforrest Apr 12 '17 at 00:36
  • @saforrest Thanks for the correction. It's been years since I used Maple. – John Wayland Bales Apr 12 '17 at 01:46
1

I believe that

op(2,f(x))

is what you are looking for. If your piecewises are always fully defined (i.e. no 'otherwise' branch), then

[seq(op(2*i, f(x)), i=1..nops(f(x))/2)]

would give you all the pieces.