Mercurial > octave
annotate liboctave/numeric/oct-norm.cc @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
1 /* |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
2 |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 Copyright (C) 2008-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26569
diff
changeset
|
4 |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26569
diff
changeset
|
5 See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26569
diff
changeset
|
6 or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26569
diff
changeset
|
7 |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
8 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
9 This file is part of Octave. |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
10 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23475
diff
changeset
|
11 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
12 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23475
diff
changeset
|
13 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
14 (at your option) any later version. |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
15 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
16 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
17 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
19 GNU General Public License for more details. |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
21 You should have received a copy of the GNU General Public License |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
22 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23475
diff
changeset
|
23 <https://www.gnu.org/licenses/>. |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
24 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
25 */ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
27 // author: Jaroslav Hajek <highegg@gmail.com> |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
28 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21723
diff
changeset
|
29 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21275
diff
changeset
|
30 # include "config.h" |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
31 #endif |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 #include <cmath> |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
34 |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23449
diff
changeset
|
35 #include <algorithm> |
23443
3f1bf237908b
maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
36 #include <limits> |
8308
5fe0f4dfdbec
use std::vector as a simple linear container in oct-norm.cc to avoid problems with instantiating Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents:
8303
diff
changeset
|
37 #include <vector> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
38 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
39 #include "Array.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
40 #include "CColVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
41 #include "CMatrix.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
42 #include "CRowVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
43 #include "CSparse.h" |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23449
diff
changeset
|
44 #include "MArray.h" |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
45 #include "dColVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
46 #include "dDiagMatrix.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
47 #include "dMatrix.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
48 #include "dRowVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
49 #include "dSparse.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
50 #include "fCColVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
51 #include "fCMatrix.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
52 #include "fCRowVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
53 #include "fColVector.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
54 #include "fDiagMatrix.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
55 #include "fMatrix.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
56 #include "fRowVector.h" |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 #include "lo-error.h" |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
58 #include "lo-ieee.h" |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23449
diff
changeset
|
59 #include "lo-mappers.h" |
19269
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
18084
diff
changeset
|
60 #include "mx-cm-s.h" |
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
18084
diff
changeset
|
61 #include "mx-fcm-fs.h" |
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
18084
diff
changeset
|
62 #include "mx-fs-fcm.h" |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
63 #include "mx-s-cm.h" |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
64 #include "oct-cmplx.h" |
23475
d691ed308237
maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents:
23449
diff
changeset
|
65 #include "quit.h" |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
66 #include "svd.h" |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
67 |
13934
7a756a7e145b
Add a citation to Higham's matrix norm paper
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
11586
diff
changeset
|
68 // Theory: norm accumulator is an object that has an accum method able |
7a756a7e145b
Add a citation to Higham's matrix norm paper
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
11586
diff
changeset
|
69 // to handle both real and complex element, and a cast operator |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
70 // returning the intermediate norm. Reference: Higham, N. "Estimating |
13934
7a756a7e145b
Add a citation to Higham's matrix norm paper
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
11586
diff
changeset
|
71 // the Matrix p-Norm." Numer. Math. 62, 539-555, 1992. |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
72 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
73 // norm accumulator for the p-norm |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
74 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
75 class norm_accumulator_p |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
76 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
77 R p,scl,sum; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
78 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
79 norm_accumulator_p () { } // we need this one for Array |
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
80 norm_accumulator_p (R pp) : p(pp), scl(0), sum(1) { } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
81 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
82 template <typename U> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
83 void accum (U val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
84 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
85 octave_quit (); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
86 R t = std::abs (val); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
87 if (scl == t) // we need this to handle Infs properly |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
88 sum += 1; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
89 else if (scl < t) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
90 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
91 sum *= std::pow (scl/t, p); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
92 sum += 1; |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
93 scl = t; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
94 } |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
95 else if (t != 0) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
96 sum += std::pow (t/scl, p); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
97 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
98 operator R () { return scl * std::pow (sum, 1/p); } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
100 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
101 // norm accumulator for the minus p-pseudonorm |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
102 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
103 class norm_accumulator_mp |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
104 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
105 R p,scl,sum; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
106 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
107 norm_accumulator_mp () { } // we need this one for Array |
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
108 norm_accumulator_mp (R pp) : p(pp), scl(0), sum(1) { } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
109 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
110 template <typename U> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
111 void accum (U val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
112 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
113 octave_quit (); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
114 R t = 1 / std::abs (val); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
115 if (scl == t) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
116 sum += 1; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
117 else if (scl < t) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
118 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
119 sum *= std::pow (scl/t, p); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 sum += 1; |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
121 scl = t; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
122 } |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
123 else if (t != 0) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
124 sum += std::pow (t/scl, p); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
125 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
126 operator R () { return scl * std::pow (sum, -1/p); } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
127 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
128 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
129 // norm accumulator for the 2-norm (euclidean) |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
130 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
131 class norm_accumulator_2 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
132 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
133 R scl,sum; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
134 static R pow2 (R x) { return x*x; } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
135 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
136 norm_accumulator_2 () : scl(0), sum(1) { } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
137 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
138 void accum (R val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
139 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
140 R t = std::abs (val); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
141 if (scl == t) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
142 sum += 1; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
143 else if (scl < t) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
144 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
145 sum *= pow2 (scl/t); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
146 sum += 1; |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
147 scl = t; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
148 } |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
149 else if (t != 0) |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
150 sum += pow2 (t/scl); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
151 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
152 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
153 void accum (std::complex<R> val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
154 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
155 accum (val.real ()); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
156 accum (val.imag ()); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
157 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
158 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
159 operator R () { return scl * std::sqrt (sum); } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
160 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
161 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
162 // norm accumulator for the 1-norm (city metric) |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
163 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
164 class norm_accumulator_1 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
165 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
166 R sum; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
167 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
168 norm_accumulator_1 () : sum (0) { } |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
169 template <typename U> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
170 void accum (U val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
171 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
172 sum += std::abs (val); |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
173 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
174 operator R () { return sum; } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
175 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
176 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
177 // norm accumulator for the inf-norm (max metric) |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
178 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
179 class norm_accumulator_inf |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
180 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
181 R max; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
182 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
183 norm_accumulator_inf () : max (0) { } |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
184 template <typename U> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
185 void accum (U val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
186 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
187 if (octave::math::isnan (val)) |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
188 max = octave::numeric_limits<R>::NaN (); |
21275
85d8280c64f4
Return NaN for norm (..., +/-Inf) if input contains NaN (bug #32855).
Rik <rik@octave.org>
parents:
21273
diff
changeset
|
189 else |
85d8280c64f4
Return NaN for norm (..., +/-Inf) if input contains NaN (bug #32855).
Rik <rik@octave.org>
parents:
21273
diff
changeset
|
190 max = std::max (max, std::abs (val)); |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
191 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
192 operator R () { return max; } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
193 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
194 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
195 // norm accumulator for the -inf pseudonorm (min abs value) |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
196 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
197 class norm_accumulator_minf |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
198 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
199 R min; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
200 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
201 norm_accumulator_minf () : min (octave::numeric_limits<R>::Inf ()) { } |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
202 template <typename U> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
203 void accum (U val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
204 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
205 if (octave::math::isnan (val)) |
21723
bae585228161
use namespace for numeric_limits
John W. Eaton <jwe@octave.org>
parents:
21721
diff
changeset
|
206 min = octave::numeric_limits<R>::NaN (); |
21275
85d8280c64f4
Return NaN for norm (..., +/-Inf) if input contains NaN (bug #32855).
Rik <rik@octave.org>
parents:
21273
diff
changeset
|
207 else |
85d8280c64f4
Return NaN for norm (..., +/-Inf) if input contains NaN (bug #32855).
Rik <rik@octave.org>
parents:
21273
diff
changeset
|
208 min = std::min (min, std::abs (val)); |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
209 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
210 operator R () { return min; } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
211 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
212 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
213 // norm accumulator for the 0-pseudonorm (hamming distance) |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
214 template <typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
215 class norm_accumulator_0 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
216 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
217 unsigned int num; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
218 public: |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
219 norm_accumulator_0 () : num (0) { } |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
220 template <typename U> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
221 void accum (U val) |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
222 { |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
223 if (val != static_cast<U> (0)) ++num; |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
224 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
225 operator R () { return num; } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
226 }; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
227 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
228 // OK, we're armed :) Now let's go for the fun |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
229 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
230 template <typename T, typename R, typename ACC> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
231 inline void vector_norm (const Array<T>& v, R& res, ACC acc) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
232 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
233 for (octave_idx_type i = 0; i < v.numel (); i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
234 acc.accum (v(i)); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
235 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
236 res = acc; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
237 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
238 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
239 // dense versions |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
240 template <typename T, typename R, typename ACC> |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10145
diff
changeset
|
241 void column_norms (const MArray<T>& m, MArray<R>& res, ACC acc) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
242 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
243 res = MArray<R> (dim_vector (1, m.columns ())); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
244 for (octave_idx_type j = 0; j < m.columns (); j++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
245 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
246 ACC accj = acc; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
247 for (octave_idx_type i = 0; i < m.rows (); i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
248 accj.accum (m(i, j)); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
249 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
250 res.xelem (j) = accj; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
251 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
252 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
253 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
254 template <typename T, typename R, typename ACC> |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10145
diff
changeset
|
255 void row_norms (const MArray<T>& m, MArray<R>& res, ACC acc) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
256 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
257 res = MArray<R> (dim_vector (m.rows (), 1)); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
258 std::vector<ACC> acci (m.rows (), acc); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
259 for (octave_idx_type j = 0; j < m.columns (); j++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
260 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
261 for (octave_idx_type i = 0; i < m.rows (); i++) |
8308
5fe0f4dfdbec
use std::vector as a simple linear container in oct-norm.cc to avoid problems with instantiating Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents:
8303
diff
changeset
|
262 acci[i].accum (m(i, j)); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
263 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
264 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
265 for (octave_idx_type i = 0; i < m.rows (); i++) |
8308
5fe0f4dfdbec
use std::vector as a simple linear container in oct-norm.cc to avoid problems with instantiating Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents:
8303
diff
changeset
|
266 res.xelem (i) = acci[i]; |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
267 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
268 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
269 // sparse versions |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
270 template <typename T, typename R, typename ACC> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
271 void column_norms (const MSparse<T>& m, MArray<R>& res, ACC acc) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
272 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
273 res = MArray<R> (dim_vector (1, m.columns ())); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
274 for (octave_idx_type j = 0; j < m.columns (); j++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
275 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
276 ACC accj = acc; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
277 for (octave_idx_type k = m.cidx (j); k < m.cidx (j+1); k++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
278 accj.accum (m.data (k)); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
279 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
280 res.xelem (j) = accj; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
281 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
282 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
283 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
284 template <typename T, typename R, typename ACC> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
285 void row_norms (const MSparse<T>& m, MArray<R>& res, ACC acc) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
286 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
287 res = MArray<R> (dim_vector (m.rows (), 1)); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
288 std::vector<ACC> acci (m.rows (), acc); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
289 for (octave_idx_type j = 0; j < m.columns (); j++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
290 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
291 for (octave_idx_type k = m.cidx (j); k < m.cidx (j+1); k++) |
8308
5fe0f4dfdbec
use std::vector as a simple linear container in oct-norm.cc to avoid problems with instantiating Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents:
8303
diff
changeset
|
292 acci[m.ridx (k)].accum (m.data (k)); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
293 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
294 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
295 for (octave_idx_type i = 0; i < m.rows (); i++) |
8309
05b7a8ffec65
omission from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
8308
diff
changeset
|
296 res.xelem (i) = acci[i]; |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
297 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
298 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
299 // now the dispatchers |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
300 #define DEFINE_DISPATCHER(FUNC_NAME, ARG_TYPE, RES_TYPE) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
301 template <typename T, typename R> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
302 RES_TYPE FUNC_NAME (const ARG_TYPE& v, R p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
303 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
304 RES_TYPE res; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
305 if (p == 2) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
306 FUNC_NAME (v, res, norm_accumulator_2<R> ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
307 else if (p == 1) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
308 FUNC_NAME (v, res, norm_accumulator_1<R> ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
309 else if (lo_ieee_isinf (p)) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
310 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
311 if (p > 0) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
312 FUNC_NAME (v, res, norm_accumulator_inf<R> ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
313 else \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
314 FUNC_NAME (v, res, norm_accumulator_minf<R> ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
315 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
316 else if (p == 0) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
317 FUNC_NAME (v, res, norm_accumulator_0<R> ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
318 else if (p > 0) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
319 FUNC_NAME (v, res, norm_accumulator_p<R> (p)); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
320 else \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
321 FUNC_NAME (v, res, norm_accumulator_mp<R> (p)); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
322 return res; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
323 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
324 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
325 DEFINE_DISPATCHER (vector_norm, MArray<T>, R) |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10145
diff
changeset
|
326 DEFINE_DISPATCHER (column_norms, MArray<T>, MArray<R>) |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10145
diff
changeset
|
327 DEFINE_DISPATCHER (row_norms, MArray<T>, MArray<R>) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
328 DEFINE_DISPATCHER (column_norms, MSparse<T>, MArray<R>) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
329 DEFINE_DISPATCHER (row_norms, MSparse<T>, MArray<R>) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
330 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
331 // The approximate subproblem in Higham's method. Find lambda and mu such that |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
332 // norm ([lambda, mu], p) == 1 and norm (y*lambda + col*mu, p) is maximized. |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
333 // Real version. As in Higham's paper. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
334 template <typename ColVectorT, typename R> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
335 static void |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
336 higham_subp (const ColVectorT& y, const ColVectorT& col, |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
337 octave_idx_type nsamp, R p, R& lambda, R& mu) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
338 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
339 R nrm = 0; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
340 for (octave_idx_type i = 0; i < nsamp; i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
341 { |
10145
fa01c1670b3e
make p-norms breakable
Jaroslav Hajek <highegg@gmail.com>
parents:
10142
diff
changeset
|
342 octave_quit (); |
19924
277b12eed117
oct-norm.cc: Use static_cast<R> (M_PI) for cases where R is not double.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
343 R fi = i * static_cast<R> (M_PI) / nsamp; |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
344 R lambda1 = cos (fi); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
345 R mu1 = sin (fi); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
346 R lmnr = std::pow (std::pow (std::abs (lambda1), p) + |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
347 std::pow (std::abs (mu1), p), 1/p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
348 lambda1 /= lmnr; mu1 /= lmnr; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
349 R nrm1 = vector_norm (lambda1 * y + mu1 * col, p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
350 if (nrm1 > nrm) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
351 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
352 lambda = lambda1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
353 mu = mu1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
354 nrm = nrm1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
355 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
356 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
357 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
358 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
359 // Complex version. Higham's paper does not deal with complex case, so we use |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
360 // a simple extension. First, guess the magnitudes as in real version, then |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
361 // try to rotate lambda to improve further. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
362 template <typename ColVectorT, typename R> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
363 static void |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
364 higham_subp (const ColVectorT& y, const ColVectorT& col, |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
365 octave_idx_type nsamp, R p, |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
366 std::complex<R>& lambda, std::complex<R>& mu) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
367 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
368 typedef std::complex<R> CR; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
369 R nrm = 0; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
370 lambda = 1.0; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
371 CR lamcu = lambda / std::abs (lambda); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
372 // Probe magnitudes |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
373 for (octave_idx_type i = 0; i < nsamp; i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
374 { |
10145
fa01c1670b3e
make p-norms breakable
Jaroslav Hajek <highegg@gmail.com>
parents:
10142
diff
changeset
|
375 octave_quit (); |
19924
277b12eed117
oct-norm.cc: Use static_cast<R> (M_PI) for cases where R is not double.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
376 R fi = i * static_cast<R> (M_PI) / nsamp; |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
377 R lambda1 = cos (fi); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
378 R mu1 = sin (fi); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
379 R lmnr = std::pow (std::pow (std::abs (lambda1), p) + |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
380 std::pow (std::abs (mu1), p), 1/p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
381 lambda1 /= lmnr; mu1 /= lmnr; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
382 R nrm1 = vector_norm (lambda1 * lamcu * y + mu1 * col, p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
383 if (nrm1 > nrm) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
384 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
385 lambda = lambda1 * lamcu; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
386 mu = mu1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
387 nrm = nrm1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
388 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
389 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
390 R lama = std::abs (lambda); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
391 // Probe orientation |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
392 for (octave_idx_type i = 0; i < nsamp; i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
393 { |
10145
fa01c1670b3e
make p-norms breakable
Jaroslav Hajek <highegg@gmail.com>
parents:
10142
diff
changeset
|
394 octave_quit (); |
19924
277b12eed117
oct-norm.cc: Use static_cast<R> (M_PI) for cases where R is not double.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
395 R fi = i * static_cast<R> (M_PI) / nsamp; |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
396 lamcu = CR (cos (fi), sin (fi)); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
397 R nrm1 = vector_norm (lama * lamcu * y + mu * col, p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
398 if (nrm1 > nrm) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
399 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
400 lambda = lama * lamcu; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
401 nrm = nrm1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
402 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
403 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
404 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
405 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
406 // the p-dual element (should work for both real and complex) |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
407 template <typename T, typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
408 inline T elem_dual_p (T x, R p) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
409 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
410 return octave::math::signum (x) * std::pow (std::abs (x), p-1); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
411 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
412 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
413 // the VectorT is used for vectors, but actually it has to be |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
414 // a Matrix type to allow all the operations. For instance SparseMatrix |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
415 // does not support multiplication with column/row vectors. |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
416 // the dual vector |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
417 template <typename VectorT, typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
418 VectorT dual_p (const VectorT& x, R p, R q) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
419 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
420 VectorT res (x.dims ()); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
421 for (octave_idx_type i = 0; i < x.numel (); i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
422 res.xelem (i) = elem_dual_p (x(i), p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
423 return res / vector_norm (res, q); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
424 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
425 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
426 // Higham's hybrid method |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
427 template <typename MatrixT, typename VectorT, typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
428 R higham (const MatrixT& m, R p, R tol, int maxiter, |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
429 VectorT& x) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
430 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
431 x.resize (m.columns (), 1); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
432 // the OSE part |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
433 VectorT y(m.rows (), 1, 0), z(m.rows (), 1); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
434 typedef typename VectorT::element_type RR; |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
435 RR lambda = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
436 RR mu = 1; |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
437 for (octave_idx_type k = 0; k < m.columns (); k++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
438 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
439 octave_quit (); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
440 VectorT col (m.column (k)); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
441 if (k > 0) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
442 higham_subp (y, col, 4*k, p, lambda, mu); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
443 for (octave_idx_type i = 0; i < k; i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
444 x(i) *= lambda; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
445 x(k) = mu; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
446 y = lambda * y + mu * col; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
447 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
448 |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
449 // the PM part |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
450 x = x / vector_norm (x, p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
451 R q = p/(p-1); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
452 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
453 R gamma = 0, gamma1; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
454 int iter = 0; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
455 while (iter < maxiter) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
456 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
457 octave_quit (); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
458 y = m*x; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
459 gamma1 = gamma; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
460 gamma = vector_norm (y, p); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
461 z = dual_p (y, p, q); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
462 z = z.hermitian (); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
463 z = z * m; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
464 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
465 if (iter > 0 && (vector_norm (z, q) <= gamma |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
466 || (gamma - gamma1) <= tol*gamma)) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
467 break; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
468 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
469 z = z.hermitian (); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
470 x = dual_p (z, q, p); |
20714
7b6d8c19dab0
Cuddle increment (++) and decrement (--) operators with variable in question.
Rik <rik@octave.org>
parents:
19924
diff
changeset
|
471 iter++; |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
472 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
473 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
474 return gamma; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
475 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
476 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
477 // derive column vector and SVD types |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
478 |
23141
bfa4ad642fa0
Disallow p-norm of -Inf for matrices (bug #50194).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
479 static const char *p_less1_gripe = "xnorm: p must be >= 1"; |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
480 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
481 // Static constant to control the maximum number of iterations. 100 seems to |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
482 // be a good value. Eventually, we can provide a means to change this |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
483 // constant from Octave. |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
484 static int max_norm_iter = 100; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
485 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
486 // version with SVD for dense matrices |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
487 template <typename MatrixT, typename VectorT, typename R> |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
488 R svd_matrix_norm (const MatrixT& m, R p, VectorT) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
489 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
490 R res = 0; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
491 if (p == 2) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
492 { |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
493 octave::math::svd<MatrixT> fact |
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
494 (m, octave::math::svd<MatrixT>::Type::sigma_only); |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
495 res = fact.singular_values () (0,0); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
496 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
497 else if (p == 1) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
498 res = xcolnorms (m, 1).max (); |
23141
bfa4ad642fa0
Disallow p-norm of -Inf for matrices (bug #50194).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
499 else if (lo_ieee_isinf (p) && p > 1) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
500 res = xrownorms (m, 1).max (); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
501 else if (p > 1) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
502 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
503 VectorT x; |
8995
1b097d86a61a
remove a TODO in oct-norm.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
8319
diff
changeset
|
504 const R sqrteps = std::sqrt (std::numeric_limits<R>::epsilon ()); |
1b097d86a61a
remove a TODO in oct-norm.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
8319
diff
changeset
|
505 res = higham (m, p, sqrteps, max_norm_iter, x); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
506 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
507 else |
26569
0e77df67b522
Add static compile-time checking of printf functions in liboctave.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
508 (*current_liboctave_error_handler) ("%s", p_less1_gripe); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
509 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
510 return res; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
511 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
512 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
513 // SVD-free version for sparse matrices |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
514 template <typename MatrixT, typename VectorT, typename R> |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
515 R matrix_norm (const MatrixT& m, R p, VectorT) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
516 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
517 R res = 0; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
518 if (p == 1) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
519 res = xcolnorms (m, 1).max (); |
23141
bfa4ad642fa0
Disallow p-norm of -Inf for matrices (bug #50194).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
520 else if (lo_ieee_isinf (p) && p > 1) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
521 res = xrownorms (m, 1).max (); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
522 else if (p > 1) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
523 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
524 VectorT x; |
8995
1b097d86a61a
remove a TODO in oct-norm.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
8319
diff
changeset
|
525 const R sqrteps = std::sqrt (std::numeric_limits<R>::epsilon ()); |
1b097d86a61a
remove a TODO in oct-norm.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
8319
diff
changeset
|
526 res = higham (m, p, sqrteps, max_norm_iter, x); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
527 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
528 else |
26569
0e77df67b522
Add static compile-time checking of printf functions in liboctave.
Markus Mützel <markus.muetzel@gmx.de>
parents:
26376
diff
changeset
|
529 (*current_liboctave_error_handler) ("%s", p_less1_gripe); |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
530 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
531 return res; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
532 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
533 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
534 // and finally, here's what we've promised in the header file |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
535 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
536 #define DEFINE_XNORM_FUNCS(PREFIX, RTYPE) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
537 OCTAVE_API RTYPE xnorm (const PREFIX##ColumnVector& x, RTYPE p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
538 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
539 return vector_norm (x, p); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
540 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
541 OCTAVE_API RTYPE xnorm (const PREFIX##RowVector& x, RTYPE p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
542 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
543 return vector_norm (x, p); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
544 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
545 OCTAVE_API RTYPE xnorm (const PREFIX##Matrix& x, RTYPE p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
546 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
547 return svd_matrix_norm (x, p, PREFIX##Matrix ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
548 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
549 OCTAVE_API RTYPE xfrobnorm (const PREFIX##Matrix& x) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
550 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
551 return vector_norm (x, static_cast<RTYPE> (2)); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
552 } |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
553 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
554 DEFINE_XNORM_FUNCS(, double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
555 DEFINE_XNORM_FUNCS(Complex, double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
556 DEFINE_XNORM_FUNCS(Float, float) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
557 DEFINE_XNORM_FUNCS(FloatComplex, float) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
558 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
559 // this is needed to avoid copying the sparse matrix for xfrobnorm |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
560 template <typename T, typename R> |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
561 inline void array_norm_2 (const T *v, octave_idx_type n, R& res) |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
562 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
563 norm_accumulator_2<R> acc; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
564 for (octave_idx_type i = 0; i < n; i++) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
565 acc.accum (v[i]); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
566 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
567 res = acc; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
568 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
569 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
570 #define DEFINE_XNORM_SPARSE_FUNCS(PREFIX, RTYPE) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
571 OCTAVE_API RTYPE xnorm (const Sparse##PREFIX##Matrix& x, RTYPE p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
572 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
573 return matrix_norm (x, p, PREFIX##Matrix ()); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
574 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
575 OCTAVE_API RTYPE xfrobnorm (const Sparse##PREFIX##Matrix& x) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
576 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
577 RTYPE res; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
578 array_norm_2 (x.data (), x.nnz (), res); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
579 return res; \ |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
580 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
581 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
582 DEFINE_XNORM_SPARSE_FUNCS(, double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
583 DEFINE_XNORM_SPARSE_FUNCS(Complex, double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
584 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
585 #define DEFINE_COLROW_NORM_FUNCS(PREFIX, RPREFIX, RTYPE) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
586 extern OCTAVE_API RPREFIX##RowVector \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
587 xcolnorms (const PREFIX##Matrix& m, RTYPE p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
588 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
589 return column_norms (m, p); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
590 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
591 extern OCTAVE_API RPREFIX##ColumnVector \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
592 xrownorms (const PREFIX##Matrix& m, RTYPE p) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
593 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
594 return row_norms (m, p); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
595 } \ |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
596 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
597 DEFINE_COLROW_NORM_FUNCS(, , double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
598 DEFINE_COLROW_NORM_FUNCS(Complex, , double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
599 DEFINE_COLROW_NORM_FUNCS(Float, Float, float) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
600 DEFINE_COLROW_NORM_FUNCS(FloatComplex, Float, float) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
601 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
602 DEFINE_COLROW_NORM_FUNCS(Sparse, , double) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
603 DEFINE_COLROW_NORM_FUNCS(SparseComplex, , double) |