## 24 Reputation

16 years, 294 days

## MaplePrimes Activity

### These are answers submitted by Graham Gill

Thank you for your comments Robert and Acer.

Yes I do need the function to take on values other than the matrix entries M[i,j]. I need to be able to interpolate values. Currently I'm just using simple linear interpolation, but this might change. Yes, there is absolutely no need to have a table of ten separate functions f,...,f. A single function f(n,x) with 1<=n<=10 an integer, x in [1,18] real, would do just as well. It's just because of math: I usually write f_n(x) instead of f(n,x) in such cases.

Acer's remark on how to try avoiding flattening of sequences inside seq() solved the whole problem for me. All I needed was the correct application of unevaluation quotes, and the remark showed me the way.

As I expected, I can do it all in one line:

f := (i,x) -> piecewise( seq( 'j - 1 < x <= j, (j - x) * `if`(j = 1, 0, M[i,j - 1]) + (x - j + 1) * M[i,j]', j = 1..18 ) );

[Trying to create a table g of functions g to g in a single statement this way seems quite a bit more complicated unfortunately. Again it boils down to getting just the right unevaluations I think - I'm not sure because I haven't got it working this way. The following does not work correctly:

g := 'g'; assign( seq( g[i] = proc(x) piecewise( seq( 'j - 1 < x <= j, (j - x) * `if`(j = 1, 0, M[i,j - 1]) + (x - j + 1) * M[i,j]', j = 1..18 ) ) end proc, i = 1..10 ) );

(The initial unassignment of g is necessary to avoid even more complicated unevaluations inside the assign(), in the case that g has already been assigned, such as when testing out variations on such a statement repeatedly!) This statement does not produce the desired effect because the i inside the unevaluation quotes in the inner seq() remains in the proc() as the name i with global scope.]

The test

seq( seq( evalb( f(i,j) = M[i,j]), j = 1..18), i = 1..10 );

returns a sequence of 180 trues, as desired, and statements such as

f(3,5.5);

do the right thing. However, the pretty-printed version of f produced when f is defined or produced by op(f) does not appear correct, or is at least confusing. op(f) produces a case defined function with a single case, "otherwise", the case definition being the literal contents of the call to piecewise() in the definition of f above.