# 2.2.3.9.12 matrixbase::CumulativeProduct

## Description

Compute the cumulative product of this matrix

## Syntax

int CumulativeProduct( matrixbase & mbProduct, int nDim = 1 )

## Parameters

mbProduct
[output] The result matrix containing the cumulative product
nDim
[input] The CumulativeProduct function can operate row wise (relative to the first dimension when nDim=1) or column wise (relative to the second dimension when nDim=2)

## Return

Returns 0 on success and a non-zero error code on failure.

## Examples

EX1

void matrixbase_CumulativeProduct_ex1()
{
matrix<double> mat1 = {
{1, 1, 1},
{2, 2, 2},
{3, 3, 3}
};
int rc = mat1.CumulativeProduct(mat1);
if(rc!=0)
printf("Error: CumulativeProduct failed. rc=%d\n", rc);
else{
printf("The matrix is:\n");
for(int ii=0; ii< mat1.GetNumRows(); ii++){
for(int jj=0; jj< mat1.GetNumCols(); jj++)
printf("%g  ", mat1[ii][jj]);
printf("\r\n");
}
}
}

EX2

// Cumulative product of a matrix
void matrixbase_CumulativeProduct_ex2()
{
int rc, rc2;

matrix<double> mat1 = {
{1, 1, 1},
{2, 2, 2},
{3, 3, 3}
};

matrix<double> mat2; // result matrix
matrix<double> mat3; // result matrix

// Comutative Product of mat1 (row-wise):
//   {1,  1,  1}
//   {2,  2,  2}
//   {6,  6,  6}
// that is, each column is:
//   {1    }
//   {1*2  }
//   {1*2*3}
//
// Comutative Product of mat1 (column-wise):
//   {1,  1,  1}
//   {2,  4,  8}
//   {3,  9, 27}
// that is,
//   {1,  1^2, 1^3}
//   {2,  2^2, 2^3}
//   {3,  3^2, 3^3}

MatrixPage MatPg1;
MatPg1.Create("Origin");
MatrixLayer MatLy1 = MatPg1.Layers(0);
Matrix Mat1(MatLy1);
Mat1 = mat1;

MatrixPage MatPg2;
MatPg2.Create("Origin");
MatrixLayer MatLy2 = MatPg2.Layers(0);
Matrix Mat2(MatLy2);

MatrixPage MatPg3;
MatPg3.Create("Origin");
MatrixLayer MatLy3 = MatPg3.Layers(0);
Matrix Mat3(MatLy3);

rc=mat1.CumulativeProduct(mat2);  // Assign the row-wise cumulative product of mat1 to mat2
if(rc!=0)
printf("  Error: ComutativeProduct(row-wise) on %s by %s failed. rc=%d\n",
Mat1.GetName(),Mat2.GetName(),rc);
else
{
Mat2 = mat2;
printf("  Comutative Product(row wise) of %s = %s\n",
Mat1.GetName(), Mat2.GetName());
}

rc2=mat1.CumulativeProduct(mat3,2);  // Assign the column-wise cumulative product of mat1 to mat3
if(rc2!=0)
printf("  Error: ComutativeProduct(column-wise) on %s by %s failed. rc=%d\n",
Mat1.GetName(),Mat3.GetName(),rc);
else
{
Mat3 = mat3;
printf("  Comutative Product(column wise) of %s = %s\n",
Mat1.GetName(), Mat3.GetName());
}
}

EX3

#define MAKEMAT(X) MatrixLayer MatLy##X;MatLy##X.Create();Matrix Mat##X(MatLy##X)

void matrixbase_CumulativeProduct_ex3()

{
matrix mat1 = {
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}
};

matrix mat2 = {
{2, 3, 4},
{3, 4, 5},
{4, 5, 6}
};

MAKEMAT(1); MAKEMAT(2); MAKEMAT(3); MAKEMAT(4);
MAKEMAT(5); MAKEMAT(6);MAKEMAT(7);

Mat1=mat1; Mat2=mat2;

Mat3=mat1*mat2;
printf("%s = %s * %s <== proper multiplication of 2 matrices\n",Mat3.GetName(),Mat1.GetName(),Mat2.GetName());

Mat4=mat1*10;
printf("%s = 10 * %s <== scalr multiplication of a matrix\n",Mat4.GetName(),Mat1.GetName());

Mat5=mat1;
Mat5.DotMultiply(mat2);
printf("%s = DotMultiply(%s,%s) <== element-wise multiplication of 2 matrices\n",Mat5.GetName(),Mat1.GetName(),Mat2.GetName());

Mat6=mat1;
Mat6.Cross(mat2);
printf("%s = Cross(%s,%s) <== cross product of 2 matrices\n",Mat6.GetName(),Mat1.GetName(),Mat2.GetName());

mat1.CumulativeProduct(Mat7);
printf("%s = CumulativeProduct(%s) <== cumulative product of a matrix\n",Mat7.GetName(),Mat1.GetName());
}

## Remark

Compute the cumulative product of this matrix. The underlying base type of this matrix must be less than or equal to the underlying base type of the result matrix. Currently only the double and complex types are supported for the result matrix.

Note

The following is a summary of various matrix operations:

1) Multiplication:

1-1) * ... Proper multiplication of 2 matrices (mat1*mat2)

1-2) * ... Scaler multiplication of a matrix (mat1*A or A*mat1)

1-3) DotMultiply ... Element-wise multiplication of 2 matrices

1-4) Cross ... Cross product of 2 matrices

1-5) CumulativeProduct ... cumulative product of a matrix

2) Division:

2-1) / ... Not defined for 2 matrices. For proper division, multiply Inverse(mat1)

2-2) / ... Scaler division of a matrix (mat1/A)

2-3) DotDivide ... Element-wise division of 2 matrices

3-1) + ... Element-wise addition of 2 matrices

3-2) + ... Scaler addition of a matrix (mat1+A or A+mat1)

3-3) SumColumns ... Summation of each column in a matrix

3-4) CumulativeSum ... Cumulative product of a matrix

4) Subtraction:

4-1) - ... Element-wise subtraction of 2 matrices

4-2) - ... Scaler subtraction of a matrix (mat1-A or A-mat1)

4-3) Difference ... Difference of adjacent 2 rows in a matrix (1st order differential)

5) Power:

5-1) ^ (or pow) ... Not defined as element-wise power of 2 matrices

5-2) ^ (or pow) ... Not defined as scaler power of a matrix

5-3) DotPower ... Element-wise power of 2 matrices

To depict the differences of above multiplications, try Ex2.