intNDArray.cc

Go to the documentation of this file.
00001 // N-D Array  manipulations.
00002 /*
00003 
00004 Copyright (C) 2004-2012 John W. Eaton
00005 Copyright (C) 2009 VZLU Prague, a.s.
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 "Array-util.h"
00030 #include "mx-base.h"
00031 #include "lo-ieee.h"
00032 #include "mx-inlines.cc"
00033 
00034 // unary operations
00035 
00036 template <class T>
00037 boolNDArray
00038 intNDArray<T>::operator ! (void) const
00039 {
00040   boolNDArray b (this->dims ());
00041 
00042   for (octave_idx_type i = 0; i < this->length (); i++)
00043     b.elem (i) = ! this->elem (i);
00044 
00045   return b;
00046 }
00047 
00048 template <class T>
00049 bool
00050 intNDArray<T>::any_element_not_one_or_zero (void) const
00051 {
00052   octave_idx_type nel = this->nelem ();
00053 
00054   for (octave_idx_type i = 0; i < nel; i++)
00055     {
00056       T val = this->elem (i);
00057 
00058       if (val != 0.0 && val != 1.0)
00059         return true;
00060     }
00061 
00062   return false;
00063 }
00064 
00065 template <class T>
00066 intNDArray<T>
00067 intNDArray<T>::diag (octave_idx_type k) const
00068 {
00069   return MArray<T>::diag (k);
00070 }
00071 
00072 // FIXME -- this is not quite the right thing.
00073 
00074 template <class T>
00075 boolNDArray
00076 intNDArray<T>::all (int dim) const
00077 {
00078   return do_mx_red_op<bool, T > (*this, dim, mx_inline_all);
00079 }
00080 
00081 template <class T>
00082 boolNDArray
00083 intNDArray<T>::any (int dim) const
00084 {
00085   return do_mx_red_op<bool, T > (*this, dim, mx_inline_any);
00086 }
00087 
00088 template <class T>
00089 void
00090 intNDArray<T>::increment_index (Array<octave_idx_type>& ra_idx,
00091                                const dim_vector& dimensions,
00092                                int start_dimension)
00093 {
00094   ::increment_index (ra_idx, dimensions, start_dimension);
00095 }
00096 
00097 template <class T>
00098 octave_idx_type
00099 intNDArray<T>::compute_index (Array<octave_idx_type>& ra_idx,
00100                               const dim_vector& dimensions)
00101 {
00102   return ::compute_index (ra_idx, dimensions);
00103 }
00104 
00105 template <class T>
00106 intNDArray<T>
00107 intNDArray<T>::concat (const intNDArray<T>& rb, const Array<octave_idx_type>& ra_idx)
00108 {
00109   if (rb.numel () > 0)
00110     insert (rb, ra_idx);
00111   return *this;
00112 }
00113 
00114 template <class T>
00115 intNDArray<T>&
00116 intNDArray<T>::insert (const intNDArray<T>& a, octave_idx_type r, octave_idx_type c)
00117 {
00118   Array<T>::insert (a, r, c);
00119   return *this;
00120 }
00121 
00122 template <class T>
00123 intNDArray<T>&
00124 intNDArray<T>::insert (const intNDArray<T>& a, const Array<octave_idx_type>& ra_idx)
00125 {
00126   Array<T>::insert (a, ra_idx);
00127   return *this;
00128 }
00129 
00130 // This contains no information on the array structure !!!
00131 
00132 template <class T>
00133 std::ostream&
00134 operator << (std::ostream& os, const intNDArray<T>& a)
00135 {
00136   octave_idx_type nel = a.nelem ();
00137 
00138   for (octave_idx_type i = 0; i < nel; i++)
00139     os << " " << a.elem (i) << "\n";
00140 
00141   return os;
00142 }
00143 
00144 template <class T>
00145 std::istream&
00146 operator >> (std::istream& is, intNDArray<T>& a)
00147 {
00148   octave_idx_type nel = a.nelem ();
00149 
00150   if (nel > 0)
00151     {
00152       T tmp;
00153 
00154       for (octave_idx_type i = 0; i < nel; i++)
00155         {
00156           is >> tmp;
00157 
00158           if (is)
00159             a.elem (i) = tmp;
00160           else
00161             goto done;
00162         }
00163     }
00164 
00165  done:
00166 
00167   return is;
00168 }
00169 
00170 // FIXME -- should abs and signum just be mapper functions?
00171 
00172 template <class T>
00173 intNDArray<T>
00174 intNDArray<T>::abs (void) const
00175 {
00176   octave_idx_type nel = this->nelem ();
00177   intNDArray<T> ret (this->dims ());
00178 
00179   for (octave_idx_type i = 0; i < nel; i++)
00180     {
00181       T val = this->elem (i);
00182       ret.xelem (i) = val.abs ();
00183     }
00184 
00185   return ret;
00186 }
00187 
00188 template <class T>
00189 intNDArray<T>
00190 intNDArray<T>::signum (void) const
00191 {
00192   octave_idx_type nel = this->nelem ();
00193   intNDArray<T> ret (this->dims ());
00194 
00195   for (octave_idx_type i = 0; i < nel; i++)
00196     {
00197       T val = this->elem (i);
00198       ret.xelem (i) = val.signum ();
00199     }
00200 
00201   return ret;
00202 }
00203 
00204 template <class T>
00205 intNDArray<T>
00206 intNDArray<T>::sum (int dim) const
00207 {
00208   return do_mx_red_op<T, T> (*this, dim, mx_inline_sum);
00209 }
00210 
00211 template <class T>
00212 NDArray
00213 intNDArray<T>::dsum (int dim) const
00214 {
00215   return do_mx_red_op<double, T> (*this, dim, mx_inline_dsum);
00216 }
00217 
00218 template <class T>
00219 intNDArray<T>
00220 intNDArray<T>::cumsum (int dim) const
00221 {
00222   return do_mx_cum_op<T, T> (*this, dim, mx_inline_cumsum);
00223 }
00224 
00225 template <class T>
00226 intNDArray<T>
00227 intNDArray<T>::max (int dim) const
00228 {
00229   return do_mx_minmax_op<T> (*this, dim, mx_inline_max);
00230 }
00231 
00232 template <class T>
00233 intNDArray<T>
00234 intNDArray<T>::max (Array<octave_idx_type>& idx_arg, int dim) const
00235 {
00236   return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_max);
00237 }
00238 
00239 template <class T>
00240 intNDArray<T>
00241 intNDArray<T>::min (int dim) const
00242 {
00243   return do_mx_minmax_op<T> (*this, dim, mx_inline_min);
00244 }
00245 
00246 template <class T>
00247 intNDArray<T>
00248 intNDArray<T>::min (Array<octave_idx_type>& idx_arg, int dim) const
00249 {
00250   return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_min);
00251 }
00252 
00253 template <class T>
00254 intNDArray<T>
00255 intNDArray<T>::cummax (int dim) const
00256 {
00257   return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummax);
00258 }
00259 
00260 template <class T>
00261 intNDArray<T>
00262 intNDArray<T>::cummax (Array<octave_idx_type>& idx_arg, int dim) const
00263 {
00264   return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummax);
00265 }
00266 
00267 template <class T>
00268 intNDArray<T>
00269 intNDArray<T>::cummin (int dim) const
00270 {
00271   return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummin);
00272 }
00273 
00274 template <class T>
00275 intNDArray<T>
00276 intNDArray<T>::cummin (Array<octave_idx_type>& idx_arg, int dim) const
00277 {
00278   return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummin);
00279 }
00280 
00281 template <class T>
00282 intNDArray<T>
00283 intNDArray<T>::diff (octave_idx_type order, int dim) const
00284 {
00285   return do_mx_diff_op<T> (*this, dim, order, mx_inline_diff);
00286 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines