Changeset 415


Ignore:
Timestamp:
Dec 1, 2005, 4:52:36 PM (16 years ago)
Author:
Jari Häkkinen
Message:

Removed some gslapi copy intensive operators.
Made gslapi assignment operators ignore views, and change them
into normal vectors if assigned.
Cleaning up of code aswell.

Location:
branches/better_matrix_class
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • branches/better_matrix_class/lib/gslapi/matrix.cc

    r413 r415  
    9898
    9999
    100   gsl_matrix* matrix::gsl_matrix_copy(void) const
     100  gsl_matrix* matrix::create_gsl_matrix_copy(void) const
    101101  {
    102102    gsl_matrix* m = gsl_matrix_alloc(rows(),columns());
     
    122122
    123123
    124   /*
    125   vector matrix::operator*( const vector &other ) const
    126   {
    127     gsl_vector* gslvec=gsl_vector_alloc(rows());
    128     gsl_blas_dgemv(CblasNoTrans, 1.0, m_, other.gsl_vector_pointer(), 0.0,
    129                    gslvec );
    130     vector res(gslvec);
    131     return res;
    132   }
    133   */
    134 
    135 
    136124  const matrix& matrix::operator=( const matrix& other )
    137125  {
    138126    if ( this != &other ) {
    139       if ( m_ )
    140         gsl_matrix_free( m_ );
    141       m_ = other.gsl_matrix_copy();
     127      if (view_)
     128        delete view_;
     129      else if (m_)
     130        gsl_matrix_free(m_);
     131      m_ = other.create_gsl_matrix_copy();
    142132    }
    143133    return *this;
  • branches/better_matrix_class/lib/gslapi/matrix.h

    r413 r415  
    7070    /// of the view will be copied, i.e. the view is not copied.
    7171    ///
    72     inline matrix(const matrix& o) : view_(NULL) { m_ = o.gsl_matrix_copy(); }
     72    inline matrix(const matrix& o) : view_(NULL) {m_=o.create_gsl_matrix_copy();}
    7373
    7474    ///
     
    156156    /// @return A const pointer to the internal GSL matrix.
    157157    ///
     158    inline const gsl_matrix* gsl_matrix_p(void) const { return m_; }
     159
     160    ///
     161    /// @return A pointer to the internal GSL matrix.
     162    ///
    158163    // Jari, is this needed?
    159     inline const gsl_matrix* gsl_matrix_pointer(void) const { return m_; }
    160 
    161     ///
    162     /// @return A pointer to the internal GSL matrix.
    163     ///
    164     // Jari, is this needed?
    165     inline gsl_matrix* gsl_matrix_pointer(void) { return m_; };
     164    inline gsl_matrix* gsl_matrix_p(void) { return m_; };
    166165
    167166    ///
     
    245244
    246245    ///
     246    /// Set element values to values in \a mat. This function is
     247    /// needed for assignment of viewed elements.
     248    ///
     249    /// @return Whatever GSL returns.
     250    ///
     251    /// @note No check on size is done.
     252    ///
     253    /// @see const matrix& operator=(const matrix&)
     254    ///
     255    inline int set(const matrix& mat) { return gsl_matrix_memcpy(m_,mat.m_); }
     256
     257    ///
    247258    /// Set all elements to \a value.
    248259    ///
     
    250261
    251262    ///
    252     /// Set \a column to \a vec.
    253     ///
    254     /// @return Whatever GSL returns.
     263    /// Set \a column values to values in \a vec.
     264    ///
     265    /// @return Whatever GSL returns.
     266    ///
     267    /// @note No check on size is done.
    255268    ///
    256269    inline int set_column(const size_t column, const vector& vec)
    257       { return gsl_matrix_set_col(m_, column, vec.gsl_vector_pointer()); }
    258 
    259     ///
    260     /// Set \a row to \a vec.
    261     ///
    262     /// @return Whatever GSL returns.
     270      { return gsl_matrix_set_col(m_, column, vec.gsl_vector_p()); }
     271
     272    ///
     273    /// Set \a row values to values in \a vec.
     274    ///
     275    /// @return Whatever GSL returns.
     276    ///
     277    /// @note No check on size is done.
    263278    ///
    264279    inline int set_row(const size_t row, const vector& vec)
    265       { return gsl_matrix_set_row(m_, row, vec.gsl_vector_pointer()); }
     280      { return gsl_matrix_set_row(m_, row, vec.gsl_vector_p()); }
    266281
    267282    ///
     
    346361    ///
    347362    /// The assignment operator. There is no requirements on
    348     /// dimensions.
     363    /// dimensions, i.e. the matrix is remapped in memory if
     364    /// necessary. This implies that in general views cannot be
     365    /// assigned using this operator. Views will be mutated into
     366    /// normal matrices. The only exception to this behaviour on views
     367    /// is when self-assignemnt is done, since self-assignment is
     368    /// ignored.
     369    ///
     370    /// @return A const reference to the resulting matrix.
     371    ///
     372    /// @see int set(const matrix&)
    349373    ///
    350374    const matrix& operator=(const matrix& other);
     
    389413    /// @return A pointer to a copy of the internal GSL matrix.
    390414    ///
    391     gsl_matrix* gsl_matrix_copy(void) const;
     415    gsl_matrix* create_gsl_matrix_copy(void) const;
    392416
    393417    gsl_matrix* m_;
  • branches/better_matrix_class/lib/gslapi/vector.cc

    r413 r415  
    4444    : const_view_(NULL)
    4545  {
    46     view_ = new gsl_vector_view(row ?
    47                                 gsl_matrix_row   (m.gsl_matrix_pointer(),i) :
    48                                 gsl_matrix_column(m.gsl_matrix_pointer(),i) );
     46    view_=new gsl_vector_view(row ?
     47                              gsl_matrix_row   (m.gsl_matrix_p(),i) :
     48                              gsl_matrix_column(m.gsl_matrix_p(),i) );
    4949    v_ = &(view_->vector);
    5050  }
     
    5656  {
    5757    const_view_ = new gsl_vector_const_view(row ?
    58                              gsl_matrix_const_row   (m.gsl_matrix_pointer(),i) :
    59                              gsl_matrix_const_column(m.gsl_matrix_pointer(),i) );
     58                                   gsl_matrix_const_row   (m.gsl_matrix_p(),i) :
     59                                   gsl_matrix_const_column(m.gsl_matrix_p(),i) );
    6060    v_ = const_cast<gsl_vector*>(&(const_view_->vector));
    6161  }
     
    123123
    124124
    125   gsl_vector* vector::gsl_vector_copy(void) const
     125  gsl_vector* vector::create_gsl_vector_copy(void) const
    126126  {
    127127    gsl_vector* vec = gsl_vector_alloc(size());
     
    166166    for ( size_t i = 0; i < size(); ++i )
    167167      res += other[i] * (*this)[i];
    168     return res;
    169   }
    170 
    171 
    172 
    173   vector vector::operator+(const vector &other) const
    174   {
    175     vector res(*this);
    176     gsl_vector_add(res.v_,other.v_);
    177     return res;
    178   }
    179 
    180 
    181 
    182   vector vector::operator-( const vector &other ) const
    183   {
    184     vector res( *this );
    185     gsl_vector_sub(res.v_,other.v_);
    186168    return res;
    187169  }
     
    204186  {
    205187    if( this != &other ) {
    206       if ( v_ )
     188      if (view_)
     189        delete view_;
     190      else if (const_view_)
     191        delete const_view_;
     192      else if ( v_ )
    207193        gsl_vector_free( v_ );
    208       v_ = other.gsl_vector_copy();
     194      v_ = other.create_gsl_vector_copy();
    209195    }
    210196    return *this;
  • branches/better_matrix_class/lib/gslapi/vector.h

    r413 r415  
    6262    ///
    6363    inline vector(const vector& other) : view_(NULL), const_view_(NULL)
    64       { v_ = other.gsl_vector_copy(); }
     64      { v_ = other.create_gsl_vector_copy(); }
    6565
    6666    ///
     
    185185    /// @return A const pointer to the internal GSL vector,
    186186    ///
    187     // Jari, is this needed?
    188     inline const gsl_vector* gsl_vector_pointer(void) const { return v_; }
     187    inline const gsl_vector* gsl_vector_p(void) const { return v_; }
    189188
    190189    ///
    191190    /// @return A pointer to the internal GSL vector,
    192191    ///
    193     // Jari, is this needed?
    194     inline gsl_vector* TEMP_gsl_vector_pointer(void) { return v_; }
     192    inline gsl_vector* gsl_vector_p(void) { return v_; }
    195193
    196194    ///
     
    276274
    277275    ///
     276    /// Set element values to values in \a vec. This function is
     277    /// needed for assignment of viewed elements.
     278    ///
     279    /// @return Whatever GSL returns.
     280    ///
     281    /// @note No check on size is done.
     282    ///
     283    /// @see const vector& operator=(const vector&)
     284    ///
     285    inline int set(const vector& vec) { return gsl_vector_memcpy(v_,vec.v_); }
     286
     287    ///
    278288    /// Set all elements to \a value.
    279289    ///
     
    373383
    374384    ///
    375     /// Vector with scalar multiplication.
    376     ///
    377     /// @note This operator is not implemented!
    378     ///
    379     vector operator*(const double d) const;
    380 
    381     ///
    382     /// Vector addition.
    383     ///
    384     /// @return The resulting vector.
    385     ///
    386     vector operator+( const vector& other ) const;
    387 
    388     ///
    389     /// Vector subtraction.
    390     ///
    391     /// @return The resulting vector.
    392     ///
    393     vector operator-( const vector& other ) const;
    394 
    395     ///
    396385    /// Comparison operator. Takes linear time.
    397386    ///
     
    403392    ///
    404393    /// The assignment operator. There is no requirements on
    405     /// dimensions.
     394    /// dimensions, i.e. the vector is remapped in memory if
     395    /// necessary. This implies that in general views cannot be
     396    /// assigned using this operator. Views will be mutated into
     397    /// normal vectors. The only exception to this behaviour on views
     398    /// is when self-assignemnt is done, since self-assignment is
     399    /// ignored.
    406400    ///
    407401    /// @return A const reference to the resulting vector.
     402    ///
     403    /// @see int set(const vector&)
    408404    ///
    409405    const vector& operator=(const vector&);
     
    444440    /// @return A pointer to a copy of the internal GSL vector.
    445441    ///
    446     gsl_vector* gsl_vector_copy(void) const;
     442    gsl_vector* create_gsl_vector_copy(void) const;
    447443
    448444    gsl_vector* v_;
  • branches/better_matrix_class/lib/statistics/Averager.cc

    r295 r415  
    1111 
    1212
    13   Averager::Averager(void)
    14     : n_(0), x_(0), xx_(0)
    15   {
    16   }
    17 
    18   Averager::Averager(const double x,const double xx,const long n)
    19     : n_(n), x_(x), xx_(xx)
    20   {
    21   }
    22 
    23   Averager::Averager(const Averager& a)
    24     : n_(a.n_), x_(a.x_), xx_(a.xx_)
    25   {
    26   }
    27 
    2813  const Averager& Averager::operator+=(const Averager& a)
    2914  {
     
    3419  }
    3520
     21
    3622}} // of namespace statistics and namespace theplu
  • branches/better_matrix_class/lib/statistics/Averager.h

    r349 r415  
    2626    /// Default constructor
    2727    ///
    28     Averager(void);
     28    inline Averager(void) : n_(0), x_(0), xx_(0) {}
    2929   
    3030    ///
     
    3232    /// number of samples \a n.
    3333    ///
    34     Averager(const double x, const double xx, const long n);
     34    inline Averager(const double x,const double xx,const long n)
     35      : n_(n), x_(x), xx_(xx) {}
    3536
    3637    ///
    3738    /// Copy constructor
    3839    ///
    39     Averager(const Averager&);
    40    
     40    inline Averager(const Averager& a) : n_(a.n_), x_(a.x_), xx_(a.xx_) {}
     41
    4142    ///
    4243    /// Adding \a n (default=1) number of data point(s) with value \a d.
    4344    ///
    44     inline void add(const double d,const long n=1)
    45     {n_+=n; x_+=n*d; xx_+=n*d*d;}
     45    inline void add(const double d,const long n=1) { n_+=n; x_+=n*d; xx_+=n*d*d;}
    4646
    4747    ///
  • branches/better_matrix_class/lib/statistics/AveragerPair.cc

    r295 r415  
    1010 
    1111
    12   AveragerPair::AveragerPair(void)
    13     : x_(Averager()), y_(Averager()), xy_(0.0)
    14   {
    15   }
    16 
    17   AveragerPair::AveragerPair(const double x, const double xx, const double y,
    18                      const double yy, const double xy, const long n)
    19     : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy)
    20   {
    21   }
    22 
    23   AveragerPair::AveragerPair(const AveragerPair& a)
    24     : x_(a.x_averager()), y_(a.y_averager()), xy_(a.sum_xy())
    25   {
    26   }
    27 
    2812  const AveragerPair& AveragerPair::operator+=(const AveragerPair& a)
    2913  {
     
    3418  }
    3519
     20
    3621}} // of namespace statistics and namespace theplu
  • branches/better_matrix_class/lib/statistics/AveragerPair.h

    r355 r415  
    2525    /// Default constructor
    2626    ///
    27     AveragerPair(void);
     27    inline AveragerPair(void) : x_(Averager()), y_(Averager()), xy_(0.0) {}
    2828   
    2929    ///
     
    3131    /// number of pair of values \a n
    3232    ///
    33     AveragerPair(const double x, const double xx, const double y,
    34                  const double yy, const double xy, const long);
     33    inline AveragerPair(const double x, const double xx, const double y,
     34                        const double yy, const double xy, const long n)
     35      : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {}
    3536
    3637    ///
    3738    /// Copy constructor
    3839    ///
    39     AveragerPair(const AveragerPair&);
     40    inline AveragerPair(const AveragerPair& a)
     41      : x_(a.x_averager()), y_(a.y_averager()), xy_(a.sum_xy()) {}
    4042   
    4143    ///
  • branches/better_matrix_class/lib/statistics/AveragerWeighted.cc

    r397 r415  
    1111 
    1212
    13   AveragerWeighted::AveragerWeighted(void)
    14     : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0)
    15   {
    16   }
    17 
    18   AveragerWeighted::AveragerWeighted(const AveragerWeighted& a)
    19     : w_(Averager(a.sum_w(),a.sum_ww(),1)),
    20       wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)),
    21       wwx_(a.sum_wwx()),
    22       wxx_(a.sum_wxx())
    23   {
    24   }
    25 
    2613  AveragerWeighted AveragerWeighted::operator+=(const AveragerWeighted& a)
    2714  {
  • branches/better_matrix_class/lib/statistics/AveragerWeighted.h

    r400 r415  
    3333    ///
    3434    /// Default constructor
    35     ///
    36     AveragerWeighted(void);
     35    ///
     36    inline AveragerWeighted(void)
     37      : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0) {}
    3738
    3839    ///
    3940    /// Copy constructor
    4041    ///
    41     AveragerWeighted(const AveragerWeighted&);
     42    inline AveragerWeighted(const AveragerWeighted& a)
     43      : w_(Averager(a.sum_w(),a.sum_ww(),1)),
     44        wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), wwx_(a.sum_wwx()),
     45        wxx_(a.sum_wxx()) {}
    4246
    4347    ///
  • branches/better_matrix_class/lib/statistics/Linear.h

    r389 r415  
    3232    /// Destructor
    3333    ///
    34     virtual ~Linear(void) {};
     34    inline virtual ~Linear(void) {};
    3535         
    3636    ///
  • branches/better_matrix_class/lib/statistics/MultiDimensional.cc

    r386 r415  
    1010
    1111
    12   MultiDimensional::~MultiDimensional(void)
    13   {
    14     if (work_)
    15       gsl_multifit_linear_free(work_);
    16   }
    17 
    18 
    19 
    2012  void MultiDimensional::fit(const gslapi::matrix& x, const gslapi::vector& y)
    2113  {
     
    2517      gsl_multifit_linear_free(work_);
    2618    work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size());
    27     gsl_multifit_linear(x.gsl_matrix_pointer(),y.gsl_vector_pointer(),
    28                         fit_parameters_.TEMP_gsl_vector_pointer(),
    29                         covariance_.gsl_matrix_pointer(),&chisquare_,work_);
     19    gsl_multifit_linear(x.gsl_matrix_p(),y.gsl_vector_p(),
     20                        fit_parameters_.gsl_vector_p(),
     21                        covariance_.gsl_matrix_p(),&chisquare_,work_);
    3022  }
    3123
  • branches/better_matrix_class/lib/statistics/MultiDimensional.h

    r389 r415  
    2929    /// @brief Destructor
    3030    ///
    31     ~MultiDimensional(void);
     31    inline ~MultiDimensional(void) { if (work_) gsl_multifit_linear_free(work_);}
    3232
    3333    ///
  • branches/better_matrix_class/lib/utility/SVD.cc

    r413 r415  
    2929  {
    3030    gslapi::vector w(U_.columns());
    31     return gsl_linalg_SV_decomp(U_.gsl_matrix_pointer(),
    32                                 V_.gsl_matrix_pointer(),
    33                                 s_.TEMP_gsl_vector_pointer(),
    34                                 w.TEMP_gsl_vector_pointer());
     31    return gsl_linalg_SV_decomp(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
     32                                s_.gsl_vector_p(), w.gsl_vector_p());
    3533  }
    3634
     
    4139    gslapi::vector w(U_.columns());
    4240    gslapi::matrix X(U_.columns(),U_.columns());
    43     return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_pointer(),
    44                                     X.gsl_matrix_pointer(),
    45                                     V_.gsl_matrix_pointer(),
    46                                     s_.TEMP_gsl_vector_pointer(),
    47                                     w.TEMP_gsl_vector_pointer());
     41    return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_p(), X.gsl_matrix_p(),
     42                                    V_.gsl_matrix_p(), s_.gsl_vector_p(),
     43                                    w.gsl_vector_p());
    4844  }
    4945
  • branches/better_matrix_class/lib/utility/SVD.h

    r413 r415  
    8484    ///
    8585    inline int solve(gslapi::vector b, gslapi::vector x)
    86       { return gsl_linalg_SV_solve(U_.gsl_matrix_pointer(),
    87                                    V_.gsl_matrix_pointer(),
    88                                    s_.gsl_vector_pointer(),
    89                                    b.gsl_vector_pointer(),
    90                                    x.TEMP_gsl_vector_pointer());
    91       }
     86      { return gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
     87                                   s_.gsl_vector_p(), b.gsl_vector_p(),
     88                                   x.gsl_vector_p()); }
    9289
    9390    ///
     
    113110  private:
    114111    inline int jacobi(void)
    115       { return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_pointer(),
    116                                            V_.gsl_matrix_pointer(),
    117                                            s_.TEMP_gsl_vector_pointer());
    118       }
     112      { return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
     113                                           s_.gsl_vector_p()); }
    119114    int golub_reinsch(void);
    120115    int modified_golub_reinsch(void);
  • branches/better_matrix_class/test/svm_test.cc

    r337 r415  
    8282     
    8383  // Comparing alpha to alpha_matlab
    84   theplu::gslapi::vector diff_alpha = alpha - alpha_matlab;
     84  theplu::gslapi::vector diff_alpha(alpha);
     85  diff_alpha-=alpha_matlab;
    8586  if (diff_alpha*diff_alpha> 1e-10 ){
    8687    if (print)
     
    9091
    9192  // Comparing output to target
    92   theplu::gslapi::vector output = svm.output();
     93  theplu::gslapi::vector output(svm.output());
    9394  double slack = 0;
    9495  for (unsigned int i=0; i<target.size(); i++){
Note: See TracChangeset for help on using the changeset viewer.