Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-3.900.4/include/armadillo_bits/spglue_plus_meat.hpp @ 49:1ec0e2823891
Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author | Chris Cannam |
---|---|
date | Thu, 13 Jun 2013 10:25:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
48:69251e11a913 | 49:1ec0e2823891 |
---|---|
1 // Copyright (C) 2012 Ryan Curtin | |
2 // Copyright (C) 2012 Conrad Sanderson | |
3 // | |
4 // This Source Code Form is subject to the terms of the Mozilla Public | |
5 // License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | |
8 | |
9 //! \addtogroup spglue_plus | |
10 //! @{ | |
11 | |
12 | |
13 | |
14 template<typename T1, typename T2> | |
15 arma_hot | |
16 inline | |
17 void | |
18 spglue_plus::apply(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_plus>& X) | |
19 { | |
20 arma_extra_debug_sigprint(); | |
21 | |
22 typedef typename T1::elem_type eT; | |
23 | |
24 const SpProxy<T1> pa(X.A); | |
25 const SpProxy<T2> pb(X.B); | |
26 | |
27 const bool is_alias = pa.is_alias(out) || pb.is_alias(out); | |
28 | |
29 if(is_alias == false) | |
30 { | |
31 spglue_plus::apply_noalias(out, pa, pb); | |
32 } | |
33 else | |
34 { | |
35 SpMat<eT> tmp; | |
36 spglue_plus::apply_noalias(tmp, pa, pb); | |
37 | |
38 out.steal_mem(tmp); | |
39 } | |
40 } | |
41 | |
42 | |
43 | |
44 template<typename eT, typename T1, typename T2> | |
45 arma_hot | |
46 inline | |
47 void | |
48 spglue_plus::apply_noalias(SpMat<eT>& out, const SpProxy<T1>& pa, const SpProxy<T2>& pb) | |
49 { | |
50 arma_extra_debug_sigprint(); | |
51 | |
52 arma_debug_assert_same_size(pa.get_n_rows(), pa.get_n_cols(), pb.get_n_rows(), pb.get_n_cols(), "addition"); | |
53 | |
54 if( (pa.get_n_nonzero() != 0) && (pb.get_n_nonzero() != 0) ) | |
55 { | |
56 out.set_size(pa.get_n_rows(), pa.get_n_cols()); | |
57 | |
58 // Resize memory to correct size. | |
59 out.mem_resize(n_unique(pa, pb, op_n_unique_add())); | |
60 | |
61 // Now iterate across both matrices. | |
62 typename SpProxy<T1>::const_iterator_type x_it = pa.begin(); | |
63 typename SpProxy<T2>::const_iterator_type y_it = pb.begin(); | |
64 | |
65 typename SpProxy<T1>::const_iterator_type x_end = pa.end(); | |
66 typename SpProxy<T2>::const_iterator_type y_end = pb.end(); | |
67 | |
68 uword cur_val = 0; | |
69 while( (x_it != x_end) || (y_it != y_end) ) | |
70 { | |
71 if(x_it == y_it) | |
72 { | |
73 const eT val = (*x_it) + (*y_it); | |
74 | |
75 if (val != eT(0)) | |
76 { | |
77 access::rw(out.values[cur_val]) = val; | |
78 access::rw(out.row_indices[cur_val]) = x_it.row(); | |
79 ++access::rw(out.col_ptrs[x_it.col() + 1]); | |
80 ++cur_val; | |
81 } | |
82 | |
83 ++x_it; | |
84 ++y_it; | |
85 } | |
86 else | |
87 { | |
88 const uword x_it_row = x_it.row(); | |
89 const uword x_it_col = x_it.col(); | |
90 | |
91 const uword y_it_row = y_it.row(); | |
92 const uword y_it_col = y_it.col(); | |
93 | |
94 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 | |
95 { | |
96 access::rw(out.values[cur_val]) = (*x_it); | |
97 access::rw(out.row_indices[cur_val]) = x_it_row; | |
98 ++access::rw(out.col_ptrs[x_it_col + 1]); | |
99 ++cur_val; | |
100 ++x_it; | |
101 } | |
102 else | |
103 { | |
104 access::rw(out.values[cur_val]) = (*y_it); | |
105 access::rw(out.row_indices[cur_val]) = y_it_row; | |
106 ++access::rw(out.col_ptrs[y_it_col + 1]); | |
107 ++cur_val; | |
108 ++y_it; | |
109 } | |
110 } | |
111 } | |
112 | |
113 const uword out_n_cols = out.n_cols; | |
114 | |
115 uword* col_ptrs = access::rwp(out.col_ptrs); | |
116 | |
117 // Fix column pointers to be cumulative. | |
118 for(uword c = 1; c <= out_n_cols; ++c) | |
119 { | |
120 col_ptrs[c] += col_ptrs[c - 1]; | |
121 } | |
122 } | |
123 else | |
124 { | |
125 if(pa.get_n_nonzero() == 0) | |
126 { | |
127 out = pb.Q; | |
128 return; | |
129 } | |
130 | |
131 if(pb.get_n_nonzero() == 0) | |
132 { | |
133 out = pa.Q; | |
134 return; | |
135 } | |
136 } | |
137 } | |
138 | |
139 | |
140 | |
141 // | |
142 // | |
143 // spglue_plus2: scalar*(A + B) | |
144 | |
145 | |
146 | |
147 template<typename T1, typename T2> | |
148 arma_hot | |
149 inline | |
150 void | |
151 spglue_plus2::apply(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_plus2>& X) | |
152 { | |
153 arma_extra_debug_sigprint(); | |
154 | |
155 typedef typename T1::elem_type eT; | |
156 | |
157 const SpProxy<T1> pa(X.A); | |
158 const SpProxy<T2> pb(X.B); | |
159 | |
160 const bool is_alias = pa.is_alias(out) || pb.is_alias(out); | |
161 | |
162 if(is_alias == false) | |
163 { | |
164 spglue_plus::apply_noalias(out, pa, pb); | |
165 } | |
166 else | |
167 { | |
168 SpMat<eT> tmp; | |
169 spglue_plus::apply_noalias(tmp, pa, pb); | |
170 | |
171 out.steal_mem(tmp); | |
172 } | |
173 | |
174 out *= X.aux; | |
175 } | |
176 | |
177 | |
178 | |
179 //! @} |