From bd56f524b2014566ea3200f74f0eb2fe9dab0336 Mon Sep 17 00:00:00 2001 From: conrad Date: Sat, 8 Jun 2024 16:57:40 +1000 Subject: [PATCH] convert sparse diagvec to use SpToDOp framework --- include/armadillo | 2 + include/armadillo_bits/fn_diagvec.hpp | 4 +- .../armadillo_bits/op_sp_diagvec_bones.hpp | 35 +++++++++++ include/armadillo_bits/op_sp_diagvec_meat.hpp | 62 +++++++++++++++++++ include/armadillo_bits/spop_misc_bones.hpp | 11 ---- include/armadillo_bits/spop_misc_meat.hpp | 62 ------------------- 6 files changed, 101 insertions(+), 75 deletions(-) create mode 100644 include/armadillo_bits/op_sp_diagvec_bones.hpp create mode 100644 include/armadillo_bits/op_sp_diagvec_meat.hpp diff --git a/include/armadillo b/include/armadillo index 0a24ae71..c94313f7 100644 --- a/include/armadillo +++ b/include/armadillo @@ -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" diff --git a/include/armadillo_bits/fn_diagvec.hpp b/include/armadillo_bits/fn_diagvec.hpp index 5dc587ea..a96a8200 100644 --- a/include/armadillo_bits/fn_diagvec.hpp +++ b/include/armadillo_bits/fn_diagvec.hpp @@ -51,12 +51,12 @@ diagvec(const Base& X, const sword diag_id) template arma_warn_unused arma_inline -const SpOp +const SpToDOp diagvec(const SpBase& X, const sword diag_id = 0) { arma_debug_sigprint(); - return SpOp(X.get_ref(), ((diag_id < 0) ? -diag_id : diag_id), ((diag_id < 0) ? 1 : 0) ); + return SpToDOp(X.get_ref(), ((diag_id < 0) ? -diag_id : diag_id), ((diag_id < 0) ? 1 : 0) ); } diff --git a/include/armadillo_bits/op_sp_diagvec_bones.hpp b/include/armadillo_bits/op_sp_diagvec_bones.hpp new file mode 100644 index 00000000..f8901971 --- /dev/null +++ b/include/armadillo_bits/op_sp_diagvec_bones.hpp @@ -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 + inline static void apply(Mat& out, const SpToDOp& in); + }; + + + +//! @} diff --git a/include/armadillo_bits/op_sp_diagvec_meat.hpp b/include/armadillo_bits/op_sp_diagvec_meat.hpp new file mode 100644 index 00000000..f83d1bb3 --- /dev/null +++ b/include/armadillo_bits/op_sp_diagvec_meat.hpp @@ -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 +inline +void +op_sp_diagvec::apply(Mat& out, const SpToDOp& in) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const unwrap_spmat U(in.m); + const SpMat& 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); + } + } + + + +//! @} diff --git a/include/armadillo_bits/spop_misc_bones.hpp b/include/armadillo_bits/spop_misc_bones.hpp index ed62df20..e8a4c721 100644 --- a/include/armadillo_bits/spop_misc_bones.hpp +++ b/include/armadillo_bits/spop_misc_bones.hpp @@ -240,17 +240,6 @@ class spop_sign -class spop_diagvec - : public traits_op_col - { - public: - - template - inline static void apply(SpMat& out, const SpOp& in); - }; - - - class spop_flipud : public traits_op_passthru { diff --git a/include/armadillo_bits/spop_misc_meat.hpp b/include/armadillo_bits/spop_misc_meat.hpp index 7207e3af..3560ffce 100644 --- a/include/armadillo_bits/spop_misc_meat.hpp +++ b/include/armadillo_bits/spop_misc_meat.hpp @@ -530,68 +530,6 @@ spop_sign::apply(SpMat& out, const SpOp& i -template -inline -void -spop_diagvec::apply(SpMat& out, const SpOp& in) - { - arma_debug_sigprint(); - - typedef typename T1::elem_type eT; - - const unwrap_spmat U(in.m); - - const SpMat& 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 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 inline void