1#ifndef THEORETICA_MULTIDUAL_H
2#define THEORETICA_MULTIDUAL_H
9#ifndef THEORETICA_NO_PRINT
14#include "../algebra/vec.h"
15#include "../algebra/mat.h"
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) {
182 for (
unsigned int i = 0; i <
res_v.size(); ++i) {
210 if (
v.size() ==
other.v.size()) {
219 if (
other.v.size() == 0) {
228 for (
unsigned int i = 0; i <
res_v.size(); ++i) {
282 if (
v.size() ==
other.v.size()) {
295 if (
other.v.size() == 0) {
306 for (
unsigned int i = 0; i <
new_v.size(); ++i) {
335 if (
v.size() ==
other.v.size()) {
348 if (
other.v.size() == 0) {
359 for (
unsigned int i = 0; i <
new_v.size(); ++i) {
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);
499#ifndef THEORETICA_NO_PRINT
505 std::stringstream
res;
513 inline operator std::string() {
521 return out <<
obj.to_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
Sum two multidual numbers.
Definition multidual.h:131
real & Dual(unsigned int i)
Access the i-th element of the multidual part, corresponding to the i-th independent variable in auto...
Definition multidual.h:101
multidual operator+() const
Identity (for consistency)
Definition multidual.h:125
multidual operator/(const multidual &other) const
Dual division.
Definition multidual.h:199
bool operator==(const multidual &other)
Check whether two multidual numbers have the same real and multidual parts.
Definition multidual.h:392
multidual & operator*=(real r)
Multiply this multidual number by a real number.
Definition multidual.h:321
multidual & operator-=(real r)
Subtract a real number from this multidual number.
Definition multidual.h:270
multidual & operator*=(const multidual &other)
Multiply this multidual number by another one.
Definition multidual.h:277
multidual & operator/=(const multidual &other)
Divide this multidual number by another one.
Definition multidual.h:329
multidual & operator+=(const multidual &other)
Sum a real number to this one.
Definition multidual.h:246
real Dual(unsigned int i) const
Get the i-th element of the multidual part, corresponding to the i-th independent variable in automat...
Definition multidual.h:93
vec< real, N > v
The dual part of the multidimensional dual number as a real vector.
Definition multidual.h:35
multidual & operator-=(const multidual &other)
Subtract a real number from this one.
Definition multidual.h:262
friend std::ostream & operator<<(std::ostream &out, const multidual &obj)
Stream the multidual number in string representation to an output stream (std::ostream)
Definition multidual.h:520
unsigned int size() const
Get the number of independent variables associated with the multidual number.
Definition multidual.h:467
multidual operator-(real r) const
Subtract a real number from a multidual number.
Definition multidual.h:155
multidual operator*(const multidual &other) const
Multiply two multidual numbers.
Definition multidual.h:161
multidual & operator+=(real r)
Sum a real number to this multidual number.
Definition multidual.h:255
multidual operator*(real r) const
Multiply a multidual number by a real number.
Definition multidual.h:193
multidual(real r, vec< real, N > u)
Construct a multidual number from a real number and an N dimensional vector.
Definition multidual.h:48
real a
The real part of the multidimensional dual number.
Definition multidual.h:31
multidual operator/(real r) const
Divide a multidual number by a real number.
Definition multidual.h:240
multidual & operator/=(real r)
Divide a multidual number by a real number.
Definition multidual.h:374
std::string to_string(const std::string &epsilon="ε") const
Convert the multidual number to string representation.
Definition multidual.h:503
vec< real, N > & Dual()
Access the multidual part.
Definition multidual.h:85
static vec< real, N > extract_real(const vec< multidual< N >, N > &v)
Extract the real vector from a vector of multidual numbers as a vec<real, N>
Definition multidual.h:417
vec< real, N > Dual() const
Get the multidual part.
Definition multidual.h:79
multidual(real r)
Construct a multidual number from a real number.
Definition multidual.h:53
static vec< multidual< N >, N > make_argument(const Vector &x)
Construct an N-dimensional vector of multidual numbers to be passed as argument to a multidual functi...
Definition multidual.h:401
real Re() const
Get the real part.
Definition multidual.h:67
static mat< real, N, N > extract_dual(const vec< multidual< N >, N > &v)
Extract the dual matrix (Jacobian) from a vector of multidual numbers as a mat<N, N>
Definition multidual.h:433
multidual inverse() const
Get the inverse of a multidual number.
Definition multidual.h:113
multidual operator+(real r) const
Sum a real number to a multidual number.
Definition multidual.h:137
multidual operator-() const
Get the opposite of a multidual number.
Definition multidual.h:143
static void extract(const vec< multidual< N >, N > &v, vec< real, N > &x, mat< real, N, N > &J)
Extract the real vector and dual matrix from a vector of multidual numbers as a vec<real,...
Definition multidual.h:450
void resize(unsigned int size)
Change the size of the dual part of the number (only for dynamically allocated vectors)
Definition multidual.h:474
multidual conjugate() const
Get the multidual conjugate.
Definition multidual.h:107
real & Re()
Access the real part.
Definition multidual.h:73
multidual()
Construct a multidual number as .
Definition multidual.h:43
multidual & operator=(real x)
Initialize a multidual number from a real number.
Definition multidual.h:59
multidual operator-(const multidual &other) const
Subtract two multidual numbers.
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
#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
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
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.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216