7
$\begingroup$

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?

$\endgroup$
8
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Why would you do 'function = {a,b} [Function] a x^b' instead of function[a_, b_, x_] = a*x^b? $\endgroup$ – Pineapple Mar 5 at 23:45
  • 1
    $\begingroup$ 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. $\endgroup$ – Henrik Schumacher Mar 5 at 23:49
  • $\begingroup$ @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 (:=). $\endgroup$ – MarcoB Mar 6 at 0:03
  • 1
    $\begingroup$ If you want a pure function, why not go with function = # x^#2 &? $\endgroup$ – Greg Martin Mar 6 at 1:12
  • $\begingroup$ @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. $\endgroup$ – Henrik Schumacher Mar 6 at 8:05
7
$\begingroup$
Total[#*x^#2&@@@list]

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

$\endgroup$
5
$\begingroup$

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 *)
$\endgroup$
4
$\begingroup$

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]
$\endgroup$
3
$\begingroup$
Total[(#[[1]] x^#[[2]]) & /@ list]
$\endgroup$
  • $\begingroup$ Amazing, thank you! Sorry - I'm very new to Mathematica. $\endgroup$ – Pineapple Mar 5 at 23:44
3
$\begingroup$
ClearAll[fa, fb]
fa = FromCoefficientRules[Thread[#[[All, 2;;]] -> #[[All, 1]]], #2] &;
fb = Internal`FromCoefficientList[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}

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

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

$\endgroup$
2
$\begingroup$
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

$\endgroup$
1
$\begingroup$
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.