# How to substitute values from a list into a function?

I was hoping to take pairs of numbers from a list and substitute them into a function. So if my list was

list = {{1,2}, {3,4}, {5,6}}


and my function was

function = a x^b


The output I'm hoping to get is

result =1x^2 + 3x^4 + 5x^6


How would I best do this?

• #1 x^#2 & @@@ {{1, 2}, {3, 4}, {5, 6}} // Total – OkkesDulgerci Mar 6 '19 at 0:46
• You might find this post useful. – N.J.Evans Mar 6 '19 at 14:53
• – Michael E2 Aug 16 '19 at 15:58

This is not a Function:

function = a x^b


But this is:

function = {a,b} \[Function] a x^b


You can Apply it to each element of

list = {{1,2}, {3,4}, {5,6}}


with

function @@@ list


{x^3, 3 x^5, 5 x^7}

and sum it up with Total:

Total[ function @@@ list ]


x^3 + 3 x^5 + 5 x^7

• Why would you do 'function = {a,b} [Function] a x^b' instead of function[a_, b_, x_] = a*x^b? – Pineapple Mar 5 '19 at 23:45
• Because \[Function] is easily entered with the escape sequence esc f n esc . Also Function defines a pure function while function[a_, b_, x_] = a*x^b defines a replacement rule. However, they act the same way - most of the time. So it is a matter of taste. – Henrik Schumacher Mar 5 '19 at 23:49
• @Pineapple Take a look at this tutorial on Defining Functions. Another good resource is the tutorial on Immediate and Delayed Definitions for the difference between Set (=) and SetDelayed (:=). – MarcoB Mar 6 '19 at 0:03
• If you want a pure function, why not go with function = # x^#2 &? – Greg Martin Mar 6 '19 at 1:12
• @GregMartin Simply because I am a mathematician: I am more familiar with the $\mapsto$-syntax. Actually, I find the $\lambda$-calculus very hard to read, in particular when you try to produce functions that generate functions as their result. – Henrik Schumacher Mar 6 '19 at 8:05
Total[#*x^#2&@@@list]


x^2 + 3 x^4 + 5 x^6

Another way, perhaps easier on the eyes. Use a pattern to deconstruct the pairs in a function definition.

term[{a_, b_}] := a x^b


Then, Map it to the list.

Total[term /@ list]
(* x^2 + 3 x^4 + 5 x^6 *)


Supposing

function = a x^b


is already set and can't be changed. How to do what the OP wants?

One way to is to use ReplaceAll:

function /. Thread[{a, b} -> Transpose@list] // Total
(*  x^2 + 3 x^4 + 5 x^6  *)


Another standard method is to use Block:

Block[{a, b},
{a, b} = Transpose@list;
Total@function
]
(*  x^2 + 3 x^4 + 5 x^6  *)


If you like the functional solutions presented by others, function can be turned into a pure function as follows:

fn = Function @@ {{a, b}, function}       (* or *)
fn = Function[{a, b}, Evaluate@function]

Total[(#[] x^#[]) & /@ list]

• Amazing, thank you! Sorry - I'm very new to Mathematica. – Pineapple Mar 5 '19 at 23:44
ClearAll[fa, fb]
fa = FromCoefficientRules[Thread[#[[All, 2;;]] -> #[[All, 1]]], #2] &;
fb = InternalFromCoefficientList[Normal@SparseArray[1 + #[[All, 2;;]]->#[[All, 1]]], #2] &;


Examples:

list1 = {{1, 2}, {3, 4}, {5, 6}};
{fa[list1, x], fb[list1, x]}


{x^2 + 3 x^4 + 5 x^6, x^2 + 3 x^4 + 5 x^6}

list2 = {{1, 3, 0}, {3, 2, 1}, {3, 1, 2}, {1, 0, 3}};
{fa[list2, {x, y}], fb[list2, {x, y}]}


{x^3 + 3 x^2 y + 3 x y^2 + y^3, x^3 + 3 x^2 y + 3 x y^2 + y^3}

list = {{1, 2}, {3, 4}, {5, 6}};
list /. {a_, i_} -> a x^i // Total


x^2 + 3 x^4 + 5 x^6

f = Apply[# . x^#2 &] @* Transpose;

f @ list


x^2 + 3 x^4 + 5 x^6

and

☺ = # x^#2&@@@#&/*Tr;
☺ @ list


x^2 + 3 x^4 + 5 x^6

☺☺ = +## & @@ (# x^#2 & @@@ #) &;
☺☺ @ list


x^2 + 3 x^4 + 5 x^6

☺☺☺ = #.x^#2 & @@ (#\[Transpose])&;
☺☺☺ @ list


x^2 + 3 x^4 + 5 x^6

Inner[#1 x^#2 &, Sequence @@ Transpose@list, Plus]


x^2 + 3 x^4 + 5 x^6

One could expand this a bit to allow for different variables:

Clear[f]
f[coefflist_][var_] := Inner[#1 var^#2 &, Sequence @@ Transpose@coefflist, Plus]


so that

f[list][x]


x^2 + 3 x^4 + 5 x^6

but then:

f[list][t]
`

t^2 + 3 t^4 + 5 t^6