GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
fColVector.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2018 John W. Eaton
4 Copyright (C) 2010 VZLU Prague
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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://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 // Column 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 
57 {
58  octave_idx_type a_len = a.numel ();
59 
60  if (r < 0 || r + 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 (r+i) = a.elem (i);
69  }
70 
71  return *this;
72 }
73 
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 
92 {
93  octave_idx_type len = numel ();
94 
95  if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len)
96  (*current_liboctave_error_handler) ("range error for fill");
97 
98  if (r1 > r2) { std::swap (r1, r2); }
99 
100  if (r2 >= r1)
101  {
102  make_unique ();
103 
104  for (octave_idx_type i = r1; i <= r2; i++)
105  xelem (i) = val;
106  }
107 
108  return *this;
109 }
110 
113 {
114  octave_idx_type len = numel ();
115  octave_idx_type nr_insert = len;
116  FloatColumnVector retval (len + a.numel ());
117  retval.insert (*this, 0);
118  retval.insert (a, nr_insert);
119  return retval;
120 }
121 
124 {
125  return MArray<float>::transpose ();
126 }
127 
130 {
131  return do_mx_unary_map<float, float, std::abs> (*this);
132 }
133 
136 {
137  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
138 }
139 
142 {
143  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_imag);
144 }
145 
146 // resize is the destructive equivalent for this one
147 
150 {
151  if (r1 > r2) { std::swap (r1, r2); }
152 
153  octave_idx_type new_r = r2 - r1 + 1;
154 
155  FloatColumnVector result (new_r);
156 
157  for (octave_idx_type i = 0; i < new_r; i++)
158  result.xelem (i) = elem (r1+i);
159 
160  return result;
161 }
162 
165 {
167 
168  for (octave_idx_type i = 0; i < n; i++)
169  result.xelem (i) = elem (r1+i);
170 
171  return result;
172 }
173 
174 // matrix by column vector -> column vector operations
175 
178 {
180 
181  F77_INT nr = octave::to_f77_int (m.rows ());
182  F77_INT nc = octave::to_f77_int (m.cols ());
183 
184  F77_INT a_len = octave::to_f77_int (a.numel ());
185 
186  if (nc != a_len)
187  octave::err_nonconformant ("operator *", nr, nc, a_len, 1);
188 
189  retval.clear (nr);
190 
191  if (nr != 0)
192  {
193  if (nc == 0)
194  retval.fill (0.0);
195  else
196  {
197  float *y = retval.fortran_vec ();
198 
199  F77_XFCN (sgemv, SGEMV, (F77_CONST_CHAR_ARG2 ("N", 1),
200  nr, nc, 1.0f, m.data (), nr,
201  a.data (), 1, 0.0f, y, 1
202  F77_CHAR_ARG_LEN (1)));
203  }
204  }
205 
206  return retval;
207 }
208 
209 // diagonal matrix by column vector -> column vector operations
210 
213 {
215 
216  F77_INT nr = octave::to_f77_int (m.rows ());
217  F77_INT nc = octave::to_f77_int (m.cols ());
218 
219  F77_INT a_len = octave::to_f77_int (a.numel ());
220 
221  if (nc != a_len)
222  octave::err_nonconformant ("operator *", nr, nc, a_len, 1);
223 
224  if (nr == 0 || nc == 0)
225  retval.resize (nr, 0.0);
226  else
227  {
228  retval.resize (nr);
229 
230  for (octave_idx_type i = 0; i < a_len; i++)
231  retval.elem (i) = a.elem (i) * m.elem (i, i);
232 
233  for (octave_idx_type i = a_len; i < nr; i++)
234  retval.elem (i) = 0.0;
235  }
236 
237  return retval;
238 }
239 
240 // other operations
241 
242 float
244 {
245  octave_idx_type len = numel ();
246  if (len == 0)
247  return 0.0;
248 
249  float res = elem (0);
250 
251  for (octave_idx_type i = 1; i < len; i++)
252  if (elem (i) < res)
253  res = elem (i);
254 
255  return res;
256 }
257 
258 float
260 {
261  octave_idx_type len = numel ();
262  if (len == 0)
263  return 0.0;
264 
265  float res = elem (0);
266 
267  for (octave_idx_type i = 1; i < len; i++)
268  if (elem (i) > res)
269  res = elem (i);
270 
271  return res;
272 }
273 
274 std::ostream&
275 operator << (std::ostream& os, const FloatColumnVector& a)
276 {
277 // int field_width = os.precision () + 7;
278  for (octave_idx_type i = 0; i < a.numel (); i++)
279  os << /* setw (field_width) << */ a.elem (i) << "\n";
280  return os;
281 }
282 
283 std::istream&
285 {
286  octave_idx_type len = a.numel ();
287 
288  if (len > 0)
289  {
290  float tmp;
291  for (octave_idx_type i = 0; i < len; i++)
292  {
293  is >> tmp;
294  if (is)
295  a.elem (i) = tmp;
296  else
297  break;
298  }
299  }
300  return is;
301 }
float max(void) const
Definition: fColVector.cc:259
octave_idx_type rows(void) const
Definition: Array.h:404
FloatColumnVector stack(const FloatColumnVector &a) const
Definition: fColVector.cc:112
const float * data(void) const
Definition: Array.h:582
FloatColumnVector abs(void) const
Definition: fColVector.cc:129
FloatColumnVector operator*(const FloatMatrix &m, const FloatColumnVector &a)
Definition: fColVector.cc:177
FloatColumnVector & fill(float val)
Definition: fColVector.cc:75
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
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 * f
void mx_inline_real(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:320
float & elem(octave_idx_type n)
Definition: Array.h:488
octave_idx_type rows(void) const
Definition: DiagArray2.h:87
MArray< T > transpose(void) const
Definition: MArray.h:103
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:41
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:115
void mx_inline_imag(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:327
octave_idx_type cols(void) const
Definition: Array.h:412
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
FloatColumnVector extract(octave_idx_type r1, octave_idx_type r2) const
Definition: fColVector.cc:149
FloatRowVector transpose(void) const
Definition: fColVector.cc:123
octave_idx_type cols(void) const
Definition: DiagArray2.h:88
bool operator==(const FloatColumnVector &a) const
Definition: fColVector.cc:41
void make_unique(void)
Definition: Array.h:187
std::ostream & operator<<(std::ostream &os, const FloatColumnVector &a)
Definition: fColVector.cc:275
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
FloatColumnVector & insert(const FloatColumnVector &a, octave_idx_type r)
Definition: fColVector.cc:56
bool operator!=(const FloatColumnVector &a) const
Definition: fColVector.cc:50
With real return the complex result
Definition: data.cc:3260
FloatColumnVector imag(const FloatComplexColumnVector &a)
Definition: fColVector.cc:141
float & xelem(octave_idx_type n)
Definition: Array.h:458
float min(void) const
Definition: fColVector.cc:243
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
FloatColumnVector real(const FloatComplexColumnVector &a)
Definition: fColVector.cc:135
the element is set to zero In other the statement xample y
Definition: data.cc:5264
for i
Definition: data.cc:5264
std::istream & operator>>(std::istream &is, FloatColumnVector &a)
Definition: fColVector.cc:284
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
FloatColumnVector extract_n(octave_idx_type r1, octave_idx_type n) const
Definition: fColVector.cc:164
octave::stream os
Definition: file-io.cc:627