Re: Can I Map[] this code?

*To*: mathgroup at smc.vnet.net*Subject*: [mg95985] Re: [mg95928] Can I Map[] this code?*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Sat, 31 Jan 2009 06:42:54 -0500 (EST)*References*: <200901301042.FAA06428@smc.vnet.net>*Reply-to*: drmajorbob at longhorns.com

Mean, Outer, and Inner will do the trick: values = {{1, 1, 1, 1}, {1.03, 1.01, .90, .87}, {1.15, .987, 1.1, 1}, {.925, 1.1, 1.03, 1}}; points = {{0.0842331, 0.445974, 0.0762394, 0.393553}, {0.0507857, 0.316297, 0.217557, 0.415361}, {0.0772796, 0.715707, 0.0966355, 0.110378}, {0.407287, 0.0992621, 0.294046, 0.199405}, {0.115973, 0.483231, 0.125979, 0.274817}, {0.0819106, 0.64151, 0.0656714, 0.210908}, {0.0240671, 0.164786, 0.230353, 0.580795}, {0.0402821, 0.582524, 0.131371, 0.245823}, {0.374207, 0.242838, 0.0277256, 0.355229}, {0.130812, 0.459584, 0.331101, 0.078503}}; Clear[f1, f2] f2[vals_List, pts_List] := Inner[f2, pts, vals] f2[point_, 0] = 0; f2[point_, value_] := 1 + (value - 1)*point f1[points_, values_] := Table[Sum[ f2[points[[j, k]], values[[i, k]]], {j, 1, Length@points}, {k, 1, Length@values[[1]]}]/Length[points], {i, 1, Length@values}] f1[points, values] {4, 3.9551, 4.03137, 4.03591} Mean /@ Outer[f2, values, points, 1, 1] {4, 3.9551, 4.03137, 4.03591} It would have been a bit more intuitive if you'd defined f2 differently, with the arguments reversed (which requires redefining f1 as well): Clear[f1, f2] f2[vals_List, pts_List] := Inner[f2, vals, pts] f2[0, point_] = 0; f2[value_, point_] := 1 + (value - 1)*point f1[points_, values_] := Table[Sum[ f2[values[[i, k]], points[[j, k]]], {j, 1, Length@points}, {k, 1, Length@values[[1]]}]/Length[points], {i, 1, Length@values}] f1[points, values] {4, 3.9551, 4.03137, 4.03591} Mean /@ Outer[f2, values, points, 1, 1] {4, 3.9551, 4.03137, 4.03591} You could also use three functions: Clear[f1, f2, f3] f3[vals_List, pts_List] := Inner[f2, vals, pts] f2[0, point_] = 0; f2[value_, point_] := 1 + (value - 1)*point f1[points_, values_] := Table[Sum[ f2[values[[i, k]], points[[j, k]]], {j, 1, Length@points}, {k, 1, Length@values[[1]]}]/Length[points], {i, 1, Length@values}] f1[points, values] {4, 3.9551, 4.03137, 4.03591} Mean /@ Outer[f3, values, points, 1, 1] {4, 3.9551, 4.03137, 4.03591} Bobby On Fri, 30 Jan 2009 04:42:43 -0600, Andreas <aagas at ix.netcom.com> wrote: > As I have more experience with procedural programming, I'd like to > better understand how to use Mathematica's capabilities to do operations > to or mapping functions to lists. > > As an example I have two lists of lists: > > values = {{1, 1, 1, 1}, {1.03, 1.01, .90, .87}, {1.15, .987, 1.1, 1}, > {.925, 1.1, 1.03, 1}} > > points = { > {0.0842331, 0.445974, 0.0762394, 0.393553}, > {0.0507857, 0.316297, 0.217557, 0.415361}, > {0.0772796, 0.715707, 0.0966355, 0.110378}, > {0.407287, 0.0992621, 0.294046, 0.199405}, > {0.115973, 0.483231, 0.125979, 0.274817}, > {0.0819106, 0.64151, 0.0656714, 0.210908}, > {0.0240671, 0.164786, 0.230353, 0.580795}, > {0.0402821, 0.582524, 0.131371, 0.245823}, > {0.374207, 0.242838, 0.0277256, 0.355229}, > {0.130812, 0.459584, 0.331101, 0.078503} > } > > I have a named function: > > function2[point_, value_] := If[value == 0, 0, 1 + ((value - 1) * point)] > > That I want to apply across the elements in the list in a function like > the following: > > function1[points_, values_] := > Table[ > Sum[ > Sum[ > function2[points[[j, k]], values[[i, k]]], > {k, 1, Length[values[[1]]]} > ], {j, 1, Length[points]} > ]/Length[points], > {i, 1, Length[values]} > ] > > function1 works OK. I just wonder if I can do this kind of calculation > more elegantly using something like the Map[] function and how I would > do it in a situation like the above. > > I also have a concern about processing speed as the lengths of both > lists of lists can get quite large. > > The larger question goes to this. In what kind of situations should one > use iterative functions like Table[], Sum[], etc and when does one > better use Map[] or other such functions? What advantages and > disadvantages does either approach have over the other? > > Any guidance much appreciated. > -- DrMajorBob at longhorns.com

**References**:**Can I Map[] this code?***From:*Andreas <aagas@ix.netcom.com>