メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

MArrayN.cc

解説を見る。
00001 /*
00002 
00003 Copyright (C) 1996, 1997 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 2, or (at your option) any
00010 later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, write to the Free
00019 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 */
00022 
00023 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION)
00024 #pragma implementation
00025 #endif
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 
00031 #include "MArrayN.h"
00032 #include "Array-util.h"
00033 #include "ArrayN-idx.h"
00034 #include "lo-error.h"
00035 
00036 #include "MArray-defs.h"
00037 
00038 // N-dimensional array with math ops.
00039 
00040 // Element by element MArrayN by scalar ops.
00041 
00042 template <class T>
00043 MArrayN<T>&
00044 operator += (MArrayN<T>& a, const T& s)
00045 {
00046   DO_VS_OP2 (T, a, +=, s)
00047   return a;
00048 }
00049 
00050 template <class T>
00051 MArrayN<T>&
00052 operator -= (MArrayN<T>& a, const T& s)
00053 {
00054   DO_VS_OP2 (T, a, -=, s)
00055   return a;
00056 }
00057 
00058 // Element by element MArrayN by MArrayN ops.
00059 
00060 template <class T>
00061 MArrayN<T>&
00062 operator += (MArrayN<T>& a, const MArrayN<T>& b)
00063 {
00064   int l = a.length ();
00065 
00066   if (l > 0)
00067     {
00068       dim_vector a_dims = a.dims ();
00069       dim_vector b_dims = b.dims ();
00070 
00071       if (a_dims != b_dims)
00072         gripe_nonconformant ("operator +=", a_dims, b_dims);
00073       else
00074         DO_VV_OP2 (T, a, +=, b);
00075     }
00076 
00077   return a;
00078 }
00079 
00080 template <class T>
00081 MArrayN<T>&
00082 operator -= (MArrayN<T>& a, const MArrayN<T>& b)
00083 {
00084   int l = a.length ();
00085 
00086   if (l > 0)
00087     {
00088       dim_vector a_dims = a.dims ();
00089       dim_vector b_dims = b.dims ();
00090 
00091       if (a_dims != b_dims)
00092         gripe_nonconformant ("operator -=", a_dims, b_dims);
00093       else
00094         DO_VV_OP2 (T, a, -=, b);
00095     }
00096   return a;
00097 }
00098 
00099 // Element by element MArrayN by scalar ops.
00100 
00101 #define MARRAYN_NDS_OP(OP) \
00102   template <class T> \
00103   MArrayN<T> \
00104   operator OP (const MArrayN<T>& a, const T& s) \
00105     { \
00106       MArrayN<T> result (a.dims ()); \
00107       T *r = result.fortran_vec (); \
00108       int l = a.length (); \
00109       const T *v = a.data (); \
00110       DO_VS_OP (r, l, v, OP, s); \
00111       return result; \
00112     }
00113 
00114 MARRAYN_NDS_OP (+)
00115 MARRAYN_NDS_OP (-)
00116 MARRAYN_NDS_OP (*)
00117 MARRAYN_NDS_OP (/)
00118 
00119 // Element by element MArrayN by scalar ops.
00120 
00121 #define MARRAYN_SND_OP(OP) \
00122   template <class T> \
00123   MArrayN<T> \
00124   operator OP (const T& s, const MArrayN<T>& a) \
00125   { \
00126     MArrayN<T> result (a.dims ()); \
00127     T *r = result.fortran_vec (); \
00128     int l = a.length (); \
00129     const T *v = a.data (); \
00130     DO_SV_OP (r, l, s, OP, v); \
00131     return result; \
00132   }
00133 
00134 MARRAYN_SND_OP (+)
00135 MARRAYN_SND_OP (-)
00136 MARRAYN_SND_OP (*)
00137 MARRAYN_SND_OP (/)
00138 
00139 #define MARRAY_NDND_OP(FCN, OP) \
00140 template <class T> \
00141 MArrayN<T> \
00142 FCN (const MArrayN<T>& a, const MArrayN<T>& b) \
00143 { \
00144 dim_vector a_dims = a.dims (); \
00145 dim_vector b_dims = b.dims (); \
00146 int dims_ok = 1; \
00147 int any_dims_zero = 0; \
00148 if (a_dims.length () != b_dims.length ()) \
00149  dims_ok = 0; \
00150  else \
00151    { \
00152      for (int i = 0; i < a_dims.length (); i++) \
00153        { \
00154          if (a_dims (i) != b_dims (i)) \
00155            { dims_ok = 0; break; } \
00156          if (a_dims (i) == 0) \
00157            any_dims_zero = 1; \
00158        } \
00159    } \
00160  if (!dims_ok) \
00161    { \
00162      gripe_nonconformant (#FCN, a_dims, b_dims); \
00163      return MArrayN<T> (); \
00164    } \
00165  if (any_dims_zero) \
00166    return MArrayN<T> (a_dims); \
00167  int l = a.length (); \
00168  MArrayN<T> result (a_dims); \
00169  T* r = result.fortran_vec (); \
00170  const T *x = a.data (); \
00171  const T *y = b.data (); \
00172  DO_VV_OP (r, l, x, OP, y); \
00173  return result; \
00174 }
00175 
00176 MARRAY_NDND_OP (operator +, +)
00177 MARRAY_NDND_OP (operator -, -)
00178 MARRAY_NDND_OP (product,    *)
00179 MARRAY_NDND_OP (quotient,   /)
00180 
00181 template <class T>
00182 MArrayN<T>
00183 operator + (const MArrayN<T>& a)
00184 {
00185   return a;
00186 }
00187 
00188 template <class T>
00189 MArrayN<T>
00190 operator - (const MArrayN<T>& a)
00191 {
00192   int l = a.length ();
00193   MArrayN<T> result (a.dims ());
00194   T *r = result.fortran_vec ();
00195   const T *x = a.data ();
00196   NEG_V (r, l, x);
00197   return result;
00198 }
00199 
00200 /*
00201 ;;; Local Variables: ***
00202 ;;; mode: C++ ***
00203 ;;; End: ***
00204 */

Wed Dec 29 11:51:45 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送