1
$\begingroup$

Three days ago, I asked a question about B-Spline Basis fuction here

@Michael E2 given a solution to make the order of interval looks normal

The easiest way would be to sort it afterwards: MapAt[SortBy[Last], piecewisefn, 1], piecewisefn is the result of NBSpline. (Use SortBy[#, Last] & instead of SortBy[Last] if you're using V9 or earlier.)

Trial 1

 sortResult[res_] := MapAt[SortBy[#, Last] &, res, 1] /; res != 0
 sortResult[0] := 0;
 knots={1, 2, 2, 4, 5, 7};
 sortResult[NBSpline[1, 2, u, knots]]

enter image description here

Trial 2

 NBSpline[1, 3, u, {1, 2, 2, 4, 5, 7}]// MapAt[SortBy[#, Last] &, #, 1] &

enter image description here

This time, it gives me a normal order $2 \leq u <4, 4 \leq u <5, 5 \leq u <7$

Question:

How to revise it ? And can someone give me a explanation about this case?

$\endgroup$
  • $\begingroup$ Do you get the desired result if you change the last line of Trial 1 to tmp = NBSpline[1, 2, u, knots]; sortResult[tmp]; tmp? $\endgroup$ – kglr Oct 18 '14 at 7:43
  • $\begingroup$ @kguler, it didn't sort the $interval$. $\endgroup$ – xyz Oct 18 '14 at 7:47
3
$\begingroup$

I hope I understand your question. I think you are wondering why Trial 1 does not work the same as Trial 2. In that case the answer lies with != or Unequal. The Condition will not match because != does not evaluate when one side is symbolic and the other numeric. Instead you should use:

sortResult[x_ /; x == 0] := 0;
sortResult[res_] := MapAt[SortBy[#, Last] &, res, 1]

The first definition will catch all arguments Equal to zero. See:

$\endgroup$
  • $\begingroup$ +2, It works well, thanks a lot!:-) $\endgroup$ – xyz Oct 18 '14 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.