CoolProp 8.0.0
An open-source fluid property and humid air property database
Classes | Macros | Functions
numerics.h File Reference
#include <vector>
#include <set>
#include <cfloat>
#include <stdlib.h>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <array>
#include "CoolProp/detail/PlatformDetermination.h"
#include "CoolProp/detail/strings.h"
#include "CoolProp/Exceptions.h"
#include <iostream>

Go to the source code of this file.

Classes

class  Spline< X, Y >
 
class  Spline< X, Y >::Element
 
class  SplineClass
 

Macros

#define M_PI   3.14159265358979323846
 
#define COOLPROP_OK   1
 
#define POW5(x)   ((x) * (x) * (x) * (x) * (x))
 
#define POW6(x)   ((x) * (x) * (x) * (x) * (x) * (x))
 
#define POW7(x)   ((x) * (x) * (x) * (x) * (x) * (x) * (x))
 

Functions

template<typename T , size_t N>
std::array< T, N > create_filled_array (T value)
 
bool ValidNumber (double x)
 
template<typename T >
maxvectordiff (const std::vector< T > &z1, const std::vector< T > &z2)
 Return the maximum difference between elements in two vectors where comparing z1[i] and z2[i]. More...
 
template<typename T >
std::vector< T > linspace (T xmin, T xmax, std::size_t n)
 Make a linearly spaced vector of points. More...
 
template<typename T >
std::vector< T > log10space (T xmin, T xmax, std::size_t n)
 Make a base-10 logarithmically spaced vector of points. More...
 
template<typename T >
std::vector< T > logspace (T xmin, T xmax, std::size_t n)
 Make a base-e logarithmically spaced vector of points. More...
 
template<typename T >
void bisect_vector (const std::vector< T > &vec, T val, std::size_t &i)
 Use bisection to find the inputs that bisect the value you want, the trick here is that this function is allowed to have "holes" where parts of the the array are also filled with invalid numbers for which ValidNumber(x) is false. More...
 
template<typename T >
void bisect_segmented_vector_slice (const std::vector< std::vector< T > > &mat, std::size_t j, T val, std::size_t &i)
 Use bisection to find the inputs that bisect the value you want, the trick here is that this function is allowed to have "holes" where parts of the the array are also filled with invalid numbers for which ValidNumber(x) is false. More...
 
std::size_t powerset_dereference (std::set< std::size_t >::const_iterator v)
 
std::set< std::set< std::size_t > > powerset (std::set< std::size_t > const &set)
 
bool check_abs (double A, double B, double D)
 Some functions related to testing and comparison of values. More...
 
bool check_abs (double A, double B)
 
template<class T >
void normalize_vector (std::vector< T > &x)
 
template<class T >
factorial (T n)
 from http://stackoverflow.com/a/5721830/1360263 More...
 
template<class T1 , class T2 >
T1 nth_derivative_of_x_to_m (T1 x, T2 n, T2 m)
 
void MatInv_2 (double A[2][2], double B[2][2])
 
double root_sum_square (const std::vector< double > &x)
 
double interp1d (const std::vector< double > *x, const std::vector< double > *y, double x0)
 
double powInt (double x, int y)
 
template<class T >
POW2 (const T &x)
 
template<class T >
POW3 (T x)
 
template<class T >
POW4 (T x)
 
template<class T >
LinearInterp (T x0, T x1, T y0, T y1, T x)
 
template<class T1 , class T2 >
T2 LinearInterp (const std::vector< T1 > &x, const std::vector< T1 > &y, std::size_t i0, std::size_t i1, T2 val)
 
template<class T >
QuadInterp (T x0, T x1, T x2, T f0, T f1, T f2, T x)
 
template<class T1 , class T2 >
T2 QuadInterp (const std::vector< T1 > &x, const std::vector< T1 > &y, std::size_t i0, std::size_t i1, std::size_t i2, T2 val)
 
