GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
CRowVector.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 
29 #include "Array-util.h"
30 #include "functor.h"
31 #include "lo-blas-proto.h"
32 #include "lo-error.h"
33 #include "mx-base.h"
34 #include "mx-inlines.cc"
35 #include "oct-cmplx.h"
36 
37 // Complex Row Vector class
38 
39 bool
41 {
42  octave_idx_type len = numel ();
43  if (len != a.numel ())
44  return 0;
45  return mx_inline_equal (len, data (), a.data ());
46 }
47 
48 bool
50 {
51  return !(*this == a);
52 }
53 
54 // destructive insert/delete/reorder operations
55 
58 {
59  octave_idx_type a_len = a.numel ();
60 
61  if (c < 0 || c + a_len > numel ())
62  (*current_liboctave_error_handler) ("range error for insert");
63 
64  if (a_len > 0)
65  {
66  make_unique ();
67 
68  for (octave_idx_type i = 0; i < a_len; i++)
69  xelem (c+i) = a.elem (i);
70  }
71 
72  return *this;
73 }
74 
77 {
78  octave_idx_type a_len = a.numel ();
79 
80  if (c < 0 || c + a_len > numel ())
81  (*current_liboctave_error_handler) ("range error for insert");
82 
83  if (a_len > 0)
84  {
85  make_unique ();
86 
87  for (octave_idx_type i = 0; i < a_len; i++)
88  xelem (c+i) = a.elem (i);
89  }
90 
91  return *this;
92 }
93 
96 {
97  octave_idx_type len = numel ();
98 
99  if (len > 0)
100  {
101  make_unique ();
102 
103  for (octave_idx_type i = 0; i < len; i++)
104  xelem (i) = val;
105  }
106 
107  return *this;
108 }
109 
112 {
113  octave_idx_type len = numel ();
114 
115  if (len > 0)
116  {
117  make_unique ();
118 
119  for (octave_idx_type i = 0; i < len; i++)
120  xelem (i) = val;
121  }
122 
123  return *this;
124 }
125 
128 {
129  octave_idx_type len = numel ();
130 
131  if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len)
132  (*current_liboctave_error_handler) ("range error for fill");
133 
134  if (c1 > c2) { std::swap (c1, c2); }
135 
136  if (c2 >= c1)
137  {
138  make_unique ();
139 
140  for (octave_idx_type i = c1; i <= c2; i++)
141  xelem (i) = val;
142  }
143 
144  return *this;
145 }
146 
150 {
151  octave_idx_type len = numel ();
152 
153  if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len)
154  (*current_liboctave_error_handler) ("range error for fill");
155 
156  if (c1 > c2) { std::swap (c1, c2); }
157 
158  if (c2 >= c1)
159  {
160  make_unique ();
161 
162  for (octave_idx_type i = c1; i <= c2; i++)
163  xelem (i) = val;
164  }
165 
166  return *this;
167 }
168 
171 {
172  octave_idx_type len = numel ();
173  octave_idx_type nc_insert = len;
174  ComplexRowVector retval (len + a.numel ());
175  retval.insert (*this, 0);
176  retval.insert (a, nc_insert);
177  return retval;
178 }
179 
182 {
183  octave_idx_type len = numel ();
184  octave_idx_type nc_insert = len;
185  ComplexRowVector retval (len + a.numel ());
186  retval.insert (*this, 0);
187  retval.insert (a, nc_insert);
188  return retval;
189 }
190 
193 {
195 }
196 
199 {
200  return MArray<Complex>::transpose ();
201 }
202 
205 {
206  return do_mx_unary_map<Complex, Complex, std::conj<double>> (a);
207 }
208 
209 // resize is the destructive equivalent for this one
210 
213 {
214  if (c1 > c2) { std::swap (c1, c2); }
215 
216  octave_idx_type new_c = c2 - c1 + 1;
217 
218  ComplexRowVector result (new_c);
219 
220  for (octave_idx_type i = 0; i < new_c; i++)
221  result.elem (i) = elem (c1+i);
222 
223  return result;
224 }
225 
228 {
230 
231  for (octave_idx_type i = 0; i < n; i++)
232  result.elem (i) = elem (r1+i);
233 
234  return result;
235 }
236 
237 // row vector by row vector -> row vector operations
238 
241 {
242  octave_idx_type len = numel ();
243 
244  octave_idx_type a_len = a.numel ();
245 
246  if (len != a_len)
247  octave::err_nonconformant ("operator +=", len, a_len);
248 
249  if (len == 0)
250  return *this;
251 
252  Complex *d = fortran_vec (); // Ensures only one reference to my privates!
253 
254  mx_inline_add2 (len, d, a.data ());
255  return *this;
256 }
257 
260 {
261  octave_idx_type len = numel ();
262 
263  octave_idx_type a_len = a.numel ();
264 
265  if (len != a_len)
266  octave::err_nonconformant ("operator -=", len, a_len);
267 
268  if (len == 0)
269  return *this;
270 
271  Complex *d = fortran_vec (); // Ensures only one reference to my privates!
272 
273  mx_inline_sub2 (len, d, a.data ());
274  return *this;
275 }
276 
277 // row vector by matrix -> row vector
278 
281 {
283 
284  F77_INT len = octave::to_f77_int (v.numel ());
285 
286  F77_INT a_nr = octave::to_f77_int (a.rows ());
287  F77_INT a_nc = octave::to_f77_int (a.cols ());
288 
289  if (a_nr != len)
290  octave::err_nonconformant ("operator *", 1, len, a_nr, a_nc);
291 
292  if (len == 0)
293  retval.resize (a_nc, 0.0);
294  else
295  {
296  // Transpose A to form A'*x == (x'*A)'
297 
298  F77_INT ld = a_nr;
299 
300  retval.resize (a_nc);
301  Complex *y = retval.fortran_vec ();
302 
303  F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("T", 1),
304  a_nr, a_nc, 1.0, F77_CONST_DBLE_CMPLX_ARG (a.data ()),
305  ld, F77_CONST_DBLE_CMPLX_ARG (v.data ()), 1, 0.0, F77_DBLE_CMPLX_ARG (y), 1
306  F77_CHAR_ARG_LEN (1)));
307  }
308 
309  return retval;
310 }
311 
314 {
315  ComplexRowVector tmp (v);
316  return tmp * a;
317 }
318 
319 // other operations
320 
321 Complex
323 {
324  octave_idx_type len = numel ();
325  if (len == 0)
326  return Complex (0.0);
327 
328  Complex res = elem (0);
329  double absres = std::abs (res);
330 
331  for (octave_idx_type i = 1; i < len; i++)
332  if (std::abs (elem (i)) < absres)
333  {
334  res = elem (i);
335  absres = std::abs (res);
336  }
337 
338  return res;
339 }
340 
341 Complex
343 {
344  octave_idx_type len = numel ();
345  if (len == 0)
346  return Complex (0.0);
347 
348  Complex res = elem (0);
349  double absres = std::abs (res);
350 
351  for (octave_idx_type i = 1; i < len; i++)
352  if (std::abs (elem (i)) > absres)
353  {
354  res = elem (i);
355  absres = std::abs (res);
356  }
357 
358  return res;
359 }
360 
361 // i/o
362 
363 std::ostream&
364 operator << (std::ostream& os, const ComplexRowVector& a)
365 {
366 // int field_width = os.precision () + 7;
367  for (octave_idx_type i = 0; i < a.numel (); i++)
368  os << ' ' /* setw (field_width) */ << a.elem (i);
369  return os;
370 }
371 
372 std::istream&
374 {
375  octave_idx_type len = a.numel ();
376 
377  if (len > 0)
378  {
379  Complex tmp;
380  for (octave_idx_type i = 0; i < len; i++)
381  {
382  is >> tmp;
383  if (is)
384  a.elem (i) = tmp;
385  else
386  break;
387  }
388  }
389  return is;
390 }
391 
392 // row vector by column vector -> scalar
393 
394 // row vector by column vector -> scalar
395 
396 Complex
398 {
400  return v * tmp;
401 }
402 
403 Complex
405 {
406  Complex retval (0.0, 0.0);
407 
408  F77_INT len = octave::to_f77_int (v.numel ());
409 
410  F77_INT a_len = octave::to_f77_int (a.numel ());
411 
412  if (len != a_len)
413  octave::err_nonconformant ("operator *", len, a_len);
414  if (len != 0)
415  F77_FUNC (xzdotu, XZDOTU) (len, F77_CONST_DBLE_CMPLX_ARG (v.data ()), 1,
417 
418  return retval;
419 }
420 
421 // other operations
422 
424 linspace (const Complex& x1, const Complex& x2, octave_idx_type n)
425 {
427 
428  if (n < 1)
429  return retval;
430  else
431  retval.clear (n);
432 
433  retval(0) = x1;
434 
435  Complex delta = (x2 - x1) / (n - 1.0);
436  for (octave_idx_type i = 1; i < n-1; i++)
437  retval(i) = x1 + static_cast<double> (i)*delta;
438 
439  retval(n-1) = x2;
440 
441  return retval;
442 }
void mx_inline_add2(size_t n, R *r, const X *x)
Definition: mx-inlines.cc:125
ComplexRowVector & fill(double val)
Definition: CRowVector.cc:95
void mx_inline_sub2(size_t n, R *r, const X *x)
Definition: mx-inlines.cc:126
ComplexColumnVector transpose(void) const
Definition: CRowVector.cc:198
const Complex * data(void) const
Definition: Array.h:582
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: MArray.h:106
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
#define F77_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:315
const Complex * fortran_vec(void) const
Definition: Array.h:584
static T abs(T x)
Definition: pr-output.cc:1696
bool operator==(const ComplexRowVector &a) const
Definition: CRowVector.cc:40
Complex & elem(octave_idx_type n)
Definition: Array.h:488
std::istream & operator>>(std::istream &is, ComplexRowVector &a)
Definition: CRowVector.cc:373
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
MArray< T > transpose(void) const
Definition: MArray.h:103
std::ostream & operator<<(std::ostream &os, const ComplexRowVector &a)
Definition: CRowVector.cc:364
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:41
octave_idx_type a_nc
Definition: sylvester.cc:74
ComplexRowVector extract(octave_idx_type c1, octave_idx_type c2) const
Definition: CRowVector.cc:212
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:511
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
bool swap
Definition: load-save.cc:738
subroutine xzdotu(n, zx, incx, zy, incy, retval)
Definition: xzdotu.f:2
octave_idx_type a_nr
Definition: sylvester.cc:73
void make_unique(void)
Definition: Array.h:187
double tmp
Definition: data.cc:6252
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
octave_value retval
Definition: data.cc:6246
ComplexRowVector append(const RowVector &a) const
Definition: CRowVector.cc:170
F77_RET_T F77_FUNC(xerbla, XERBLA)
Definition: xerbla.c:57
With real return the complex result
Definition: data.cc:3260
ComplexRowVector extract_n(octave_idx_type c1, octave_idx_type n) const
Definition: CRowVector.cc:227
Complex & xelem(octave_idx_type n)
Definition: Array.h:458
ComplexColumnVector hermitian(void) const
Definition: CRowVector.cc:192
Complex min(void) const
Definition: CRowVector.cc:322
#define F77_CONST_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:318
This is a simple wrapper template that will subclass an Array<T> type or any later type derived from ...
Definition: Array.h:892
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
bool operator!=(const ComplexRowVector &a) const
Definition: CRowVector.cc:49
ComplexRowVector & insert(const RowVector &a, octave_idx_type c)
Definition: CRowVector.cc:57
Complex max(void) const
Definition: CRowVector.cc:342
ComplexRowVector & operator+=(const RowVector &a)
Definition: CRowVector.cc:240
the element is set to zero In other the statement xample y
Definition: data.cc:5264
ComplexRowVector & operator-=(const RowVector &a)
Definition: CRowVector.cc:259
ComplexRowVector operator*(const ComplexRowVector &v, const ComplexMatrix &a)
Definition: CRowVector.cc:280
for i
Definition: data.cc:5264
std::complex< double > Complex
Definition: oct-cmplx.h:31
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
bool mx_inline_equal(size_t n, const T1 *x, const T2 *y)
Definition: mx-inlines.cc:569
write the output to stdout if nargout is
Definition: load-save.cc:1612
ComplexRowVector linspace(const Complex &x1, const Complex &x2, octave_idx_type n)
Definition: CRowVector.cc:424
octave::stream os
Definition: file-io.cc:627
ComplexRowVector conj(const ComplexRowVector &a)
Definition: CRowVector.cc:204