annotate liboctave/numeric/oct-norm.cc @ 31608:23664317f0d3

maint: merge stable to default
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 20:05:44 -0800
parents dfa5d9c3ae72 aac27ad79be6
children 5f11de0e7440
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
3 // Copyright (C) 2008-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21723
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21275
diff changeset
27 # include "config.h"
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #endif
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include <cmath>
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
31
23475
d691ed308237 maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents: 23449
diff changeset
32 #include <algorithm>
23443
3f1bf237908b maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents: 23220
diff changeset
33 #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
34 #include <vector>
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
36 #include "Array.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
37 #include "CColVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
38 #include "CMatrix.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
39 #include "CRowVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
40 #include "CSparse.h"
23475
d691ed308237 maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents: 23449
diff changeset
41 #include "MArray.h"
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
42 #include "dColVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
43 #include "dDiagMatrix.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
44 #include "dMatrix.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
45 #include "dRowVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
46 #include "dSparse.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
47 #include "fCColVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
48 #include "fCMatrix.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
49 #include "fCRowVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
50 #include "fColVector.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
51 #include "fDiagMatrix.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
52 #include "fMatrix.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
53 #include "fRowVector.h"
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 #include "lo-error.h"
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 #include "lo-ieee.h"
23475
d691ed308237 maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents: 23449
diff changeset
56 #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
57 #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
58 #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
59 #include "mx-fs-fcm.h"
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
60 #include "mx-s-cm.h"
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
61 #include "oct-cmplx.h"
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
62 #include "oct-norm.h"
23475
d691ed308237 maint: Clean up #includes in liboctave/numeric directory.
Rik <rik@octave.org>
parents: 23449
diff changeset
63 #include "quit.h"
21273
cbced1c09916 better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents: 21202
diff changeset
64 #include "svd.h"
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30898
diff changeset
66 OCTAVE_BEGIN_NAMESPACE(octave)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30898
diff changeset
67
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
68 // Theory: norm accumulator is an object that has an accum method able
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
69 // to handle both real and complex element, and a cast operator
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
70 // returning the intermediate norm. Reference: Higham, N. "Estimating
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
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
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
73 // norm accumulator for the p-norm
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
74 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
75 class norm_accumulator_p
19861
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
76 {
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
77 public:
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
78 norm_accumulator_p () { } // we need this one for Array
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
79 norm_accumulator_p (R pp) : m_p(pp), m_scl(0), m_sum(1) { }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
80
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
81 template <typename U>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
82 void accum (U val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
83 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
84 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
85 R t = std::abs (val);
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
86 if (m_scl == t) // we need this to handle Infs properly
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
87 m_sum += 1;
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
88 else if (m_scl < t)
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
89 {
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
90 m_sum *= std::pow (m_scl/t, m_p);
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
91 m_sum += 1;
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
92 m_scl = t;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
93 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
94 else if (t != 0)
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
95 m_sum += std::pow (t/m_scl, m_p);
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
96 }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
97
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
98 operator R () { return m_scl * std::pow (m_sum, 1/m_p); }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
99
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
100 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
101 R m_p, m_scl, m_sum;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
102 };
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
103
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
104 // norm accumulator for the minus p-pseudonorm
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
105 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
106 class norm_accumulator_mp
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
107 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
108 public:
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
109 norm_accumulator_mp () { } // we need this one for Array
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
110 norm_accumulator_mp (R pp) : m_p(pp), m_scl(0), m_sum(1) { }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
111
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
112 template <typename U>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
113 void accum (U val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
114 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
115 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
116 R t = 1 / std::abs (val);
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
117 if (m_scl == t)
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
118 m_sum += 1;
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
119 else if (m_scl < t)
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
120 {
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
121 m_sum *= std::pow (m_scl/t, m_p);
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
122 m_sum += 1;
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
123 m_scl = t;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
124 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
125 else if (t != 0)
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
126 m_sum += std::pow (t/m_scl, m_p);
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
127 }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
128
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
129 operator R () { return m_scl * std::pow (m_sum, -1/m_p); }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
130
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
131 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
132 R m_p, m_scl, m_sum;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
133 };
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
135 // norm accumulator for the 2-norm (euclidean)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
136 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
137 class norm_accumulator_2
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
138 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
139 public:
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
140 norm_accumulator_2 () : m_scl(0), m_sum(1) { }
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
141
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
142 void accum (R val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
143 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
144 R t = std::abs (val);
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
145 if (m_scl == t)
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
146 m_sum += 1;
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
147 else if (m_scl < t)
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
148 {
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
149 m_sum *= pow2 (m_scl/t);
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
150 m_sum += 1;
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
151 m_scl = t;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
152 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
153 else if (t != 0)
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
154 m_sum += pow2 (t/m_scl);
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
155 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
156
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
157 void accum (std::complex<R> val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
158 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
159 accum (val.real ());
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
160 accum (val.imag ());
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
161 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
162
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
163 operator R () { return m_scl * std::sqrt (m_sum); }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
164
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
165 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
166 static inline R pow2 (R x) { return x*x; }
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
167
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
168 //--------
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
170 R m_scl, m_sum;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
171 };
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
172
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
173 // norm accumulator for the 1-norm (city metric)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
174 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
175 class norm_accumulator_1
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
176 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
177 public:
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
178 norm_accumulator_1 () : m_sum (0) { }
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
179 template <typename U>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
180 void accum (U val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
181 {
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
182 m_sum += std::abs (val);
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
183 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
184
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
185 operator R () { return m_sum; }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
186
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
187 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
188 R m_sum;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
189 };
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
190
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
191 // norm accumulator for the inf-norm (max metric)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
192 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
193 class norm_accumulator_inf
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
194 {
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
195 public:
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
196 norm_accumulator_inf () : m_max (0) { }
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
197 template <typename U>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
198 void accum (U val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
199 {
29950
6549fa7558ba eliminate some unnecessary octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents: 29874
diff changeset
200 if (math::isnan (val))
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
201 m_max = numeric_limits<R>::NaN ();
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
202 else
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
203 m_max = std::max (m_max, std::abs (val));
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
204 }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
205
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
206 operator R () { return m_max; }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
207
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
208 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
209 R m_max;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
210 };
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
211
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
212 // norm accumulator for the -inf pseudonorm (min abs value)
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
213 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
214 class norm_accumulator_minf
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
215 {
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
216 public:
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
217 norm_accumulator_minf () : m_min (numeric_limits<R>::Inf ()) { }
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
218 template <typename U>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
219 void accum (U val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
220 {
29950
6549fa7558ba eliminate some unnecessary octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents: 29874
diff changeset
221 if (math::isnan (val))
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
222 m_min = numeric_limits<R>::NaN ();
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
223 else
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
224 m_min = std::min (m_min, std::abs (val));
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
225 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
226
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
227 operator R () { return m_min; }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
228
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
229 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
230 R m_min;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
231 };
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
232
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
233 // norm accumulator for the 0-pseudonorm (hamming distance)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
234 template <typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
235 class norm_accumulator_0
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
236 {
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
237 public:
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
238 norm_accumulator_0 () : m_num (0) { }
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
239 template <typename U>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
240 void accum (U val)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
241 {
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
242 if (val != static_cast<U> (0)) ++m_num;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
243 }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
244
30098
de8ba056e8e2 maint: use "m_" prefix for more member variables in oct-norm.cc (da3659538d16)
Rik <rik@octave.org>
parents: 30097
diff changeset
245 operator R () { return m_num; }
30114
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
246
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
247 private:
b0af792466dc maint: Add "private:" access specifier to classes in oct-norm.cc.
Rik <rik@octave.org>
parents: 30098
diff changeset
248 unsigned int m_num;
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
249 };
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
250
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
251 // OK, we're armed :) Now let's go for the fun
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
252
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
253 template <typename T, typename R, typename ACC>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
254 inline void vector_norm (const Array<T>& v, R& res, ACC acc)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
255 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
256 for (octave_idx_type i = 0; i < v.numel (); i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
257 acc.accum (v(i));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
258
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
259 res = acc;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
260 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
261
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
262 // dense versions
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
263 template <typename T, typename R, typename ACC>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
264 void column_norms (const MArray<T>& m, MArray<R>& res, ACC acc)
19861
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
265 {
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
266 res = MArray<R> (dim_vector (1, m.columns ()));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
267 for (octave_idx_type j = 0; j < m.columns (); j++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
268 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
269 ACC accj = acc;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
270 for (octave_idx_type i = 0; i < m.rows (); i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
271 accj.accum (m(i, j));
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
272
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
273 res.xelem (j) = accj;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
274 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
275 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
276
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
277 template <typename T, typename R, typename ACC>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
278 void row_norms (const MArray<T>& m, MArray<R>& res, ACC acc)
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
279 {
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
280 res = MArray<R> (dim_vector (m.rows (), 1));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
281 std::vector<ACC> acci (m.rows (), acc);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
282 for (octave_idx_type j = 0; j < m.columns (); j++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
283 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
284 for (octave_idx_type i = 0; i < m.rows (); i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
285 acci[i].accum (m(i, j));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
286 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
287
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
288 for (octave_idx_type i = 0; i < m.rows (); i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
289 res.xelem (i) = acci[i];
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
290 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
291
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
292 // sparse versions
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
293 template <typename T, typename R, typename ACC>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
294 void column_norms (const MSparse<T>& m, MArray<R>& res, ACC acc)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
295 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
296 res = MArray<R> (dim_vector (1, m.columns ()));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
297 for (octave_idx_type j = 0; j < m.columns (); j++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
298 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
299 ACC accj = acc;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
300 for (octave_idx_type k = m.cidx (j); k < m.cidx (j+1); k++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
301 accj.accum (m.data (k));
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
302
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
303 res.xelem (j) = accj;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
304 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
305 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
306
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
307 template <typename T, typename R, typename ACC>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
308 void row_norms (const MSparse<T>& m, MArray<R>& res, ACC acc)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
309 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
310 res = MArray<R> (dim_vector (m.rows (), 1));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
311 std::vector<ACC> acci (m.rows (), acc);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
312 for (octave_idx_type j = 0; j < m.columns (); j++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
313 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
314 for (octave_idx_type k = m.cidx (j); k < m.cidx (j+1); k++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
315 acci[m.ridx (k)].accum (m.data (k));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
316 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
317
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
318 for (octave_idx_type i = 0; i < m.rows (); i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
319 res.xelem (i) = acci[i];
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
320 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
321
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
322 // now the dispatchers
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
323 #define DEFINE_DISPATCHER(FCN_NAME, ARG_TYPE, RES_TYPE) \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
324 template <typename T, typename R> \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
325 RES_TYPE FCN_NAME (const ARG_TYPE& v, R p) \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
326 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
327 RES_TYPE res; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
328 if (p == 2) \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
329 FCN_NAME (v, res, norm_accumulator_2<R> ()); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
330 else if (p == 1) \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
331 FCN_NAME (v, res, norm_accumulator_1<R> ()); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
332 else if (lo_ieee_isinf (p)) \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
333 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
334 if (p > 0) \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
335 FCN_NAME (v, res, norm_accumulator_inf<R> ()); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
336 else \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
337 FCN_NAME (v, res, norm_accumulator_minf<R> ()); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
338 } \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
339 else if (p == 0) \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
340 FCN_NAME (v, res, norm_accumulator_0<R> ()); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
341 else if (p > 0) \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
342 FCN_NAME (v, res, norm_accumulator_p<R> (p)); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
343 else \
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
344 FCN_NAME (v, res, norm_accumulator_mp<R> (p)); \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
345 return res; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
346 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
347
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
348 DEFINE_DISPATCHER (vector_norm, MArray<T>, R)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
349 DEFINE_DISPATCHER (column_norms, MArray<T>, MArray<R>)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
350 DEFINE_DISPATCHER (row_norms, MArray<T>, MArray<R>)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
351 DEFINE_DISPATCHER (column_norms, MSparse<T>, MArray<R>)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
352 DEFINE_DISPATCHER (row_norms, MSparse<T>, MArray<R>)
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
353
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
354 // The approximate subproblem in Higham's method. Find lambda and mu such
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
355 // that norm ([lambda, mu], p) == 1 and norm (y*lambda + col*mu, p) is
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
356 // maximized.
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
357 // Real version. As in Higham's paper.
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
358 template <typename ColVectorT, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
359 static void
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
360 higham_subp (const ColVectorT& y, const ColVectorT& col,
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
361 octave_idx_type nsamp, R p, R& lambda, R& mu)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
362 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
363 R nrm = 0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
364 for (octave_idx_type i = 0; i < nsamp; i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
365 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
366 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
367 R fi = i * static_cast<R> (M_PI) / nsamp;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
368 R lambda1 = cos (fi);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
369 R mu1 = sin (fi);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
370 R lmnr = std::pow (std::pow (std::abs (lambda1), p) +
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
371 std::pow (std::abs (mu1), p), 1/p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
372 lambda1 /= lmnr; mu1 /= lmnr;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
373 R nrm1 = vector_norm (lambda1 * y + mu1 * col, p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
374 if (nrm1 > nrm)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
375 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
376 lambda = lambda1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
377 mu = mu1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
378 nrm = nrm1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
379 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
380 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
381 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
382
30097
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
383 // Complex version. Higham's paper does not deal with complex case, so we
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
384 // use a simple extension. First, guess the magnitudes as in real version,
da3659538d16 maint: use "m_" prefix for member variables in oct-norm.cc accumulator classes.
Rik <rik@octave.org>
parents: 29950
diff changeset
385 // then try to rotate lambda to improve further.
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
386 template <typename ColVectorT, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
387 static void
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
388 higham_subp (const ColVectorT& y, const ColVectorT& col,
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
389 octave_idx_type nsamp, R p,
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
390 std::complex<R>& lambda, std::complex<R>& mu)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
391 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
392 typedef std::complex<R> CR;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
393 R nrm = 0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
394 lambda = 1.0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
395 CR lamcu = lambda / std::abs (lambda);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
396 // Probe magnitudes
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
397 for (octave_idx_type i = 0; i < nsamp; i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
398 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
399 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
400 R fi = i * static_cast<R> (M_PI) / nsamp;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
401 R lambda1 = cos (fi);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
402 R mu1 = sin (fi);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
403 R lmnr = std::pow (std::pow (std::abs (lambda1), p) +
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
404 std::pow (std::abs (mu1), p), 1/p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
405 lambda1 /= lmnr; mu1 /= lmnr;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
406 R nrm1 = vector_norm (lambda1 * lamcu * y + mu1 * col, p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
407 if (nrm1 > nrm)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
408 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
409 lambda = lambda1 * lamcu;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
410 mu = mu1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
411 nrm = nrm1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
412 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
413 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
414 R lama = std::abs (lambda);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
415 // Probe orientation
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
416 for (octave_idx_type i = 0; i < nsamp; i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
417 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
418 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
419 R fi = i * static_cast<R> (M_PI) / nsamp;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
420 lamcu = CR (cos (fi), sin (fi));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
421 R nrm1 = vector_norm (lama * lamcu * y + mu * col, p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
422 if (nrm1 > nrm)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
423 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
424 lambda = lama * lamcu;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
425 nrm = nrm1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
426 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
427 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
428 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
429
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
430 // the p-dual element (should work for both real and complex)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
431 template <typename T, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
432 inline T elem_dual_p (T x, R p)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
433 {
29950
6549fa7558ba eliminate some unnecessary octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents: 29874
diff changeset
434 return math::signum (x) * std::pow (std::abs (x), p-1);
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
435 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
436
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
437 // the VectorT is used for vectors, but actually it has to be
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
438 // a Matrix type to allow all the operations. For instance SparseMatrix
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
439 // does not support multiplication with column/row vectors.
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
440 // the dual vector
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
441 template <typename VectorT, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
442 VectorT dual_p (const VectorT& x, R p, R q)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
443 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
444 VectorT res (x.dims ());
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
445 for (octave_idx_type i = 0; i < x.numel (); i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
446 res.xelem (i) = elem_dual_p (x(i), p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
447 return res / vector_norm (res, q);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
448 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
449
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
450 // Higham's hybrid method
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
451 template <typename MatrixT, typename VectorT, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
452 R higham (const MatrixT& m, R p, R tol, int maxiter,
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
453 VectorT& x)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
454 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
455 x.resize (m.columns (), 1);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
456 // the OSE part
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
457 VectorT y(m.rows (), 1, 0), z(m.rows (), 1);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
458 typedef typename VectorT::element_type RR;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
459 RR lambda = 0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
460 RR mu = 1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
461 for (octave_idx_type k = 0; k < m.columns (); k++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
462 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
463 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
464 VectorT col (m.column (k));
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
465 if (k > 0)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
466 higham_subp (y, col, 4*k, p, lambda, mu);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
467 for (octave_idx_type i = 0; i < k; i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
468 x(i) *= lambda;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
469 x(k) = mu;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
470 y = lambda * y + mu * col;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
471 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
472
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
473 // the PM part
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
474 x = x / vector_norm (x, p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
475 R q = p/(p-1);
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
476
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
477 R gamma = 0, gamma1;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
478 int iter = 0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
479 while (iter < maxiter)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
480 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
481 octave_quit ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
482 y = m*x;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
483 gamma1 = gamma;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
484 gamma = vector_norm (y, p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
485 z = dual_p (y, p, q);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
486 z = z.hermitian ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
487 z = z * m;
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
488
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
489 if (iter > 0 && (vector_norm (z, q) <= gamma
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
490 || (gamma - gamma1) <= tol*gamma))
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
491 break;
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
492
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
493 z = z.hermitian ();
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
494 x = dual_p (z, q, p);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
495 iter++;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
496 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
497
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
498 return gamma;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
499 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
500
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
501 // derive column vector and SVD types
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
502
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
503 static const char *p_less1_gripe = "xnorm: p must be >= 1";
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
504
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
505 // Static constant to control the maximum number of iterations. 100 seems to
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
506 // be a good value. Eventually, we can provide a means to change this
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
507 // constant from Octave.
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
508 static int max_norm_iter = 100;
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
509
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
510 // version with SVD for dense matrices
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
511 template <typename MatrixT, typename VectorT, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
512 R svd_matrix_norm (const MatrixT& m, R p, VectorT)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
513 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
514 R res = 0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
515 if (p == 2)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
516 {
29950
6549fa7558ba eliminate some unnecessary octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents: 29874
diff changeset
517 math::svd<MatrixT> fact (m, math::svd<MatrixT>::Type::sigma_only);
30394
f3f3e3793fb5 maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30114
diff changeset
518 res = fact.singular_values () (0, 0);
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
519 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
520 else if (p == 1)
31457
a96f68a48e9e maint: Remove more deprecated code from Octave 9.
Rik <rik@octave.org>
parents: 30898
diff changeset
521 res = xcolnorms (m, static_cast<R> (1)).max ();
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
522 else if (lo_ieee_isinf (p) && p > 1)
31457
a96f68a48e9e maint: Remove more deprecated code from Octave 9.
Rik <rik@octave.org>
parents: 30898
diff changeset
523 res = xrownorms (m, static_cast<R> (1)).max ();
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
524 else if (p > 1)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
525 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
526 VectorT x;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
527 const R sqrteps = std::sqrt (std::numeric_limits<R>::epsilon ());
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
528 res = higham (m, p, sqrteps, max_norm_iter, x);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
529 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
530 else
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
531 (*current_liboctave_error_handler) ("%s", p_less1_gripe);
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
532
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
533 return res;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
534 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
535
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
536 // SVD-free version for sparse matrices
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
537 template <typename MatrixT, typename VectorT, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
538 R matrix_norm (const MatrixT& m, R p, VectorT)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
539 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
540 R res = 0;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
541 if (p == 1)
31457
a96f68a48e9e maint: Remove more deprecated code from Octave 9.
Rik <rik@octave.org>
parents: 30898
diff changeset
542 res = xcolnorms (m, static_cast<R> (1)).max ();
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
543 else if (lo_ieee_isinf (p) && p > 1)
31457
a96f68a48e9e maint: Remove more deprecated code from Octave 9.
Rik <rik@octave.org>
parents: 30898
diff changeset
544 res = xrownorms (m, static_cast<R> (1)).max ();
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
545 else if (p > 1)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
546 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
547 VectorT x;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
548 const R sqrteps = std::sqrt (std::numeric_limits<R>::epsilon ());
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
549 res = higham (m, p, sqrteps, max_norm_iter, x);
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
550 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
551 else
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
552 (*current_liboctave_error_handler) ("%s", p_less1_gripe);
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
553
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
554 return res;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
555 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
556
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
557 // and finally, here's what we've promised in the header file
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
558
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
559 #define DEFINE_XNORM_FCNS(PREFIX, RTYPE) \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
560 RTYPE xnorm (const PREFIX##ColumnVector& x, RTYPE p) \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
561 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
562 return vector_norm (x, p); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
563 } \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
564 RTYPE xnorm (const PREFIX##RowVector& x, RTYPE p) \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
565 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
566 return vector_norm (x, p); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
567 } \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
568 RTYPE xnorm (const PREFIX##Matrix& x, RTYPE p) \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
569 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
570 return svd_matrix_norm (x, p, PREFIX##Matrix ()); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
571 } \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
572 RTYPE xfrobnorm (const PREFIX##Matrix& x) \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
573 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
574 return vector_norm (x, static_cast<RTYPE> (2)); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
575 }
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
576
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
577 DEFINE_XNORM_FCNS(, double)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
578 DEFINE_XNORM_FCNS(Complex, double)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
579 DEFINE_XNORM_FCNS(Float, float)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
580 DEFINE_XNORM_FCNS(FloatComplex, float)
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
581
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
582 // this is needed to avoid copying the sparse matrix for xfrobnorm
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
583 template <typename T, typename R>
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
584 inline void array_norm_2 (const T *v, octave_idx_type n, R& res)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
585 {
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
586 norm_accumulator_2<R> acc;
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
587 for (octave_idx_type i = 0; i < n; i++)
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
588 acc.accum (v[i]);
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
589
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
590 res = acc;
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
591 }
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
592
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
593 #define DEFINE_XNORM_SPARSE_FCNS(PREFIX, RTYPE) \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
594 RTYPE xnorm (const Sparse##PREFIX##Matrix& x, RTYPE p) \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
595 { \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
596 return matrix_norm (x, p, PREFIX##Matrix ()); \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
597 } \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
598 RTYPE xfrobnorm (const Sparse##PREFIX##Matrix& x) \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
599 { \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
600 RTYPE res; \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
601 array_norm_2 (x.data (), x.nnz (), res); \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
602 return res; \
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
603 }
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
604
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
605 DEFINE_XNORM_SPARSE_FCNS(, double)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
606 DEFINE_XNORM_SPARSE_FCNS(Complex, double)
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
607
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
608 #define DEFINE_COLROW_NORM_FCNS(PREFIX, RPREFIX, RTYPE) \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
609 RPREFIX##RowVector \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
610 xcolnorms (const PREFIX##Matrix& m, RTYPE p) \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
611 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
612 return column_norms (m, p); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
613 } \
29874
92662b17ef7e move liboctave xnorm functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
614 RPREFIX##ColumnVector \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
615 xrownorms (const PREFIX##Matrix& m, RTYPE p) \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
616 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
617 return row_norms (m, p); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22022
diff changeset
618 } \
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
619
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
620 DEFINE_COLROW_NORM_FCNS(, , double)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
621 DEFINE_COLROW_NORM_FCNS(Complex, , double)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
622 DEFINE_COLROW_NORM_FCNS(Float, Float, float)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
623 DEFINE_COLROW_NORM_FCNS(FloatComplex, Float, float)
8303
b11c31849b44 improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
624
30898
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
625 DEFINE_COLROW_NORM_FCNS(Sparse, , double)
51a3d3a69193 maint: Use "fcn" as preferred abbreviation for "function" in liboctave/.
Rik <rik@octave.org>
parents: 30564
diff changeset
626 DEFINE_COLROW_NORM_FCNS(SparseComplex, , double)
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30898
diff changeset
627
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30898
diff changeset
628 OCTAVE_END_NAMESPACE(octave)