1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17
18
19 //! \addtogroup fn_n_unique
20 //! @{
21
22
23 //! \brief
24 //! Get the number of unique nonzero elements in two sparse matrices.
25 //! This is very useful for determining the amount of memory necessary before
26 //! a sparse matrix operation on two matrices.
27
28 template<typename T1, typename T2, typename op_n_unique_type>
29 inline
30 uword
n_unique(const SpBase<typename T1::elem_type,T1> & x,const SpBase<typename T2::elem_type,T2> & y,const op_n_unique_type junk)31 n_unique
32 (
33 const SpBase<typename T1::elem_type, T1>& x,
34 const SpBase<typename T2::elem_type, T2>& y,
35 const op_n_unique_type junk
36 )
37 {
38 arma_extra_debug_sigprint();
39
40 const SpProxy<T1> pa(x.get_ref());
41 const SpProxy<T2> pb(y.get_ref());
42
43 return n_unique(pa,pb,junk);
44 }
45
46
47
48 template<typename T1, typename T2, typename op_n_unique_type>
49 arma_hot
50 inline
51 uword
n_unique(const SpProxy<T1> & pa,const SpProxy<T2> & pb,const op_n_unique_type junk)52 n_unique
53 (
54 const SpProxy<T1>& pa,
55 const SpProxy<T2>& pb,
56 const op_n_unique_type junk
57 )
58 {
59 arma_extra_debug_sigprint();
60 arma_ignore(junk);
61
62 typename SpProxy<T1>::const_iterator_type x_it = pa.begin();
63 typename SpProxy<T1>::const_iterator_type x_it_end = pa.end();
64
65 typename SpProxy<T2>::const_iterator_type y_it = pb.begin();
66 typename SpProxy<T2>::const_iterator_type y_it_end = pb.end();
67
68 uword total_n_nonzero = 0;
69
70 while( (x_it != x_it_end) || (y_it != y_it_end) )
71 {
72 if(x_it == y_it)
73 {
74 if(op_n_unique_type::eval((*x_it), (*y_it)) != typename T1::elem_type(0))
75 {
76 ++total_n_nonzero;
77 }
78
79 ++x_it;
80 ++y_it;
81 }
82 else
83 {
84 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
85 {
86 if(op_n_unique_type::eval((*x_it), typename T1::elem_type(0)) != typename T1::elem_type(0))
87 {
88 ++total_n_nonzero;
89 }
90
91 ++x_it;
92 }
93 else // x is closer to the end
94 {
95 if(op_n_unique_type::eval(typename T1::elem_type(0), (*y_it)) != typename T1::elem_type(0))
96 {
97 ++total_n_nonzero;
98 }
99
100 ++y_it;
101 }
102 }
103 }
104
105 return total_n_nonzero;
106 }
107
108
109 // Simple operators.
110 struct op_n_unique_add
111 {
evalop_n_unique_add112 template<typename eT> inline static eT eval(const eT& l, const eT& r) { return (l + r); }
113 };
114
115 struct op_n_unique_sub
116 {
evalop_n_unique_sub117 template<typename eT> inline static eT eval(const eT& l, const eT& r) { return (l - r); }
118 };
119
120 struct op_n_unique_mul
121 {
evalop_n_unique_mul122 template<typename eT> inline static eT eval(const eT& l, const eT& r) { return (l * r); }
123 };
124
125 struct op_n_unique_count
126 {
evalop_n_unique_count127 template<typename eT> inline static eT eval(const eT&, const eT&) { return eT(1); }
128 };
129
130
131
132 //! @}
133