nonzeros.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_NONZEROS_HPP
27 #define CASADI_NONZEROS_HPP
28 
29 namespace casadi {
30 
31 
39 template<typename M, typename K>
40 class NonZeros : public M {
41  public:
43  NonZeros(M& mat, const K& k) : mat_(mat), k_(k) { mat.get_nz(*this, false, k); }
44 
46  NonZeros(const NonZeros<M, K> &y) = default;
47 
50  const M& operator=(const NonZeros<M, K> &y);
51  const M& operator=(const M &y);
52  M operator+=(const M &y);
53  M operator-=(const M &y);
54  M operator*=(const M &y);
55  M operator/=(const M &y);
57 
58  private:
60  M& mat_;
61 
63  K k_;
64 };
65 
66 // Implementation
67 template<typename M, typename K>
69  mat_.set_nz(y, false, k_);
70  return y;
71 }
72 
73 // Implementation
74 template<typename M, typename K>
75 const M& NonZeros<M, K>::operator=(const M &y) {
76  mat_.set_nz(y, false, k_);
77  return y;
78 }
79 
80 template<typename M, typename K>
82  M s = *this+y;
83  mat_.set_nz(s, false, k_);
84  return s;
85 }
86 
87 template<typename M, typename K>
89  M s = *this-y;
90  mat_.set_nz(s, false, k_);
91  return s;
92 }
93 
94 template<typename M, typename K>
96  M s = *this*y;
97  mat_.set_nz(s, false, k_);
98  return s;
99 }
100 
101 template<typename M, typename K>
103  M s = *this/y;
104  mat_.set_nz(s, false, k_);
105  return s;
106 }
107 
108 } // namespace casadi
109 
110 
111 #endif // CASADI_NONZEROS_HPP
Access to a set of nonzeros.
Definition: nonzeros.hpp:40
M operator*=(const M &y)
Definition: nonzeros.hpp:95
const M & operator=(const NonZeros< M, K > &y)
Definition: nonzeros.hpp:68
M operator+=(const M &y)
Definition: nonzeros.hpp:81
NonZeros(const NonZeros< M, K > &y)=default
Default copy constructor.
NonZeros(M &mat, const K &k)
Constructor.
Definition: nonzeros.hpp:43
M operator-=(const M &y)
Definition: nonzeros.hpp:88
M operator/=(const M &y)
Definition: nonzeros.hpp:102
The casadi namespace.