0

1. My main question

I have a function with two argument slots. I wan't to apply this function to 2 lists with different length's. I thought in this solution:

Map[Map[f[# &, #], b] &, c]

But it doesn't work. Why is that?

Example

f[x_, y_] := Sin[x y]


  b = {1, 2}
  c = {1, 2, 3}

The output seems pretty close of what i wanted but not close enough:

{{Sin[#1 &][1], Sin[#1 &][2]}, {Sin[2 (#1 &)][1], 
  Sin[2 (#1 &)][2]}, {Sin[3 (#1 &)][1], Sin[3 (#1 &)][2]}}

2. It seems that i only need to take the &'s out of the square brackets.

a) Is it so? Why?

b) how can i do that?

Thanks

2 Answers2

0

You could use Outer as in :

Outer[Sin[#1 #2] &, {1, 2}, {1, 2, 3}]
(* {{Sin[1], Sin[2], Sin[3]}, {Sin[2], Sin[4], Sin[6]}} *)
b.gatessucks
  • 1,242
  • 1
  • 15
  • 19
  • Thank you. I was aware of outer. But in my case i needed something more general.This question results from another that i asked [here](http://stackoverflow.com/questions/15952918/table-tree-of-values). My goal is to be able to apply a function to different kind of structured data. What i mean is that the each element of the output list has as arugments a "transformation" of the "arguments". In this case, that transformation happend to be covered by outer – João Cortes Apr 13 '13 at 15:40
0

you can use the two arg form of function to name one of the parameters..

Map[Map[Function[ci,f[ci, #]], b] &, c]

Outer works great for this example, but named pure function args are useful for more general cases..Often they aid readability even if not strictly necessary.

agentp
  • 6,849
  • 2
  • 19
  • 37
  • Thank you.I'm trying to create a routine that receives a function, a list of arguments, and a "map of calculation" and outputs a list of results. I gave an example on [here](http://stackoverflow.com/questions/15952918/table-tree-of-values).In this particular case, tha "map of calculation" can be obtained with outer – João Cortes Apr 13 '13 at 15:45