mmul, mmdiv - Multiplication and Division

Prev Next

Function Names

mmul, mdiv

Description

These functions do true matrix multiplication and division (and not not simple scalar multiplication or division) Matrices must be provided as 2-level sets, e.g. {{1,2},{3,4}}

mmul: For matrix multiplications, the number of columns (and / rows) in the left matrix needs to match with the number of rows (/ columns) in the right matrix.
mdiv: For matrix divisions, both matrices need to be squares, i.e. row and column count must equal.


In case of division, 2nd matrix must also be a square matrix as it is inverted before a multiplication (like mmul) is applied. Result = A • B-1, The function will reject calculations and assert exceptions if not all criteria are properly met.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

2

Parameters

No.TypeDescription
1
input
matrix of numerals Left Matrix

2
input
matrix of numerals Right Matrix

Return value

TypeDescription
matrix of numerals Result

Matrix with number of rows as specified in the left matrix and number of columns as specified in the right matrix

Exceptions

Mismatching matrix sizes
Division: Matrices are not squares

Examples

m1[] = {{1,2},{3,4}};
m2[] = {{8,6},{4,2}};
m3[] = {{3,1},{1,2}};

echo matrix( "0.0", 4, " | ", m1[], " * ", m2[], " = ", mmul( m1[], m2[] ) );
echo matrix( "0.0", 4, " | ", m1[], " / ", m3[], " = ", mdiv( m1[], m3[] ) );

Output

 |  1.0   2.0 |     |  8.0   6.0 |     | 16.0  10.0 |
|  3.0   4.0 |  *  |  4.0   2.0 |  =  | 40.0  26.0 |

|  1.0   2.0 |     |  3.0   1.0 |     |  0.0   1.0 |
|  3.0   4.0 |  /  |  1.0   2.0 |  =  |  0.4   1.8 |

Try it yourself: Open LIB_Function_mmul.b4p in B4P_Examples.zip. Decompress before use.