GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CRowVector.cc
Go to the documentation of this file.
1 // RowVector manipulations.
2 /*
3 
4 Copyright (C) 1994-2017 John W. Eaton
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include <iostream>
29 
30 #include "Array-util.h"
31 #include "functor.h"
32 #include "lo-blas-proto.h"
33 #include "lo-error.h"
34 #include "mx-base.h"
35 #include "mx-inlines.cc"
36 #include "oct-cmplx.h"
37 
38 // Complex Row Vector class
39 
40 bool
42 {
43  octave_idx_type len = numel ();
44  if (len != a.numel ())
45  return 0;
46  return mx_inline_equal (len, data (), a.data ());
47 }
48 
49 bool
51 {
52  return !(*this == a);
53 }
54 
55 // destructive insert/delete/reorder operations
56 
59 {
60  octave_idx_type a_len = a.numel ();
61 
62  if (c < 0 || c + a_len > numel ())
63  (*current_liboctave_error_handler) ("range error for insert");
64 
65  if (a_len > 0)
66  {
67  make_unique ();
68 
69  for (octave_idx_type i = 0; i < a_len; i++)
70  xelem (c+i) = a.elem (i);
71  }
72 
73  return *this;
74 }
75 
78 {
79  octave_idx_type a_len = a.numel ();
80 
81  if (c < 0 || c + a_len > numel ())
82  (*current_liboctave_error_handler) ("range error for insert");
83 
84  if (a_len > 0)
85  {
86  make_unique ();
87 
88  for (octave_idx_type i = 0; i < a_len; i++)
89  xelem (c+i) = a.elem (i);
90  }
91 
92  return *this;
93 }
94 
97 {
98  octave_idx_type len = numel ();
99 
100  if (len > 0)
101  {
102  make_unique ();
103 
104  for (octave_idx_type i = 0; i < len; i++)
105  xelem (i) = val;
106  }
107 
108  return *this;
109 }
110 
113 {
114  octave_idx_type len = numel ();
115 
116  if (len > 0)
117  {
118  make_unique ();
119 
120  for (octave_idx_type i = 0; i < len; i++)
121  xelem (i) = val;
122  }
123 
124  return *this;
125 }
126 
129 {
130  octave_idx_type len = numel ();
131 
132  if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len)
133  (*current_liboctave_error_handler) ("range error for fill");
134 
135  if (c1 > c2) { std::swap (c1, c2); }
136 
137  if (c2 >= c1)
138  {
139  make_unique ();
140 
141  for (octave_idx_type i = c1; i <= c2; i++)
142  xelem (i) = val;
143  }
144 
145  return *this;
146 }
147 
151 {
152  octave_idx_type len = numel ();
153 
154  if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len)
155  (*current_liboctave_error_handler) ("range error for fill");
156 
157  if (c1 > c2) { std::swap (c1, c2); }
158 
159  if (c2 >= c1)
160  {
161  make_unique ();
162 
163  for (octave_idx_type i = c1; i <= c2; i++)
164  xelem (i) = val;
165  }
166 
167  return *this;
168 }
169 
172 {
173  octave_idx_type len = numel ();
174  octave_idx_type nc_insert = len;
175  ComplexRowVector retval (len + a.numel ());
176  retval.insert (*this, 0);
177  retval.insert (a, nc_insert);
178  return retval;
179 }
180 
183 {
184  octave_idx_type len = numel ();
185  octave_idx_type nc_insert = len;
186  ComplexRowVector retval (len + a.numel ());
187  retval.insert (*this, 0);
188  retval.insert (a, nc_insert);
189  return retval;
190 }
191 
194 {
196 }
197 
200 {
201  return MArray<Complex>::transpose ();
202 }
203 
206 {
207  return do_mx_unary_map<Complex, Complex, std::conj<double> > (a);
208 }
209 
210 // resize is the destructive equivalent for this one
211 
214 {
215  if (c1 > c2) { std::swap (c1, c2); }
216 
217  octave_idx_type new_c = c2 - c1 + 1;
218 
219  ComplexRowVector result (new_c);
220 
221  for (octave_idx_type i = 0; i < new_c; i++)
222  result.elem (i) = elem (c1+i);
223 
224  return result;
225 }
226 
229 {
231 
232  for (octave_idx_type i = 0; i < n; i++)
233  result.elem (i) = elem (r1+i);
234 
235  return result;
236 }
237 
238 // row vector by row vector -> row vector operations
239 
242 {
243  octave_idx_type len = numel ();
244 
245  octave_idx_type a_len = a.numel ();
246 
247  if (len != a_len)
248  octave::err_nonconformant ("operator +=", len, a_len);
249 
250  if (len == 0)
251  return *this;
252 
253  Complex *d = fortran_vec (); // Ensures only one reference to my privates!
254 
255  mx_inline_add2 (len, d, a.data ());
256  return *this;
257 }
258 
261 {
262  octave_idx_type len = numel ();
263 
264  octave_idx_type a_len = a.numel ();
265 
266  if (len != a_len)
267  octave::err_nonconformant ("operator -=", len, a_len);
268 
269  if (len == 0)
270  return *this;
271 
272  Complex *d = fortran_vec (); // Ensures only one reference to my privates!
273 
274  mx_inline_sub2 (len, d, a.data ());
275  return *this;
276 }
277 
278 // row vector by matrix -> row vector
279 
282 {
284 
285  octave_idx_type len = v.numel ();
286 
287  octave_idx_type a_nr = a.rows ();
288  octave_idx_type a_nc = a.cols ();
289 
290  if (a_nr != len)
291  octave::err_nonconformant ("operator *", 1, len, a_nr, a_nc);
292 
293  if (len == 0)
294  retval.resize (a_nc, 0.0);
295  else
296  {
297  // Transpose A to form A'*x == (x'*A)'
298 
299  octave_idx_type ld = a_nr;
300 
301  retval.resize (a_nc);
302  Complex *y = retval.fortran_vec ();
303 
304  F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("T", 1),
305  a_nr, a_nc, 1.0, F77_CONST_DBLE_CMPLX_ARG (a.data ()),
306  ld, F77_CONST_DBLE_CMPLX_ARG (v.data ()), 1, 0.0, F77_DBLE_CMPLX_ARG (y), 1
307  F77_CHAR_ARG_LEN (1)));
308  }
309 
310  return retval;
311 }
312 
315 {
316  ComplexRowVector tmp (v);
317  return tmp * a;
318 }
319 
320 // other operations
321 
322 Complex
324 {
325  octave_idx_type len = numel ();
326  if (len == 0)
327  return Complex (0.0);
328 
329  Complex res = elem (0);
330  double absres = std::abs (res);
331 
332  for (octave_idx_type i = 1; i < len; i++)
333  if (std::abs (elem (i)) < absres)
334  {
335  res = elem (i);
336  absres = std::abs (res);
337  }
338 
339  return res;
340 }
341 
342 Complex
344 {
345  octave_idx_type len = numel ();
346  if (len == 0)
347  return Complex (0.0);
348 
349  Complex res = elem (0);
350  double absres = std::abs (res);
351 
352  for (octave_idx_type i = 1; i < len; i++)
353  if (std::abs (elem (i)) > absres)
354  {
355  res = elem (i);
356  absres = std::abs (res);
357  }
358 
359  return res;
360 }
361 
362 // i/o
363 
364 std::ostream&
365 operator << (std::ostream& os, const ComplexRowVector& a)
366 {
367 // int field_width = os.precision () + 7;
368  for (octave_idx_type i = 0; i < a.numel (); i++)
369  os << " " /* setw (field_width) */ << a.elem (i);
370  return os;
371 }
372 
373 std::istream&
375 {
376  octave_idx_type len = a.numel ();
377 
378  if (len > 0)
379  {
380  Complex tmp;
381  for (octave_idx_type i = 0; i < len; i++)
382  {
383  is >> tmp;
384  if (is)
385  a.elem (i) = tmp;
386  else
387  break;
388  }
389  }
390  return is;
391 }
392 
393 // row vector by column vector -> scalar
394 
395 // row vector by column vector -> scalar
396 
397 Complex
399 {
401  return v * tmp;
402 }
403 
404 Complex
406 {
407  Complex retval (0.0, 0.0);
408 
409  octave_idx_type len = v.numel ();
410 
411  octave_idx_type a_len = a.numel ();
412 
413  if (len != a_len)
414  octave::err_nonconformant ("operator *", len, a_len);
415  if (len != 0)
416  F77_FUNC (xzdotu, XZDOTU) (len, F77_CONST_DBLE_CMPLX_ARG (v.data ()), 1,
417  F77_CONST_DBLE_CMPLX_ARG (a.data ()), 1, F77_DBLE_CMPLX_ARG (&retval));
418 
419  return retval;
420 }
421 
422 // other operations
423 
425 linspace (const Complex& x1, const Complex& x2, octave_idx_type n)
426 {
428 
429  if (n < 1)
430  return retval;
431  else
432  retval.clear (n);
433 
434  retval(0) = x1;
435 
436  Complex delta = (x2 - x1) / (n - 1.0);
437  for (octave_idx_type i = 1; i < n-1; i++)
438  retval(i) = x1 + static_cast<double> (i)*delta;
439 
440  retval(n-1) = x2;
441 
442  return retval;
443 }
void mx_inline_add2(size_t n, R *r, const X *x)
Definition: mx-inlines.cc:127
ComplexRowVector & fill(double val)
Definition: CRowVector.cc:96
ComplexColumnVector hermitian(void) const
Definition: CRowVector.cc:193
void mx_inline_sub2(size_t n, R *r, const X *x)
Definition: mx-inlines.cc:128
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
#define F77_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:345
ComplexRowVector extract_n(octave_idx_type c1, octave_idx_type n) const
Definition: CRowVector.cc:228
ComplexRowVector extract(octave_idx_type c1, octave_idx_type c2) const
Definition: CRowVector.cc:213
MArray< T > transpose(void) const
Definition: MArray.h:103
Complex min(void) const
Definition: CRowVector.cc:323
bool operator!=(const ComplexRowVector &a) const
Definition: CRowVector.cc:50
T & elem(octave_idx_type n)
Definition: Array.h:482
std::istream & operator>>(std::istream &is, ComplexRowVector &a)
Definition: CRowVector.cc:374
std::ostream & operator<<(std::ostream &os, const ComplexRowVector &a)
Definition: CRowVector.cc:365
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:52
octave_idx_type a_nc
Definition: sylvester.cc:72
octave_idx_type rows(void) const
Definition: Array.h:401
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
ComplexRowVector append(const RowVector &a) const
Definition: CRowVector.cc:171
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:398
bool swap
Definition: load-save.cc:725
subroutine xzdotu(n, zx, incx, zy, incy, retval)
Definition: xzdotu.f:1
octave_idx_type a_nr
Definition: sylvester.cc:71
void make_unique(void)
Definition: Array.h:185
const Complex * data(void) const
Definition: Array.h:582
double tmp
Definition: data.cc:6300
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
octave_value retval
Definition: data.cc:6294
F77_RET_T F77_FUNC(xstopx, XSTOPX) const
Definition: f77-fcn.c:53
bool operator==(const ComplexRowVector &a) const
Definition: CRowVector.cc:41
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
MArray< T > hermitian(T(*fcn)(const T &)=0) const
Definition: MArray.h:106
With real return the complex result
Definition: data.cc:3375
void resize(octave_idx_type n, const Complex &rfv=Complex(0))
Definition: CRowVector.h:119
Complex & xelem(octave_idx_type n)
Definition: Array.h:455
#define F77_CONST_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:348
This is a simple wrapper template that will subclass an Array type or any later type derived from ...
Definition: Array.h:888
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup abs(local error in x(i))<
ComplexRowVector & insert(const RowVector &a, octave_idx_type c)
Definition: CRowVector.cc:58
ComplexRowVector & operator+=(const RowVector &a)
Definition: CRowVector.cc:241
the element is set to zero In other the statement xample y
Definition: data.cc:5342
ComplexRowVector & operator-=(const RowVector &a)
Definition: CRowVector.cc:260
ComplexRowVector operator*(const ComplexRowVector &v, const ComplexMatrix &a)
Definition: CRowVector.cc:281
ComplexColumnVector transpose(void) const
Definition: CRowVector.cc:199
std::complex< double > Complex
Definition: oct-cmplx.h:31
const Complex * fortran_vec(void) const
Definition: Array.h:584
bool mx_inline_equal(size_t n, const T1 *x, const T2 *y)
Definition: mx-inlines.cc:532
octave_idx_type cols(void) const
Definition: Array.h:409
write the output to stdout if nargout is
Definition: load-save.cc:1576
Complex max(void) const
Definition: CRowVector.cc:343
ComplexRowVector linspace(const Complex &x1, const Complex &x2, octave_idx_type n)
Definition: CRowVector.cc:425
ComplexRowVector conj(const ComplexRowVector &a)
Definition: CRowVector.cc:205