Biorbd
Vector.cpp
1 #define BIORBD_API_EXPORTS
2 #include "Utils/Vector.h"
3 
4 #include "Utils/Error.h"
5 #include "Utils/String.h"
6 #include "Utils/Vector3d.h"
7 
9  RigidBodyDynamics::Math::VectorNd()
10 {
11 
12 }
13 
15  unsigned int size) :
16  RigidBodyDynamics::Math::VectorNd(size)
17 {
18 
19 }
20 
22  const biorbd::utils::Vector& other) :
23  RigidBodyDynamics::Math::VectorNd (other)
24 {
25 
26 }
27 
29  const RigidBodyDynamics::Math::VectorNd &other) :
30  RigidBodyDynamics::Math::VectorNd (other)
31 {
32 
33 }
34 
36  const biorbd::utils::Vector3d& other) :
37  RigidBodyDynamics::Math::VectorNd (other)
38 {
39 
40 }
41 
42 #ifdef BIORBD_USE_CASADI_MATH
43 
44 biorbd::utils::Vector::Vector(const casadi::MX &other) :
45  RigidBodyDynamics::Math::VectorNd(other)
46 {
47 
48 }
49 
51  const RBDLCasadiMath::MX_Xd_SubMatrix &other) :
52  RigidBodyDynamics::Math::VectorNd (other)
53 {
54 
55 }
56 
57 #endif
58 
59 biorbd::utils::Scalar biorbd::utils::Vector::norm(
60  unsigned int p,
61  bool skipRoot) const
62 {
63  biorbd::utils::Error::check(p >= 2, "p must be superior or equal to 2");
64 
65  if (p == 2){
66  biorbd::utils::Scalar n = dot(*this);
67  if (skipRoot)
68  return n;
69  else
70  return std::sqrt(n);
71  } else {
72  biorbd::utils::Scalar res(0);
73  for(unsigned int i=0; i < size(); ++i)
74  res += std::pow(fabs((*this)[i]), p);
75  if (skipRoot)
76  return res;
77  else
78  return std::pow(res, 1.0/p);
79  }
80 }
81 
83  unsigned int p,
84  bool skipRoot)
85 {
86  biorbd::utils::Error::check(p >= 2, "p must be superior or equal to 2");
87 
88  if (p == 2){
89  if (skipRoot)
90  return biorbd::utils::Vector(*this * 2.);
91  else
92  return biorbd::utils::Vector(*this * 1.0/norm(2));
93  } else {
94  biorbd::utils::Vector res(static_cast<unsigned int>(size()));
95  double normalized(std::pow(norm(), p-1));
96  for (unsigned int i=0; i<size(); ++i)
97  res[i] = (*this)[i] * std::pow(fabs((*this)[i]), p - 2);
98  res /= normalized;
99  if (skipRoot)
100  biorbd::utils::Error::raise("skip root not implemented for p > 2");
101  return res;
102  }
103 }
104 
106  const biorbd::utils::Vector &other)
107 {
108  this->RigidBodyDynamics::Math::VectorNd::operator=(other);
109 }
110 
111 #ifdef BIORBD_USE_CASADI_MATH
112 
114  const RBDLCasadiMath::MX_Xd_SubMatrix& other)
115 {
116  this->MX_Xd_dynamic::operator=(other);
117 }
118 
120  const casadi::MX &other)
121 {
122  this->MX_Xd_dynamic::operator=(other);
123 }
124 
125 #endif
biorbd::utils::Vector::operator=
void operator=(const biorbd::utils::Vector &other)
operator= For submatrices
Definition: Vector.cpp:105
biorbd::utils::Vector::normGradient
biorbd::utils::Vector normGradient(unsigned int p=2, bool skipRoot=false)
Return the gradient of the p-norm.
Definition: Vector.cpp:82
biorbd::utils::Vector
Wrapper of the Eigen VectorXd.
Definition: Vector.h:20
biorbd::utils::Vector3d
Wrapper around Eigen Vector3d and attach it to a parent.
Definition: Vector3d.h:24
biorbd::utils::Error::raise
static void raise(const biorbd::utils::String &message)
Throw an error message.
Definition: Error.cpp:4
biorbd::utils::Vector::Vector
Vector()
Construct vector.
Definition: Vector.cpp:8
biorbd::utils::Vector::norm
biorbd::utils::Scalar norm(unsigned int p=2, bool skipRoot=false) const
Return the Euclidian p-norm of the vector.
Definition: Vector.cpp:59
biorbd::utils::Error::check
static void check(bool cond, const biorbd::utils::String &message)
Assert that raises the error message if false.
Definition: Error.cpp:10