Changeset 1789


Ignore:
Timestamp:
Feb 10, 2009, 5:11:04 PM (11 years ago)
Author:
Peter
Message:

Speeding up copying from Matrix to MatrixWeighted? using
BinaryWeight?. Moving BinaryWeight? from stl_utility.h to
utility.h. Improved docs in MatrixWeighted?.

Location:
trunk/yat/utility
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Matrix.cc

    r1786 r1789  
    2424
    2525#include "Matrix.h"
    26 #include "stl_utility.h"
    2726#include "Vector.h"
    2827#include "VectorBase.h"
  • trunk/yat/utility/MatrixWeighted.cc

    r1706 r1789  
    2828#include "DataIterator.h"
    2929#include "Matrix.h"
     30#include "utility.h"
    3031#include "WeightIterator.h"
    3132
     
    112113  void MatrixWeighted::copy(const Matrix& data)
    113114  {
    114     Matrix weight;
    115     nan(data, weight);
    116     copy(data, weight);
    117   }
    118 
    119 
    120   void MatrixWeighted::copy(const Matrix& data, const Matrix& weight)
    121   {
    122     assert(data.rows()==weight.rows());
    123     assert(data.columns()==weight.columns());
    124115    columns_ = data.columns();
    125116    resize(data.rows(), data.columns());
     
    127118    assert(columns()==data.columns());
    128119    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
    129     std::copy(weight.begin(), weight.end(), weight_iterator(vec_.begin()));
     120    BinaryWeight()(data.begin(), data.end(), weight_iterator(vec_.begin()));
    130121  }
    131122
  • trunk/yat/utility/MatrixWeighted.h

    r1706 r1789  
    3838  class Matrix;
    3939
    40   ///
    41   /// @brief Weighted Matrix
    42   ///
     40  /**
     41     \brief Weighted Matrix
     42 
     43     \since New in yat 0.5
     44
     45  */
    4346  class MatrixWeighted
    4447  {
     
    4649    /**
    4750       value_type is DataWeight
    48 
    49        \since New in yat 0.5
    5051     */
    5152    typedef DataWeight value_type;
     
    118119
    119120       Data is copied from \a other and weights are calculated using
    120        the nan function.
     121       the BinaryWeight functor.
    121122    */
    122123    explicit MatrixWeighted(const Matrix& other);
     
    211212
    212213    /**
     214       \brief Resize Matrix
     215
     216       \note this function may invalidate iterators.
     217    */
     218    void resize(size_t rows, size_t columns);
     219
     220    /**
     221       \return The number of rows in the matrix.
     222    */
     223    size_t rows(void) const;
     224
     225    /**
    213226       \brief swap objects
    214227
    215228       Takes constant time. Invalidates iterators.
     229       There is no requirement on the size of \a other.
    216230     */
    217231    void swap(MatrixWeighted& other);
    218 
    219     /**
    220        \brief Resize Matrix
    221 
    222        \note this function may invalidate iterators.
    223     */
    224     void resize(size_t, size_t);
    225 
    226     /**
    227        \return The number of rows in the matrix.
    228     */
    229     size_t rows(void) const;
    230232
    231233    /**
     
    254256  private:
    255257    void copy(const Matrix&);
    256     void copy(const Matrix& data, const Matrix& weight);
    257258
    258259    std::vector<DataWeight> vec_;
  • trunk/yat/utility/stl_utility.h

    r1786 r1789  
    7777    inline T operator()(T x) const
    7878    { return std::abs(x); }
    79   };
    80 
    81 
    82   /**
    83      For each element in resulting range assign it to 0.0 if
    84      corresponding element in input range is NaN else assign it to
    85      1.0.
    86 
    87      \since New in yat 0.5
    88    */
    89   struct BinaryWeight
    90   {
    91     /**
    92         \return true if there is at least one NaN in input range
    93         [first, last).
    94      */
    95     template<typename InputIterator, typename OutputIterator>
    96     bool inline operator()(InputIterator first, InputIterator last,
    97                            OutputIterator result) const
    98     {
    99       bool nan=false;
    100       while (first!=last) {
    101         if (std::isnan(*first)) {
    102           *result=0;
    103           nan=true;
    104         }
    105         else
    106           *result = 1.0;
    107         ++first;
    108         ++result;
    109       }
    110       return nan;
    111     }
    11279  };
    11380
  • trunk/yat/utility/utility.h

    r1500 r1789  
    3434#include "deprecate.h"
    3535
     36#include <cmath>
    3637#include <limits>
    3738#include <string>
     
    4546namespace utility {
    4647
    47   class Matrix;
     48  /**
     49     For each element in resulting range assign it to 0.0 if
     50     corresponding element in input range is NaN else assign it to
     51     1.0.
     52
     53     \since New in yat 0.5
     54   */
     55  struct BinaryWeight
     56  {
     57    /**
     58        \return true if there is at least one NaN in input range
     59        [first, last).
     60     */
     61    template<typename InputIterator, typename OutputIterator>
     62    bool operator()(InputIterator first, InputIterator last,
     63                    OutputIterator result) const;
     64  };
     65
    4866
    4967  /**
     
    99117  bool is_nan(const std::string& s);
    100118
     119
     120  template<typename InputIterator, typename OutputIterator>
     121  bool BinaryWeight::operator()(InputIterator first, InputIterator last,
     122                                OutputIterator result) const
     123  {
     124    bool nan=false;
     125    while (first!=last) {
     126      if (std::isnan(*first)) {
     127        *result=0;
     128        nan=true;
     129      }
     130      else
     131        *result = 1.0;
     132      ++first;
     133      ++result;
     134    }
     135    return nan;
     136  }
     137
     138
    101139  // template implementations
    102140  template<typename T>
Note: See TracChangeset for help on using the changeset viewer.