Function Repository Resource:

PowerTotal

Source Notebook

Compute the total of a list of numbers all taken to some power

Contributed by: Seth J. Chandler

ResourceFunction["PowerTotal"][y,n,x]

takes x to the yth power and then applies the Total function using level specification n.

ResourceFunction["PowerTotal"][y,n]

represents an operator form of ResourceFunction["PowerTotal"] that can be applied to an expression x.

ResourceFunction["PowerTotal"][y]

represents an operator form of ResourceFunction["PowerTotal"] that, when applied to x, computes the total at the top level.

ResourceFunction["PowerTotal"][]

represents an operator form of ResourceFunction["PowerTotal"] that, when applied to x, takes the sum of the squares.

Details and Options

Both x and y can be real, complex or symbolic.
The level specification n may take the form of:
an integer n, meaning the summation is performed at all levels down to level n;
an integer n wrapped with List, meaning the summation is performed at level n only; and
a List of two integers {n1,n2}, meaning the summation is performed at all levels n1 through n2.
See the documentation for Total for a more complete explanation of how the level specification works there.

Examples

Basic Examples (4) 

Compute the sum of the squares of a list:

In[1]:=
ResourceFunction["PowerTotal"][][{3, 4}]
Out[1]=

Compute the sum of the cubes of a list with symbolic parts:

In[2]:=
ResourceFunction["PowerTotal"][3][{a, b, c}]
Out[2]=

Compute the sum of the cubes of a symbolic array:

In[3]:=
ResourceFunction["PowerTotal"][3, 2][Array[x, {5, 2}]]
Out[3]=

Create an operator which when confronted with an expression computes the sum of its square roots:

In[4]:=
ResourceFunction["PowerTotal"][1/2][{3, 4}]
Out[4]=

Scope (5) 

The power may be complex, as may the list:

In[5]:=
ResourceFunction["PowerTotal"][2 + I][{3 + I, 4.2 - I}]
Out[5]=

The level specification can affect the results when the data has more than one dimension:

In[6]:=
t = Array[Subscript[a, ##] &, {2, 3, 4}]
Out[6]=

The default behavior is to apply at level 1:

In[7]:=
ResourceFunction["PowerTotal"][2][t]
Out[7]=

Apply the total down to level 2:

In[8]:=
ResourceFunction["PowerTotal"][2, 2][t]
Out[8]=

Apply the total in the last two dimensions:

In[9]:=
ResourceFunction["PowerTotal"][2, {-2, -1}][t]
Out[9]=

Applications (3) 

Use PowerTotal to conduct ordinary least squares linear regression by finding the values of two parameters a and b that minimize the sum of the squared distances between the actual value of the independent variable and a value that depends on a and b:

In[10]:=
Minimize[ResourceFunction["PowerTotal"][2][
  Map[5 # - 6 &][{3, 4, 5}] - Map[a *# + b &][{3, 4, 5}]], {a, b}]
Out[10]=

Use PowerTotal to perform "Tikhonov" (ridge) regression:

In[11]:=
Minimize[{ResourceFunction["PowerTotal"][2][
    Map[5 # - 6 &][{3, 4, 5}] - Map[a *# + b &][{3, 4, 5}]], ResourceFunction["PowerTotal"][2][{a, b}] < 59}, {a, b}] // N
Out[11]=

Use PowerTotal to perform "LASSO" regression:

In[12]:=
Minimize[{ResourceFunction["PowerTotal"][2][
    Map[5 # - 6 &][{3, 4, 5}] - Map[a *# + b &][{3, 4, 5}]], ResourceFunction["PowerTotal"][1][Abs@{a, b}] < 10}, {a, b}] // N
Out[12]=

Properties and Relations (2) 

PowerTotal[] is the same as the square of the results from the Norm function if the arguments it confronts are real-valued, but is not necessarily the same if the values it confronts are complex:

In[13]:=
ResourceFunction["PowerTotal"][][{3, 4}] == Norm[{3, 4}]^2
Out[13]=
In[14]:=
ResourceFunction["PowerTotal"][][{3 + I, 4 - I}] == Norm[{3 + I, 4 - I}]^2
Out[14]=

PowerTotal is the same as Total if the power argument is 1:

In[15]:=
ResourceFunction["PowerTotal"][1, {2, 3}][
  Array[x, {3, 4, 5, 6, 7, 8}]] == Total[Array[x, {3, 4, 5, 6, 7, 8}], {2, 3}]
Out[15]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 30 December 2019

License Information