Fuzzy Logic Tools  v1.0.5.1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
tnt_extension.hpp
Go to the documentation of this file.
1 /* Copyright (C) 2004-2015
2  ANTONIO JAVIER BARRAGAN, antonio.barragan@diesia.uhu.es
3  http://uhu.es/antonio.barragan
4 
5  Collaborators:
6  JOSE MANUEL ANDUJAR, andujar@diesia.uhu.es
7 
8  DPTO. DE ING. ELECTRONICA, DE SISTEMAS INFORMATICOS Y AUTOMATICA
9  ETSI, UNIVERSITY OF HUELVA (SPAIN)
10 
11  For more information, please contact with authors.
12 
13  This software is free software: you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  This software is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
27 #ifndef _TNT_EXTENSION_HPP_
28 #define _TNT_EXTENSION_HPP_
29 
37 #include <tnt/tnt.h> // Template Numerical Toolkit (http://math.nist.gov/tnt)
38 
39 namespace TNT{
40 
44  template <class T>
45  inline Vector<T> Array1D2Vector(Array1D<T> A)
46  {
47  Vector<T> V(A.dim(), &A[0]);
48  return V;
49  }
50 
54  template <class T>
55  inline Array1D<T> Vector2Array1D(Vector<T> V)
56  {
57  Array1D<T> A(V.dim(), &V[0]);
58  return A;
59  }
60 
64  template <class T>
65  inline Matrix<T> Array2D2Matrix(Array2D<T> A)
66  {
67  Matrix<T> M(A.dim1(), A.dim2(), A[0]);
68  return M;
69  }
70 
74  template <class T>
75  inline Array2D<T> Matrix2Array2D(Matrix<T> M)
76  {
77  Array2D<T> A(M.dim1(), M.dim2(), M[0]);
78  return A;
79  }
80 
84  template <class T>
85  inline Matrix<T> Array1D2Matrix(Array1D<T> A)
86  {
87  Matrix<T> M(1, A.dim(), &A[0]);
88  return M;
89  }
90 
96  template <class T>
97  inline Array1D<T> Matrix2Array1D(Matrix<T> M)
98  {
99  if (M.dim1() == 1)
100  {
101  Array1D<T> A(M.dim2(), M[0]);
102  return A;
103  }
104  else if (M.dim2() == 1)
105  {
106  Array1D<T> A(M.dim1(), M[0]);
107  return A;
108  }
109  else
110  {
111  Array1D<T> A(0,0);
112  return A;
113  }
114  }
115 
119  inline Array2D<double> makeIdentity(int order)
120  {
121  Array2D<double> I(order,order,0.0);
122  for (int i=0;i<order;i++)
123  I[i][i]=1.0;
124  return I;
125  }
126 
130  template <class T>
131  inline Array1D<T> copyrow(const Array2D<T> &Array,
132  int r)
133  {
134  if (r >= Array.dim1())
135  {
136  Array1D<T> A;
137  return A;
138  }
139  int m = Array.dim2();
140  Array1D<T> A(m);
141  for (int c=0; c<m; c++)
142  A[c] = Array[r][c];
143  return A;
144  }
145 
149  template <class T>
150  inline Array1D<T> copycolumn(const Array2D<T> &Array,
151  int c)
152  {
153  if (c >= Array.dim2())
154  {
155  Array1D<T> A;
156  return A;
157  }
158  int n = Array.dim1();
159  Array1D<T> A(n);
160  for (int r=0; r<n; r++)
161  A[r] = Array[r][c];
162  return A;
163  }
164 
168  template <class T>
169  inline Array2D<T> copyArray2D(const Array3D<T> &Array,
170  int m)
171  {
172  if (m >= Array.dim1())
173  {
174  Array2D<T> A;
175  return A;
176  }
177  int n = Array.dim2();
178  int o = Array.dim3();
179  Array2D<T> A(n, o);
180  for (int r=0; r<n; r++)
181  for (int c=0; c<o; c++)
182  A[r][c] = Array[m][r][c];
183  return A;
184  }
185 
189  template <class T>
190  inline int injectrow(Array2D<T> &A2D,
191  const Array1D<T> &A1D,
192  int r)
193  {
194  int m = A2D.dim2();
195  if (m == A1D.dim() && r < A2D.dim1())
196  {
197  for (int c=0; c<m; c++)
198  A2D[r][c] = A1D[c];
199  return 0;
200  }
201  else
202  return 1;
203  }
204 
208  template <class T>
209  inline int injectcolumn(Array2D<T> &A2D,
210  const Array1D<T> &A1D,
211  int c)
212  {
213  int n = A2D.dim1();
214  if (n == A1D.dim() && c < A2D.dim2())
215  {
216  for (int r=0; r<n; r++)
217  A2D[r][c] = A1D[r];
218  return 0;
219  }
220  return 1;
221  }
222 
226  template <class T>
227  inline int injectArray2D(Array3D<T> &A3D,
228  const Array2D<T> &A2D,
229  int m)
230  {
231  int n = A3D.dim2();
232  int o = A3D.dim3();
233  if (n == A2D.dim1() && o == A2D.dim2() && m < A3D.dim1())
234  {
235  for (int r=0; r<n; r++)
236  for (int c=0; c<o; c++)
237  A3D[m][r][c] = A2D[r][c];
238  return 0;
239  }
240  else
241  return 1;
242  }
243 
255  template <class T>
256  inline Matrix<T> mult_transpose(const Matrix<T> &A,
257  const Matrix<T> &B)
258  {
259 
260  #ifdef TNT_BOUNDS_CHECK
261  assert(A.num_cols() == B.num_cols());
262  #endif
263 
264  Subscript M = A.num_cols();
265  Subscript N = A.num_rows();
266  Subscript K = B.num_rows();
267 
268  Matrix<T> tmp(N,M);
269  T sum;
270 
271  for (Subscript i=0; i<N; i++)
272  for (Subscript k=0; k<K; k++)
273  {
274  sum = 0;
275  for (Subscript j=0; j<M; j++)
276  sum = sum + A[i][j] * B[k][j];
277 
278  tmp[i][k] = sum;
279  }
280 
281  return tmp;
282  }
283 
284 } // TNT
285 
286 #endif
Definition: tnt_extension.hpp:39