Mercurial > octave
annotate liboctave/array/dColVector.cc @ 31225:3eab70385569
sparse-xpow.cc: Use faster multiplication technique, this time for complex
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Sun, 11 Sep 2022 13:53:38 -0400 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
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:
29358
diff
changeset
|
3 // Copyright (C) 1994-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 //////////////////////////////////////////////////////////////////////// |
458 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
27 # include "config.h" |
458 | 28 #endif |
29 | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
30 #include <istream> |
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
31 #include <ostream> |
458 | 32 |
4669 | 33 #include "Array-util.h" |
22322
93b3cdd36854
move most f77 function decls to separate header files
John W. Eaton <jwe@octave.org>
parents:
22133
diff
changeset
|
34 #include "lo-blas-proto.h" |
1368 | 35 #include "lo-error.h" |
458 | 36 #include "mx-base.h" |
37 #include "mx-inlines.cc" | |
1650 | 38 #include "oct-cmplx.h" |
458 | 39 |
1360 | 40 // Column Vector class. |
458 | 41 |
2386 | 42 bool |
458 | 43 ColumnVector::operator == (const ColumnVector& a) const |
44 { | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
45 octave_idx_type len = numel (); |
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
46 if (len != a.numel ()) |
458 | 47 return 0; |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
48 return mx_inline_equal (len, data (), a.data ()); |
458 | 49 } |
50 | |
2386 | 51 bool |
458 | 52 ColumnVector::operator != (const ColumnVector& a) const |
53 { | |
54 return !(*this == a); | |
55 } | |
56 | |
57 ColumnVector& | |
5275 | 58 ColumnVector::insert (const ColumnVector& a, octave_idx_type r) |
458 | 59 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
60 octave_idx_type a_len = a.numel (); |
4316 | 61 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
62 if (r < 0 || r + a_len > numel ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21120
diff
changeset
|
63 (*current_liboctave_error_handler) ("range error for insert"); |
458 | 64 |
4316 | 65 if (a_len > 0) |
66 { | |
67 make_unique (); | |
68 | |
5275 | 69 for (octave_idx_type i = 0; i < a_len; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
70 xelem (r+i) = a.elem (i); |
4316 | 71 } |
458 | 72 |
73 return *this; | |
74 } | |
75 | |
76 ColumnVector& | |
77 ColumnVector::fill (double val) | |
78 { | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
79 octave_idx_type len = numel (); |
4316 | 80 |
458 | 81 if (len > 0) |
4316 | 82 { |
83 make_unique (); | |
84 | |
5275 | 85 for (octave_idx_type i = 0; i < len; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
86 xelem (i) = val; |
4316 | 87 } |
88 | |
458 | 89 return *this; |
90 } | |
91 | |
92 ColumnVector& | |
5275 | 93 ColumnVector::fill (double val, octave_idx_type r1, octave_idx_type r2) |
458 | 94 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
95 octave_idx_type len = numel (); |
4316 | 96 |
458 | 97 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21120
diff
changeset
|
98 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 99 |
17663
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
100 if (r1 > r2) { std::swap (r1, r2); } |
458 | 101 |
4316 | 102 if (r2 >= r1) |
103 { | |
104 make_unique (); | |
105 | |
5275 | 106 for (octave_idx_type i = r1; i <= r2; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
107 xelem (i) = val; |
4316 | 108 } |
458 | 109 |
110 return *this; | |
111 } | |
112 | |
113 ColumnVector | |
114 ColumnVector::stack (const ColumnVector& a) const | |
115 { | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
116 octave_idx_type len = numel (); |
5275 | 117 octave_idx_type nr_insert = len; |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
118 ColumnVector retval (len + a.numel ()); |
458 | 119 retval.insert (*this, 0); |
120 retval.insert (a, nr_insert); | |
121 return retval; | |
122 } | |
123 | |
124 RowVector | |
125 ColumnVector::transpose (void) const | |
126 { | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14394
diff
changeset
|
127 return MArray<double>::transpose (); |
458 | 128 } |
129 | |
1205 | 130 ColumnVector |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
131 ColumnVector::abs (void) const |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
132 { |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
133 return do_mx_unary_map<double, double, std::abs> (*this); |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
134 } |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
135 |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
136 ColumnVector |
1205 | 137 real (const ComplexColumnVector& a) |
138 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
139 return do_mx_unary_op<double, Complex> (a, mx_inline_real); |
1205 | 140 } |
141 | |
142 ColumnVector | |
143 imag (const ComplexColumnVector& a) | |
144 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
145 return do_mx_unary_op<double, Complex> (a, mx_inline_imag); |
1205 | 146 } |
147 | |
458 | 148 // resize is the destructive equivalent for this one |
149 | |
150 ColumnVector | |
5275 | 151 ColumnVector::extract (octave_idx_type r1, octave_idx_type r2) const |
458 | 152 { |
17663
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
153 if (r1 > r2) { std::swap (r1, r2); } |
458 | 154 |
5275 | 155 octave_idx_type new_r = r2 - r1 + 1; |
458 | 156 |
157 ColumnVector result (new_r); | |
158 | |
5275 | 159 for (octave_idx_type i = 0; i < new_r; i++) |
4316 | 160 result.xelem (i) = elem (r1+i); |
161 | |
162 return result; | |
163 } | |
164 | |
165 ColumnVector | |
5275 | 166 ColumnVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 167 { |
168 ColumnVector result (n); | |
169 | |
5275 | 170 for (octave_idx_type i = 0; i < n; i++) |
4316 | 171 result.xelem (i) = elem (r1+i); |
458 | 172 |
173 return result; | |
174 } | |
175 | |
1205 | 176 // matrix by column vector -> column vector operations |
458 | 177 |
1205 | 178 ColumnVector |
179 operator * (const Matrix& m, const ColumnVector& a) | |
458 | 180 { |
1947 | 181 ColumnVector retval; |
182 | |
22988
cd33c785e80e
put to_f77_int inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22847
diff
changeset
|
183 F77_INT nr = octave::to_f77_int (m.rows ()); |
cd33c785e80e
put to_f77_int inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22847
diff
changeset
|
184 F77_INT nc = octave::to_f77_int (m.cols ()); |
1947 | 185 |
22988
cd33c785e80e
put to_f77_int inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22847
diff
changeset
|
186 F77_INT a_len = octave::to_f77_int (a.numel ()); |
2386 | 187 |
188 if (nc != a_len) | |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
189 octave::err_nonconformant ("operator *", nr, nc, a_len, 1); |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
190 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
191 retval.clear (nr); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
192 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
193 if (nr != 0) |
458 | 194 { |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
195 if (nc == 0) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
196 retval.fill (0.0); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
197 else |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
198 { |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
199 double *y = retval.fortran_vec (); |
1947 | 200 |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
201 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
202 nr, nc, 1.0, m.data (), nr, |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
203 a.data (), 1, 0.0, y, 1 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
204 F77_CHAR_ARG_LEN (1))); |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
205 } |
458 | 206 } |
207 | |
1947 | 208 return retval; |
458 | 209 } |
210 | |
1205 | 211 // diagonal matrix by column vector -> column vector operations |
212 | |
213 ColumnVector | |
214 operator * (const DiagMatrix& m, const ColumnVector& a) | |
458 | 215 { |
1947 | 216 ColumnVector retval; |
217 | |
22988
cd33c785e80e
put to_f77_int inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22847
diff
changeset
|
218 F77_INT nr = octave::to_f77_int (m.rows ()); |
cd33c785e80e
put to_f77_int inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22847
diff
changeset
|
219 F77_INT nc = octave::to_f77_int (m.cols ()); |
1947 | 220 |
22988
cd33c785e80e
put to_f77_int inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22847
diff
changeset
|
221 F77_INT a_len = octave::to_f77_int (a.numel ()); |
1947 | 222 |
1205 | 223 if (nc != a_len) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
224 octave::err_nonconformant ("operator *", nr, nc, a_len, 1); |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
225 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
226 if (nr == 0 || nc == 0) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
227 retval.resize (nr, 0.0); |
1947 | 228 else |
458 | 229 { |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
230 retval.resize (nr); |
1947 | 231 |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
232 for (octave_idx_type i = 0; i < a_len; i++) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
233 retval.elem (i) = a.elem (i) * m.elem (i, i); |
1947 | 234 |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
235 for (octave_idx_type i = a_len; i < nr; i++) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
236 retval.elem (i) = 0.0; |
458 | 237 } |
238 | |
1947 | 239 return retval; |
458 | 240 } |
241 | |
242 // other operations | |
243 | |
244 double | |
245 ColumnVector::min (void) const | |
246 { | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
247 octave_idx_type len = numel (); |
458 | 248 if (len == 0) |
249 return 0.0; | |
250 | |
251 double res = elem (0); | |
252 | |
5275 | 253 for (octave_idx_type i = 1; i < len; i++) |
458 | 254 if (elem (i) < res) |
255 res = elem (i); | |
256 | |
257 return res; | |
258 } | |
259 | |
260 double | |
261 ColumnVector::max (void) const | |
262 { | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
263 octave_idx_type len = numel (); |
458 | 264 if (len == 0) |
265 return 0.0; | |
266 | |
267 double res = elem (0); | |
268 | |
5275 | 269 for (octave_idx_type i = 1; i < len; i++) |
458 | 270 if (elem (i) > res) |
271 res = elem (i); | |
272 | |
273 return res; | |
274 } | |
275 | |
3504 | 276 std::ostream& |
277 operator << (std::ostream& os, const ColumnVector& a) | |
458 | 278 { |
279 // int field_width = os.precision () + 7; | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
280 for (octave_idx_type i = 0; i < a.numel (); i++) |
458 | 281 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
282 return os; | |
283 } | |
284 | |
3504 | 285 std::istream& |
286 operator >> (std::istream& is, ColumnVector& a) | |
458 | 287 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
288 octave_idx_type len = a.numel (); |
458 | 289 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
290 if (len > 0) |
458 | 291 { |
292 double tmp; | |
5275 | 293 for (octave_idx_type i = 0; i < len; i++) |
458 | 294 { |
295 is >> tmp; | |
296 if (is) | |
297 a.elem (i) = tmp; | |
298 else | |
299 break; | |
300 } | |
301 } | |
532 | 302 return is; |
458 | 303 } |