template<class T >
CubicInterp (T x0, T x1, T x2, T x3, T f0, T f1, T f2, T f3, T x)
 
template<class T >
CubicInterpFirstDeriv (T x0, T x1, T x2, T x3, T f0, T f1, T f2, T f3, T x)
 
template<class T1 , class T2 >
T2 CubicInterp (const std::vector< T1 > &x, const std::vector< T1 > &y, std::size_t i0, std::size_t i1, std::size_t i2, std::size_t i3, T2 val)
 
template<class T >
bool is_in_closed_range (T x1, T x2, T x)
 
void solve_cubic (double a, double b, double c, double d, int &N, double &x0, double &x1, double &x2)
 Solve a cubic with coefficients in decreasing order. More...
 
void solve_quartic (double a, double b, double c, double d, double e, int &N, double &x0, double &x1, double &x2, double &x3)
 
template<class T >
min3 (T x1, T x2, T x3)
 
template<class T >
max3 (T x1, T x2, T x3)
 
template<class T >
min4 (T x1, T x2, T x3, T x4)
 
template<class T >
max4 (T x1, T x2, T x3, T x4)
 
bool double_equal (double a, double b)
 
template<class T >
max_abs_value (const std::vector< T > &x)
 
template<class T >
min_abs_value (const std::vector< T > &x)
 
int Kronecker_delta (std::size_t i, std::size_t j)
 
int Kronecker_delta (int i, int j)
 
template<typename T >
void sort3 (T &a, T &b, T &c)
 Sort three values in place; see http://codereview.stackexchange.com/a/64763. More...
 
template<class T >
angle_difference (T angle1, T angle2)
 
double get_HUGE ()
 A simple function for use in wrappers where macros cause problems. More...
 

Macro Definition Documentation

◆ COOLPROP_OK

#define COOLPROP_OK   1

Definition at line 43 of file numerics.h.

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 39 of file numerics.h.

◆ POW5

#define POW5 (   x)    ((x) * (x) * (x) * (x) * (x))

Definition at line 497 of file numerics.h.

◆ POW6

#define POW6 (   x)    ((x) * (x) * (x) * (x) * (x) * (x))

Definition at line 498 of file numerics.h.

◆ POW7

#define POW7 (   x)    ((x) * (x) * (x) * (x) * (x) * (x) * (x))

Definition at line 499 of file numerics.h.

Function Documentation

◆ angle_difference()

template<class T >
T angle_difference ( angle1,
angle2 
)

Due to the periodicity of angles, you need to handle the case where the angles wrap around - suppose theta_d is 6.28 and you are at an angles of 0.1 rad, the difference should be around 0.1, not -6.27

This brilliant method is from http://blog.lexique-du-net.com/index.php?post/Calculate-the-real-difference-between-two-angles-keeping-the-sign and the comment of user tk

Originally implemented in PDSim

Definition at line 669 of file numerics.h.

◆ bisect_segmented_vector_slice()

template<typename T >
void bisect_segmented_vector_slice ( const std::vector< std::vector< T > > &  mat,
std::size_t  j,
val,
std::size_t &  i 
)

Use bisection to find the inputs that bisect the value you want, the trick here is that this function is allowed to have "holes" where parts of the the array are also filled with invalid numbers for which ValidNumber(x) is false.

Parameters
matThe vector to be bisected
jThe index of the matric in the off-grain dimension
valThe value to be found
iThe index to the left of the final point; i and i+1 bound the value

Definition at line 312 of file numerics.h.

◆ bisect_vector()

template<typename T >
void bisect_vector ( const std::vector< T > &  vec,
val,
std::size_t &  i 
)

Use bisection to find the inputs that bisect the value you want, the trick here is that this function is allowed to have "holes" where parts of the the array are also filled with invalid numbers for which ValidNumber(x) is false.

Parameters
vecThe vector to be bisected
valThe value to be found
iThe index to the left of the final point; i and i+1 bound the value

