Fuzzy Logic Tools v1.0
tnt_extension.hpp
Go to the documentation of this file.
00001 /*  Copyright (C) 2004-2011
00002     ANTONIO JAVIER BARRAGAN, antonio.barragan@diesia.uhu.es
00003     http://uhu.es/antonio.barragan
00004 
00005     Collaborators:
00006     JOSE MANUEL ANDUJAR, andujar@diesia.uhu.es
00007   MARIANO J. AZNAR, marianojose.aznar@alu.uhu.es
00008 
00009     DPTO. DE ING. ELECTRONICA, DE SISTEMAS INFORMATICOS Y AUTOMATICA
00010     ETSI, UNIVERSITY OF HUELVA (SPAIN)
00011 
00012     For more information, please contact with authors.
00013     
00014     This software is free software: you can redistribute it and/or modify
00015     it under the terms of the GNU General Public License as published by
00016     the Free Software Foundation, either version 3 of the License, or
00017     (at your option) any later version.
00018 
00019     This software is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022     GNU General Public License for more details.
00023 
00024     You should have received a copy of the GNU General Public License
00025     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00026 */
00027 
00028 #ifndef _TNT_EXTENSION_HPP_
00029 #define _TNT_EXTENSION_HPP_
00030 
00038 #include <tnt/tnt.h> // Template Numerical Toolkit (http://math.nist.gov/tnt)
00039 
00040 namespace TNT{
00041   
00045   template <class T>
00046   inline Vector<T> Array1D2Vector(Array1D<T> A)
00047   {
00048     Vector<T> V(A.dim(), &A[0]);
00049     return V;
00050   }
00051 
00055   template <class T>
00056   inline Array1D<T> Vector2Array1D(Vector<T> V)
00057   {
00058     Array1D<T> A(V.dim(), &V[0]);
00059     return A;
00060   }
00061 
00065   template <class T>
00066   inline Matrix<T> Array2D2Matrix(Array2D<T> A)
00067   {
00068     Matrix<T> M(A.dim1(), A.dim2(), A[0]);
00069     return M;
00070   }
00071 
00075   template <class T>
00076   inline Array2D<T> Matrix2Array2D(Matrix<T> M)
00077   {
00078     Array2D<T> A(M.dim1(), M.dim2(), M[0]);
00079     return A;
00080   }
00081 
00085   template <class T>
00086   inline Matrix<T> Array1D2Matrix(Array1D<T> A)
00087   {
00088     Matrix<T> M(1, A.dim(), &A[0]);
00089     return M;
00090   }
00091 
00097   template <class T>
00098   inline Array1D<T> Matrix2Array1D(Matrix<T> M)
00099   {
00100     if (M.dim1() == 1)
00101     {
00102       Array1D<T> A(M.dim2(), M[0]);
00103       return A;
00104     }
00105     else if (M.dim2() == 1)
00106     {
00107       Array1D<T> A(M.dim1(), M[0]);
00108       return A;
00109     }
00110     else
00111     {
00112       Array1D<T> A(0,0);
00113       return A;
00114     } 
00115   }
00116 
00120   inline Array2D<double> makeIdentity(int order)
00121   {
00122     Array2D<double> I(order,order,0.0);
00123     for (int i=0;i<order;i++)
00124       I[i][i]=1.0;
00125     return I;
00126   }
00127 
00131   template <class T>
00132   inline Array1D<T> copyrow(const Array2D<T> &Array,
00133                             int r)
00134   {
00135     if (r >= Array.dim1())
00136     {
00137       Array1D<T> A;
00138       return A;
00139     }
00140     int m = Array.dim2();
00141     Array1D<T> A(m);
00142     for (int c=0; c<m; c++)
00143       A[c] = Array[r][c];
00144     return A;
00145   }
00146 
00150   template <class T>
00151   inline Array1D<T> copycolumn(const Array2D<T> &Array,
00152                                int c)
00153   {
00154     if (c >= Array.dim2())
00155     {
00156       Array1D<T> A;
00157       return A;
00158     }
00159     int n = Array.dim1();
00160     Array1D<T> A(n);
00161     for (int r=0; r<n; r++)
00162       A[r] = Array[r][c];
00163     return A;
00164   }
00165 
00169   template <class T>
00170   inline Array2D<T> copyArray2D(const Array3D<T> &Array,
00171                                 int m)
00172   {
00173     if (m >= Array.dim1())
00174     {
00175       Array2D<T> A;
00176       return A;
00177     }
00178     int n = Array.dim2();
00179     int o = Array.dim3();
00180     Array2D<T> A(n, o);
00181     for (int r=0; r<n; r++)
00182       for (int c=0; c<o; c++)
00183         A[r][c] = Array[m][r][c];
00184     return A;
00185   }
00186 
00190   template <class T>
00191   inline int injectrow(Array2D<T> &A2D,
00192                        const Array1D<T> &A1D,
00193                        int r)
00194   {
00195     int m = A2D.dim2();
00196     if (m == A1D.dim() && r < A2D.dim1())
00197     {
00198       for (int c=0; c<m; c++)
00199         A2D[r][c] = A1D[c];
00200       return 0;
00201     }
00202     else
00203       return 1;
00204   }
00205 
00209   template <class T>
00210   inline int injectcolumn(Array2D<T> &A2D,
00211                           const Array1D<T> &A1D,
00212               int c)
00213   {
00214     int n = A2D.dim1();
00215     if (n == A1D.dim() && c < A2D.dim2())
00216     {
00217       for (int r=0; r<n; r++)
00218         A2D[r][c] = A1D[r];
00219       return 0;
00220     }
00221     return 1;
00222   }
00223 
00227   template <class T>
00228   inline int injectArray2D(Array3D<T> &A3D,
00229                            const Array2D<T> &A2D,
00230                int m)
00231   {
00232     int n = A3D.dim2();
00233     int o = A3D.dim3();
00234     if (n == A2D.dim1() && o == A2D.dim2() && m < A3D.dim1())
00235     {
00236       for (int r=0; r<n; r++)
00237         for (int c=0; c<o; c++)
00238           A3D[m][r][c] = A2D[r][c];
00239       return 0;
00240     }
00241     else
00242       return 1;
00243   }
00244 
00256   template <class T>
00257   inline Matrix<T> mult_transpose(const Matrix<T>  &A,
00258                                   const Matrix<T> &B)
00259   {
00260 
00261   #ifdef TNT_BOUNDS_CHECK
00262     assert(A.num_cols() == B.num_cols());
00263   #endif
00264 
00265     Subscript M = A.num_cols();
00266     Subscript N = A.num_rows();
00267     Subscript K = B.num_rows();
00268 
00269     Matrix<T> tmp(N,M);
00270     T sum;
00271 
00272     for (Subscript i=0; i<N; i++)
00273     for (Subscript k=0; k<K; k++)
00274     {
00275       sum = 0;
00276       for (Subscript j=0; j<M; j++)
00277         sum = sum +  A[i][j] * B[k][j];
00278 
00279       tmp[i][k] = sum; 
00280     }
00281 
00282     return tmp;
00283   }
00284 
00285 } // TNT
00286 
00287 #endif
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines