Prev Next add

Elementwise Addition

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

Matlab or Octave
If x and y are a @(@ m \times n @)@ matrices,
     
z = x + y
sets z to the @(@ m \times n @)@ matrix with the @(@ (i,j) @)@ element given by @[@ z_{i,j} = x_{i,j} + y_{i,j} @]@

Example
     function [ok] = add_ok()
     ok  = true;
     m   = 3;
     n   = 2;
     x   = rand(m, n);
     y   = rand(m, n);
     % -------------
     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(i, j);
     		ok  = ok & abs(z(i, j) - zij) < 1e-10;
     	end
     end
     return 


C++
The corresponding C++ syntax is
     
z = x + y
where x, y, and z are ublas matrix<double> objects with size @(@ m \times n @)@.

Example
     # include <cmath>
     # include <mat2cpp.hpp>
     bool add_ok(void)
     {	bool   ok  = true;
     	using namespace mat2cpp;
     
     	size_t i, j, m(3), n(2);
     	matrix<double> x = rand(m, n);
     	matrix<double> y = rand(m, n);
     	// ----------------------
     	matrix<double> z = x + 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(i,j);
     			ok &= std::fabs(z(i, j) - zij) < 1e-10;
     		}
     	}
     	return ok;
     }


Input File: omh/add.omh