Prev Next scalar_div

Scalar Division

Syntax
Matlab or Octave z = x / y
C++ z = x / y
z /= y

Matlab or Octave
x
is an @(@ m \times n @)@ matrix and y is a scalar,
     
z = x / y
set z to the @(@ m \times n @)@ matrix with the @(@ (i,j) @)@ element given by @[@ z_{i,j} = x_{i,j} / y @]@

Example
     function [ok] = scalar_div_ok()
     ok  = true;
     m   = 3;
     n   = 2;
     x   = rand(m, n);
     y   = rand(1, 1);
     % -------------
     z      = x / y;
     % -------------
     [m, n] = size(z);
     ok     = ok & (m == 3);
     ok     = ok & (n == 2);
     for i = [1 : m]
     	for j = [1 : n]
     		zij = x(i, j) / y;
     		ok  = ok & abs(z(i, j) - zij) < 1e-10;
     	end
     end
     return 


C++
The corresponding C++ syntax is
     
z = x / y
where x, z are ublas matrix<double> objects of size @(@ m \times n @)@ and y is a double object. In the case where x and z are the same matrix, you should use
     
z /= y

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


Input File: omh/scalar_div.omh