dColVector.cc

Go to the documentation of this file.
00001 // ColumnVector manipulations.
00002 /*
00003 
00004 Copyright (C) 1994-2012 John W. Eaton
00005 Copyright (C) 2010 VZLU Prague
00006 
00007 This file is part of Octave.
00008 
00009 Octave is free software; you can redistribute it and/or modify it
00010 under the terms of the GNU General Public License as published by the
00011 Free Software Foundation; either version 3 of the License, or (at your
00012 option) any later version.
00013 
00014 Octave is distributed in the hope that it will be useful, but WITHOUT
00015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00017 for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with Octave; see the file COPYING.  If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include <iostream>
00030 
00031 #include "Array-util.h"
00032 #include "f77-fcn.h"
00033 #include "functor.h"
00034 #include "lo-error.h"
00035 #include "mx-base.h"
00036 #include "mx-inlines.cc"
00037 #include "oct-cmplx.h"
00038 
00039 // Fortran functions we call.
00040 
00041 extern "C"
00042 {
00043   F77_RET_T
00044   F77_FUNC (dgemv, DGEMV) (F77_CONST_CHAR_ARG_DECL,
00045                            const octave_idx_type&, const octave_idx_type&,
00046                            const double&, const double*,
00047                            const octave_idx_type&, const double*,
00048                            const octave_idx_type&, const double&, double*,
00049                            const octave_idx_type&
00050                            F77_CHAR_ARG_LEN_DECL);
00051 }
00052 
00053 // Column Vector class.
00054 
00055 bool
00056 ColumnVector::operator == (const ColumnVector& a) const
00057 {
00058   octave_idx_type len = length ();
00059   if (len != a.length ())
00060     return 0;
00061   return mx_inline_equal (len, data (), a.data ());
00062 }
00063 
00064 bool
00065 ColumnVector::operator != (const ColumnVector& a) const
00066 {
00067   return !(*this == a);
00068 }
00069 
00070 ColumnVector&
00071 ColumnVector::insert (const ColumnVector& a, octave_idx_type r)
00072 {
00073   octave_idx_type a_len = a.length ();
00074 
00075   if (r < 0 || r + a_len > length ())
00076     {
00077       (*current_liboctave_error_handler) ("range error for insert");
00078       return *this;
00079     }
00080 
00081   if (a_len > 0)
00082     {
00083       make_unique ();
00084 
00085       for (octave_idx_type i = 0; i < a_len; i++)
00086         xelem (r+i) = a.elem (i);
00087     }
00088 
00089   return *this;
00090 }
00091 
00092 ColumnVector&
00093 ColumnVector::fill (double val)
00094 {
00095   octave_idx_type len = length ();
00096 
00097   if (len > 0)
00098     {
00099       make_unique ();
00100 
00101       for (octave_idx_type i = 0; i < len; i++)
00102         xelem (i) = val;
00103     }
00104 
00105   return *this;
00106 }
00107 
00108 ColumnVector&
00109 ColumnVector::fill (double val, octave_idx_type r1, octave_idx_type r2)
00110 {
00111   octave_idx_type len = length ();
00112 
00113   if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len)
00114     {
00115       (*current_liboctave_error_handler) ("range error for fill");
00116       return *this;
00117     }
00118 
00119   if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; }
00120 
00121   if (r2 >= r1)
00122     {
00123       make_unique ();
00124 
00125       for (octave_idx_type i = r1; i <= r2; i++)
00126         xelem (i) = val;
00127     }
00128 
00129   return *this;
00130 }
00131 
00132 ColumnVector
00133 ColumnVector::stack (const ColumnVector& a) const
00134 {
00135   octave_idx_type len = length ();
00136   octave_idx_type nr_insert = len;
00137   ColumnVector retval (len + a.length ());
00138   retval.insert (*this, 0);
00139   retval.insert (a, nr_insert);
00140   return retval;
00141 }
00142 
00143 RowVector
00144 ColumnVector::transpose (void) const
00145 {
00146   return MArray<double>::transpose();
00147 }
00148 
00149 ColumnVector
00150 ColumnVector::abs (void) const
00151 {
00152   return do_mx_unary_map<double, double, std::abs> (*this);
00153 }
00154 
00155 ColumnVector
00156 real (const ComplexColumnVector& a)
00157 {
00158   return do_mx_unary_op<double, Complex> (a, mx_inline_real);
00159 }
00160 
00161 ColumnVector
00162 imag (const ComplexColumnVector& a)
00163 {
00164   return do_mx_unary_op<double, Complex> (a, mx_inline_imag);
00165 }
00166 
00167 // resize is the destructive equivalent for this one
00168 
00169 ColumnVector
00170 ColumnVector::extract (octave_idx_type r1, octave_idx_type r2) const
00171 {
00172   if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; }
00173 
00174   octave_idx_type new_r = r2 - r1 + 1;
00175 
00176   ColumnVector result (new_r);
00177 
00178   for (octave_idx_type i = 0; i < new_r; i++)
00179     result.xelem (i) = elem (r1+i);
00180 
00181   return result;
00182 }
00183 
00184 ColumnVector
00185 ColumnVector::extract_n (octave_idx_type r1, octave_idx_type n) const
00186 {
00187   ColumnVector result (n);
00188 
00189   for (octave_idx_type i = 0; i < n; i++)
00190     result.xelem (i) = elem (r1+i);
00191 
00192   return result;
00193 }
00194 
00195 // matrix by column vector -> column vector operations
00196 
00197 ColumnVector
00198 operator * (const Matrix& m, const ColumnVector& a)
00199 {
00200   ColumnVector retval;
00201 
00202   octave_idx_type nr = m.rows ();
00203   octave_idx_type nc = m.cols ();
00204 
00205   octave_idx_type a_len = a.length ();
00206 
00207   if (nc != a_len)
00208     gripe_nonconformant ("operator *", nr, nc, a_len, 1);
00209   else
00210     {
00211       retval.clear (nr);
00212 
00213       if (nr != 0)
00214         {
00215           if (nc == 0)
00216             retval.fill (0.0);
00217           else
00218             {
00219               double *y = retval.fortran_vec ();
00220 
00221               F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1),
00222                                        nr, nc, 1.0, m.data (), nr,
00223                                        a.data (), 1, 0.0, y, 1
00224                                        F77_CHAR_ARG_LEN (1)));
00225             }
00226         }
00227     }
00228 
00229   return retval;
00230 }
00231 
00232 // diagonal matrix by column vector -> column vector operations
00233 
00234 ColumnVector
00235 operator * (const DiagMatrix& m, const ColumnVector& a)
00236 {
00237   ColumnVector retval;
00238 
00239   octave_idx_type nr = m.rows ();
00240   octave_idx_type nc = m.cols ();
00241 
00242   octave_idx_type a_len = a.length ();
00243 
00244   if (nc != a_len)
00245     gripe_nonconformant ("operator *", nr, nc, a_len, 1);
00246   else
00247     {
00248       if (nr == 0 || nc == 0)
00249         retval.resize (nr, 0.0);
00250       else
00251         {
00252           retval.resize (nr);
00253 
00254           for (octave_idx_type i = 0; i < a_len; i++)
00255             retval.elem (i) = a.elem (i) * m.elem (i, i);
00256 
00257           for (octave_idx_type i = a_len; i < nr; i++)
00258             retval.elem (i) = 0.0;
00259         }
00260     }
00261 
00262   return retval;
00263 }
00264 
00265 // other operations
00266 
00267 double
00268 ColumnVector::min (void) const
00269 {
00270   octave_idx_type len = length ();
00271   if (len == 0)
00272     return 0.0;
00273 
00274   double res = elem (0);
00275 
00276   for (octave_idx_type i = 1; i < len; i++)
00277     if (elem (i) < res)
00278       res = elem (i);
00279 
00280   return res;
00281 }
00282 
00283 double
00284 ColumnVector::max (void) const
00285 {
00286   octave_idx_type len = length ();
00287   if (len == 0)
00288     return 0.0;
00289 
00290   double res = elem (0);
00291 
00292   for (octave_idx_type i = 1; i < len; i++)
00293     if (elem (i) > res)
00294       res = elem (i);
00295 
00296   return res;
00297 }
00298 
00299 std::ostream&
00300 operator << (std::ostream& os, const ColumnVector& a)
00301 {
00302 //  int field_width = os.precision () + 7;
00303   for (octave_idx_type i = 0; i < a.length (); i++)
00304     os << /* setw (field_width) << */ a.elem (i) << "\n";
00305   return os;
00306 }
00307 
00308 std::istream&
00309 operator >> (std::istream& is, ColumnVector& a)
00310 {
00311   octave_idx_type len = a.length();
00312 
00313   if (len > 0)
00314     {
00315       double tmp;
00316       for (octave_idx_type i = 0; i < len; i++)
00317         {
00318           is >> tmp;
00319           if (is)
00320             a.elem (i) = tmp;
00321           else
00322             break;
00323         }
00324     }
00325   return is;
00326 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines