GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
fRowVector.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 
29 #include "Array-util.h"
30 #include "functor.h"
31 #include "lo-blas-proto.h"
32 #include "lo-error.h"
33 #include "mx-base.h"
34 #include "mx-inlines.cc"
35 #include "oct-cmplx.h"
36 
37 // Row Vector class.
38 
39 bool
41 {
42  octave_idx_type len = numel ();
43  if (len != a.numel ())
44  return 0;
45  return mx_inline_equal (len, data (), a.data ());
46 }
47 
48 bool
50 {
51  return !(*this == a);
52 }
53 
56 {
57  octave_idx_type a_len = a.numel ();
58 
59  if (c < 0 || c + a_len > numel ())
60  (*current_liboctave_error_handler) ("range error for insert");
61 
62  if (a_len > 0)
63  {
64  make_unique ();
65 
66  for (octave_idx_type i = 0; i < a_len; i++)
67  xelem (c+i) = a.elem (i);
68  }
69 
70  return *this;
71 }
72 
75 {
76  octave_idx_type len = numel ();
77 
78  if (len > 0)
79  {
80  make_unique ();
81 
82  for (octave_idx_type i = 0; i < len; i++)
83  xelem (i) = val;
84  }
85 
86  return *this;
87 }
88 
91 {
92  octave_idx_type len = numel ();
93 
94  if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len)
95  (*current_liboctave_error_handler) ("range error for fill");
96 
97  if (c1 > c2) { std::swap (c1, c2); }
98 
99  if (c2 >= c1)
100  {
101  make_unique ();
102 
103  for (octave_idx_type i = c1; i <= c2; i++)
104  xelem (i) = val;
105  }
106 
107  return *this;
108 }
109 
112 {
113  octave_idx_type len = numel ();
114  octave_idx_type nc_insert = len;
115  FloatRowVector retval (len + a.numel ());
116  retval.insert (*this, 0);
117  retval.insert (a, nc_insert);
118  return retval;
119 }
120 
123 {
124  return MArray<float>::transpose ();
125 }
126 
129 {
130  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
131 }
132 
135 {
136  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_imag);
137 }
138 
141 {
142  if (c1 > c2) { std::swap (c1, c2); }
143 
144  octave_idx_type new_c = c2 - c1 + 1;
145 
146  FloatRowVector result (new_c);
147 
148  for (octave_idx_type i = 0; i < new_c; i++)
149  result.xelem (i) = elem (c1+i);
150 
151  return result;
152 }
153 
156 {
158 
159  for (octave_idx_type i = 0; i < n; i++)
160  result.xelem (i) = elem (r1+i);
161 
162  return result;
163 }
164 
165 // row vector by matrix -> row vector
166 
169 {
171 
172  F77_INT len = octave::to_f77_int (v.numel ());
173 
174  F77_INT a_nr = octave::to_f77_int (a.rows ());
175  F77_INT a_nc = octave::to_f77_int (a.cols ());
176 
177  if (a_nr != len)
178  octave::err_nonconformant ("operator *", 1, len, a_nr, a_nc);
179 
180  if (len == 0)
181  retval.resize (a_nc, 0.0);
182  else
183  {
184  // Transpose A to form A'*x == (x'*A)'
185 
186  F77_INT ld = a_nr;
187 
188  retval.resize (a_nc);
189  float *y = retval.fortran_vec ();
190 
191  F77_XFCN (sgemv, SGEMV, (F77_CONST_CHAR_ARG2 ("T", 1),
192  a_nr, a_nc, 1.0, a.data (),
193  ld, v.data (), 1, 0.0, y, 1
194  F77_CHAR_ARG_LEN (1)));
195  }
196 
197  return retval;
198 }
199 
200 // other operations
201 
202 float
204 {
205  octave_idx_type len = numel ();
206  if (len == 0)
207  return 0;
208 
209  float res = elem (0);
210 
211  for (octave_idx_type i = 1; i < len; i++)
212  if (elem (i) < res)
213  res = elem (i);
214 
215  return res;
216 }
217 
218 float
220 {
221  octave_idx_type len = numel ();
222  if (len == 0)
223  return 0;
224 
225  float res = elem (0);
226 
227  for (octave_idx_type i = 1; i < len; i++)
228  if (elem (i) > res)
229  res = elem (i);
230 
231  return res;
232 }
233 
234 std::ostream&
235 operator << (std::ostream& os, const FloatRowVector& a)
236 {
237 // int field_width = os.precision () + 7;
238 
239  for (octave_idx_type i = 0; i < a.numel (); i++)
240  os << ' ' /* setw (field_width) */ << a.elem (i);
241  return os;
242 }
243 
244 std::istream&
245 operator >> (std::istream& is, FloatRowVector& a)
246 {
247  octave_idx_type len = a.numel ();
248 
249  if (len > 0)
250  {
251  float tmp;
252  for (octave_idx_type i = 0; i < len; i++)
253  {
254  is >> tmp;
255  if (is)
256  a.elem (i) = tmp;
257  else
258  break;
259  }
260  }
261  return is;
262 }
263 
264 // other operations
265 
267 linspace (float x1, float x2, octave_idx_type n)
268 {
270 
271  if (n < 1)
272  return retval;
273  else
274  retval.clear (n);
275 
276  retval(0) = x1;
277 
278  float delta = (x2 - x1) / (n - 1);
279  for (octave_idx_type i = 1; i < n-1; i++)
280  retval(i) = x1 + i*delta;
281 
282  retval(n-1) = x2;
283 
284  return retval;
285 }
286 
287 // row vector by column vector -> scalar
288 
289 float
291 {
292  float retval = 0.0;
293 
294  F77_INT len = octave::to_f77_int (v.numel ());
295 
296  F77_INT a_len = octave::to_f77_int (a.numel ());
297 
298  if (len != a_len)
299  octave::err_nonconformant ("operator *", len, a_len);
300 
301  if (len != 0)
302  F77_FUNC (xsdot, XSDOT) (len, v.data (), 1, a.data (), 1, retval);
303 
304  return retval;
305 }
306 
309 {
311  return tmp * a;
312 }
FloatRowVector imag(const FloatComplexRowVector &a)
Definition: fRowVector.cc:134
FloatColumnVector transpose(void) const
Definition: fRowVector.cc:122
float max(void) const
Definition: fRowVector.cc:219
const float * data(void) const
Definition: Array.h:582
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
void mx_inline_real(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:320
FloatRowVector real(const FloatComplexRowVector &a)
Definition: fRowVector.cc:128
FloatRowVector extract_n(octave_idx_type c1, octave_idx_type n) const
Definition: fRowVector.cc:155
float & elem(octave_idx_type n)
Definition: Array.h:488
std::ostream & operator<<(std::ostream &os, const FloatRowVector &a)
Definition: fRowVector.cc:235
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
MArray< T > transpose(void) const
Definition: MArray.h:103
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:41
octave_idx_type a_nc
Definition: sylvester.cc:74
void mx_inline_imag(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:327
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
bool operator!=(const FloatRowVector &a) const
Definition: fRowVector.cc:49
octave_idx_type a_nr
Definition: sylvester.cc:73
void make_unique(void)
Definition: Array.h:187
FloatRowVector extract(octave_idx_type c1, octave_idx_type c2) const
Definition: fRowVector.cc:140
FloatRowVector append(const FloatRowVector &a) const
Definition: fRowVector.cc:111
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
subroutine xsdot(n, dx, incx, dy, incy, retval)
Definition: xsdot.f:2
bool operator==(const FloatRowVector &a) const
Definition: fRowVector.cc:40
F77_RET_T F77_FUNC(xerbla, XERBLA)
Definition: xerbla.c:57
With real return the complex result
Definition: data.cc:3260
FloatRowVector & insert(const FloatRowVector &a, octave_idx_type c)
Definition: fRowVector.cc:55
FloatRowVector linspace(float x1, float x2, octave_idx_type n)
Definition: fRowVector.cc:267
float & xelem(octave_idx_type n)
Definition: Array.h:458
This is a simple wrapper template that will subclass an Array<T> type or any later type derived from ...
Definition: Array.h:892
FloatRowVector operator*(const FloatRowVector &v, const FloatMatrix &a)
Definition: fRowVector.cc:168
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
the element is set to zero In other the statement xample y
Definition: data.cc:5264
std::istream & operator>>(std::istream &is, FloatRowVector &a)
Definition: fRowVector.cc:245
for i
Definition: data.cc:5264
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
FloatRowVector & fill(float val)
Definition: fRowVector.cc:74
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
float min(void) const
Definition: fRowVector.cc:203
octave::stream os
Definition: file-io.cc:627