Definition at line 232 of file numerics.h.

◆ check_abs() [1/2]

bool check_abs ( double  A,
double  B 
)
inline

Definition at line 428 of file numerics.h.

◆ check_abs() [2/2]

bool check_abs ( double  A,
double  B,
double  D 
)
inline

Some functions related to testing and comparison of values.

Definition at line 415 of file numerics.h.

◆ create_filled_array()

template<typename T , size_t N>
std::array< T, N > create_filled_array ( value)

Definition at line 27 of file numerics.h.

◆ CubicInterp() [1/2]

template<class T1 , class T2 >
T2 CubicInterp ( const std::vector< T1 > &  x,
const std::vector< T1 > &  y,
std::size_t  i0,
std::size_t  i1,
std::size_t  i2,
std::size_t  i3,
T2  val 
)

Definition at line 556 of file numerics.h.

◆ CubicInterp() [2/2]

template<class T >
T CubicInterp ( x0,
x1,
x2,
x3,
f0,
f1,
f2,
f3,
x 
)

Definition at line 528 of file numerics.h.

◆ CubicInterpFirstDeriv()

template<class T >
T CubicInterpFirstDeriv ( x0,
x1,
x2,
x3,
f0,
f1,
f2,
f3,
x 
)

/brief Calculate the first derivative of the function using a cubic interpolation form

Definition at line 543 of file numerics.h.

◆ double_equal()

bool double_equal ( double  a,
double  b 
)
inline

Definition at line 599 of file numerics.h.

◆ factorial()

template<class T >
T factorial ( n)

from http://stackoverflow.com/a/5721830/1360263

Definition at line 464 of file numerics.h.

◆ get_HUGE()

double get_HUGE ( )
inline

A simple function for use in wrappers where macros cause problems.

Definition at line 674 of file numerics.h.

◆ interp1d()

double interp1d ( const std::vector< double > *  x,
const std::vector< double > *  y,
double  x0 
)

Definition at line 13 of file CPnumerics.cpp.

◆ is_in_closed_range()

template<class T >
bool is_in_closed_range ( x1,
x2,
x 
)

Definition at line 561 of file numerics.h.

◆ Kronecker_delta() [1/2]

int Kronecker_delta ( int  i,
int  j 
)
inline

Definition at line 636 of file numerics.h.

◆ Kronecker_delta() [2/2]

int Kronecker_delta ( std::size_t  i,
std::size_t  j 
)
inline

Definition at line 629 of file numerics.h.

◆ LinearInterp() [1/2]

template<class T1 , class T2 >
T2 LinearInterp ( const std::vector< T1 > &  x,
const std::vector< T1 > &  y,
std::size_t  i0,
std::size_t  i1,
T2  val 
)

Definition at line 506 of file numerics.h.

◆ LinearInterp() [2/2]

template<class T >
T LinearInterp ( x0,
x1,
y0,
y1,
x 
)

Definition at line 502 of file numerics.h.

◆ linspace()

template<typename T >
std::vector< T > linspace ( xmin,
xmax,
std::size_t  n 
)

Make a linearly spaced vector of points.

Definition at line 192 of file numerics.h.

◆ log10space()

template<typename T >
std::vector< T > log10space ( xmin,
xmax,
std::size_t  n 
)

Make a base-10 logarithmically spaced vector of points.

Definition at line 202 of file numerics.h.

◆ logspace()

template<typename T >
std::vector< T > logspace ( xmin,
xmax,
std::size_t  n 
)

Make a base-e logarithmically spaced vector of points.

Definition at line 213 of file numerics.h.

◆ MatInv_2()

void MatInv_2 ( double  A[2][2],
double  B[2][2] 
)

Definition at line 71 of file CPnumerics.cpp.

◆ max3()

template<class T >
T max3 ( x1,
x2,
x3 
)
inline

Definition at line 587 of file numerics.h.

