7

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?

Pineapple
  • 111
  • 4

9 Answers9

8

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

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • Why would you do 'function = {a,b} [Function] a x^b' instead of function[a_, b_, x_] = a*x^b? – Pineapple Mar 05 '19 at 23:45
  • 1
    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 05 '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 06 '19 at 00:03
  • 1
    If you want a pure function, why not go with function = # x^#2 &? – Greg Martin Mar 06 '19 at 01: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 06 '19 at 08:05
7
Total[#*x^#2&@@@list]

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

ZaMoC
  • 6,697
  • 11
  • 31
5

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 *)
John Doty
  • 13,712
  • 1
  • 22
  • 42
4

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]
Michael E2
  • 235,386
  • 17
  • 334
  • 747
3
Total[(#[[1]] x^#[[2]]) & /@ list]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
3
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}

kglr
  • 394,356
  • 18
  • 477
  • 896
3
list = {{1, 2}, {3, 4}, {5, 6}};
list /. {a_, i_} -> a x^i // Total

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

a.p.
  • 31
  • 1
2
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

kglr
  • 394,356
  • 18
  • 477
  • 896
1
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

MarcoB
  • 67,153
  • 18
  • 91
  • 189