Calculate Pure Function in Map - Mathematica

In summary, the conversation discusses using a function in Mathematica, where a # symbol is used as a placeholder for an argument. The issue arises when trying to use the same symbol for multiple arguments in a Map function. A For loop is suggested as a solution, but the person wants to use it in a Plot or ListPlot instead.
  • #1
Barioth
49
0
Hi!

Let's say in mathematica I declare this function

t[x_,y_]:= (x'+y')^2

Now I can call it with

\(\displaystyle L=1;\)
t[(#^2)+L &, (#^3)+L &]

if I call it this way it will remplace the # with s and evalute the derivative.

Now let's say I wana do this for for every L from 1 to 10.

so i got

Map[t[(#^2)+# &, (#^3)+# &],Range[1,10]]

and indeed this doesn't work. how can I tell mathematica that I want a # for my function t and one different # to use as my argument in my Map?

(*Note t I know that in this problem in make no sense to change the value of L since we're calculating the derivative. But I want to know for general purpose)
 
Physics news on Phys.org
  • #2
I'm afraid I'm not terribly familiar with pure functions in MMA. However, I do have one question: could you do this in a For loop to get all your L values?
 
  • #3
Ackbach said:
I'm afraid I'm not terribly familiar with pure functions in MMA. However, I do have one question: could you do this in a For loop to get all your L values?
A for loop would work, altough I want to use this in a Plot or a ListPlot. (I could do it with a For and save what's come out. But I would feel like its a ''Dirty'' solution.)

Thanks for passing by!
 

FAQ: Calculate Pure Function in Map - Mathematica

What is a pure function in Mathematica?

A pure function in Mathematica is a function that does not rely on any external variables or definitions, and only operates on its input arguments. It is denoted by the symbol # or #n, where n is the position of the argument in the function. Pure functions are useful for concise and efficient code, especially when used in functions like Map.

How do I calculate a pure function using Map in Mathematica?

To calculate a pure function using Map in Mathematica, you can use the syntax: Map[function, list]. This applies the function to each element in the list and returns a new list with the results. For example, to square each element in a list of numbers, you can use the code: Map[#^2&, {1, 2, 3, 4, 5}].

Can I use pure functions with multiple arguments in Map?

Yes, you can use pure functions with multiple arguments in Map by using the syntax: Map[function[#1, #2, ...], list]. This will apply the function to each element in the list, using the corresponding arguments from the list. For example, to add two lists element-wise, you can use the code: Map[#1 + #2&, {1, 2, 3}, {4, 5, 6}].

How do I use other functions within a pure function in Map?

You can use other functions within a pure function in Map by using the syntax: Map[function[#]&@otherfunction[#], list]. This will apply the other function to each element in the list, and then apply the pure function to the result. For example, to find the square root of each element in a list of numbers, you can use the code: Map[Sqrt[#]&@#^2&, {1, 4, 9, 16, 25}].

Are there any advantages to using pure functions in Map over other methods?

Yes, there are several advantages to using pure functions in Map. They are concise and efficient, as they do not require external definitions or variables. They also allow for easy parallelization, as each element in the list can be processed independently. Additionally, pure functions can be easily applied to lists of any type, making them versatile for a variety of tasks.

Similar threads

Replies
1
Views
744
Replies
5
Views
2K
Replies
2
Views
473
Replies
2
Views
2K
Replies
1
Views
2K
Replies
19
Views
1K
Replies
2
Views
2K
Back
Top