Friday 15 May 2015

Mathematica: Applying a function to a list of list -


I have a function f [d _] . I intend to list this function as a complete list. Say, d = {1,2,3, ...} , then f [d] gives me results (a number or whatever). So far, everything is clear.

Now that I have the following list of lists: p = {l1, l2, l3, ...} Is there a more efficient f [p] Method to compare the map to calculate where the expected result is {f [l1], f [l2], f [l3], ...} ?

For example, with the SIN [x] function, it's mapping in the list is slow only by putting the list inside the logic, it's my function f [d] ] And the lists do not seem to work with the list of P. What should I do to do this work? Can this be faster than the map?

To clarify yourself,

  f [d _]: = total [d] then, f [{a1, a2, a3}] Gives 1 + A2 + A3, as expected, but kills the machine {{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3, c4}}]   

Thanks!

When I change my code, it returns: {<{a1, b1, c1} {B1, b2, b3}, {c1, c2, c3}}} {a1 + b1 + c1, b1 + b2} When a logic takes a list, it only connects the members, in your case, it adds A1, b1, c1}, {b1, b2, b3}, {c1, c2, c3} >

But they are not equal dimensions, so you can not get the correct answer.

In this situation, the map should be used.

  Map [total, {{a1, b1, c1}, {b1, b2, b3}, {c1, c2, c3, c4}}]   

or

  plus @@@ {{a1, b1, c1}, {b1, b2, b3} {c1, c2, c3, c4}}    

No comments:

Post a Comment