Ignore:
Timestamp:
Jun 14, 2006, 12:03:40 AM (17 years ago)
Author:
Jari Häkkinen
Message:

Updated parts of C++ tools.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/se/lu/thep/wenni/lib/c++_tools/gslapi/vector.cc

    r95 r110  
    2727
    2828#include <c++_tools/gslapi/vector.h>
     29#include <c++_tools/gslapi/matrix.h>
    2930#include <c++_tools/utility/stl_utility.h>
     31#include <c++_tools/utility/utility.h>
     32
    3033
    3134#include <iostream>
    3235#include <sstream>
    33 #include <string>
    3436#include <vector>
    3537#include <utility>
     
    4143
    4244
    43   vector::vector(void)
    44     : v_(NULL), view_(NULL)
    45   {
    46   }
    47 
    48 
    49 
    50   vector::vector(const size_t n,const double init_value)
    51     : view_(NULL)
    52   {
    53     v_ = gsl_vector_alloc(n);
    54     set_all(init_value);
    55   }
    56 
    57 
    58 
    59   vector::vector(const vector& other)
    60     : view_(NULL)
    61   {
    62     v_ = other.TEMP_gsl_vector_copy();
    63   }
    64 
    65 
    66 
    6745  vector::vector(vector& v, size_t offset, size_t n, size_t stride)
    68   {
     46    : const_view_(NULL)
     47  {
     48    // Jari, exception handling needed here. Failure in setting up a
     49    // proper gsl_vector_view is communicated by NULL pointer in the
     50    // view structure (cf. GSL manual). How about GSL error state?
    6951    view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset,
    7052                                                                 stride,n));
     
    7456
    7557
    76   vector::vector(gsl_vector* vec)
    77     : v_(vec), view_(NULL)
    78   {
    79   }
    80 
    81 
    82 
    83   vector::vector(std::istream& is) throw (utility::IO_error,std::exception)
     58  vector::vector(const vector& v, size_t offset, size_t n, size_t stride)
    8459    : view_(NULL)
     60  {
     61    // Jari, exception handling needed here. Failure in setting up a
     62    // proper gsl_vector_view is communicated by NULL pointer in the
     63    // view structure (cf. GSL manual). How about GSL error state?
     64    const_view_ = new gsl_vector_const_view(
     65                   gsl_vector_const_subvector_with_stride(v.v_,offset,stride,n));
     66    v_ = const_cast<gsl_vector*>(&(const_view_->vector));
     67  }
     68
     69
     70
     71  vector::vector(matrix& m, size_t i, bool row)
     72    : const_view_(NULL)
     73  {
     74    view_=new gsl_vector_view(row ?
     75                              gsl_matrix_row   (m.gsl_matrix_p(),i) :
     76                              gsl_matrix_column(m.gsl_matrix_p(),i)  );
     77    v_ = &(view_->vector);
     78  }
     79
     80
     81
     82  vector::vector(const matrix& m, size_t i, bool row)
     83    : view_(NULL)
     84  {
     85    const_view_ = new gsl_vector_const_view(row ?
     86                                   gsl_matrix_const_row   (m.gsl_matrix_p(),i) :
     87                                   gsl_matrix_const_column(m.gsl_matrix_p(),i) );
     88    v_ = const_cast<gsl_vector*>(&(const_view_->vector));
     89  }
     90
     91
     92
     93  vector::vector(std::istream& is, char sep)
     94    throw (utility::IO_error,std::exception)
     95    : view_(NULL), const_view_(NULL)
    8596  {
    8697    // read the data file and store in stl vectors (dynamically
     
    89100    u_int nof_columns=0;
    90101    u_int nof_rows=0;
    91     std::vector<double> v;
    92     for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) {
    93       // Ignoring empty lines
    94       if (!v.size()) {
    95         nof_rows--;
    96         continue;
    97       }
     102    std::string line;
     103    while(getline(is, line, '\n')) {
     104      // Empty lines
     105      if (!line.size())
     106          continue;
     107      nof_rows++;
     108     
     109      std::vector<double> v;
     110      std::string element;
     111      std::stringstream ss(line);
     112      bool ok=true;
     113      while(ok) {
     114        if(sep=='\0')
     115          ok=(ss>>element);
     116        else
     117          ok=getline(ss, element, sep);
     118        if(!ok)
     119          break;       
     120
     121        if(utility::is_double(element)) {
     122          v.push_back(atof(element.c_str()));
     123        }
     124        else if (!element.size() || utility::is_nan(element)) {
     125          v.push_back(std::numeric_limits<double>::quiet_NaN());
     126        }
     127        else {
     128          // Jari, this should be communicated with as an exception.
     129          // std::cerr << "Warning: '" << element
     130          //           << "' is not an integer." << std::endl;
     131        }
     132      }
     133      if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
     134          v.push_back(std::numeric_limits<double>::quiet_NaN());
    98135      if (!nof_columns)
    99136        nof_columns=v.size();
     
    101138        std::ostringstream s;
    102139        s << "vector::vector(std::istream&) data file error:\n"
    103           << "    File has inconsistent number of rows (" << nof_rows+1
     140          << "    File has inconsistent number of rows (" << nof_rows
    104141          << ") and columns (" << nof_columns
    105142          << ").\n    Expected a row or a column vector.";
    106         std::string m=s.str();
    107         throw utility::IO_error(m);
     143        throw utility::IO_error(s.str());
    108144      }
    109       else if(v.size()!=nof_columns) {
     145      else if (v.size()!=nof_columns) {
    110146        std::ostringstream s;
    111147        s << "vector::vector(std::istream&) data file error:\n"
    112           << "    Line " << (nof_rows+1) << " has " << v.size()
     148          << "    Line " << nof_rows << " has " << v.size()
    113149          << " columns; expected " << nof_columns << " column.";
    114         std::string m=s.str();
    115         throw utility::IO_error(m);
     150        throw utility::IO_error(s.str());
    116151      }
    117152      data_matrix.push_back(v);
     
    134169    if (view_)
    135170      delete view_;
     171    else if (const_view_)
     172      delete const_view_;
    136173    else if (v_)
    137174      gsl_vector_free(v_);
     
    141178
    142179
    143   gsl_vector* vector::TEMP_gsl_vector_copy(void) const
     180  gsl_vector* vector::create_gsl_vector_copy(void) const
    144181  {
    145182    gsl_vector* vec = gsl_vector_alloc(size());
     
    172209    double sum = 0;
    173210    for (size_t i=0; i<size(); i++)
    174       sum += gsl_vector_get( v_, i );
     211      sum += (*this)(i);
    175212    return( sum );
    176213  } 
    177 
    178 
    179 
    180   double vector::operator*( const vector &other ) const
    181   {
    182     // Jari, check for gsl_support
    183     double res = 0.0;;
    184     for ( size_t i = 0; i < size(); ++i )
    185       res += other[i] * (*this)[i];
    186     return res;
    187   }
    188 
    189 
    190 
    191   vector vector::operator+(const vector &other) const
    192   {
    193     vector res(*this);
    194     gsl_vector_add(res.v_,other.v_);
    195     return res;
    196   }
    197 
    198 
    199 
    200   vector vector::operator-( const vector &other ) const
    201   {
    202     vector res( *this );
    203     gsl_vector_sub(res.v_,other.v_);
    204     return res;
    205   }
    206 
    207 
    208214
    209215  bool vector::operator==(const vector& a) const
     
    219225
    220226
     227  double vector::operator*( const vector &other ) const
     228  {
     229    double res = 0.0;;
     230    for ( size_t i = 0; i < size(); ++i )
     231      res += other(i) * (*this)(i);
     232    return res;
     233  }
     234
     235
     236
    221237  const vector& vector::operator=( const vector& other )
    222238  {
    223239    if( this != &other ) {
    224       if ( v_ )
     240      if (view_)
     241        delete view_;
     242      else if (const_view_)
     243        delete const_view_;
     244      else if ( v_ )
    225245        gsl_vector_free( v_ );
    226       v_ = other.TEMP_gsl_vector_copy();
     246      v_ = other.create_gsl_vector_copy();
    227247    }
    228248    return *this;
     
    233253  std::ostream& operator<<(std::ostream& s, const vector& a)
    234254  {
    235     s.setf(std::ios::fixed);
     255    s.setf(std::ios::dec);
    236256    s.precision(12);
    237257    for (size_t j = 0; j < a.size(); ++j) {
Note: See TracChangeset for help on using the changeset viewer.