2.2.3.9.15 matrixbase::DotDivide

Description

Divide this matrix by another matrix.

Syntax

BOOL DotDivide( matrixbase & mb )

Parameters

mb
[input] The input divisor matrix which is divided into this matrix

Return

Returns TRUE on success and FALSE on Failure.

Examples

EX1

void matrixbase_DotDivide_ex1()
{
    matrix<double> mat1 = {
        {1,  2,  3},
        {4,  5,  6}
    };
    matrix<double> mat2 = {
        {2,  0,  2},
        {2,  0,  2}
    };

    int rc = mat1.DotDivide( mat2 );  // Divide mat1 by mat2
    if(!rc) 
        printf("  Error: DotDivide failed. rc=%d\n");
    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

// Dividing a matrix by another matrix element-wise
void matrixbase_DotDivide_ex2()
{
    int rc;
    
    matrix<double> mat1 = {
        {1,  2,  3},
        {4,  5,  6}
    };

    matrix<double> mat2 = {
        {2,  0,  2},
        {2,  0,  2}
    };

    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s.\n",Mat1.GetName());
    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat1;  // Mat2 contents are initially identical to the original mat1
    
    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
    Mat3 = mat2; // This is used to multiply on Mat2

    rc=mat1.DotDivide( mat2 );  // Divide mat1 by mat2
      // Note that zero-division causes no error, but results a missing value
    if(!rc) 
        printf("  Error: DotDivide on %s by %s failed.\n",
          Mat2.GetName(),Mat3.GetName());
    else
    {
        Mat2 = mat1;
        printf("  The result of division by %s is %s.\n",
          Mat3.GetName(),Mat2.GetName());
    }
}

EX3

// Dividing a matrix by another matrix element-wise - Error case
void matrixbase_DotDivide_ex3()
{
    int rc;
    
    matrix<double> mat1 = {
        {1,  2,  3},
        {4,  5,  6}
    };

    matrix<double> mat2 = {
        {2,  2,  2}
    };

    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s.\n",Mat1.GetName());
    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat1;  // Mat2 contents are initially identical to the original mat1
    
    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
    Mat3 = mat2; // This is used to multiply on Mat2

    rc=mat1.DotDivide( mat2 );    // Divide mat1 by mat2 - they have different shapes
        // Note that no changes result on mat1.And rc will not equal to 0.
    if(!rc) 
        printf("  Error: DotDivide on %s by %s failed.\n",
          Mat2.GetName(),Mat3.GetName());
    else
    {
        Mat2 = mat1;
        printf("  The result of division by %s is %s.\n",
          Mat3.GetName(),Mat2.GetName());
    }
}

EX4

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

void  matrixbase_DotDivide_ex4()

{
    matrix mat1 = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 1, 1}
    };

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

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

    Mat1=mat1; Mat2=mat2;
    matrix mat3;

    mat3=mat2;
    mat3.Inverse();

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

    Mat4=mat1/10;
    printf("%s = %s/10 <== scaler division of a matrix\n",Mat4.GetName(),Mat1.GetName(),Mat2.GetName());

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

Remark

Divide this matrix by another matrix on an element by element basis placing the result in this matrix. The underlying base type of the matrix passed as an argument must be "less than or equal" to the underlying base type of this matrix. The matrices must have the same size.


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) Addition:

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 divisions, try the EX3.

See Also

matrixbase::DotMultiply, matrixbase::DotPower, matrixbase::Cross

Header to Include

origin.h