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
fRowVector.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 
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 
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 (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 
113 {
114  octave_idx_type len = numel ();
115  octave_idx_type nc_insert = len;
116  FloatRowVector retval (len + a.numel ());
117  retval.insert (*this, 0);
118  retval.insert (a, nc_insert);
119  return retval;
120 }
121 
124 {
125  return MArray<float>::transpose ();
126 }
127 
130 {
131  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
132 }
133 
136 {
137  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_imag);
138 }
139 
142 {
143  if (c1 > c2) { std::swap (c1, c2); }
144 
145  octave_idx_type new_c = c2 - c1 + 1;
146 
147  FloatRowVector 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 
157 {
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 
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  float *y = retval.fortran_vec ();
191 
192  F77_XFCN (sgemv, SGEMV, (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 float
205 {
206  octave_idx_type len = numel ();
207  if (len == 0)
208  return 0;
209 
210  float 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 float
221 {
222  octave_idx_type len = numel ();
223  if (len == 0)
224  return 0;
225 
226  float 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 FloatRowVector& 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, FloatRowVector& a)
247 {
248  octave_idx_type len = a.numel ();
249 
250  if (len > 0)
251  {
252  float 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 
268 linspace (float x1, float 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  float 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 float
292 {
293  float 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 (xsdot, XSDOT) (len, v.data (), 1, a.data (), 1, retval);
304 
305  return retval;
306 }
307 
310 {
312  return tmp * a;
313 }
FloatRowVector imag(const FloatComplexRowVector &a)
Definition: fRowVector.cc:135
float min(void) const
Definition: fRowVector.cc:204
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
void mx_inline_real(size_t n, T *r, const std::complex< T > *x)
Definition: mx-inlines.cc:315
FloatRowVector real(const FloatComplexRowVector &a)
Definition: fRowVector.cc:129
FloatRowVector append(const FloatRowVector &a) const
Definition: fRowVector.cc:112
MArray< T > transpose(void) const
Definition: MArray.h:103
T & elem(octave_idx_type n)
Definition: Array.h:482
std::ostream & operator<<(std::ostream &os, const FloatRowVector &a)
Definition: fRowVector.cc:236
#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
bool operator!=(const FloatRowVector &a) const
Definition: fRowVector.cc:50
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
bool operator==(const FloatRowVector &a) const
Definition: fRowVector.cc:41
octave_idx_type a_nr
Definition: sylvester.cc:71
void make_unique(void)
Definition: Array.h:185
const float * data(void) const
Definition: Array.h:582
FloatRowVector extract(octave_idx_type c1, octave_idx_type c2) const
Definition: fRowVector.cc:141
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
subroutine xsdot(n, dx, incx, dy, incy, retval)
Definition: xsdot.f:1
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
FloatRowVector & insert(const FloatRowVector &a, octave_idx_type c)
Definition: fRowVector.cc:56
FloatRowVector linspace(float x1, float x2, octave_idx_type n)
Definition: fRowVector.cc:268
float & 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
FloatRowVector operator*(const FloatRowVector &v, const FloatMatrix &a)
Definition: fRowVector.cc:169
void resize(octave_idx_type n, const float &rfv=0)
Definition: fRowVector.h:101
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
float max(void) const
Definition: fRowVector.cc:220
the element is set to zero In other the statement xample y
Definition: data.cc:5342
std::istream & operator>>(std::istream &is, FloatRowVector &a)
Definition: fRowVector.cc:246
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
const T * fortran_vec(void) const
Definition: Array.h:584
FloatRowVector & fill(float val)
Definition: fRowVector.cc:75
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 transpose(void) const
Definition: fRowVector.cc:123
FloatRowVector extract_n(octave_idx_type c1, octave_idx_type n) const
Definition: fRowVector.cc:156