00001 
00002
00003 /******************************************************************************
00004
00005 Copyright 2008 Departamento de Realidad Virtual
00006 y Unidad de Cómputo Aplicado DGSGA, UNAM.
00007
00008
00009 This file is part of RBF++.
00010
00011 RBF++ is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version 3 of the License, or
00014 (at your option) any later version.
00015
00016 RBF++ is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU General Public License for more details.
00020
00021 You should have received a copy of the GNU General Public License
00022 along with RBF++. If not, see <http://www.gnu.org/licenses/>.
00023
00024
00025 *******************************************************************************/
00026
00027
00028 /*
00029 Author: Daniel Cervantes Cabrera.
00030 Project: RBF++
00031 Institution: DGSCA, UNAM.
00032 Date: 3/05/08
00033 Description: Template class base point.
00034 */
00035
00036
00037 #include <LA/Point.h>
00038
00039 namespace LA{
00040
00041
00042 template<typename T>
00043 Point<T>::Point(){
00044 dim = 0;
00045 }
00046
00047
00048 template<typename T>
00049 Point<T>::Point(int Dim){
00050 _v = new T [Dim];
00051 dim = Dim;
00052 for(int i=0; i < Dim; i++)
00053 _v[i]= 0.0;
00054 }
00055
00056 template<typename T>
00057 Point<T>::Point(const Point<T>& v)
00058 {
00059 dim = v.dimension();
00060 _v = new T[dim];
00061
00062 for(int i=0; i< dim; i++)
00063 _v[i] = v[i];
00064 }
00065
00066
00067
00068 template<typename T>
00069 Point<T>::~Point()
00070 {
00071 delete [] _v;
00072 }
00073
00074 template<typename T>
00075 void Point<T>::resize(int n){
00076 if(dim !=0)
00077 delete [] _v;
00078 dim = n;
00079
00080 _v = new T[n];
00081 for(int i=0; i< dim; i++)
00082 _v[i] = 0.0;
00083 }
00084
00085
00086 template<typename T>
00087 T& Point<T>::operator [] (int i){
00088 if(i < 0 && i >= dim)
00089 cout << "Error: index out of range " << endl;
00090
00091 return _v[i];
00092 }
00093
00094 template<typename T>
00095 const T& Point<T>::operator [] (int i) const{
00096 if(i < 0 && i >= dim)
00097 cout << "Error: index out of range " << endl;
00098
00099 return _v[i];
00100 }
00101
00102
00103 template<typename U>
00104 ostream& operator<<(ostream& s, const Point<U>& v){
00105
00106 for(int i=0; i< v.dimension(); i++)
00107 s << v[i] << " ";
00108 return s;
00109 }
00110
00111
00112 template<typename U>
00113 Point<U>& Point<U>::operator=(const Point<U>& v2)
00114 {
00115
00116 if(this != &v2)
00117 for(int i=0; i< dim; i++)
00118 _v[i] = v2[i];
00119
00120 return *this;
00121
00122 }
00123
00124 template <typename U>
00125 Point<U> operator-(const Point<U>& v1, const Point<U>& v2)
00126 {
00127
00128 Point<U> v3;
00129
00130 if(v1.dimension() != v2.dimension()){
00131 cout << "Error: dimensions must be equals"<< endl;
00132 return v3;
00133 }
00134
00135 v3.resize(v2.dim);
00136
00137 for(int i=0; i< v2.dim; i++)
00138 v3[i] = v1[i]-v2[i];
00139
00140 return v3;
00141 }
00142
00143 template <typename U>
00144 Point<U> operator+(const Point<U>& v1, const Point<U>& v2)
00145 {
00146
00147 Point<U> v3;
00148
00149 if(v1.dim != v2.dim){
00150 cout << "Error: dimensions must be equals"<< endl;
00151 return v3;
00152 }
00153
00154 v3.resize(v2.dim);
00155
00156 for(int i=0; i< v2.dim; i++)
00157 v3[i] = v1[i]+v2[i];
00158
00159 return v3;
00160
00161 }
00162
00163
00164
00165 template<typename U>
00166 Point<U> operator*(const U & c, const Point<U>& v )
00167 {
00168 Point<U> vt(v.dim);
00169
00170 for(int i=0; i<v.dim; i++)
00171 vt[i] = v[i]*c;
00172
00173 return vt;
00174 }
00175
00176
00177 template<typename U>
00178 Point<U> operator*(const Point<U>& v, const U & c)
00179 {
00180 Point<U> vt(v.dim);
00181
00182 for(int i=0; i<v.dim; i++)
00183 vt[i] = v[i]*c;
00184
00185 return vt;
00186 }
00187
00188
00189
00190 template<typename U>
00191 U operator*(const Point<U>& v1, const Point<U> & v2 )
00192 {
00193
00194 if(v1.dim != v2.dim){
00195 cout << "Error: dimensions must be equals"<< endl;
00196 return 0;
00197 }
00198
00199
00200 U temp = v1[0]*v2[0];
00201 for(int i=1; i<v1.dim;i++)
00202 temp+= v1[i]*v2[i];
00203
00204 return temp;
00205 }
00206
00207
00208
00209 template<typename U>
00210 Point<U> operator+=( Point<U> & v1,const Point<U> & v2 )
00211 {
00212
00213 if(v1.dim != v2.dim){
00214 cout << "Error: dimensions must be equals"<< endl;
00215 return v1;
00216 }
00217
00218 for(int i=0; i<v1.dim; i++)
00219 v1[i] = v1[i]+v2[i];
00220
00221 return v1;
00222 }
00223
00224 }
Superior

Tenemos 27 visitantes y ningun miembro en Línea