Mathematica Mathematica: array of functions

Click For Summary
In this discussion, a user seeks assistance with Mathematica for creating a list of values derived from a set of functions and a vector. The user has defined a list of functions and a vector of values but is unsure how to apply each function to the corresponding value in the vector. The solution involves using the Transpose function to pair the functions with the values, followed by an anonymous function to replace variables within the functions. Another user suggests defining the functions in a more structured way, allowing for easier application to the vector elements. The discussion also touches on understanding the [[]] operator, which retrieves specific elements from lists, clarifying its use in the context of applying functions to the vector. Overall, the conversation emphasizes methods for efficiently manipulating lists and functions in Mathematica.
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
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K