1 | // Copyright (C) 2014 Conrad Sanderson |
---|
2 | // Copyright (C) 2014 NICTA (www.nicta.com.au) |
---|
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 | |
---|
10 | //! \addtogroup op_normalise |
---|
11 | //! @{ |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | template<typename T1> |
---|
16 | inline |
---|
17 | void |
---|
18 | op_normalise_colvec::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_normalise_colvec>& in) |
---|
19 | { |
---|
20 | arma_extra_debug_sigprint(); |
---|
21 | |
---|
22 | typedef typename T1::pod_type T; |
---|
23 | |
---|
24 | const uword p = in.aux_uword_a; |
---|
25 | |
---|
26 | arma_debug_check( (p == 0), "normalise(): p must be greater than zero" ); |
---|
27 | |
---|
28 | const quasi_unwrap<T1> tmp(in.m); |
---|
29 | |
---|
30 | const T norm_val_a = norm(tmp.M, p); |
---|
31 | const T norm_val_b = (norm_val_a != T(0)) ? norm_val_a : T(1); |
---|
32 | |
---|
33 | out = tmp.M / norm_val_b; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | template<typename T1> |
---|
39 | inline |
---|
40 | void |
---|
41 | op_normalise_rowvec::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_normalise_rowvec>& in) |
---|
42 | { |
---|
43 | arma_extra_debug_sigprint(); |
---|
44 | |
---|
45 | typedef typename T1::pod_type T; |
---|
46 | |
---|
47 | const uword p = in.aux_uword_a; |
---|
48 | |
---|
49 | arma_debug_check( (p == 0), "normalise(): p must be greater than zero" ); |
---|
50 | |
---|
51 | const unwrap<T1> tmp(in.m); |
---|
52 | |
---|
53 | const T norm_val_a = norm(tmp.M, p); |
---|
54 | const T norm_val_b = (norm_val_a != T(0)) ? norm_val_a : T(1); |
---|
55 | |
---|
56 | out = tmp.M / norm_val_b; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | template<typename T1> |
---|
62 | inline |
---|
63 | void |
---|
64 | op_normalise_mat::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_normalise_mat>& in) |
---|
65 | { |
---|
66 | arma_extra_debug_sigprint(); |
---|
67 | |
---|
68 | typedef typename T1::elem_type eT; |
---|
69 | |
---|
70 | const uword p = in.aux_uword_a; |
---|
71 | const uword dim = in.aux_uword_b; |
---|
72 | |
---|
73 | arma_debug_check( (p == 0), "normalise(): p must be greater than zero" ); |
---|
74 | arma_debug_check( (dim > 1), "normalise(): dim must be 0 or 1" ); |
---|
75 | |
---|
76 | const unwrap<T1> tmp(in.m); |
---|
77 | const Mat<eT>& A = tmp.M; |
---|
78 | |
---|
79 | const bool alias = ( (&out) == (&A) ); |
---|
80 | |
---|
81 | if(alias) |
---|
82 | { |
---|
83 | Mat<eT> out2; |
---|
84 | |
---|
85 | op_normalise_mat::apply(out2, A, p, dim); |
---|
86 | |
---|
87 | out.steal_mem(out2); |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | op_normalise_mat::apply(out, A, p, dim); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | template<typename eT> |
---|
98 | inline |
---|
99 | void |
---|
100 | op_normalise_mat::apply(Mat<eT>& out, const Mat<eT>& A, const uword p, const uword dim) |
---|
101 | { |
---|
102 | arma_extra_debug_sigprint(); |
---|
103 | |
---|
104 | typedef typename get_pod_type<eT>::result T; |
---|
105 | |
---|
106 | out.copy_size(A); |
---|
107 | |
---|
108 | if(A.n_elem == 0) { return; } |
---|
109 | |
---|
110 | if(dim == 0) |
---|
111 | { |
---|
112 | const uword n_cols = A.n_cols; |
---|
113 | |
---|
114 | for(uword i=0; i<n_cols; ++i) |
---|
115 | { |
---|
116 | const T norm_val_a = norm(A.col(i), p); |
---|
117 | const T norm_val_b = (norm_val_a != T(0)) ? norm_val_a : T(1); |
---|
118 | |
---|
119 | out.col(i) = A.col(i) / norm_val_b; |
---|
120 | } |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | // better-than-nothing implementation |
---|
125 | |
---|
126 | const uword n_rows = A.n_rows; |
---|
127 | |
---|
128 | for(uword i=0; i<n_rows; ++i) |
---|
129 | { |
---|
130 | const T norm_val_a = norm(A.row(i), p); |
---|
131 | const T norm_val_b = (norm_val_a != T(0)) ? norm_val_a : T(1); |
---|
132 | |
---|
133 | out.row(i) = A.row(i) / norm_val_b; |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | //! @} |
---|