GNU Octave  3.8.0
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-2013 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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <iostream>
30 
31 #include "Array-util.h"
32 #include "f77-fcn.h"
33 #include "functor.h"
34 #include "lo-error.h"
35 #include "mx-base.h"
36 #include "mx-inlines.cc"
37 #include "oct-cmplx.h"
38 
39 // Fortran functions we call.
40 
41 extern "C"
42 {
43  F77_RET_T
44  F77_FUNC (sgemv, SGEMV) (F77_CONST_CHAR_ARG_DECL,
46  const float&, const float*, const octave_idx_type&,
47  const float*, const octave_idx_type&, const float&,
48  float*, const octave_idx_type&
50 }
51 
52 // Column Vector class.
53 
54 bool
56 {
57  octave_idx_type len = length ();
58  if (len != a.length ())
59  return 0;
60  return mx_inline_equal (len, data (), a.data ());
61 }
62 
63 bool
65 {
66  return !(*this == a);
67 }
68 
71 {
72  octave_idx_type a_len = a.length ();
73 
74  if (r < 0 || r + a_len > length ())
75  {
76  (*current_liboctave_error_handler) ("range error for insert");
77  return *this;
78  }
79 
80  if (a_len > 0)
81  {
82  make_unique ();
83 
84  for (octave_idx_type i = 0; i < a_len; i++)
85  xelem (r+i) = a.elem (i);
86  }
87 
88  return *this;
89 }
90 
93 {
94  octave_idx_type len = length ();
95 
96  if (len > 0)
97  {
98  make_unique ();
99 
100  for (octave_idx_type i = 0; i < len; i++)
101  xelem (i) = val;
102  }
103 
104  return *this;
105 }
106 
109 {
110  octave_idx_type len = length ();
111 
112  if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len)
113  {
114  (*current_liboctave_error_handler) ("range error for fill");
115  return *this;
116  }
117 
118  if (r1 > r2) { std::swap (r1, r2); }
119 
120  if (r2 >= r1)
121  {
122  make_unique ();
123 
124  for (octave_idx_type i = r1; i <= r2; i++)
125  xelem (i) = val;
126  }
127 
128  return *this;
129 }
130 
133 {
134  octave_idx_type len = length ();
135  octave_idx_type nr_insert = len;
136  FloatColumnVector retval (len + a.length ());
137  retval.insert (*this, 0);
138  retval.insert (a, nr_insert);
139  return retval;
140 }
141 
144 {
145  return MArray<float>::transpose ();
146 }
147 
150 {
151  return do_mx_unary_map<float, float, std::abs> (*this);
152 }
153 
156 {
157  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
158 }
159 
162 {
163  return do_mx_unary_op<float, FloatComplex> (a, mx_inline_imag);
164 }
165 
166 // resize is the destructive equivalent for this one
167 
170 {
171  if (r1 > r2) { std::swap (r1, r2); }
172 
173  octave_idx_type new_r = r2 - r1 + 1;
174 
175  FloatColumnVector result (new_r);
176 
177  for (octave_idx_type i = 0; i < new_r; i++)
178  result.xelem (i) = elem (r1+i);
179 
180  return result;
181 }
182 
185 {
186  FloatColumnVector result (n);
187 
188  for (octave_idx_type i = 0; i < n; i++)
189  result.xelem (i) = elem (r1+i);
190 
191  return result;
192 }
193 
194 // matrix by column vector -> column vector operations
195 
198 {
199  FloatColumnVector retval;
200 
201  octave_idx_type nr = m.rows ();
202  octave_idx_type nc = m.cols ();
203 
204  octave_idx_type a_len = a.length ();
205 
206  if (nc != a_len)
207  gripe_nonconformant ("operator *", nr, nc, a_len, 1);
208  else
209  {
210  retval.clear (nr);
211 
212  if (nr != 0)
213  {
214  if (nc == 0)
215  retval.fill (0.0);
216  else
217  {
218  float *y = retval.fortran_vec ();
219 
220  F77_XFCN (sgemv, SGEMV, (F77_CONST_CHAR_ARG2 ("N", 1),
221  nr, nc, 1.0f, m.data (), nr,
222  a.data (), 1, 0.0f, y, 1
223  F77_CHAR_ARG_LEN (1)));
224  }
225  }
226  }
227 
228  return retval;
229 }
230 
231 // diagonal matrix by column vector -> column vector operations
232 
235 {
236  FloatColumnVector retval;
237 
238  octave_idx_type nr = m.rows ();
239  octave_idx_type nc = m.cols ();
240 
241  octave_idx_type a_len = a.length ();
242 
243  if (nc != a_len)
244  gripe_nonconformant ("operator *", nr, nc, a_len, 1);
245  else
246  {
247  if (nr == 0 || nc == 0)
248  retval.resize (nr, 0.0);
249  else
250  {
251  retval.resize (nr);
252 
253  for (octave_idx_type i = 0; i < a_len; i++)
254  retval.elem (i) = a.elem (i) * m.elem (i, i);
255 
256  for (octave_idx_type i = a_len; i < nr; i++)
257  retval.elem (i) = 0.0;
258  }
259  }
260 
261  return retval;
262 }
263 
264 // other operations
265 
266 float
268 {
269  octave_idx_type len = length ();
270  if (len == 0)
271  return 0.0;
272 
273  float res = elem (0);
274 
275  for (octave_idx_type i = 1; i < len; i++)
276  if (elem (i) < res)
277  res = elem (i);
278 
279  return res;
280 }
281 
282 float
284 {
285  octave_idx_type len = length ();
286  if (len == 0)
287  return 0.0;
288 
289  float res = elem (0);
290 
291  for (octave_idx_type i = 1; i < len; i++)
292  if (elem (i) > res)
293  res = elem (i);
294 
295  return res;
296 }
297 
298 std::ostream&
299 operator << (std::ostream& os, const FloatColumnVector& a)
300 {
301 // int field_width = os.precision () + 7;
302  for (octave_idx_type i = 0; i < a.length (); i++)
303  os << /* setw (field_width) << */ a.elem (i) << "\n";
304  return os;
305 }
306 
307 std::istream&
308 operator >> (std::istream& is, FloatColumnVector& a)
309 {
310  octave_idx_type len = a.length ();
311 
312  if (len > 0)
313  {
314  float tmp;
315  for (octave_idx_type i = 0; i < len; i++)
316  {
317  is >> tmp;
318  if (is)
319  a.elem (i) = tmp;
320  else
321  break;
322  }
323  }
324  return is;
325 }