convert sparse diagvec to use SpToDOp framework

This commit is contained in:
conrad
2024-06-08 16:57:40 +10:00
parent 5cbd10b9e3
commit bd56f524b2
6 changed files with 101 additions and 75 deletions
+2
View File
@@ -324,6 +324,7 @@ namespace arma
#include "armadillo_bits/op_sp_var_bones.hpp"
#include "armadillo_bits/op_sp_stddev_bones.hpp"
#include "armadillo_bits/op_sp_vecnorm_bones.hpp"
#include "armadillo_bits/op_sp_diagvec_bones.hpp"
#include "armadillo_bits/op_powmat_bones.hpp"
#include "armadillo_bits/op_rank_bones.hpp"
#include "armadillo_bits/op_row_as_mat_bones.hpp"
@@ -774,6 +775,7 @@ namespace arma
#include "armadillo_bits/op_sp_var_meat.hpp"
#include "armadillo_bits/op_sp_stddev_meat.hpp"
#include "armadillo_bits/op_sp_vecnorm_meat.hpp"
#include "armadillo_bits/op_sp_diagvec_meat.hpp"
#include "armadillo_bits/op_powmat_meat.hpp"
#include "armadillo_bits/op_rank_meat.hpp"
#include "armadillo_bits/op_row_as_mat_meat.hpp"
+2 -2
View File
@@ -51,12 +51,12 @@ diagvec(const Base<typename T1::elem_type,T1>& X, const sword diag_id)
template<typename T1>
arma_warn_unused
arma_inline
const SpOp<T1, spop_diagvec>
const SpToDOp<T1, op_sp_diagvec>
diagvec(const SpBase<typename T1::elem_type,T1>& X, const sword diag_id = 0)
{
arma_debug_sigprint();
return SpOp<T1, spop_diagvec>(X.get_ref(), ((diag_id < 0) ? -diag_id : diag_id), ((diag_id < 0) ? 1 : 0) );
return SpToDOp<T1, op_sp_diagvec>(X.get_ref(), ((diag_id < 0) ? -diag_id : diag_id), ((diag_id < 0) ? 1 : 0) );
}
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup op_sp_diagvec
//! @{
class op_sp_diagvec
: public traits_op_col
{
public:
template<typename T1>
inline static void apply(Mat<typename T1::elem_type>& out, const SpToDOp<T1, op_sp_diagvec>& in);
};
//! @}
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup op_sp_diagvec
//! @{
template<typename T1>
inline
void
op_sp_diagvec::apply(Mat<typename T1::elem_type>& out, const SpToDOp<T1,op_sp_diagvec>& in)
{
arma_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> U(in.m);
const SpMat<eT>& X = U.M;
const uword a = in.aux_uword_a;
const uword b = in.aux_uword_b;
const uword row_offset = (b > 0) ? a : 0;
const uword col_offset = (b == 0) ? a : 0;
arma_conform_check_bounds
(
((row_offset > 0) && (row_offset >= X.n_rows)) || ((col_offset > 0) && (col_offset >= X.n_cols)),
"diagvec(): requested diagonal out of bounds"
);
const uword len = (std::min)(X.n_rows - row_offset, X.n_cols - col_offset);
out.set_size(len, 1);
eT* out_mem = out.memptr();
for(uword i=0; i < len; ++i)
{
out_mem[i] = X.at(i + row_offset, i + col_offset);
}
}
//! @}
@@ -240,17 +240,6 @@ class spop_sign
class spop_diagvec
: public traits_op_col
{
public:
template<typename T1>
inline static void apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_diagvec>& in);
};
class spop_flipud
: public traits_op_passthru
{
-62
View File
@@ -530,68 +530,6 @@ spop_sign::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_sign>& i
template<typename T1>
inline
void
spop_diagvec::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_diagvec>& in)
{
arma_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> U(in.m);
const SpMat<eT>& X = U.M;
const uword a = in.aux_uword_a;
const uword b = in.aux_uword_b;
const uword row_offset = (b > 0) ? a : 0;
const uword col_offset = (b == 0) ? a : 0;
arma_conform_check_bounds
(
((row_offset > 0) && (row_offset >= X.n_rows)) || ((col_offset > 0) && (col_offset >= X.n_cols)),
"diagvec(): requested diagonal out of bounds"
);
const uword len = (std::min)(X.n_rows - row_offset, X.n_cols - col_offset);
Col<eT> cache(len, arma_nozeros_indicator());
eT* cache_mem = cache.memptr();
uword n_nonzero = 0;
for(uword i=0; i < len; ++i)
{
const eT val = X.at(i + row_offset, i + col_offset);
cache_mem[i] = val;
n_nonzero += (val != eT(0)) ? uword(1) : uword(0);
}
out.reserve(len, 1, n_nonzero);
uword count = 0;
for(uword i=0; i < len; ++i)
{
const eT val = cache_mem[i];
if(val != eT(0))
{
access::rw(out.row_indices[count]) = i;
access::rw(out.values[count]) = val;
++count;
}
}
access::rw(out.col_ptrs[0]) = 0;
access::rw(out.col_ptrs[1]) = n_nonzero;
}
template<typename T1>
inline
void