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
dRowVector.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 // 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 RowVector&
57 {
58  octave_idx_type a_len = a.numel ();
59 
60  if (c < 0 || c + a_len > numel ())
61  (*current_liboctave_error_handler) ("range error for insert");
62 
63  if (a_len > 0)
64  {
65  make_unique ();
66 
67  for (octave_idx_type i = 0; i < a_len; i++)
68  xelem (c+i) = a.elem (i);
69  }
70 
71  return *this;
72 }
73 
74 RowVector&
76 {
77  octave_idx_type len = numel ();
78 
79  if (len > 0)
80  {
81  make_unique ();
82 
83  for (octave_idx_type i = 0; i < len; i++)
84  xelem (i) = val;
85  }
86 
87  return *this;
88 }
89 
90 RowVector&
92 {
93  octave_idx_type len = numel ();
94 
95  if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len)
96  (*current_liboctave_error_handler) ("range error for fill");
97 
98  if (c1 > c2) { std::swap (c1, c2); }
99 
100  if (c2 >= c1)
101  {
102  make_unique ();
103 
104  for (octave_idx_type i = c1; i <= c2; i++)
105  xelem (i) = val;
106  }
107 
108  return *this;
109 }
110 
111 RowVector
113 {
114  octave_idx_type len = numel ();
115  octave_idx_type nc_insert = len;
116  RowVector retval (len + a.numel ());
117  retval.insert (*this, 0);
118  retval.insert (a, nc_insert);
119  return retval;
120 }
121 
124 {
125  return MArray<double>::transpose ();
126 }
127 
128 RowVector
130 {
131  return do_mx_unary_op<double, Complex> (a, mx_inline_real);
132 }
133 
134 RowVector
136 {
137  return do_mx_unary_op<double, Complex> (a, mx_inline_imag);
138 }
139 
140 RowVector
142 {
143  if (c1 > c2) { std::swap (c1, c2); }
144 
145  octave_idx_type new_c = c2 - c1 + 1;
146 
147  RowVector result (new_c);
148 
149  for (octave_idx_type i = 0; i < new_c; i++)
150  result.xelem (i) = elem (c1+i);
151 
152  return result;
153 }
154 
155 RowVector
157 {
158  RowVector result (n);
159 
160  for (octave_idx_type i = 0; i < n; i++)
161  result.xelem (i) = elem (r1+i);
162 
163  return result;
164 }
165 
166 // row vector by matrix -> row vector
167 
168 RowVector
169 operator * (const RowVector& v, const Matrix& a)
170 {
172 
173  octave_idx_type len = v.numel ();
174 
175  octave_idx_type a_nr = a.rows ();
176  octave_idx_type a_nc = a.cols ();
177 
178  if (a_nr != len)
179  octave::err_nonconformant ("operator *", 1, len, a_nr, a_nc);
180 
181  if (len == 0)
182  retval.resize (a_nc, 0.0);
183  else
184  {
185  // Transpose A to form A'*x == (x'*A)'
186 
187  octave_idx_type ld = a_nr;
188 
189  retval.resize (a_nc);
190  double *y = retval.fortran_vec ();
191 
192  F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("T", 1),
193  a_nr, a_nc, 1.0, a.data (),
194  ld, v.data (), 1, 0.0, y, 1
195  F77_CHAR_ARG_LEN (1)));
196  }
197 
198  return retval;
199 }
200 
201 // other operations
202 
203 double
204 RowVector::min (void) const
205 {
206  octave_idx_type len = numel ();
207  if (len == 0)
208  return 0;
209 
210  double res = elem (0);
211 
212  for (octave_idx_type i = 1; i < len; i++)
213  if (elem (i) < res)
214  res = elem (i);
215 
216  return res;
217 }
218 
219 double
220 RowVector::max (void) const
221 {
222  octave_idx_type len = numel ();
223  if (len == 0)
224  return 0;
225 
226  double res = elem (0);
227 
228  for (octave_idx_type i = 1; i < len; i++)
229  if (elem (i) > res)
230  res = elem (i);
231 
232  return res;
233 }
234 
235 std::ostream&
236 operator << (std::ostream& os, const RowVector& a)
237 {
238 // int field_width = os.precision () + 7;
239 
240  for (octave_idx_type i = 0; i < a.numel (); i++)
241  os << " " /* setw (field_width) */ << a.elem (i);
242  return os;
243 }
244 
245 std::istream&
246 operator >> (std::istream& is, RowVector& a)
247 {
248  octave_idx_type len = a.numel ();
249 
250  if (len > 0)
251  {
252  double tmp;
253  for (octave_idx_type i = 0; i < len; i++)
254  {
255  is >> tmp;
256  if (is)
257  a.elem (i) = tmp;
258  else
259  break;
260  }
261  }
262  return is;
263 }
264 
265 // other operations
266 
267 RowVector
268 linspace (double x1, double x2, octave_idx_type n)
269 {
271 
272  if (n < 1)
273  return retval;
274  else
275  retval.clear (n);
276 
277  retval(0) = x1;
278 
279  double delta = (x2 - x1) / (n - 1);
280  for (octave_idx_type i = 1; i < n-1; i++)
281  retval(i) = x1 + i*delta;
282 
283  retval(n-1) = x2;
284 
285  return retval;
286 }
287 
288 // row vector by column vector -> scalar
289 
290 double
292 {
293  double retval = 0.0;
294 
295  octave_idx_type len = v.numel ();
296 
297  octave_idx_type a_len = a.numel ();
298 
299  if (len != a_len)
300  octave::err_nonconformant ("operator *", len, a_len);
301 
302  if (len != 0)
303  F77_FUNC (xddot, XDDOT) (len, v.data (), 1, a.data (), 1, retval);
304 
305  return retval;
306 }
307 
308 Complex
310 {
311  ComplexRowVector tmp (v);
312  return tmp * a;
313 }
RowVector linspace(double x1, double x2, octave_idx_type n)
Definition: dRowVector.cc:268
RowVector real(const ComplexRowVector &a)
Definition: dRowVector.cc:129
subroutine xddot(n, dx, incx, dy, incy, retval)
Definition: xddot.f:1
void resize(octave_idx_type n, const double &rfv=0)
Definition: dRowVector.h:98
RowVector operator*(const RowVector &v, const Matrix &a)
Definition: dRowVector.cc:169
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
RowVector extract(octave_idx_type c1, octave_idx_type c2) const
Definition: dRowVector.cc:141
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
bool operator!=(const RowVector &a) const
Definition: dRowVector.cc:50
void mx_inline_real(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:315
MArray< T > transpose(void) const
Definition: MArray.h:103
T & elem(octave_idx_type n)
Definition: Array.h:482
#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
void mx_inline_imag(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:322
std::ostream & operator<<(std::ostream &os, const RowVector &a)
Definition: dRowVector.cc:236
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
RowVector extract_n(octave_idx_type c1, octave_idx_type n) const
Definition: dRowVector.cc:156
octave_idx_type a_nr
Definition: sylvester.cc:71
void make_unique(void)
Definition: Array.h:185
double max(void) const
Definition: dRowVector.cc:220
bool operator==(const RowVector &a) const
Definition: dRowVector.cc:41
const double * 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
Definition: dMatrix.h:37
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
With real return the complex result
Definition: data.cc:3375
double & xelem(octave_idx_type n)
Definition: Array.h:455
This is a simple wrapper template that will subclass an Array type or any later type derived from ...
Definition: Array.h:888
double min(void) const
Definition: dRowVector.cc:204
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
the element is set to zero In other the statement xample y
Definition: data.cc:5342
ColumnVector transpose(void) const
Definition: dRowVector.cc:123
RowVector append(const RowVector &a) const
Definition: dRowVector.cc:112
std::complex< double > Complex
Definition: oct-cmplx.h:31
RowVector & insert(const RowVector &a, octave_idx_type c)
Definition: dRowVector.cc:56
const T * 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
std::istream & operator>>(std::istream &is, RowVector &a)
Definition: dRowVector.cc:246
RowVector & fill(double val)
Definition: dRowVector.cc:75
RowVector imag(const ComplexRowVector &a)
Definition: dRowVector.cc:135