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
fColVector.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  FloatColumnVector retval (len + a.numel ());
118  retval.insert (*this, 0);
119  retval.insert (a, nr_insert);
120  return retval;
121 }
122 
125 {
126  return MArray<float>::transpose ();
127 }
128 
131 {
132  return do_mx_unary_map<float, float, std::abs> (*this);
133 }
134 
137 {
138  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
139 }
140 
143 {
144  return do_mx_unary_op<float, FloatComplex> (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  FloatColumnVector 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 {
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  float *y = retval.fortran_vec ();
199 
200  F77_XFCN (sgemv, SGEMV, (F77_CONST_CHAR_ARG2 ("N", 1),
201  nr, nc, 1.0f, m.data (), nr,
202  a.data (), 1, 0.0f, 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 float
245 {
246  octave_idx_type len = numel ();
247  if (len == 0)
248  return 0.0;
249 
250  float 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 float
261 {
262  octave_idx_type len = numel ();
263  if (len == 0)
264  return 0.0;
265 
266  float 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 FloatColumnVector& 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&
286 {
287  octave_idx_type len = a.numel ();
288 
289  if (len > 0)
290  {
291  float 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 }
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:114
FloatColumnVector operator*(const FloatMatrix &m, const FloatColumnVector &a)
Definition: fColVector.cc:178
FloatColumnVector & fill(float val)
Definition: fColVector.cc:76
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
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 * f
void mx_inline_real(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:315
void resize(octave_idx_type n, const float &rfv=0)
Definition: fColVector.h:111
void clear(octave_idx_type n)
Definition: fColVector.h:116
octave_idx_type rows(void) const
Definition: DiagArray2.h:88
MArray< T > transpose(void) const
Definition: MArray.h:103
FloatColumnVector abs(void) const
Definition: fColVector.cc:130
bool operator==(const FloatColumnVector &a) const
Definition: fColVector.cc:42
T & elem(octave_idx_type n)
Definition: Array.h:482
FloatColumnVector stack(const FloatColumnVector &a) const
Definition: fColVector.cc:113
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:52
bool operator!=(const FloatColumnVector &a) const
Definition: fColVector.cc:51
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
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
float min(void) const
Definition: fColVector.cc:244
void make_unique(void)
Definition: Array.h:185
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
FloatColumnVector extract(octave_idx_type r1, octave_idx_type r2) const
Definition: fColVector.cc:150
const float * data(void) const
Definition: Array.h:582
std::ostream & operator<<(std::ostream &os, const FloatColumnVector &a)
Definition: fColVector.cc:276
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
FloatColumnVector & insert(const FloatColumnVector &a, octave_idx_type r)
Definition: fColVector.cc:57
FloatRowVector transpose(void) const
Definition: fColVector.cc:124
With real return the complex result
Definition: data.cc:3375
FloatColumnVector imag(const FloatComplexColumnVector &a)
Definition: fColVector.cc:142
float & xelem(octave_idx_type n)
Definition: Array.h:455
octave_idx_type cols(void) const
Definition: DiagArray2.h:89
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
FloatColumnVector real(const FloatComplexColumnVector &a)
Definition: fColVector.cc:136
float max(void) const
Definition: fColVector.cc:260
the element is set to zero In other the statement xample y
Definition: data.cc:5342
std::istream & operator>>(std::istream &is, FloatColumnVector &a)
Definition: fColVector.cc:285
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
FloatColumnVector extract_n(octave_idx_type r1, octave_idx_type n) const
Definition: fColVector.cc:165