1#ifndef THEORETICA_MULTIDUAL_H
2#define THEORETICA_MULTIDUAL_H
9#ifndef THEORETICA_NO_PRINT
25 template<
unsigned int N = 0>
38 static constexpr size_t size_argument = N;
164 if (
v.size() == other.
v.size()) {
173 if (other.
v.size() == 0) {
178 const unsigned int new_size =
th::max(
v.size(), other.
v.size());
182 for (
unsigned int i = 0; i < res_v.
size(); ++i) {
183 real lhs = (i < other.
v.size()) ? other.
v[i] : 0.0;
184 real rhs = (i <
v.size()) ?
v[i] : 0.0;
185 res_v[i] = lhs *
a + rhs * other.
a;
206 const real res_a =
a / other.
a;
207 const real denom = other.
a * other.
a;
210 if (
v.size() == other.
v.size()) {
211 return multidual(res_a, (
v * other.
a - other.
v *
a) / denom);
219 if (other.
v.size() == 0) {
224 const unsigned int new_size =
th::max(
v.size(), other.
v.size());
228 for (
unsigned int i = 0; i < res_v.
size(); ++i) {
230 real lhs = (i <
v.size()) ?
v[i] : 0.0;
231 real rhs = (i < other.
v.size()) ? other.
v[i] : 0.0;
233 res_v[i] = (lhs * other.
a - rhs *
a) / denom;
279 const real a_old =
a;
282 if (
v.size() == other.
v.size()) {
284 v = other.
v * a_old +
v * other.
a;
295 if (other.
v.size() == 0) {
302 const unsigned int new_size =
th::max(
v.size(), other.
v.size());
306 for (
unsigned int i = 0; i < new_v.
size(); ++i) {
308 real lhs = (i < other.
v.size()) ? other.
v[i] : 0.0;
309 real rhs = (i <
v.size()) ?
v[i] : 0.0;
311 new_v[i] = lhs * a_old + rhs * other.
a;
331 const real a_old =
a;
332 const real denom = other.
a * other.
a;
335 if (
v.size() == other.
v.size()) {
337 v = (
v * other.
a - other.
v * a_old) / denom;
344 v = other.
v * (-a_old / denom);
348 if (other.
v.size() == 0) {
355 const unsigned int new_size =
th::max(
v.size(), other.
v.size());
359 for (
unsigned int i = 0; i < new_v.
size(); ++i) {
361 real lhs = (i <
v.size()) ?
v[i] : 0.0;
362 real rhs = (i < other.
v.size()) ? other.
v[i] : 0.0;
364 new_v[i] = (lhs * other.
a - rhs * a_old) / denom;
393 return (
a == other.
a) && (
v == other.
v);
400 template<
typename Vector = vec<real, N>, enable_vector<Vector> = true>
406 for (
unsigned int i = 0; i < x.size(); ++i)
423 for (
unsigned int i = 0; i < N; ++i)
439 for (
unsigned int i = 0; i < N; ++i)
440 for (
unsigned int j = 0; j < N; ++j)
441 J(j, i) =
v[j].Dual(i);
455 for (
unsigned int i = 0; i < N; ++i) {
457 for (
unsigned int j = 0; j < N; ++j)
458 J(j, i) =
v[j].Dual(i);
495 return a * d.inverse();
499#ifndef THEORETICA_NO_PRINT
503 inline std::string
to_string(
const std::string& epsilon =
"ε")
const {
505 std::stringstream res;
506 res <<
a <<
" + " <<
v << epsilon;
513 inline operator std::string() {
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
mat< Type, N, K > resize(unsigned int n, unsigned int k)
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:730
Multidual number algebra for functions of the form .
Definition multidual.h:26
multidual operator+(const multidual &other) const
Definition multidual.h:131
real & Dual(unsigned int i)
Definition multidual.h:101
multidual operator+() const
Definition multidual.h:125
multidual operator/(const multidual &other) const
Definition multidual.h:199
bool operator==(const multidual &other)
Definition multidual.h:392
multidual & operator*=(real r)
Definition multidual.h:321
multidual & operator-=(real r)
Definition multidual.h:270
multidual & operator*=(const multidual &other)
Definition multidual.h:277
multidual & operator/=(const multidual &other)
Definition multidual.h:329
multidual & operator+=(const multidual &other)
Definition multidual.h:246
real Dual(unsigned int i) const
Definition multidual.h:93
vec< real, N > v
Definition multidual.h:35
multidual & operator-=(const multidual &other)
Definition multidual.h:262
friend std::ostream & operator<<(std::ostream &out, const multidual &obj)
Definition multidual.h:520
unsigned int size() const
Definition multidual.h:467
multidual operator-(real r) const
Definition multidual.h:155
multidual operator*(const multidual &other) const
Definition multidual.h:161
multidual & operator+=(real r)
Definition multidual.h:255
multidual operator*(real r) const
Definition multidual.h:193
multidual(real r, vec< real, N > u)
Definition multidual.h:48
real a
Definition multidual.h:31
multidual operator/(real r) const
Definition multidual.h:240
multidual & operator/=(real r)
Definition multidual.h:374
std::string to_string(const std::string &epsilon="ε") const
Definition multidual.h:503
vec< real, N > & Dual()
Definition multidual.h:85
static vec< real, N > extract_real(const vec< multidual< N >, N > &v)
Definition multidual.h:417
vec< real, N > Dual() const
Definition multidual.h:79
multidual(real r)
Definition multidual.h:53
static vec< multidual< N >, N > make_argument(const Vector &x)
Definition multidual.h:401
real Re() const
Definition multidual.h:67
static mat< real, N, N > extract_dual(const vec< multidual< N >, N > &v)
Definition multidual.h:433
multidual inverse() const
Definition multidual.h:113
multidual operator+(real r) const
Definition multidual.h:137
multidual operator-() const
Definition multidual.h:143
static void extract(const vec< multidual< N >, N > &v, vec< real, N > &x, mat< real, N, N > &J)
Definition multidual.h:450
void resize(unsigned int size)
Definition multidual.h:474
multidual conjugate() const
Definition multidual.h:107
real & Re()
Definition multidual.h:73
multidual()
Construct a multidual number as .
Definition multidual.h:43
multidual & operator=(real x)
Definition multidual.h:59
multidual operator-(const multidual &other) const
Definition multidual.h:149
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:459
static vec< Type, N > euclidean_base(unsigned int i, unsigned int n=N)
Returns an N-dimensional euclidean base unit vector with the i-th element set to 1.
Definition vec.h:484
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N).
Definition vec.h:449
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Matrix class and operations.
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
double real
A real number, defined as a floating point type.
Definition constants.h:207
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:326
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ DivByZero
Division by zero.
Definition error.h:27
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
Vector class and operations.