yat/utility/MatrixBase.h

Code
Comments
Other
Rev Date Author Line
4119 07 Nov 21 peter 1 #ifndef _theplu_yat_utility_matrix_base_
4119 07 Nov 21 peter 2 #define _theplu_yat_utility_matrix_base_
616 31 Aug 06 jari 3
42 26 Feb 04 jari 4 // $Id$
12 19 Jun 03 daniel 5
570 05 Apr 06 jari 6 /*
570 05 Apr 06 jari 7   Copyright (C) 2003 Daniel Dalevi, Peter Johansson
2119 12 Dec 09 peter 8   Copyright (C) 2004 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 9   Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
2119 12 Dec 09 peter 10   Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 11   Copyright (C) 2012, 2017, 2018, 2020, 2021, 2022 Peter Johansson
570 05 Apr 06 jari 12
4119 07 Nov 21 peter 13   This file is part of the yat library, https://dev.thep.lu.se/yat
570 05 Apr 06 jari 14
675 10 Oct 06 jari 15   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 16   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 17   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 18   License, or (at your option) any later version.
570 05 Apr 06 jari 19
675 10 Oct 06 jari 20   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 21   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
570 05 Apr 06 jari 23   General Public License for more details.
570 05 Apr 06 jari 24
570 05 Apr 06 jari 25   You should have received a copy of the GNU General Public License
4119 07 Nov 21 peter 26   along with yat. If not, see <https://www.gnu.org/licenses/>.
570 05 Apr 06 jari 27 */
570 05 Apr 06 jari 28
3585 19 Jan 17 peter 29 #include "config_public.h"
3585 19 Jan 17 peter 30
3605 27 Jan 17 peter 31 // include these operations as they are expected when using Matrix
3605 27 Jan 17 peter 32 #include "BLAS_level2.h"
3605 27 Jan 17 peter 33 #include "BLAS_level3.h"
3605 27 Jan 17 peter 34
4136 21 Jan 22 peter 35 #include "Container2DIterator.h"
3605 27 Jan 17 peter 36 #include "MatrixExpression.h"
1120 21 Feb 08 peter 37 #include "Vector.h"
1027 02 Feb 08 peter 38 #include "VectorConstView.h"
3605 27 Jan 17 peter 39 #include "VectorExpression.h"
1015 01 Feb 08 peter 40 #include "VectorView.h"
3605 27 Jan 17 peter 41 #include "yat_assert.h"
12 19 Jun 03 daniel 42
12 19 Jun 03 daniel 43 #include <gsl/gsl_matrix.h>
12 19 Jun 03 daniel 44
1792 11 Feb 09 peter 45 #include <cstddef> // size_t
1792 11 Feb 09 peter 46
42 26 Feb 04 jari 47 namespace theplu {
680 11 Oct 06 jari 48 namespace yat {
616 31 Aug 06 jari 49 namespace utility {
12 19 Jun 03 daniel 50
4119 07 Nov 21 peter 51   /**
4119 07 Nov 21 peter 52      \since New in yat 0.20
4119 07 Nov 21 peter 53    */
4119 07 Nov 21 peter 54   class MatrixBase : public BasicMatrix<MatrixBase>
420 02 Dec 05 jari 55   {
420 02 Dec 05 jari 56   public:
1064 10 Feb 08 peter 57     /**
1549 06 Oct 08 peter 58        value_type is double
1549 06 Oct 08 peter 59
1549 06 Oct 08 peter 60        \since New in yat 0.5
1549 06 Oct 08 peter 61      */
1549 06 Oct 08 peter 62     typedef double value_type;
1549 06 Oct 08 peter 63
1549 06 Oct 08 peter 64     /**
1547 03 Oct 08 peter 65        reference type is double&
1547 03 Oct 08 peter 66
1547 03 Oct 08 peter 67        \since New in yat 0.5
1547 03 Oct 08 peter 68      */
1547 03 Oct 08 peter 69     typedef double& reference;
1547 03 Oct 08 peter 70
1547 03 Oct 08 peter 71     /**
1547 03 Oct 08 peter 72        const_reference type is const double&
1547 03 Oct 08 peter 73
1547 03 Oct 08 peter 74        \since New in yat 0.5
1547 03 Oct 08 peter 75      */
1547 03 Oct 08 peter 76     typedef const double& const_reference;
1547 03 Oct 08 peter 77
1547 03 Oct 08 peter 78     /**
1125 22 Feb 08 peter 79        Read-only iterator that iterates over all elements
1064 10 Feb 08 peter 80      */
4136 21 Jan 22 peter 81     typedef Container2DIterator<const MatrixBase, const double, const double&>
4136 21 Jan 22 peter 82     const_iterator;
1064 10 Feb 08 peter 83
1064 10 Feb 08 peter 84     /**
1125 22 Feb 08 peter 85        Read-only iterator that iterates over one column
1103 18 Feb 08 peter 86      */
1103 18 Feb 08 peter 87     typedef StrideIterator<const double*> const_column_iterator;
1103 18 Feb 08 peter 88
1103 18 Feb 08 peter 89     /**
1125 22 Feb 08 peter 90        Read-only iterator that iterates over one row
1103 18 Feb 08 peter 91      */
1103 18 Feb 08 peter 92     typedef StrideIterator<const double*> const_row_iterator;
1103 18 Feb 08 peter 93
1103 18 Feb 08 peter 94     /**
614 31 Aug 06 jari 95        @brief The default constructor.
2689 27 Feb 12 peter 96
1822 24 Feb 09 jari 97        This constructor does not initialize underlying (essential)
614 31 Aug 06 jari 98        structures.
614 31 Aug 06 jari 99     */
4124 13 Jan 22 peter 100     MatrixBase(void) = default;
12 19 Jun 03 daniel 101
754 17 Feb 07 jari 102     /**
754 17 Feb 07 jari 103        \brief The destructor.
754 17 Feb 07 jari 104     */
4119 07 Nov 21 peter 105     virtual ~MatrixBase(void);
17 08 Jul 03 peter 106
792 11 Mar 07 jari 107     /**
1065 10 Feb 08 peter 108        Iterator iterates along a row. When end of row is reached it
1065 10 Feb 08 peter 109        jumps to beginning of next row.
1065 10 Feb 08 peter 110
1065 10 Feb 08 peter 111        \return const_iterator pointing to upper-left element.
1064 10 Feb 08 peter 112      */
1064 10 Feb 08 peter 113     const_iterator begin(void) const;
1064 10 Feb 08 peter 114
1064 10 Feb 08 peter 115     /**
1065 10 Feb 08 peter 116        Iterator iterates along a column.
1065 10 Feb 08 peter 117
1065 10 Feb 08 peter 118        \return const_iterator pointing to first element of column \a i.
1064 10 Feb 08 peter 119      */
4133 21 Jan 22 peter 120     const_column_iterator begin_column(size_t i) const;
1064 10 Feb 08 peter 121
1064 10 Feb 08 peter 122     /**
1065 10 Feb 08 peter 123        Iterator iterates along a row.
1065 10 Feb 08 peter 124
1065 10 Feb 08 peter 125        \return const_iterator pointing to first element of row \a i.
1064 10 Feb 08 peter 126      */
4133 21 Jan 22 peter 127     const_row_iterator begin_row(size_t i) const;
1064 10 Feb 08 peter 128
1064 10 Feb 08 peter 129     /**
1065 10 Feb 08 peter 130        \return const vector view of column \a i
1009 01 Feb 08 peter 131      */
1028 03 Feb 08 peter 132     const VectorConstView column_const_view(size_t) const;
1009 01 Feb 08 peter 133
42 26 Feb 04 jari 134     ///
420 02 Dec 05 jari 135     /// @return The number of columns in the matrix.
272 14 Apr 05 peter 136     ///
716 25 Dec 06 jari 137     size_t columns(void) const;
272 14 Apr 05 peter 138
755 18 Feb 07 jari 139     /**
1066 10 Feb 08 peter 140        \return const_iterator pointing to end of matrix
1064 10 Feb 08 peter 141      */
1064 10 Feb 08 peter 142     const_iterator end(void) const;
1064 10 Feb 08 peter 143
1064 10 Feb 08 peter 144     /**
1065 10 Feb 08 peter 145        \return const_iterator pointing to end of column \a i
1064 10 Feb 08 peter 146      */
4133 21 Jan 22 peter 147     const_column_iterator end_column(size_t i) const;
1064 10 Feb 08 peter 148
1064 10 Feb 08 peter 149     /**
1065 10 Feb 08 peter 150        \return const_iterator pointing to end of row \a i
1064 10 Feb 08 peter 151      */
4133 21 Jan 22 peter 152     const_row_iterator end_row(size_t i) const;
1064 10 Feb 08 peter 153
1064 10 Feb 08 peter 154     /**
788 10 Mar 07 jari 155        \brief Check whether matrices are equal within a user defined
788 10 Mar 07 jari 156        precision, set by \a precision.
788 10 Mar 07 jari 157
788 10 Mar 07 jari 158        \return True if each element deviates less or equal than \a
788 10 Mar 07 jari 159        d. If any matrix contain a NaN, false is always returned.
788 10 Mar 07 jari 160
788 10 Mar 07 jari 161        \see operator== and operator!=
788 10 Mar 07 jari 162     */
4119 07 Nov 21 peter 163     bool equal(const MatrixBase&, const double precision=0) const;
12 19 Jun 03 daniel 164
42 26 Feb 04 jari 165     ///
42 26 Feb 04 jari 166     /// @return A const pointer to the internal GSL matrix.
42 26 Feb 04 jari 167     ///
4124 13 Jan 22 peter 168     virtual const gsl_matrix* gsl_matrix_p(void) const=0;
12 19 Jun 03 daniel 169
42 26 Feb 04 jari 170     ///
42 26 Feb 04 jari 171     /// @return The number of rows in the matrix.
420 02 Dec 05 jari 172     ///
716 25 Dec 06 jari 173     size_t rows(void) const;
12 19 Jun 03 daniel 174
754 17 Feb 07 jari 175     /**
1065 10 Feb 08 peter 176        \return const vector view of \a i
1009 01 Feb 08 peter 177      */
1028 03 Feb 08 peter 178     const VectorConstView row_const_view(size_t) const;
754 17 Feb 07 jari 179
754 17 Feb 07 jari 180     /**
755 18 Feb 07 jari 181        \brief Element access operator.
755 18 Feb 07 jari 182
755 18 Feb 07 jari 183        \return Const reference to the element position (\a row, \a
755 18 Feb 07 jari 184        column).
755 18 Feb 07 jari 185
755 18 Feb 07 jari 186        \throw If GSL range checks are enabled in the underlying GSL
755 18 Feb 07 jari 187        library a GSL_error exception is thrown if either index is out
755 18 Feb 07 jari 188        of range.
755 18 Feb 07 jari 189     */
716 25 Dec 06 jari 190     const double& operator()(size_t row,size_t column) const;
12 19 Jun 03 daniel 191
788 10 Mar 07 jari 192     /**
788 10 Mar 07 jari 193        \brief Comparison operator. Takes squared time.
788 10 Mar 07 jari 194
788 10 Mar 07 jari 195        Checks are performed with exact matching, i.e., rounding off
788 10 Mar 07 jari 196        effects may destroy comparison. Use the equal function for
788 10 Mar 07 jari 197        comparing elements within a user defined precision.
788 10 Mar 07 jari 198
788 10 Mar 07 jari 199        \return True if all elements are equal otherwise false.
788 10 Mar 07 jari 200
788 10 Mar 07 jari 201        \see equal
788 10 Mar 07 jari 202     */
4119 07 Nov 21 peter 203     bool operator==(const MatrixBase& other) const;
20 11 Jul 03 daniel 204
788 10 Mar 07 jari 205     /**
788 10 Mar 07 jari 206        \brief Comparison operator. Takes squared time.
788 10 Mar 07 jari 207
788 10 Mar 07 jari 208        Checks are performed with exact matching, i.e., rounding off
788 10 Mar 07 jari 209        effects may destroy comparison. Use the equal function for
788 10 Mar 07 jari 210        comparing elements within a user defined precision.
788 10 Mar 07 jari 211
788 10 Mar 07 jari 212        \return False if all elements are equal otherwise true.
788 10 Mar 07 jari 213
788 10 Mar 07 jari 214        \see equal
788 10 Mar 07 jari 215     */
4119 07 Nov 21 peter 216     bool operator!=(const MatrixBase& other) const;
20 11 Jul 03 daniel 217
4119 07 Nov 21 peter 218   protected:
3605 27 Jan 17 peter 219     /**
754 17 Feb 07 jari 220        \brief Create a new copy of the internal GSL matrix.
754 17 Feb 07 jari 221
754 17 Feb 07 jari 222        Necessary memory for the new GSL matrix is allocated and the
754 17 Feb 07 jari 223        caller is responsible for freeing the allocated memory.
754 17 Feb 07 jari 224
754 17 Feb 07 jari 225        \return A pointer to a copy of the internal GSL matrix.
754 17 Feb 07 jari 226
754 17 Feb 07 jari 227        \throw GSL_error if memory cannot be allocated for the new
1822 24 Feb 09 jari 228        copy, or if dimensions mismatch.
754 17 Feb 07 jari 229     */
420 02 Dec 05 jari 230     gsl_matrix* create_gsl_matrix_copy(void) const;
12 19 Jun 03 daniel 231
42 26 Feb 04 jari 232   };
12 19 Jun 03 daniel 233
774 01 Mar 07 jari 234   /**
1121 22 Feb 08 peter 235      \brief Check if all elements of the Matrix are zero.
774 01 Mar 07 jari 236
1121 22 Feb 08 peter 237      \return True if all elements in the Matrix is zero, false
1822 24 Feb 09 jari 238      otherwise.
1887 31 Mar 09 peter 239
1887 31 Mar 09 peter 240      \relates Matrix
774 01 Mar 07 jari 241   */
4119 07 Nov 21 peter 242   bool isnull(const MatrixBase&);
774 01 Mar 07 jari 243
774 01 Mar 07 jari 244   /**
1121 22 Feb 08 peter 245      \brief Get the maximum value of the Matrix.
774 01 Mar 07 jari 246
1121 22 Feb 08 peter 247      \return The maximum value of the Matrix.
1887 31 Mar 09 peter 248
1887 31 Mar 09 peter 249      \relates Matrix
774 01 Mar 07 jari 250   */
4119 07 Nov 21 peter 251   double max(const MatrixBase&);
774 01 Mar 07 jari 252
774 01 Mar 07 jari 253   /**
1121 22 Feb 08 peter 254      \brief Get the minimum value of the Matrix.
774 01 Mar 07 jari 255
1121 22 Feb 08 peter 256      \return The minimum value of the Matrix.
1887 31 Mar 09 peter 257
1887 31 Mar 09 peter 258      \relates Matrix
774 01 Mar 07 jari 259   */
4119 07 Nov 21 peter 260   double min(const MatrixBase&);
774 01 Mar 07 jari 261
774 01 Mar 07 jari 262   /**
4252 18 Nov 22 peter 263      \brief Locate the maximum and minimum element in the matrix, \a
4252 18 Nov 22 peter 264      M.
774 01 Mar 07 jari 265
4252 18 Nov 22 peter 266      The indices to the element with the minimum and maximum values of
4252 18 Nov 22 peter 267      the matrix are returned through parameters \a min and \a max,
4252 18 Nov 22 peter 268      respectively.
774 01 Mar 07 jari 269
776 04 Mar 07 jari 270      \note Lower index has precedence (searching in row-major order).
1887 31 Mar 09 peter 271
4252 18 Nov 22 peter 272      \relates MatrixBase
774 01 Mar 07 jari 273   */
4252 18 Nov 22 peter 274   void minmax_index(const MatrixBase& M,
774 01 Mar 07 jari 275                     std::pair<size_t,size_t>& min,
774 01 Mar 07 jari 276                     std::pair<size_t,size_t>& max);
774 01 Mar 07 jari 277
774 01 Mar 07 jari 278   /**
4119 07 Nov 21 peter 279      \brief The output operator for the MatrixBase class.
774 01 Mar 07 jari 280
4119 07 Nov 21 peter 281      \relates MatrixBase
774 01 Mar 07 jari 282   */
4119 07 Nov 21 peter 283   std::ostream& operator<< (std::ostream& s, const MatrixBase&);
774 01 Mar 07 jari 284
774 01 Mar 07 jari 285   /**
2735 29 May 12 peter 286      \brief Trace of matrix
2735 29 May 12 peter 287
2735 29 May 12 peter 288      \relates Matrix
2735 29 May 12 peter 289
2735 29 May 12 peter 290      \return sum of diagonal elements
2735 29 May 12 peter 291
2735 29 May 12 peter 292      \since New in yat 0.9
2735 29 May 12 peter 293   */
4119 07 Nov 21 peter 294   double trace(const MatrixBase&);
2735 29 May 12 peter 295
687 16 Oct 06 jari 296 }}} // of namespace utility, yat, and theplu
12 19 Jun 03 daniel 297
420 02 Dec 05 jari 298 #endif