Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.1K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to set a certain value to each place in a list of functions?

Posted 10 years ago

I have a list of functions and i want to set different values to each function. How i can do it?

Best regards Franz

POSTED BY: Franz Zwergel
6 Replies
Posted 10 years ago

Thank you very much. It works perfectly =)

POSTED BY: Franz Zwergel
Posted 10 years ago

The list you have generated is presumably a list of expressions in the variable x. I can't generate them because of the missing definitions in your code.

But we want to do the same thing, but rather than treat the first list as a list of functions which act upon corresponding elements in the second list, we want to treat the first list as expressions, with x to be replaced by corresponding elements. In the code below, I explicitly defined the function corresponding to Times in the conventional inner product, rather than using a "pure function" as I did before. I did this for clarity -- either method would work. For the /. syntax, look up Replace and also Rules.

In[1]:= expressions = Table[n x, {n, 3}]

Out[1]= {x, 2 x, 3 x}

In[2]:= xValues = {1, 2, 3}

Out[2]= {1, 2, 3}

In[3]:= replaceX[expression_, value_] := expression /. x -> value

In[4]:= Inner[replaceX, expressions, xValues, List]

Out[4]= {1, 4, 9}

Edit: MapThread is simpler:

In[8]:= MapThread[replaceX, {expressions, xValues}]

Out[8]= {1, 4, 9}
POSTED BY: David Keith
Posted 10 years ago

Hi, thank you. I think it is the right ansatz but something doesn't work.

In: dharmfunc = Table[D[harmcoeff[[i, 1]]x^2 + harmcoeff[[i, 2]]x + harmcoeff[[i, 3]], {x, 1}], {i, 1, Length[dfinum] - 3}]; xValues = Table[inc*i + xinit - inc, {i, 1, Length[dfinum] - 3}]; dharmvalues = Inner[#1[#2] &, dharmfunc, xValues, List]; dharmvalues[[3]]

Out[126]= (-18.4034 - 3.746 x)[-29.4] The value -29.4 is not set as the value of the x and i don't know why.

POSTED BY: Franz Zwergel
Posted 10 years ago

Hi Franz,

One way to do this is by using the generalized inner product:

In[1]:= Inner[#1[#2] &, {f, g, h}, {a, b, c}, List]

Out[1]= {f[a], g[b], h[c]}

In[2]:= ff[x_] := x; gg[x_] := 2 x; hh[x_] := 3 x;

In[3]:= Inner[#1[#2] &, {ff, gg, hh}, {a, b, c}, List]

Out[3]= {a, 2 b, 3 c}
POSTED BY: David Keith
Posted 10 years ago

This was not my problem. I try to explain it =) I have a list. In each place of this list is a different function. I want to set the variable of each function to a certain (but different) value.

dharmfunc = Table[D[harmcoeff[[i, 1]]x^2 + harmcoeff[[i, 2]]x + harmcoeff[[i, 3]], {x, 1}], {i, 1, Length[dfinum] - 3}]

That is my list. I want to achieve a list with no functions but with a certain value in each place derived from the former function.

POSTED BY: Franz Zwergel

Hi,

I am not really sure whether I fully understand what you want to achieve. You have different functions, say g,f.h and you want to apply them to values in a list say a,b,c. They this would do.

Outer[MapAll, {g[#] &, f[#] &, h[#] &} , {a, b, c}]
(* {{g[a], g[b], g[c]}, {f[a], f[b], f[c]}, {h[a], h[b], h[c]}}*)

If you display it in MatrixForm it looks like this:

enter image description here

So every function is applied to all elements of the list.

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard