0
|
7254 Views
|
10 Replies
|
2 Total Likes
View groups...
Share
GROUPS:

# Apply a function to multiple lists?

Posted 8 years ago
 I have a few functions, specifically, volume, number of fish and cost per fish. Here they are: vlm[nsides_, length_, depth_] := (1/4*nsides*length^2*Cot[\[Pi]/nsides]*depth); numfish[nsides_, length_, depth_] := Floor[vlm[nsides, length, depth]/.25]; costperfish[pondcost_, nsides_, length_, depth_] := pondcost/numfish[nsides, length, depth];  .25 refers to the space(volume) one fish can take up(this number could change). The pond cost, number of sides, length and depth could essentially be any inputs. Rather than using the apothem(from the area of a polygon) for the volume function, I found another formula which contains the Cot function(the one I have above).. I am trying to apply multiple list inputs to the cost per fish function. For example. costperfish[50,4,2.5,2] would result in the output 1, which is the cost per fish. I want to be able to type my function for multiple inputs, such as: costperfish[{50,4,2.5,2},{20,6,1,2.5}] and get the costs per each of these inputs. At some point I will need to apply a number to each cost, that will arrange the costs in order from least to greatest. So costperfish[{50,4,2.5,2},{20,6,1,2.5}] would essentially look like {{2,.8},{1,1.}}.. So I guess I asked 2 questions here but I am most concerned with inputting multiple lists so that the output will have each cost per fish for that input. Any suggestions or advice would be greatly helpful. I don't necessarily need an answer for the context of what I'm working on, even a simple example would help me in a more advanced problem, like this one.
10 Replies
Sort By:
Posted 8 years ago
 You may use Apply with the replaces heads at level 1 shortcut. costperfish @@@ {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}} Hope this helps.
Posted 8 years ago
 In using the input: data := {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}, {30, 5, 1, 2.5}, {20, 7, 1, 2.5}} ; Map[costperfish[#1, #2, #3, #4] & @@@ data] I get the output Map[{1, 4/5, 30/17, 5/9}] Almost as if Map didn't even do anything. For some reason, I'm having trouble using the Map command, I might just not use it..Thanks again for all your help and patience!Brandon
Posted 8 years ago
 Hi Brandon, you're using the map function with just 1 argument. that why it doesn't evaluate further. I would suggest you to invest some time on learning the Map and Apply function because they are very frequently used! What helped me was the introduction of Mr Shifrin http://www.mathprogramming-intro.org/ and the video of Mr Gaylord are also very nice http://www.wolfram.com/broadcast/s?sx=gaylord
Posted 8 years ago
 Hey there, That is the method I used, the function @@@ expression.Thanks for the links also!Best,Brandon
Posted 8 years ago
 Okay, I see now!I appreciate your clarification.I haven't used the Map command before but now I see what its doing. Thanks for clearing that up.Brandon
Posted 8 years ago
 My bad :) You can do it in a lot of ways.. costperfish[{pondcost_, nsides_, length_, depth_}] := N[ pondcost/numfish[nsides, length, depth]]; costperfishOriginal[pondcost_, nsides_, length_, depth_] := N[pondcost/numfish[nsides, length, depth]]; The costperfish has now a list as argument and that is exact what you want. Now it's easy to map it! data = {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}, {30, 5, 1, 2.5}, {20, 7, 1, 2.5}} Map[costperfish[#] &, data] or Map[Apply[costperfishOriginal, #] &, data] both give {1., 0.8, 1.76471, 0.555556} as a result
Posted 8 years ago
 Use Map[costperfish[#]&,{{50,4,2.5,2},{20,6,1,2.5}}] 
Posted 8 years ago
 Hi, thank you!I tried this code and it just gives me the same output as the input.. I will mess around with it and get it right sooner or later.I appreciate the help
Posted 8 years ago
 Or... testfunc[x_, y_, z_, t_] := x + y + z + t; testfunc[#1, #2, #3, #4] & @@@ {{1, 2, 3, 4}, {5, 6, 7, 8}} To me, it is not always intuitive as to how to use the "@" operator against lists, but interpreting the resultant error messages and trial and error often lead to the appropriate solution.
Posted 8 years ago
 Joel,This is fantastic. I appreciate the example, because I understand exactly how to apply this to my problem. I will explore the @ command because sure I got the output I needed, but not exactly WHY and WHAT its doing. In addition, I'm still trying to figure out the Map command like Ivan mentioned above.Thanks for your method,Brandon
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.