![]() |
Prev | Next |
Matlab or Octave |
x
|
C++ |
y = scalar( x)
|
x
behaves as if it were a scalar.
function [ok] = scalar_ok()
ok = true;
m = 2;
n = 3;
u = rand(m, n);
x = 5;
% ----------
v = u * x;
% ----------
[m, n] = size(v);
ok = ok & (m == 2) & (n == 3);
for i = 1 : m
for j = 1 : n
ok = ok & abs(v(i,j) - u(i,j) * x(1,1)) < 1e-10;
end
end
return
y = scalar(
x)
where x is a @(@
1 \times 1
@)@
ublas matrix<double>
and y is a double
.
# include <mat2cpp.hpp>
# include <cstdlib>
bool scalar_ok(void)
{ bool ok = true;
using namespace mat2cpp;
size_t i, j, m(2), n(3);
matrix<double> u = rand(m, n);
matrix<double> v = u;
matrix<double> x(1, 1);
x(0, 0) = 5.;
// ------------
v *= scalar(x);
// ------------
for(i = 0; i < m; i++)
{ for(j = 0; j < n; j++)
ok &= std::fabs( v(i,j) - u(i,j) * x(0,0)) <= 1e-10;
}
return ok;
}