00001 // This matrix class is a C++ wrapper for the GNU Scientific Library 00002 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00016 00017 #include<gslwrap/vector_double.h> 00018 #include<gslwrap/vector_float.h> 00019 00020 #define type_is_float 00021 #ifdef type_is 00022 #define type_is_double 00023 #endif 00024 00025 namespace gsl 00026 { 00027 00028 //vector_float::create_vector_view( const gsl_vector_float_view &other ) 00029 vector_float_view 00030 vector_float::create_vector_view( const gsl_vector_float_view &other ) 00031 { 00032 vector_float view; 00033 view.gsldata = (gsl_vector_float*)malloc(sizeof(gsl_vector_float)); 00034 *(view.gslobj()) = other.vector; 00035 view.gslobj()->owner = 0; 00036 return view; 00037 } 00038 00039 void 00040 vector_float::resize(size_t n) 00041 { 00042 if (gsldata) 00043 { 00044 if (n==size()) 00045 return; 00046 if (!gsldata->owner) 00047 { 00048 cout << "vector_float::resize ERROR can't resize a vector view" << endl; 00049 exit(-1); 00050 // GSL_ERROR("You can't resize a vector view", GSL_EINVAL); 00051 } 00052 free(); 00053 } 00054 alloc(n); 00055 } 00056 00057 void 00058 vector_float::copy(const vector_float& other) 00059 { 00060 resize(other.size()); 00061 gsl_vector_float_memcpy (gsldata,other.gsldata); 00062 } 00063 00064 bool 00065 vector_float::operator==(const vector_float& other) const 00066 { 00067 if (size() != other.size()) 00068 return false; 00069 for (int i=0;i<size(); i++) 00070 { 00071 if (this->operator[](i) != other[i]) 00072 return false; 00073 } 00074 return true; 00075 } 00076 00077 vector_float_view 00078 vector_float::subvector (size_t offset, size_t n) 00079 { 00080 gsl_vector_float_view view = gsl_vector_float_subvector (gsldata, offset, n); 00081 return vector_float_view::create_vector_view(view); 00082 } 00083 00084 //vector_float_const_view 00085 const vector_float_view 00086 vector_float::subvector (size_t offset, size_t n) const 00087 { 00088 gsl_vector_float_view view = gsl_vector_float_subvector (gsldata, offset, n); 00089 return vector_float_view::create_vector_view(view); 00090 } 00091 00092 // returns sum of all the elements. 00093 float vector_float::sum() const 00094 { 00095 int i; 00096 float sum = 0; 00097 00098 for ( i = 0; i < size(); i++ ) 00099 { 00100 sum += gsl_vector_float_get(gsldata, i); 00101 } 00102 00103 return( sum ); 00104 } 00105 00106 double 00107 vector_float::norm2() const 00108 { 00109 vector t=*this; 00110 return gsl_blas_dnrm2(t.gslobj()); 00111 } 00112 00113 ostream& 00114 operator<< ( ostream& os, const vector_float & vect ) 00115 { 00116 os.setf( ios::fixed); 00117 for (int i=0;i<vect.size();i++) 00118 { 00119 os << vect[i] << endl; 00120 } 00121 return os; 00122 } 00123 00124 //************************************************************************** 00125 // Implementation of the vector_float_view class : 00126 //************************************************************************** 00127 00128 00129 void 00130 vector_float_view::init(const vector_float& other) 00131 { 00132 free(); 00133 gsldata = (gsl_vector_float*)malloc(sizeof(gsl_vector_float)); 00134 *gsldata = *(other.gslobj()); 00135 gsldata->owner = 0; 00136 } 00137 00138 }