◆ max4()

template<class T >
T max4 ( x1,
x2,
x3,
x4 
)
inline

Definition at line 595 of file numerics.h.

◆ max_abs_value()

template<class T >
T max_abs_value ( const std::vector< T > &  x)

Definition at line 604 of file numerics.h.

◆ maxvectordiff()

template<typename T >
T maxvectordiff ( const std::vector< T > &  z1,
const std::vector< T > &  z2 
)

Return the maximum difference between elements in two vectors where comparing z1[i] and z2[i].

Definition at line 179 of file numerics.h.

◆ min3()

template<class T >
T min3 ( x1,
x2,
x3 
)
inline

Definition at line 583 of file numerics.h.

◆ min4()

template<class T >
T min4 ( x1,
x2,
x3,
x4 
)
inline

Definition at line 591 of file numerics.h.

◆ min_abs_value()

template<class T >
T min_abs_value ( const std::vector< T > &  x)

Definition at line 617 of file numerics.h.

◆ normalize_vector()

template<class T >
void normalize_vector ( std::vector< T > &  x)

Definition at line 433 of file numerics.h.

◆ nth_derivative_of_x_to_m()

template<class T1 , class T2 >
T1 nth_derivative_of_x_to_m ( T1  x,
T2  n,
T2  m 
)

◆ POW2()

template<class T >
T POW2 ( const T &  x)

Definition at line 486 of file numerics.h.

◆ POW3()

template<class T >
T POW3 ( x)

Definition at line 490 of file numerics.h.

◆ POW4()

template<class T >
T POW4 ( x)

Definition at line 494 of file numerics.h.

◆ powerset()

std::set< std::set< std::size_t > > powerset ( std::set< std::size_t > const &  set)
inline

Definition at line 388 of file numerics.h.

◆ powerset_dereference()

std::size_t powerset_dereference ( std::set< std::size_t >::const_iterator  v)
inline

Definition at line 383 of file numerics.h.

◆ powInt()

double powInt ( double  x,
int  y 
)

Definition at line 40 of file CPnumerics.cpp.

◆ QuadInterp() [1/2]

template<class T1 , class T2 >
T2 QuadInterp ( const std::vector< T1 > &  x,
const std::vector< T1 > &  y,
std::size_t  i0,
std::size_t  i1,
std::size_t  i2,
T2  val 
)

Definition at line 523 of file numerics.h.

◆ QuadInterp() [2/2]

template<class T >
T QuadInterp ( x0,
x1,
x2,
f0,
f1,
f2,
x 
)

Definition at line 511 of file numerics.h.

◆ root_sum_square()

double root_sum_square ( const std::vector< double > &  x)

Definition at line 6 of file CPnumerics.cpp.

◆ solve_cubic()

void solve_cubic ( double  a,
double  b,
double  c,
double  d,
int &  N,
double &  x0,
double &  x1,
double &  x2 
)

Solve a cubic with coefficients in decreasing order.

0 = ax^3 + b*x^2 + c*x + d

Parameters
aThe x^3 coefficient
bThe x^2 coefficient
cThe x^1 coefficient
dThe x^0 coefficient
NThe number of unique real solutions found
x0The first solution found
x1The second solution found
x2The third solution found

Definition at line 82 of file CPnumerics.cpp.

◆ solve_quartic()

void solve_quartic ( double  a,
double  b,
double  c,
double  d,
double  e,
int &  N,
double &  x0,
double &  x1,
double &  x2,
double &  x3 
)

Definition at line 134 of file CPnumerics.cpp.

◆ sort3()

template<typename T >
void sort3 ( T &  a,
T &  b,
T &  c 
)

Sort three values in place; see http://codereview.stackexchange.com/a/64763.

Definition at line 646 of file numerics.h.

◆ ValidNumber()

bool ValidNumber ( double  x)
inline

Definition at line 33 of file numerics.h.