yat/classifier/MatrixLookupWeighted.h

Code
Comments
Other
Rev Date Author Line
2862 03 Oct 12 peter 1 #ifndef _theplu_yat_classifier_matrix_lookup_weighted_
2862 03 Oct 12 peter 2 #define _theplu_yat_classifier_matrix_lookup_weighted_
595 28 Aug 06 peter 3
616 31 Aug 06 jari 4 // $Id$
616 31 Aug 06 jari 5
675 10 Oct 06 jari 6 /*
2119 12 Dec 09 peter 7   Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
2119 12 Dec 09 peter 8   Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
4019 06 Nov 20 peter 9   Copyright (C) 2009, 2010, 2012, 2020 Peter Johansson
595 28 Aug 06 peter 10
1437 25 Aug 08 peter 11   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 12
675 10 Oct 06 jari 13   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 14   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 15   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 16   License, or (at your option) any later version.
675 10 Oct 06 jari 17
675 10 Oct 06 jari 18   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 19   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 21   General Public License for more details.
675 10 Oct 06 jari 22
675 10 Oct 06 jari 23   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 24   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 25 */
675 10 Oct 06 jari 26
1110 19 Feb 08 peter 27 #include "yat/utility/Container2DIterator.h"
1549 06 Oct 08 peter 28 #include "yat/utility/DataWeight.h"
2223 19 Mar 10 peter 29 #include "yat/utility/deprecate.h"
1170 27 Feb 08 peter 30 #include "yat/utility/Index.h"
1589 17 Oct 08 peter 31 #include "yat/utility/MatrixWeighted.h"
1090 14 Feb 08 peter 32 #include "yat/utility/StrideIterator.h"
675 10 Oct 06 jari 33
1589 17 Oct 08 peter 34 #include <boost/iterator/permutation_iterator.hpp>
1589 17 Oct 08 peter 35
2076 06 Oct 09 peter 36 #include <iosfwd>
4019 06 Nov 20 peter 37 #include <memory>
1090 14 Feb 08 peter 38 #include <utility>
757 19 Feb 07 jari 39 #include <vector>
595 28 Aug 06 peter 40
595 28 Aug 06 peter 41 namespace theplu {
680 11 Oct 06 jari 42 namespace yat {
2862 03 Oct 12 peter 43 namespace classifier {
595 28 Aug 06 peter 44
1035 05 Feb 08 peter 45   class MatrixLookup;
1035 05 Feb 08 peter 46
595 28 Aug 06 peter 47   ///
1588 17 Oct 08 peter 48   /// @brief General view into utility::MatrixWeighted
767 22 Feb 07 peter 49   ///
1588 17 Oct 08 peter 50   /// A MatrixLookupWeighted can be created directly from a
1588 17 Oct 08 peter 51   /// utility::MatrixWeighted or from another MatrixLookupWeighted. In
1588 17 Oct 08 peter 52   /// the latter case, the resulting MatrixLookupWeighted is looking
1588 17 Oct 08 peter 53   /// directly into the underlying matrix to avoid multiple lookups.
595 28 Aug 06 peter 54   ///
596 28 Aug 06 peter 55   /// There is a possibility to set the MatrixLookupWeighted as owner
1588 17 Oct 08 peter 56   /// of the underlying utility::MatrixWeighted.  This implies that
1588 17 Oct 08 peter 57   /// underlying data is deleted in destructor of
1588 17 Oct 08 peter 58   /// MatrixLookupWeighted, but only if there is no other owner of the
1588 17 Oct 08 peter 59   /// underlying data.
595 28 Aug 06 peter 60   ///
1588 17 Oct 08 peter 61   /// \see MatrixLookup
1588 17 Oct 08 peter 62   ///
1170 27 Feb 08 peter 63   class MatrixLookupWeighted
595 28 Aug 06 peter 64   {
595 28 Aug 06 peter 65   public:
1549 06 Oct 08 peter 66     /**
1549 06 Oct 08 peter 67        value_type is DataWeight
1549 06 Oct 08 peter 68
1549 06 Oct 08 peter 69        \since New in yat 0.5
1549 06 Oct 08 peter 70      */
1549 06 Oct 08 peter 71     typedef utility::DataWeight value_type;
1549 06 Oct 08 peter 72
1549 06 Oct 08 peter 73     /**
1549 06 Oct 08 peter 74        const_reference type is const DataWeight
1549 06 Oct 08 peter 75
1549 06 Oct 08 peter 76        \since New in yat 0.5
1549 06 Oct 08 peter 77      */
1588 17 Oct 08 peter 78     typedef const utility::DataWeight& const_reference;
1549 06 Oct 08 peter 79
1090 14 Feb 08 peter 80     /// 'Read Only' iterator
1090 14 Feb 08 peter 81     typedef utility::StrideIterator<
2862 03 Oct 12 peter 82     utility::Container2DIterator<const MatrixLookupWeighted,
2374 12 Dec 10 peter 83                                  const value_type, const_reference> >
1090 14 Feb 08 peter 84     const_iterator;
595 28 Aug 06 peter 85
1125 22 Feb 08 peter 86     /**
1125 22 Feb 08 peter 87        'Read only' iterator used to iterate over a column
1125 22 Feb 08 peter 88      */
1589 17 Oct 08 peter 89     typedef boost::permutation_iterator<
1589 17 Oct 08 peter 90       utility::MatrixWeighted::const_column_iterator,
1589 17 Oct 08 peter 91       utility::Index::const_iterator> const_column_iterator;
1125 22 Feb 08 peter 92
1125 22 Feb 08 peter 93     /**
1125 22 Feb 08 peter 94        'Read only' iterator used to iterate over a row
1125 22 Feb 08 peter 95      */
1589 17 Oct 08 peter 96     typedef boost::permutation_iterator<
1589 17 Oct 08 peter 97       utility::MatrixWeighted::const_row_iterator,
1589 17 Oct 08 peter 98       utility::Index::const_iterator> const_row_iterator;
1105 18 Feb 08 peter 99
1482 09 Sep 08 peter 100     /**
1482 09 Sep 08 peter 101        \brief Create a lookup into \a matrix.
1482 09 Sep 08 peter 102
1482 09 Sep 08 peter 103        The created lookup, mlw, will fullfil: mlw(i,j) =
1482 09 Sep 08 peter 104        matrix(rows(i), columns(j))
1482 09 Sep 08 peter 105      */
1482 09 Sep 08 peter 106     MatrixLookupWeighted(const utility::MatrixWeighted& matrix,
1482 09 Sep 08 peter 107                          const utility::Index& rows,
1482 09 Sep 08 peter 108                          const utility::Index& columns);
1482 09 Sep 08 peter 109
1482 09 Sep 08 peter 110     /**
1482 09 Sep 08 peter 111        \brief Create a lookup into entire \a matrix.
1482 09 Sep 08 peter 112      */
1581 15 Oct 08 peter 113     explicit MatrixLookupWeighted(const utility::MatrixWeighted& matrix,
1581 15 Oct 08 peter 114                                   bool owner=false);
1482 09 Sep 08 peter 115
1035 05 Feb 08 peter 116     /**
1035 05 Feb 08 peter 117        Constructor creating a MatrixLookupWeighted from a MatrixLookup. A
1035 05 Feb 08 peter 118        weight matrix with unitary weights are created internally.
1035 05 Feb 08 peter 119
1035 05 Feb 08 peter 120        \note no check for nan is performed.
2862 03 Oct 12 peter 121
1587 17 Oct 08 peter 122        @note from yat 0.5 data is copied and further modifications in
1587 17 Oct 08 peter 123        \a matrix will not be reflected in MatrixLookupWeighted.
1035 05 Feb 08 peter 124     */
1484 09 Sep 08 peter 125     explicit MatrixLookupWeighted(const MatrixLookup& matrix);
1035 05 Feb 08 peter 126
1035 05 Feb 08 peter 127
638 06 Sep 06 markus 128     ///
826 19 Mar 07 peter 129     /// @brief Copy constructor.
595 28 Aug 06 peter 130     ///
826 19 Mar 07 peter 131     /// If \a other is owner of underlying data, constructed
826 19 Mar 07 peter 132     /// MatrixLookup will also be set as owner of underlying data.
595 28 Aug 06 peter 133     ///
595 28 Aug 06 peter 134     /// @note If underlying matrix goes out of scope or is deleted, the
595 28 Aug 06 peter 135     /// MatrixLookupWeighted becomes invalid and the result of further use is
595 28 Aug 06 peter 136     /// undefined.
595 28 Aug 06 peter 137     ///
826 19 Mar 07 peter 138     MatrixLookupWeighted(const MatrixLookupWeighted& other);
595 28 Aug 06 peter 139
595 28 Aug 06 peter 140     ///
595 28 Aug 06 peter 141     /// Creates a sub-MatrixLookupWeighted. The Lookup is independent of
595 28 Aug 06 peter 142     /// MatrixLookupWeighted @a ml. The MatrixLookupWeighted is created to look
595 28 Aug 06 peter 143     /// directly into the underlying matrix to avoid multiple lookups.
595 28 Aug 06 peter 144     ///
595 28 Aug 06 peter 145     /// The @a row and @a column define what sub-matrix to look into,
595 28 Aug 06 peter 146     /// in other words, the created MatrixLookupWeighted will fullfill the
595 28 Aug 06 peter 147     /// following: \f$ MatrixLookupWeighted(i,j)=ml(row[i],column[j]) \f$. This
595 28 Aug 06 peter 148     /// also means that number of rows in created MatrixLookupWeighted is
595 28 Aug 06 peter 149     /// equal to size of vector @a row, and number of columns is equal
595 28 Aug 06 peter 150     /// to size of vector @a column.
595 28 Aug 06 peter 151     ///
826 19 Mar 07 peter 152     /// If \a ml is owner of underlying data, constructed
826 19 Mar 07 peter 153     /// MatrixLookup will also be set as owner of underlying data.
826 19 Mar 07 peter 154     ///
595 28 Aug 06 peter 155     /// @note If underlying matrix goes out of scope or is deleted, the
595 28 Aug 06 peter 156     /// MatrixLookupWeighted becomes invalid and the result of further use is
595 28 Aug 06 peter 157     /// undefined.
595 28 Aug 06 peter 158     ///
2862 03 Oct 12 peter 159     MatrixLookupWeighted(const MatrixLookupWeighted& ml,
2862 03 Oct 12 peter 160                          const utility::Index& row,
1134 23 Feb 08 peter 161                          const utility::Index& column);
595 28 Aug 06 peter 162
595 28 Aug 06 peter 163     ///
595 28 Aug 06 peter 164     /// Constructor creating a lookup into a sub-matrix of
595 28 Aug 06 peter 165     /// @a ml. The MatrixLookupWeighted is created to look directly into the
595 28 Aug 06 peter 166     /// underlying matrix to avoid multiple lookups.
595 28 Aug 06 peter 167     ///
746 11 Feb 07 peter 168     /// If @a row_vectors is true the new MatrixLookupWeighted will consist
595 28 Aug 06 peter 169     /// of the row vectors defined by @a index. This means that the
595 28 Aug 06 peter 170     /// created MatrixLookupWeighted will fullfill:
595 28 Aug 06 peter 171     /// \f$ MatrixLookupWeighted(i,j)=ml(i,index[j])\f$
595 28 Aug 06 peter 172     ///
746 11 Feb 07 peter 173     /// If @a row_vectors is false the new MatrixLookupWeighted will consist
595 28 Aug 06 peter 174     /// of the rolumn vectors defined by @a index. This means that the
595 28 Aug 06 peter 175     /// created MatrixLookupWeighted will fullfill:
595 28 Aug 06 peter 176     /// \f$ MatrixLookupWeighted(i,j) = ml(index[i],j) \f$
595 28 Aug 06 peter 177     ///
826 19 Mar 07 peter 178     /// If \a ml is owner of underlying data, constructed
826 19 Mar 07 peter 179     /// MatrixLookup will also be set as owner of underlying data.
826 19 Mar 07 peter 180     ///
595 28 Aug 06 peter 181     /// @note If underlying matrix goes out of scope or is deleted, the
595 28 Aug 06 peter 182     /// MatrixLookupWeighted becomes invalid and the result of further use is
595 28 Aug 06 peter 183     /// undefined.
595 28 Aug 06 peter 184     ///
2223 19 Mar 10 peter 185     /// \deprecated Provided for backward compatibility with the 0.6
2223 19 Mar 10 peter 186     /// API. Use MatrixLookupWeighted(const MatrixLookupWeighted&,
2223 19 Mar 10 peter 187     /// const utility::Index&, const utility::Index&)
2223 19 Mar 10 peter 188     ///
2862 03 Oct 12 peter 189     MatrixLookupWeighted(const MatrixLookupWeighted& ml,
2862 03 Oct 12 peter 190                          const utility::Index&,
2223 19 Mar 10 peter 191                          const bool row_vectors) YAT_DEPRECATE;
595 28 Aug 06 peter 192
595 28 Aug 06 peter 193     ///
595 28 Aug 06 peter 194     /// Constructor creating a MatrixLookupWeighted with @a rows rows, @a
595 28 Aug 06 peter 195     /// columns columns, and all values are set to @a value. Created
595 28 Aug 06 peter 196     /// MatrixLookupWeighted owns its underlying matrix.
595 28 Aug 06 peter 197     ///
2862 03 Oct 12 peter 198     MatrixLookupWeighted(const size_t rows, const size_t columns,
624 05 Sep 06 peter 199                          const double value=0, const double weight=1);
595 28 Aug 06 peter 200
595 28 Aug 06 peter 201     ///
595 28 Aug 06 peter 202     /// @brief The istream constructor.
595 28 Aug 06 peter 203     ///
595 28 Aug 06 peter 204     /// In construction the underlying matrix is created from
595 28 Aug 06 peter 205     /// stream. The MatrixLookupWeighted will be owner of the underlying
595 28 Aug 06 peter 206     /// matrix.
595 28 Aug 06 peter 207     ///
1587 17 Oct 08 peter 208     /// @see utility::MatrixWeighted(istream&) for details.
595 28 Aug 06 peter 209     ///
595 28 Aug 06 peter 210     MatrixLookupWeighted(std::istream&, char sep='\0');
595 28 Aug 06 peter 211
595 28 Aug 06 peter 212     ///
826 19 Mar 07 peter 213     /// Destructor. If MatrixLookup is owner (and the only owner) of
826 19 Mar 07 peter 214     /// underlying matrix, the matrices are destroyed.
595 28 Aug 06 peter 215     ///
595 28 Aug 06 peter 216     virtual ~MatrixLookupWeighted();
595 28 Aug 06 peter 217
1091 14 Feb 08 peter 218     /**
1091 14 Feb 08 peter 219        Iterator iterates along a row. When end of row is reached it
1091 14 Feb 08 peter 220        jumps to beginning of next row.
1091 14 Feb 08 peter 221
1091 14 Feb 08 peter 222        \return const_iterator pointing to upper-left element.
1091 14 Feb 08 peter 223      */
1091 14 Feb 08 peter 224     const_iterator begin(void) const;
1091 14 Feb 08 peter 225
1091 14 Feb 08 peter 226     /**
1091 14 Feb 08 peter 227        Iterator iterates along a column.
1091 14 Feb 08 peter 228
1091 14 Feb 08 peter 229        \return iterator pointing to first element of column \a i.
1091 14 Feb 08 peter 230      */
1105 18 Feb 08 peter 231     const_column_iterator begin_column(size_t) const;
1091 14 Feb 08 peter 232
1091 14 Feb 08 peter 233     /**
1091 14 Feb 08 peter 234        Iterator iterates along a column.
1091 14 Feb 08 peter 235
1091 14 Feb 08 peter 236        \return const_iterator pointing to first element of column \a i.
1091 14 Feb 08 peter 237      */
1105 18 Feb 08 peter 238     const_row_iterator begin_row(size_t) const;
1091 14 Feb 08 peter 239
1170 27 Feb 08 peter 240     /**
1170 27 Feb 08 peter 241        \return number of columns
1170 27 Feb 08 peter 242     */
1170 27 Feb 08 peter 243     size_t columns(void) const;
1170 27 Feb 08 peter 244
595 28 Aug 06 peter 245     ///
624 05 Sep 06 peter 246     /// @return data value of element (@a row, @a column)
624 05 Sep 06 peter 247     ///
720 26 Dec 06 jari 248     double data(size_t row, size_t column) const;
624 05 Sep 06 peter 249
826 19 Mar 07 peter 250     /**
1091 14 Feb 08 peter 251        \return const_iterator pointing to end of matrix
1091 14 Feb 08 peter 252      */
1091 14 Feb 08 peter 253     const_iterator end(void) const;
1091 14 Feb 08 peter 254
1091 14 Feb 08 peter 255     /**
1091 14 Feb 08 peter 256        \return const_iterator pointing to end of column \a i
1091 14 Feb 08 peter 257      */
1105 18 Feb 08 peter 258     const_column_iterator end_column(size_t) const;
1091 14 Feb 08 peter 259
1091 14 Feb 08 peter 260     /**
1091 14 Feb 08 peter 261        \return const_iterator pointing to end of row \a i
1091 14 Feb 08 peter 262      */
1105 18 Feb 08 peter 263     const_row_iterator end_row(size_t) const;
1091 14 Feb 08 peter 264
1170 27 Feb 08 peter 265     /**
1170 27 Feb 08 peter 266        \return number of rows
1170 27 Feb 08 peter 267     */
1170 27 Feb 08 peter 268     size_t rows(void) const;
1170 27 Feb 08 peter 269
604 29 Aug 06 peter 270     ///
624 05 Sep 06 peter 271     /// @return weight value of element (@a row, @a column)
624 05 Sep 06 peter 272     ///
720 26 Dec 06 jari 273     double weight(size_t row, size_t column) const;
624 05 Sep 06 peter 274
624 05 Sep 06 peter 275     ///
631 05 Sep 06 peter 276     /// @return true
631 05 Sep 06 peter 277     ///
631 05 Sep 06 peter 278     bool weighted(void) const;
631 05 Sep 06 peter 279
631 05 Sep 06 peter 280     ///
595 28 Aug 06 peter 281     /// Access operator
595 28 Aug 06 peter 282     ///
1193 29 Feb 08 peter 283     /// @return data-weight pair (@a row, @a column)
595 28 Aug 06 peter 284     ///
1552 06 Oct 08 peter 285     const_reference operator()(const size_t row, const size_t column) const;
595 28 Aug 06 peter 286
595 28 Aug 06 peter 287     ///
595 28 Aug 06 peter 288     /// @brief assigment operator
595 28 Aug 06 peter 289     ///
826 19 Mar 07 peter 290     /// Does only change MatrixLookupWeighted not the underlying
826 19 Mar 07 peter 291     /// matrix object. However if the MatrixLookupWeighted is owner
826 19 Mar 07 peter 292     /// (and the only owner) of its underlying data, those data will
826 19 Mar 07 peter 293     /// be deleted here.
595 28 Aug 06 peter 294     ///
595 28 Aug 06 peter 295     const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&);
2862 03 Oct 12 peter 296
595 28 Aug 06 peter 297   private:
4019 06 Nov 20 peter 298     typedef std::shared_ptr<const utility::MatrixWeighted> MatrixWP;
1170 27 Feb 08 peter 299     utility::Index column_index_;
1587 17 Oct 08 peter 300     MatrixWP data_;
1170 27 Feb 08 peter 301     utility::Index row_index_;
1170 27 Feb 08 peter 302
1170 27 Feb 08 peter 303     // for assertions
1170 27 Feb 08 peter 304     bool validate(void) const;
2862 03 Oct 12 peter 305   };
2862 03 Oct 12 peter 306
595 28 Aug 06 peter 307   ///
2862 03 Oct 12 peter 308   /// The output operator MatrixLookupWeighted
595 28 Aug 06 peter 309   ///
826 19 Mar 07 peter 310   /// For eacd data element data(i,j) is printed except those being
826 19 Mar 07 peter 311   /// associated with a zero weight for which nothing is printed.
826 19 Mar 07 peter 312   ///
1883 31 Mar 09 peter 313   /// \relates MatrixLookupWeighted
1883 31 Mar 09 peter 314   ///
595 28 Aug 06 peter 315   std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&);
595 28 Aug 06 peter 316
680 11 Oct 06 jari 317 }}} // of namespace classifier, yat, and theplu
595 28 Aug 06 peter 318
595 28 Aug 06 peter 319 #endif