Mathematica: array of functions

Click For Summary

Discussion Overview

The discussion revolves around using Mathematica to create an array of function outputs based on a list of inputs. Participants explore methods to apply a list of functions to corresponding values from another list, focusing on the syntax and commands necessary to achieve this in Mathematica.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant, Anna, describes her challenge in applying a list of functions to a list of values in Mathematica and shares her expected output.
  • Another participant suggests using the Transpose function to pair the values from the two lists and then applying an anonymous function to replace variables within the functions.
  • A different approach is proposed where functions are defined with a variable, allowing for a more straightforward application of the inputs using the Table command.
  • Anna requests clarification on the syntax used in the suggested methods, particularly regarding the [[]] operator and its application in accessing elements of lists.
  • Further clarification is provided about the [[]] operator, explaining how it retrieves specific elements from lists and how it is used in conjunction with function applications.

Areas of Agreement / Disagreement

Participants generally agree on the methods to apply functions to lists but express different preferences for syntax and clarity in understanding the commands. There is no explicit consensus on a single best approach.

Contextual Notes

Some participants note the importance of understanding the specific commands and syntax in Mathematica, indicating that familiarity with the language's operators is crucial for effective problem-solving.

Who May Find This Useful

This discussion may be useful for Mathematica users, particularly those new to the software, who are looking to understand how to manipulate lists and functions effectively.

Anna Kaladze
Messages
34
Reaction score
0
Hi intelligent people,

Forgive me for a silly question, but I am really new to Mathematica and thus I need your help for my own research (I am done constructing NIntegrate values for some "non-integrable" functions, involving multiple steps, and I am kinda stuck at the last step. Here is the example which will help me if I know how to crack it).
OK, Here are the series of simple inputs-outputs in the command window:

f:={1+t^2,1/t,5-t}

f

{1+t2,1/t,5-t}

j=Table[x,{x,0,2,1}]

{0,1,2}

All I need to do, is to create a new list, where the first function in f list (i.e. 1+t^2) would take as an argument the first value in j vector (0), the 2nd function in f (i.e. 1/t) would take as an argument the 2nd element in j vector (2), and so on and then display the resulting values in an array. I tried to use some Table commands, and basic looping, could not go far ... The answer is apparently this:

{0,1,3}

But the question is how to get to there...

Many thanks,

Anna.
 
Physics news on Phys.org
I suppose the quickest way to do it is construct a list of expressions and values first:
Code:
xy = Transpose[{j, f[t]}]
gives {{0, 1 + t^2}, {1, 1/t}, {2, 5 - t}}

Then you can replace the t's in the second list by the values in the first list, using an anonymous function with two arguments:
Code:
Function[{x, y}, y /. t -> x] @@@ xy
Note that you need the @@@ to properly pass the arguments (you can see what happens if you do
Code:
g @@@ xy
But I was wondering, if f is a function, why don't you define it as such?
Code:
f[t_]:={1+t^2,1/t,5-t}
J = Range[0, 2, 1];
Then you can also do something like
Code:
Table[f[j[[i]]][[i]], {i, 1, Length[j]}]
 
Dera CompuChip,
Thanks a lot for your answers, they are really great!
Can you please help me to find out where I can read about "[[]]" command/operators to understand your 2nd method better? Wolfram online gives basic info about tables, etc. only. I am a little confused what "f[j[]][]" does, and more so about the very last [] part. Your method works fine, just would like to understand it better (the 1st one made perfect sense).
Thanks again for your time.
Anna.
 
list[] takes the i'th element from a list, for example, if
Code:
list = {2, 4, 6, 8}
then list[[1]] = 2, list[[2]] = 4, list[[3]] = 6 and list[[4]] = 8; list[[5]] will give an error.

So basically what I am doing is taking the i'th x-value from j and applying f to that.
Code:
f[j[[i]]]
Now f produces a list again. Since we are interested in the i'th (for the same value of i) output, we need to use the [[ ]] operator again. So
Code:
f[j[[i]]][[i]]

I hate defining variables when it is not necessary, but just for educational purposes, you could write it like this:
Code:
i = 3; (* for example, let's look at the third element *)
x = j[[i]]; (* or x = Part[j, i] or x = Take[j, {i}] *)
yArray = f[x];
y = yArray[[i]]; (* this is the value you want *)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K