DACE 2.0 API Manual
Differential Algebra Core Engine
AlgebraicMatrix.h
Go to the documentation of this file.
1 /******************************************************************************
2 * *
3 * DIFFERENTIAL ALGEBRA CORE ENGINE *
4 * *
5 *******************************************************************************
6 * *
7 * Copyright 2016 Politecnico di Milano (2014 Dinamica Srl) *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *******************************************************************************/
21 
22 /*
23  * AlgebraicMatrix.h
24  *
25  * Created on: July 17, 2014
26  * Author: Dinamica Srl
27  */
28 
29 #ifndef DINAMICA_DAMATRIX_H_
30 #define DINAMICA_DAMATRIX_H_
31 
32 // C++ stdlib classes required for interface definition
33 #include <vector>
34 #include <iostream>
35 
36 // DACE classes required for interface definition
37 #include "dace/PromotionTrait.h"
38 
39 namespace DACE{
40 
41 // forward declarations
42 class DA;
43 template<typename T> class AlgebraicVector;
44 
46 template <class T> class AlgebraicMatrix
47 {
48 public:
49  /***********************************************************************************
50  * Constructors & Destructors
51  ************************************************************************************/
52  AlgebraicMatrix() : _nrows(0), _ncols(0) {};
53 
58  explicit AlgebraicMatrix(const int size) : _nrows(size), _ncols(size), _data(size*size,0.0) { };
59 
65  AlgebraicMatrix(const int nrows, const int ncols) : _nrows(nrows), _ncols(ncols), _data(nrows*ncols,0.0) { };
66 
73  AlgebraicMatrix(const int nrows, const int ncols, const T &d) : _nrows(nrows), _ncols(ncols), _data(nrows*ncols, d) { };
74 
75  /***********************************************************************************
76  * Output number of rows, columns, and size
77  ************************************************************************************/
82  unsigned int ncols() const { return this->_ncols; };
83 
88  unsigned int nrows() const { return this->_nrows; };
89 
94  unsigned int size() const { return this->_data.size(); };
95 
96  void resize(int size);
97  void resize(int rows, int cols);
98 
99  /***********************************************************************************
100  * Element access routine
101  ************************************************************************************/
102  T& at(const unsigned int irow, const unsigned int icol);
103  const T& at(const unsigned int irow, const unsigned int icol) const;
104 
105  std::vector<T> getrow(const unsigned int irow) const;
106  std::vector<T> getcol(const unsigned int icol) const;
107 
108  void setrow(const unsigned int irow, const std::vector<T> &obj);
109  void setcol(const unsigned int icol, const std::vector<T> &obj);
110 
111  AlgebraicMatrix<T> submat(const unsigned int first_row, const unsigned int first_col, const unsigned int last_row, const unsigned int last_col) const;
112  AlgebraicMatrix<T> submat(const unsigned int last_row, const unsigned int last_col) const;
113 
114  /***********************************************************************************
115  * Matrix operations
116  ************************************************************************************/
117  AlgebraicMatrix<T> transpose() const;
118  T det() const;
119  AlgebraicMatrix<T> inv() const;
120 
121  /***********************************************************************************
122  * Coefficient access routines
123  ************************************************************************************/
124  AlgebraicMatrix<double> cons() const;
125 
126 private:
127  unsigned int _nrows;
128  unsigned int _ncols;
129  std::vector<T> _data;
130 
131  static unsigned int pivot(unsigned int& k, const unsigned int ii, const AlgebraicMatrix<T>& A, std::vector<unsigned int>& P, std::vector<unsigned int>& R, std::vector<unsigned int>& C1, std::vector<unsigned int >& C2, T& det);
132  static void eliminate(const unsigned int k, AlgebraicMatrix<T>& A, std::vector<unsigned int>& R);
133 };
134 
135 /***********************************************************************************
136  * Operators
137  ************************************************************************************/
138 template<typename U> std::ostream& operator<<(std::ostream &out, const AlgebraicMatrix<U> &obj);
139 template<> DACE_API std::ostream& operator<<(std::ostream &out, const AlgebraicMatrix<DA> &obj);
140 template<typename U> std::istream& operator>>(std::istream &in, AlgebraicMatrix<U> &obj);
141 template<> DACE_API std::istream& operator>>(std::istream &in, AlgebraicMatrix<DA> &obj);
142 
143 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator+( const AlgebraicMatrix<U> &obj1, const AlgebraicMatrix<V> &obj2);
144 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator+( const AlgebraicMatrix<U> &obj1, const V &obj2 );
145 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator+( const U &obj1, const AlgebraicMatrix<V> &obj2 );
146 
147 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator-( const AlgebraicMatrix<U> &obj1, const AlgebraicMatrix<V> &obj2);
148 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator-( const AlgebraicMatrix<U> &obj1, const V &obj2 );
149 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator-( const U &obj1, const AlgebraicMatrix<V> &obj2 );
150 
151 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator*( const AlgebraicMatrix<U> &obj1, const AlgebraicMatrix<V> &obj2);
152 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator*( const AlgebraicMatrix<U> &obj1, const V &obj2 );
153 template<typename U,typename V> AlgebraicMatrix<typename PromotionTrait< U, V >::returnType> operator*( const U &obj1, const AlgebraicMatrix<V> &obj2 );
154 template<typename U,typename V> AlgebraicVector<typename PromotionTrait< U, V >::returnType> operator*( const AlgebraicVector<U> &obj1, const AlgebraicMatrix<V> &obj2 );
155 template<typename U,typename V> AlgebraicVector<typename PromotionTrait< U, V >::returnType> operator*( const AlgebraicMatrix<U> &obj1, const AlgebraicVector<V> &obj2 );
156 
157 /***********************************************************************************
158  * Functional style wrappers
159  ************************************************************************************/
160 template<class T> AlgebraicMatrix<T> transpose(const AlgebraicMatrix<T> &obj);
161 template<class T> T det(const AlgebraicMatrix<T> &obj);
162 template<class T> AlgebraicMatrix<T> inv(const AlgebraicMatrix<T> &obj);
163 template<class T> AlgebraicMatrix<double> cons(const AlgebraicMatrix<T> &obj);
164 
165 /***********************************************************************************
166  * Type definitions
167  ************************************************************************************/
170 
171 }
172 
173 #endif /* AlgebraicMatrix_H_ */
std::istream & operator>>(std::istream &in, AlgebraicMatrix< DA > &obj)
DA specialization of input stream operator.
Definition: AlgebraicMatrix.cpp:65
unsigned int ncols() const
Definition: AlgebraicMatrix.h:82
AlgebraicMatrix(const int nrows, const int ncols)
Definition: AlgebraicMatrix.h:65
T det() const
Matrix determinant.
Definition: AlgebraicMatrix_t.h:605
DA operator*(const DA &da1, const DA &da2)
Definition: DA.cpp:759
DA operator-(const DA &da1, const DA &da2)
Definition: DA.cpp:714
AlgebraicMatrix< double > cons() const
Return the costant part of a AlgebraicMatrix<T>
Definition: AlgebraicMatrix_t.h:639
AlgebraicMatrix< double > matrixdb
Short for AlgebraicMatrix<double>
Definition: AlgebraicMatrix.h:169
AlgebraicMatrix(const int nrows, const int ncols, const T &d)
Definition: AlgebraicMatrix.h:73
void resize(int size)
Definition: AlgebraicMatrix_t.h:90
Definition: AlgebraicMatrix.h:43
AlgebraicMatrix< T > transpose() const
Matrix transpose.
Definition: AlgebraicMatrix_t.h:479
Definition: AlgebraicMatrix.h:46
std::vector< T > getcol(const unsigned int icol) const
Reading column.
Definition: AlgebraicMatrix_t.h:192
void setrow(const unsigned int irow, const std::vector< T > &obj)
Set row equal to std::vector.
Definition: AlgebraicMatrix_t.h:215
AlgebraicMatrix()
Definition: AlgebraicMatrix.h:52
void setcol(const unsigned int icol, const std::vector< T > &obj)
Set column equal to std::vector.
Definition: AlgebraicMatrix_t.h:230
T & at(const unsigned int irow, const unsigned int icol)
Reading/Writing single element.
Definition: AlgebraicMatrix_t.h:143
#define DACE_API
Definition: dace_s.h:33
Definition: AlgebraicMatrix.cpp:39
AlgebraicMatrix(const int size)
Default Constructor.
Definition: AlgebraicMatrix.h:58
AlgebraicMatrix< DA > matrixDA
Short for AlgebraicMatrix<DA>
Definition: AlgebraicMatrix.h:168
AlgebraicMatrix< T > inv() const
Matrix inverse XXX: name.
Definition: AlgebraicMatrix_t.h:565
AlgebraicMatrix< T > submat(const unsigned int first_row, const unsigned int first_col, const unsigned int last_row, const unsigned int last_col) const
Extract submatrix.
Definition: AlgebraicMatrix_t.h:245
std::vector< T > getrow(const unsigned int irow) const
Reading row.
Definition: AlgebraicMatrix_t.h:169
DA operator+(const DA &da1, const DA &da2)
Definition: DA.cpp:669
unsigned int size() const
Definition: AlgebraicMatrix.h:94
unsigned int nrows() const
Definition: AlgebraicMatrix.h:88