submatrix.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_SUBMATRIX_HPP
27 #define CASADI_SUBMATRIX_HPP
28 
29 namespace casadi {
30 
31 
37  template<typename M, typename I, typename J>
38  class SubMatrix : public M {
39  private:
41  M& mat_;
42 
44  I i_;
45  J j_;
46  public:
48  SubMatrix(M& mat, const I& i, const J& j) : mat_(mat), i_(i), j_(j) {
49  mat.get(*this, false, i, j);
50  }
51 
53  SubMatrix(const SubMatrix<M, I, J> &y) = default;
54 
57  inline const M& operator=(const SubMatrix<M, I, J> &y) {
58  mat_.set(y, false, i_, j_);
59  return y;
60  }
61 
62  inline const M& operator=(const M &y) {
63  mat_.set(y, false, i_, j_);
64  return y;
65  }
66 
67  inline M operator+=(const M &y) {
68  M s = *this+y;
69  mat_.set(s, false, i_, j_);
70  return s;
71  }
72 
73  inline M operator-=(const M &y) {
74  M s = *this-y;
75  mat_.set(s, false, i_, j_);
76  return s;
77  }
78 
79  inline M operator*=(const M &y) {
80  M s = *this*y;
81  mat_.set(s, false, i_, j_);
82  return s;
83  }
84 
85  inline M operator/=(const M &y) {
86  M s = *this/y;
87  mat_.set(s, false, i_, j_);
88  return s;
89  }
91  };
92 
98  template<typename M, typename I>
99  class SubIndex : public M {
100  private:
102  M& mat_;
103 
105  I i_;
106  public:
108  SubIndex(M& mat, const I& i) : mat_(mat), i_(i) {
109  mat.get(*this, false, i);
110  }
111 
113  SubIndex(const SubIndex<M, I> &y) = default;
114 
117  inline const M& operator=(const SubIndex<M, I> &y) {
118  mat_.set(y, false, i_);
119  return y;
120  }
121 
122  inline const M& operator=(const M &y) {
123  mat_.set(y, false, i_);
124  return y;
125  }
126 
127  inline M operator+=(const M &y) {
128  M s = *this+y;
129  mat_.set(s, false, i_);
130  return s;
131  }
132 
133  inline M operator-=(const M &y) {
134  M s = *this-y;
135  mat_.set(s, false, i_);
136  return s;
137  }
138 
139  inline M operator*=(const M &y) {
140  M s = *this*y;
141  mat_.set(s, false, i_);
142  return s;
143  }
144 
145  inline M operator/=(const M &y) {
146  M s = *this/y;
147  mat_.set(s, false, i_);
148  return s;
149  }
151  };
152 
153 } // namespace casadi
154 
155 
156 #endif // CASADI_SUBMATRIX_HPP
const M & operator=(const M &y)
Definition: submatrix.hpp:122
M operator-=(const M &y)
Definition: submatrix.hpp:133
M operator*=(const M &y)
Definition: submatrix.hpp:139
SubIndex(const SubIndex< M, I > &y)=default
Default copy constructor.
M operator/=(const M &y)
Definition: submatrix.hpp:145
const M & operator=(const SubIndex< M, I > &y)
Definition: submatrix.hpp:117
SubIndex(M &mat, const I &i)
Constructor.
Definition: submatrix.hpp:108
M operator+=(const M &y)
Definition: submatrix.hpp:127
const M & operator=(const SubMatrix< M, I, J > &y)
Definition: submatrix.hpp:57
M operator-=(const M &y)
Definition: submatrix.hpp:73
M operator/=(const M &y)
Definition: submatrix.hpp:85
SubMatrix(const SubMatrix< M, I, J > &y)=default
Default copy constructor.
const M & operator=(const M &y)
Definition: submatrix.hpp:62
SubMatrix(M &mat, const I &i, const J &j)
Constructor.
Definition: submatrix.hpp:48
M operator*=(const M &y)
Definition: submatrix.hpp:79
M operator+=(const M &y)
Definition: submatrix.hpp:67
The casadi namespace.