Prev Next

Matrix Division

Syntax
Matlab or Octave z = x \ y
C++ z = matrix_div(xyrank)

Matlab or Octave
If x is an @(@ m \times m @)@ matrix and y is an @(@ m \times n @)@ matrix,
     
z = x \ y
sets z to the @(@ m \times n @)@ matrix such that @[@ x * z = y @]@

Example
     function [ok] = matrix_div_ok()
     ok  = true;
     m   = 3;
     n   = 2;
     x   = rand(m, m);
     y   = rand(m, n);
     % -------------
     z      = x \ y;
     % -------------
     [m, n] = size(z);
     ok     = ok & (m == 3);
     ok     = ok & (n == 2);
     ok     = ok & all ( all( abs(x * z - y) < 1e-10 ) );
     return 


C++
The corresponding C++ syntax is
     
z = matrix_div(xyrank)
where x, y and z are ublas matrix<double> objects. The size of x is @(@ m \times m @)@, the size of y is @(@ m \times n @)@, and the size of z is @(@ m \times n @)@ matrix. The return value of the size_t argument rank is the rank of the matrix x.

Example
     # include <boost/numeric/ublas/matrix.hpp>
     # include <mat2cpp.hpp>
     bool matrix_div_ok(void)
     {	bool   ok  = true;
     	using namespace mat2cpp;
     
     	size_t i, j, m(3), n(2);
     	matrix<double> x = rand(m, m);
     	matrix<double> y = rand(m, n);
     	// ---------------------------------------
     	size_t rank;
     	matrix<double> z = matrix_div(x, y, rank);
     	// ---------------------------------------
     	ok &= (rank      == m);
     	ok &= (z.size1() == m);
     	ok &= (z.size2() == n);
     	matrix<double> x_z = prod(x, z);
     	for(i = 0; i < m; i++)
     	{	for(j = 0; j < n; j++)
     			ok &= std::fabs( x_z(i,j) - y(i,j) ) < 1e-10;
     	}
     	return ok;
     }


Source
The file matrix_div.cpp contains the C++ source code for these functions.
Input File: omh/matrix_div.omh