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
dColVector.cc
Go to the documentation of this file.
1 // ColumnVector manipulations.
2 /*
3 
4 Copyright (C) 1994-2017 John W. Eaton
5 Copyright (C) 2010 VZLU Prague
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include <iostream>
30 
31 #include "Array-util.h"
32 #include "functor.h"
33 #include "lo-blas-proto.h"
34 #include "lo-error.h"
35 #include "mx-base.h"
36 #include "mx-inlines.cc"
37 #include "oct-cmplx.h"
38 
39 // Column Vector class.
40 
41 bool
43 {
44  octave_idx_type len = numel ();
45  if (len != a.numel ())
46  return 0;
47  return mx_inline_equal (len, data (), a.data ());
48 }
49 
50 bool
52 {
53  return !(*this == a);
54 }
55 
58 {
59  octave_idx_type a_len = a.numel ();
60 
61  if (r < 0 || r + 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 (r+i) = a.elem (i);
70  }
71 
72  return *this;
73 }
74 
77 {
78  octave_idx_type len = numel ();
79 
80  if (len > 0)
81  {
82  make_unique ();
83 
84  for (octave_idx_type i = 0; i < len; i++)
85  xelem (i) = val;
86  }
87 
88  return *this;
89 }
90 
93 {
94  octave_idx_type len = numel ();
95 
96  if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len)
97  (*current_liboctave_error_handler) ("range error for fill");
98 
99  if (r1 > r2) { std::swap (r1, r2); }
100 
101  if (r2 >= r1)
102  {
103  make_unique ();
104 
105  for (octave_idx_type i = r1; i <= r2; i++)
106  xelem (i) = val;
107  }
108 
109  return *this;
110 }
111 
114 {
115  octave_idx_type len = numel ();
116  octave_idx_type nr_insert = len;
117  ColumnVector retval (len + a.numel ());
118  retval.insert (*this, 0);
119  retval.insert (a, nr_insert);
120  return retval;
121 }
122 
123 RowVector
125 {
126  return MArray<double>::transpose ();
127 }
128 
130 ColumnVector::abs (void) const
131 {
132  return do_mx_unary_map<double, double, std::abs> (*this);
133 }
134 
137 {
138  return do_mx_unary_op<double, Complex> (a, mx_inline_real);
139 }
140 
143 {
144  return do_mx_unary_op<double, Complex> (a, mx_inline_imag);
145 }
146 
147 // resize is the destructive equivalent for this one
148 
151 {
152  if (r1 > r2) { std::swap (r1, r2); }
153 
154  octave_idx_type new_r = r2 - r1 + 1;
155 
156  ColumnVector result (new_r);
157 
158  for (octave_idx_type i = 0; i < new_r; i++)
159  result.xelem (i) = elem (r1+i);
160 
161  return result;
162 }
163 
166 {
167  ColumnVector result (n);
168 
169  for (octave_idx_type i = 0; i < n; i++)
170  result.xelem (i) = elem (r1+i);
171 
172  return result;
173 }
174 
175 // matrix by column vector -> column vector operations
176 
179 {
181 
182  octave_idx_type nr = m.rows ();
183  octave_idx_type nc = m.cols ();
184 
185  octave_idx_type a_len = a.numel ();
186 
187  if (nc != a_len)
188  octave::err_nonconformant ("operator *", nr, nc, a_len, 1);
189 
190  retval.clear (nr);
191 
192  if (nr != 0)
193  {
194  if (nc == 0)
195  retval.fill (0.0);
196  else
197  {
198  double *y = retval.fortran_vec ();
199 
200  F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1),
201  nr, nc, 1.0, m.data (), nr,
202  a.data (), 1, 0.0, y, 1
203  F77_CHAR_ARG_LEN (1)));
204  }
205  }
206 
207  return retval;
208 }
209 
210 // diagonal matrix by column vector -> column vector operations
211 
214 {
216 
217  octave_idx_type nr = m.rows ();
218  octave_idx_type nc = m.cols ();
219 
220  octave_idx_type a_len = a.numel ();
221 
222  if (nc != a_len)
223  octave::err_nonconformant ("operator *", nr, nc, a_len, 1);
224 
225  if (nr == 0 || nc == 0)
226  retval.resize (nr, 0.0);
227  else
228  {
229  retval.resize (nr);
230 
231  for (octave_idx_type i = 0; i < a_len; i++)
232  retval.elem (i) = a.elem (i) * m.elem (i, i);
233 
234  for (octave_idx_type i = a_len; i < nr; i++)
235  retval.elem (i) = 0.0;
236  }
237 
238  return retval;
239 }
240 
241 // other operations
242 
243 double
244 ColumnVector::min (void) const
245 {
246  octave_idx_type len = numel ();
247  if (len == 0)
248  return 0.0;
249 
250  double res = elem (0);
251 
252  for (octave_idx_type i = 1; i < len; i++)
253  if (elem (i) < res)
254  res = elem (i);
255 
256  return res;
257 }
258 
259 double
260 ColumnVector::max (void) const
261 {
262  octave_idx_type len = numel ();
263  if (len == 0)
264  return 0.0;
265 
266  double res = elem (0);
267 
268  for (octave_idx_type i = 1; i < len; i++)
269  if (elem (i) > res)
270  res = elem (i);
271 
272  return res;
273 }
274 
275 std::ostream&
276 operator << (std::ostream& os, const ColumnVector& a)
277 {
278 // int field_width = os.precision () + 7;
279  for (octave_idx_type i = 0; i < a.numel (); i++)
280  os << /* setw (field_width) << */ a.elem (i) << "\n";
281  return os;
282 }
283 
284 std::istream&
285 operator >> (std::istream& is, ColumnVector& a)
286 {
287  octave_idx_type len = a.numel ();
288 
289  if (len > 0)
290  {
291  double tmp;
292  for (octave_idx_type i = 0; i < len; i++)
293  {
294  is >> tmp;
295  if (is)
296  a.elem (i) = tmp;
297  else
298  break;
299  }
300  }
301  return is;
302 }
ColumnVector & fill(double val)
Definition: dColVector.cc:76
ColumnVector operator*(const Matrix &m, const ColumnVector &a)
Definition: dColVector.cc:178
bool operator==(const ColumnVector &a) const
Definition: dColVector.cc:42
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:114
void clear(octave_idx_type n)
Definition: dColVector.h:113
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
std::istream & operator>>(std::istream &is, ColumnVector &a)
Definition: dColVector.cc:285
void mx_inline_real(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:315
octave_idx_type rows(void) const
Definition: DiagArray2.h:88
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 rows(void) const
Definition: Array.h:401
RowVector transpose(void) const
Definition: dColVector.cc:124
void mx_inline_imag(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:322
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
void make_unique(void)
Definition: Array.h:185
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
ColumnVector abs(void) const
Definition: dColVector.cc:130
ColumnVector extract_n(octave_idx_type r1, octave_idx_type n) const
Definition: dColVector.cc:165
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
Definition: dMatrix.h:37
With real return the complex result
Definition: data.cc:3375
double & xelem(octave_idx_type n)
Definition: Array.h:455
octave_idx_type cols(void) const
Definition: DiagArray2.h:89
double min(void) const
Definition: dColVector.cc:244
bool operator!=(const ColumnVector &a) const
Definition: dColVector.cc:51
double max(void) const
Definition: dColVector.cc:260
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
ColumnVector & insert(const ColumnVector &a, octave_idx_type r)
Definition: dColVector.cc:57
the element is set to zero In other the statement xample y
Definition: data.cc:5342
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:142
std::ostream & operator<<(std::ostream &os, const ColumnVector &a)
Definition: dColVector.cc:276
const T * fortran_vec(void) const
Definition: Array.h:584
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:136
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
ColumnVector stack(const ColumnVector &a) const
Definition: dColVector.cc:113
ColumnVector extract(octave_idx_type r1, octave_idx_type r2) const
Definition: dColVector.cc:150
void resize(octave_idx_type n, const double &rfv=0)
Definition: dColVector.h:108