Is there an easier way to obtain the results of computations like the following? >A:={ { [1,2],[3,4] } , { [5,6],[7,8] } }: > map(x->map(y->map(f,y),x),A); { {[f(1), f(2)], [f(3), f(4)]}, {[f(5), f(6)], [f(7), f(8)]} } --I would appreciate any conceptually simpler ways of doing this. Too many nested maps makes me dizzy. --Edwin
![]() |
0 |
![]() |
Edwin Clark <wclark1@tampabay.rr.com> writes: > Is there an easier way to obtain the results of computations like the > following? > > >>A:={ { [1,2],[3,4] } , { [5,6],[7,8] } }: >> map(x->map(y->map(f,y),x),A); > > { {[f(1), f(2)], [f(3), f(4)]}, {[f(5), f(6)], [f(7), f(8)]} } An easy way to accomplish this is to use evalindets (or subsindets). It has an undocumented flat option that prevents further recursing once the type parameter matches. Here you could do evalindets[flat](A, algebraic, f); { {[f(1), f(2)], [f(3), f(4)]}, {[f(5), f(6)], [f(7), f(8)]} } A better example that illustrates the effect of flat is the following A := { { [a+1,2],[3,4] } , { [5,6],[7,8] } }: evalindets[flat](A, algebraic, f); {{[f(3), f(4)], [f(a + 1), f(2)]}, {[f(5), f(6)], [f(7), f(8)]}} evalindets(A, algebraic, f); f(2) {{[f(3), f(4)], [f(f(a) f(1) + f(1) ), f(2)]}, {[f(5), f(6)], [f(7), f(8)]}} Somewhat safer is evalindets[flat](A, list, curry(map,f)); {{[f(3), f(4)], [f(a + 1), f(2)]}, {[f(5), f(6)], [f(7), f(8)]}} That works fine without the flat option. -- Joe Riel
![]() |
0 |
![]() |
Edwin Clark <wclark1@tampabay.rr.com> writes: > Is there an easier way to obtain the results of computations like the > following? > > >>A:={ { [1,2],[3,4] } , { [5,6],[7,8] } }: >> map(x->map(y->map(f,y),x),A); > > { {[f(1), f(2)], [f(3), f(4)]}, {[f(5), f(6)], [f(7), f(8)]} } > > --I would appreciate any conceptually simpler ways of doing this. Too > many nested maps makes me dizzy. Here's a different approach. Assign the function maptolev := proc(lev::nonnegint, f, e) local i; apply(seq(map[lev-i],i=0..lev-1),f,e) end proc: maptolev(3, f, A); { {[f(1), f(2)], [f(3), f(4)]}, {[f(5), f(6)], [f(7), f(8)]} } -- Joe Riel
![]() |
0 |
![]() |
Joe, Thanks for the suggestions. I think I like > evalindets(A, list, curry(map,f)); best--so far. Edwin
![]() |
0 |
![]() |