![]() |
Prev | Next |
Matlab or Octave |
z = diag( x) * y
|
C++ |
z = diag_prod( x, y)
|
z = diag(
x) *
y
sets z to the @(@
m \times n
@)@ matrix with
the @(@
(i,j)
@)@ element given by
@[@
z_{i,j} = x_i * y_{i,j}
@]@
function [ok] = diag_prod_ok()
ok = true;
m = 3;
n = 2;
x = rand(1, m);
y = rand(m, n);
% -------------------
z = diag(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) * y(i, j);
ok = ok & abs(z(i, j) - zij) < 1e-10;
end
end
return
z = diag_prod(
x,
y)
where x is an @(@
1 \times m
@)@ or @(@
m \times 1
@)@
ublas matrix<double>
object,
and y, z are ublas matrix<double>
objects
with size @(@
m \times n
@)@.
# include <mat2cpp.hpp>
bool diag_prod_ok(void)
{ bool ok = true;
using namespace mat2cpp;
size_t i, j, m(3), n(2);
matrix<double> u = rand(m, 1);
matrix<double> x = rand(1, m);
matrix<double> y = rand(m, n);
// --------------------------------
matrix<double> w = diag_prod(u, y);
matrix<double> z = diag_prod(x, y);
// --------------------------------
ok &= (z.size1() == m);
ok &= (z.size2() == n);
for(i = 0; i < m; i++)
{ for(j = 0; j < n; j++)
{ double wij = u(i,0) * y(i,j);
double zij = x(0,i) * y(i,j);
ok &= std::fabs(w(i, j) - wij) < 1e-10;
ok &= std::fabs(z(i, j) - zij) < 1e-10;
}
}
return ok;
}