本文整理汇总了C++中SpMat::zeros方法的典型用法代码示例。如果您正苦于以下问题:C++ SpMat::zeros方法的具体用法?C++ SpMat::zeros怎么用?C++ SpMat::zeros使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpMat
的用法示例。
在下文中一共展示了SpMat::zeros方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
inline
void
op_sp_plus::apply_inside_schur(SpMat<eT>& out, const T2& x, const SpToDOp<T3, op_sp_plus>& y)
{
arma_extra_debug_sigprint();
const SpProxy<T2> proxy2(x);
const SpProxy<T3> proxy3(y.m);
arma_debug_assert_same_size(proxy2.get_n_rows(), proxy2.get_n_cols(), proxy3.get_n_rows(), proxy3.get_n_cols(), "element-wise multiplication");
out.zeros(proxy2.get_n_rows(), proxy2.get_n_cols());
typename SpProxy<T2>::const_iterator_type it = proxy2.begin();
typename SpProxy<T2>::const_iterator_type it_end = proxy2.end();
const eT k = y.aux;
for(; it != it_end; ++it)
{
const uword it_row = it.row();
const uword it_col = it.col();
out.at(it_row, it_col) = (*it) * (proxy3.at(it_row, it_col) + k);
}
}
示例2: locs
arma_hot
inline
void
spop_strans::apply_proxy(SpMat<typename T1::elem_type>& out, const T1& X)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
typedef typename umat::elem_type ueT;
const SpProxy<T1> p(X);
const uword N = p.get_n_nonzero();
if(N == uword(0))
{
out.zeros(p.get_n_cols(), p.get_n_rows());
return;
}
umat locs(2, N);
Col<eT> vals(N);
eT* vals_ptr = vals.memptr();
typename SpProxy<T1>::const_iterator_type it = p.begin();
for(uword count = 0; count < N; ++count)
{
ueT* locs_ptr = locs.colptr(count);
locs_ptr[0] = it.col();
locs_ptr[1] = it.row();
vals_ptr[count] = (*it);
++it;
}
SpMat<eT> tmp(locs, vals, p.get_n_cols(), p.get_n_rows());
out.steal_mem(tmp);
}
示例3: P
inline
void
spop_scalar_times::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_scalar_times>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
if(in.aux != eT(0))
{
out.init_xform(in.m, priv::functor_scalar_times<eT>(in.aux));
}
else
{
const SpProxy<T1> P(in.m);
out.zeros( P.get_n_rows(), P.get_n_cols() );
}
}
示例4: p
arma_hot
inline
void
spop_sum::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_sum>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const uword dim = in.aux_uword_a;
arma_debug_check( (dim > 1), "sum(): parameter 'dim' must be 0 or 1" );
const SpProxy<T1> p(in.m);
const uword p_n_rows = p.get_n_rows();
const uword p_n_cols = p.get_n_cols();
if(p.get_n_nonzero() == 0)
{
if(dim == 0) { out.zeros(1,p_n_cols); }
if(dim == 1) { out.zeros(p_n_rows,1); }
return;
}
if(dim == 0) // find the sum in each column
{
Row<eT> acc(p_n_cols, fill::zeros);
if(SpProxy<T1>::must_use_iterator)
{
typename SpProxy<T1>::const_iterator_type it = p.begin();
typename SpProxy<T1>::const_iterator_type it_end = p.end();
while(it != it_end) { acc[it.col()] += (*it); ++it; }
}
else
{
for(uword col = 0; col < p_n_cols; ++col)
{
acc[col] = arrayops::accumulate
(
&p.get_values()[p.get_col_ptrs()[col]],
p.get_col_ptrs()[col + 1] - p.get_col_ptrs()[col]
);
}
}
out = acc;
}
else
if(dim == 1) // find the sum in each row
{
Col<eT> acc(p_n_rows, fill::zeros);
typename SpProxy<T1>::const_iterator_type it = p.begin();
typename SpProxy<T1>::const_iterator_type it_end = p.end();
while(it != it_end) { acc[it.row()] += (*it); ++it; }
out = acc;
}
}
示例5: acc
inline
void
spop_mean::apply_noalias_fast
(
SpMat<typename T1::elem_type>& out,
const SpProxy<T1>& p,
const uword dim
)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
typedef typename T1::pod_type T;
const uword p_n_rows = p.get_n_rows();
const uword p_n_cols = p.get_n_cols();
if( (p_n_rows == 0) || (p_n_cols == 0) || (p.get_n_nonzero() == 0) )
{
if(dim == 0) { out.zeros((p_n_rows > 0) ? 1 : 0, p_n_cols); }
if(dim == 1) { out.zeros(p_n_rows, (p_n_cols > 0) ? 1 : 0); }
return;
}
if(dim == 0) // find the mean in each column
{
Row<eT> acc(p_n_cols, fill::zeros);
if(SpProxy<T1>::must_use_iterator)
{
typename SpProxy<T1>::const_iterator_type it = p.begin();
typename SpProxy<T1>::const_iterator_type it_end = p.end();
while(it != it_end) { acc[it.col()] += (*it); ++it; }
acc /= T(p_n_rows);
}
else
{
for(uword col = 0; col < p_n_cols; ++col)
{
acc[col] = arrayops::accumulate
(
&p.get_values()[p.get_col_ptrs()[col]],
p.get_col_ptrs()[col + 1] - p.get_col_ptrs()[col]
) / T(p_n_rows);
}
}
out = acc;
}
else
if(dim == 1) // find the mean in each row
{
Col<eT> acc(p_n_rows, fill::zeros);
typename SpProxy<T1>::const_iterator_type it = p.begin();
typename SpProxy<T1>::const_iterator_type it_end = p.end();
while(it != it_end) { acc[it.row()] += (*it); ++it; }
acc /= T(p_n_cols);
out = acc;
}
if(out.is_finite() == false)
{
spop_mean::apply_noalias_slow(out, p, dim);
}
}
示例6: while
inline
void
spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpProxy<T1>& p)
{
arma_extra_debug_sigprint();
const uword n_rows = p.get_n_rows();
const uword n_cols = p.get_n_cols();
const bool p_is_vec = (n_rows == 1) || (n_cols == 1);
if(p_is_vec) // generate a diagonal matrix out of a vector
{
const uword N = (n_rows == 1) ? n_cols : n_rows;
out.zeros(N, N);
if(p.get_n_nonzero() == 0) { return; }
typename SpProxy<T1>::const_iterator_type it = p.begin();
typename SpProxy<T1>::const_iterator_type it_end = p.end();
if(n_cols == 1)
{
while(it != it_end)
{
const uword row = it.row();
out.at(row,row) = (*it);
++it;
}
}
else
if(n_rows == 1)
{
while(it != it_end)
{
const uword col = it.col();
out.at(col,col) = (*it);
++it;
}
}
}
else // generate a diagonal matrix out of a matrix
{
arma_debug_check( (n_rows != n_cols), "diagmat(): given matrix is not square" );
out.zeros(n_rows, n_rows);
if(p.get_n_nonzero() == 0) { return; }
typename SpProxy<T1>::const_iterator_type it = p.begin();
typename SpProxy<T1>::const_iterator_type it_end = p.end();
while(it != it_end)
{
const uword row = it.row();
const uword col = it.col();
if(row == col)
{
out.at(row,row) = (*it);
}
++it;
}
}
}
示例7: while
arma_hot
inline
void
spglue_plus::apply_noalias(SpMat<eT>& out, const SpProxy<T1>& pa, const SpProxy<T2>& pb)
{
arma_extra_debug_sigprint();
arma_debug_assert_same_size(pa.get_n_rows(), pa.get_n_cols(), pb.get_n_rows(), pb.get_n_cols(), "addition");
if( (pa.get_n_nonzero() != 0) && (pb.get_n_nonzero() != 0) )
{
out.zeros(pa.get_n_rows(), pa.get_n_cols());
// Resize memory to correct size.
out.mem_resize(n_unique(pa, pb, op_n_unique_add()));
// Now iterate across both matrices.
typename SpProxy<T1>::const_iterator_type x_it = pa.begin();
typename SpProxy<T2>::const_iterator_type y_it = pb.begin();
typename SpProxy<T1>::const_iterator_type x_end = pa.end();
typename SpProxy<T2>::const_iterator_type y_end = pb.end();
uword cur_val = 0;
while( (x_it != x_end) || (y_it != y_end) )
{
if(x_it == y_it)
{
const eT val = (*x_it) + (*y_it);
if(val != eT(0))
{
access::rw(out.values[cur_val]) = val;
access::rw(out.row_indices[cur_val]) = x_it.row();
++access::rw(out.col_ptrs[x_it.col() + 1]);
++cur_val;
}
++x_it;
++y_it;
}
else
{
const uword x_it_row = x_it.row();
const uword x_it_col = x_it.col();
const uword y_it_row = y_it.row();
const uword y_it_col = y_it.col();
if((x_it_col < y_it_col) || ((x_it_col == y_it_col) && (x_it_row < y_it_row))) // if y is closer to the end
{
const eT val = (*x_it);
if(val != eT(0))
{
access::rw(out.values[cur_val]) = val;
access::rw(out.row_indices[cur_val]) = x_it_row;
++access::rw(out.col_ptrs[x_it_col + 1]);
++cur_val;
}
++x_it;
}
else
{
const eT val = (*y_it);
if(val != eT(0))
{
access::rw(out.values[cur_val]) = val;
access::rw(out.row_indices[cur_val]) = y_it_row;
++access::rw(out.col_ptrs[y_it_col + 1]);
++cur_val;
}
++y_it;
}
}
}
const uword out_n_cols = out.n_cols;
uword* col_ptrs = access::rwp(out.col_ptrs);
// Fix column pointers to be cumulative.
for(uword c = 1; c <= out_n_cols; ++c)
{
col_ptrs[c] += col_ptrs[c - 1];
}
}
else
{
if(pa.get_n_nonzero() == 0)
{
out = pb.Q;
return;
}
if(pb.get_n_nonzero() == 0)
{
//.........这里部分代码省略.........
示例8: index
arma_hot
inline
void
spglue_times::apply_noalias(SpMat<eT>& c, const SpProxy<T1>& pa, const SpProxy<T2>& pb)
{
arma_extra_debug_sigprint();
const uword x_n_rows = pa.get_n_rows();
const uword x_n_cols = pa.get_n_cols();
const uword y_n_rows = pb.get_n_rows();
const uword y_n_cols = pb.get_n_cols();
arma_debug_assert_mul_size(x_n_rows, x_n_cols, y_n_rows, y_n_cols, "matrix multiplication");
// First we must determine the structure of the new matrix (column pointers).
// This follows the algorithm described in 'Sparse Matrix Multiplication
// Package (SMMP)' (R.E. Bank and C.C. Douglas, 2001). Their description of
// "SYMBMM" does not include anything about memory allocation. In addition it
// does not consider that there may be elements which space may be allocated
// for but which evaluate to zero anyway. So we have to modify the algorithm
// to work that way. For the "SYMBMM" implementation we will not determine
// the row indices but instead just the column pointers.
//SpMat<typename T1::elem_type> c(x_n_rows, y_n_cols); // Initializes col_ptrs to 0.
c.zeros(x_n_rows, y_n_cols);
//if( (pa.get_n_elem() == 0) || (pb.get_n_elem() == 0) )
if( (pa.get_n_nonzero() == 0) || (pb.get_n_nonzero() == 0) )
{
return;
}
// Auxiliary storage which denotes when items have been found.
podarray<uword> index(x_n_rows);
index.fill(x_n_rows); // Fill with invalid links.
typename SpProxy<T2>::const_iterator_type y_it = pb.begin();
typename SpProxy<T2>::const_iterator_type y_end = pb.end();
// SYMBMM: calculate column pointers for resultant matrix to obtain a good
// upper bound on the number of nonzero elements.
uword cur_col_length = 0;
uword last_ind = x_n_rows + 1;
do
{
const uword y_it_row = y_it.row();
// Look through the column that this point (*y_it) could affect.
typename SpProxy<T1>::const_iterator_type x_it = pa.begin_col(y_it_row);
while(x_it.col() == y_it_row)
{
// A point at x(i, j) and y(j, k) implies a point at c(i, k).
if(index[x_it.row()] == x_n_rows)
{
index[x_it.row()] = last_ind;
last_ind = x_it.row();
++cur_col_length;
}
++x_it;
}
const uword old_col = y_it.col();
++y_it;
// See if column incremented.
if(old_col != y_it.col())
{
// Set column pointer (this is not a cumulative count; that is done later).
access::rw(c.col_ptrs[old_col + 1]) = cur_col_length;
cur_col_length = 0;
// Return index markers to zero. Use last_ind for traversal.
while(last_ind != x_n_rows + 1)
{
const uword tmp = index[last_ind];
index[last_ind] = x_n_rows;
last_ind = tmp;
}
}
}
while(y_it != y_end);
// Accumulate column pointers.
for(uword i = 0; i < c.n_cols; ++i)
{
access::rw(c.col_ptrs[i + 1]) += c.col_ptrs[i];
}
// Now that we know a decent bound on the number of nonzero elements, allocate
// the memory and fill it.
c.mem_resize(c.col_ptrs[c.n_cols]);
// Now the implementation of the NUMBMM algorithm.
uword cur_pos = 0; // Current position in c matrix.
podarray<eT> sums(x_n_rows); // Partial sums.
sums.zeros();
// setting the size of 'sorted_indices' to x_n_rows is a better-than-nothing guess;
//.........这里部分代码省略.........