Rigid Body Dynamics Library
MX_Xd_dynamic.h
Go to the documentation of this file.
1/*
2 * RBDL - Rigid Body Dynamics Library
3 * Copyright (c) 2011-2018 Martin Felis <martin@fysx.org>
4 *
5 * Licensed under the zlib license. See LICENSE for more details.
6 */
7
8#ifndef MX_XD_DYNAMICS_H
9#define MX_XD_DYNAMICS_H
10
11#include <vector>
12#include <string>
13#include <memory>
14
15#include <casadi.hpp>
16#include "MX_Xd_scalar.h"
17#include "MX_Xd_subMatrix.h"
18
19namespace RBDLCasadiMath {
20
21class MX_Xd_dynamic : public casadi::MX{
22public:
24 unsigned int nrows = 1,
25 unsigned int ncols = 1) : casadi::MX(nrows, ncols){
26 }
27
28 virtual ~MX_Xd_dynamic(){
29
30 }
31
32
33 MX_Xd_dynamic(const casadi::MX& m) :
34 casadi::MX(m){
35 }
36
37 void conservativeResize (unsigned int nrows, unsigned int ncols = 1) {
38 MX_Xd_dynamic result = casadi::MX::zeros(nrows, ncols);
39
40 unsigned int arows = std::min (nrows, rows());
41 unsigned int acols = std::min (ncols, cols());
42
43 for (unsigned int i = 0; i < arows; i++) {
44 for (unsigned int j = 0; j < acols; j++) {
45 result(i,j) = (*this)(i,j);
46 }
47 }
48
49 *this = result;
50 }
51
52 static MX_Xd_dynamic Zero(unsigned int nrows, unsigned int ncols = 1){
53 return casadi::MX::zeros(nrows, ncols);
54 }
56 *this = casadi::MX::zeros(this->rows(), this->cols());
57 return *this;
58 }
59
60 static MX_Xd_dynamic One(unsigned int nrows, unsigned int ncols = 1){
61 return casadi::MX::ones(nrows, ncols);
62 }
64 *this = casadi::MX::ones(this->rows(), this->cols());
65 return *this;
66 }
67
68 static MX_Xd_dynamic Identity(unsigned int size, unsigned int ignoredSize = 0){
69 return casadi::MX::eye(size);
70 }
72 *this = casadi::MX::eye(cols());
73 return *this;
74 }
75
76 MX_Xd_SubMatrix operator[](unsigned int i) {
77 return (*this)(i, 0);
78 }
79 MX_Xd_scalar operator[](unsigned int i) const {
80 return (*this)(i, 0);
81 }
82 MX_Xd_SubMatrix operator()(unsigned int i, unsigned int j=0) {
83 return this->casadi::MX::operator()(
84 casadi::Slice(static_cast<casadi_int>(i), static_cast<casadi_int>(i+1)),
85 casadi::Slice(static_cast<casadi_int>(j), static_cast<casadi_int>(j+1)));
86 }
87 MX_Xd_scalar operator()(unsigned int i, unsigned int j=0) const {
88 return this->casadi::MX::operator()(i, j);
89 }
90
91 void resize(unsigned int newI, unsigned int newJ = 1){
92 *this = casadi::MX(newI, newJ);
93 }
94
95 unsigned int rows() const {
96 return static_cast<unsigned int>(this->casadi::MX::rows());
97 }
98
99 unsigned int cols() const {
100 return static_cast<unsigned int>(this->casadi::MX::columns());
101 }
102
103 unsigned int size() const {
104 return rows() * cols();
105 }
106
107 template <unsigned int row_count, unsigned int col_count>
109 unsigned int row_start,
110 unsigned int col_start)
111 {
112 return this->casadi::MX::operator()(
113 casadi::Slice(static_cast<casadi_int>(row_start), static_cast<casadi_int>(row_start+row_count)),
114 casadi::Slice(static_cast<casadi_int>(col_start), static_cast<casadi_int>(col_start+col_count)));
115 }
116 template <unsigned int row_count, unsigned int col_count>
118 unsigned int row_start,
119 unsigned int col_start) const
120 {
121 return this->casadi::MX::operator()(
122 casadi::Slice(static_cast<casadi_int>(row_start), static_cast<casadi_int>(row_start+row_count)),
123 casadi::Slice(static_cast<casadi_int>(col_start), static_cast<casadi_int>(col_start+col_count)));
124 }
125
127 unsigned int row_start,
128 unsigned int col_start,
129 unsigned int row_count,
130 unsigned int col_count)
131 {
132 return this->casadi::MX::operator()(
133 casadi::Slice(static_cast<casadi_int>(row_start), static_cast<casadi_int>(row_start+row_count)),
134 casadi::Slice(static_cast<casadi_int>(col_start), static_cast<casadi_int>(col_start+col_count)));
135 }
137 unsigned int row_start,
138 unsigned int col_start,
139 unsigned int row_count,
140 unsigned int col_count) const
141 {
142 return this->casadi::MX::operator()(
143 casadi::Slice(static_cast<casadi_int>(row_start), static_cast<casadi_int>(row_start+row_count)),
144 casadi::Slice(static_cast<casadi_int>(col_start), static_cast<casadi_int>(col_start+col_count)));
145 }
146
148 unsigned int numberRows)
149 {
150 return (*this).block(0, 0, numberRows, cols());
151 }
153 unsigned int numberRows) const
154 {
155 return (*this).block(0, 0, numberRows, cols());
156 }
157
158
160 return T();
161 }
162
164 return inv(*this);
165 }
166
167 MX_Xd_scalar dot(const MX_Xd_dynamic &other_vector) const {
168 return casadi::MX::dot(*this, other_vector);
169 }
170
172 return casadi::MX::norm_2(*this);
173 }
174
175 void normalize() {
176 *this /= norm();
177 }
178
180 return norm() * norm();
181 }
182
184 const MX_Xd_dynamic& other) {
185 this->casadi::MX::operator+=(other);
186 }
188 const MX_Xd_dynamic& other) const {
189 MX_Xd_dynamic out(*this);
190 return out.casadi::MX::operator+=(other);
191 }
193 const MX_Xd_dynamic& other) {
194 this->casadi::MX::operator-=(other);
195 }
197 const MX_Xd_dynamic& other) const {
198 MX_Xd_dynamic out(*this);
199 return out.casadi::MX::operator-=(other);
200 }
202 const MX_Xd_dynamic& m2) {
203 *this = casadi::MX::mtimes(*this, m2);
204 }
206 return casadi::MX::mtimes(*this, other);
207 }
209 return casadi::MX::mtimes(*this, other);
210 }
212 return casadi::MX::mtimes(*this, other);
213 }
214 MX_Xd_dynamic operator*(const double& other) const {
215 return casadi::MX::mtimes(*this, other);
216 }
217
218 void operator/=(const MX_Xd_scalar &scalar) {
219 for (unsigned int i = 0; i < rows() * cols(); i++)
220 this->casadi::MX::operator/=(scalar(0, 0));
221 }
222 MX_Xd_dynamic operator/(const MX_Xd_scalar &scalar) const {
223 MX_Xd_dynamic result (*this);
224 for (unsigned int i = 0; i < rows(); ++i){
225 for (unsigned int j = 0; j < cols(); ++j){
226 result(i, j) /= scalar;
227 }
228 }
229 return result;
230 }
232 const MX_Xd_SubMatrix& scalar) const {
233 MX_Xd_dynamic result (*this);
234 for (unsigned int i = 0; i < rows(); ++i){
235 for (unsigned int j = 0; j < cols(); ++j){
236 result(i, j) /= scalar;
237 }
238 }
239 return result;
240 }
241};
242
243}
244
245/* MX_XD_DYNAMICS_H */
246#endif
247
MX_Xd_dynamic transpose() const
unsigned int rows() const
Definition: MX_Xd_dynamic.h:95
MX_Xd_dynamic(const casadi::MX &m)
Definition: MX_Xd_dynamic.h:33
MX_Xd_dynamic operator*(const MX_Xd_SubMatrix &other) const
unsigned int cols() const
Definition: MX_Xd_dynamic.h:99
MX_Xd_dynamic operator*(const MX_Xd_dynamic &other) const
MX_Xd_dynamic squaredNorm() const
void resize(unsigned int newI, unsigned int newJ=1)
Definition: MX_Xd_dynamic.h:91
MX_Xd_dynamic(unsigned int nrows=1, unsigned int ncols=1)
Definition: MX_Xd_dynamic.h:23
MX_Xd_SubMatrix topRows(unsigned int numberRows)
MX_Xd_dynamic topRows(unsigned int numberRows) const
MX_Xd_dynamic norm() const
MX_Xd_scalar dot(const MX_Xd_dynamic &other_vector) const
void operator+=(const MX_Xd_dynamic &other)
MX_Xd_dynamic operator/(const MX_Xd_scalar &scalar) const
MX_Xd_dynamic operator*(const double &other) const
MX_Xd_dynamic block(unsigned int row_start, unsigned int col_start, unsigned int row_count, unsigned int col_count) const
MX_Xd_dynamic block(unsigned int row_start, unsigned int col_start) const
MX_Xd_scalar operator[](unsigned int i) const
Definition: MX_Xd_dynamic.h:79
unsigned int size() const
MX_Xd_SubMatrix operator()(unsigned int i, unsigned int j=0)
Definition: MX_Xd_dynamic.h:82
void operator*=(const MX_Xd_dynamic &m2)
static MX_Xd_dynamic Identity(unsigned int size, unsigned int ignoredSize=0)
Definition: MX_Xd_dynamic.h:68
void conservativeResize(unsigned int nrows, unsigned int ncols=1)
Definition: MX_Xd_dynamic.h:37
void operator/=(const MX_Xd_scalar &scalar)
MX_Xd_dynamic & setIdentity()
Definition: MX_Xd_dynamic.h:71
MX_Xd_SubMatrix block(unsigned int row_start, unsigned int col_start, unsigned int row_count, unsigned int col_count)
MX_Xd_dynamic operator*(const MX_Xd_scalar &other) const
MX_Xd_SubMatrix operator[](unsigned int i)
Definition: MX_Xd_dynamic.h:76
MX_Xd_dynamic inverse() const
MX_Xd_dynamic operator/(const MX_Xd_SubMatrix &scalar) const
static MX_Xd_dynamic One(unsigned int nrows, unsigned int ncols=1)
Definition: MX_Xd_dynamic.h:60
void operator-=(const MX_Xd_dynamic &other)
MX_Xd_scalar operator()(unsigned int i, unsigned int j=0) const
Definition: MX_Xd_dynamic.h:87
MX_Xd_dynamic operator+(const MX_Xd_dynamic &other) const
static MX_Xd_dynamic Zero(unsigned int nrows, unsigned int ncols=1)
Definition: MX_Xd_dynamic.h:52
MX_Xd_dynamic operator-(const MX_Xd_dynamic &other) const
MX_Xd_SubMatrix block(unsigned int row_start, unsigned int col_start)