59 #define AA_EPSILON .001
64 ({ const __typeof__(a) aa_$_max_a = (a); \
65 const __typeof__(b) aa_$_max_b = (b); \
66 (aa_$_max_a > aa_$_max_b) ? aa_$_max_a : aa_$_max_b; })
70 ({ const __typeof__(a) aa_$_min_a = (a); \
71 const __typeof__(b) aa_$_min_b = (b); \
72 (aa_$_min_a < aa_$_min_b) ? aa_$_min_a : aa_$_min_b; })
76 if( val > level )
return level;
77 if( val < -level )
return -level;
82 static inline double aa_fclamp(
double val,
double min,
double max) {
83 if( val > max )
return max;
84 if( val < min )
return min;
89 static inline double aa_fdeadzone(
double val,
double min,
double max,
double deadval) {
90 if( min < val && max > val )
return deadval;
95 static inline void aa_vclamp(
size_t n,
double *v,
double min,
double max) {
96 for(
size_t i = 0; i < n; i++ ) {
97 if( v[i] > max ) v[i] = max;
98 else if( v[i] < min ) v[i] = min;
105 if( val > 0 )
return 1;
106 if( val < 0 )
return -1;
114 static inline int aa_feq(
double a,
double b,
double tol ) {
115 return fabs(a-b) <= tol;
119 AA_API int aa_veq(
size_t n,
const double *a,
const double *b,
double tol );
123 static inline double aa_fsq(
double a ) {
128 #define AA_MODULO(a,b) (((a) % (b)) + (b)) % (b);
153 return fmod(fmod(a, b) + b, b);
183 return a0 + x * ( a1 + x*a2 );
212 static inline double aa_stat_z2x(
double z,
double mu,
double sigma) {
213 return (z * sigma) + mu;
218 static inline double aa_stat_x2z(
double x,
double mu,
double sigma) {
239 double *pmu,
double *psigma,
240 double zmin,
double zmax,
241 size_t max_iterations );
264 double *pmu,
double *psigma,
265 double zmin,
double zmax,
266 size_t max_iterations );
288 double *mu,
double *E);
293 const double *mu,
const double *E_inv);
301 return rad*180.0/M_PI;
318 return remainder( an, 2*M_PI );
339 #define AA_MATCOL(A, lda, col) ((A)+(col)*(lda))
348 #define AA_MATREF(A, lda, row, col) (AA_MATCOL(A,lda,col)[row])
416 const double *x,
const double *y,
double *z );
471 const double *point,
const double *plane );
484 for(
size_t i = 0; i < n; i ++ )
497 aa_la_mvmul(
size_t m,
size_t n,
const double *A,
const double *x,
double *b ) {
498 cblas_dgemv( CblasColMajor, CblasNoTrans, (
int)m, (
int)n,
508 const double *x,
const double *A,
const double *y );
524 AA_API int aa_la_svd(
size_t m,
size_t n,
const double *A,
double *U,
double *S,
double *Vt );
613 AA_API void aa_la_dls(
size_t m,
size_t n,
double k,
const double *A,
const double *b,
double *x );
628 const double *A,
const double *A_star,
647 const double *A,
const double *A_star,
const double *b,
648 const double *xp,
double *x );
667 AA_API void aa_la_dlsnp(
size_t m,
size_t n,
double k,
const double *A,
const double *b,
const double *xp,
double *x );
679 AA_API void aa_la_lls(
size_t m,
size_t n,
size_t p,
const double *A,
const double *b,
double *x );
707 double t0,
const double *X0,
708 double t1,
const double *X1,
709 double ti,
double *Xi );
725 double t0,
const double *X0,
726 double t1,
const double *X1,
727 double t2,
const double *X2,
728 double ti,
double *Xi );
733 double t0,
const double *X0,
734 double t1,
const double *X1,
735 double t2,
const double *X2,
736 double ti,
double *dXi );
754 const double *points,
double *plane );
831 #define AA_ODE_EULER AA_ODE_RK1
836 #define AA_ODE_HEUN AA_ODE_RK2
876 double t0,
double dt0,
889 double t0,
double dt0,
891 struct aa_mem_region *region,
size_t *n_points,
double **path );
897 double t0,
double dt,
904 double t0,
double dt,
927 double t0,
double dt,
941 double t0,
double dt,
955 double t0,
double dt,
981 double t0,
double dt,
1010 double t0,
double dt,
1041 double t0,
double dt,
1071 double t0,
double dt,
#define AA_API
calling and name mangling convention for functions
#define AA_RESTRICT
Defined restrict keyword based on language flavor.
AA_EXTERN const char *aa_verbf_prefix AA_DEPRECATED
don't use
AA_API size_t aa_stat_excluded_circ_mean_std(size_t n, const double *x, double *pmu, double *psigma, double zmin, double zmax, size_t max_iterations)
Compute mean and standard deviation, excluding outliers.
static void aa_la_diag(size_t n, double *A, double x)
Set diagonal of A to x.
AA_API size_t aa_fmaxloc(size_t n, double *v)
Returns index of maximum element in array v.
AA_API void aa_la_cross(const double a[3], const double b[3], double c[3])
Cross product.
static double aa_stat_z2x(double z, double mu, double sigma)
Convert z-score to x-score a normal distribution.
AA_API double aa_stat_circ_std(size_t n, const double *x)
Compute standard deviation of vector x.
AA_API void aa_la_transpose(size_t n, double *A)
transpose square matrix A in place
aa_ode_integrator
The known integration methods.
@ AA_ODE_RK45_F
Runge-Kutta-Fehlberg integration.
@ AA_ODE_RK45_CK
Runge-Kutta 4-5 / Cash Karp integration.
@ AA_ODE_RK4
Runge-Kutta 4 integration.
@ AA_ODE_RK23_BS
Runge-Kutta 2-3 / Bogacki-Shampine integration.
@ AA_ODE_RK2
Runge-Kutta 2 integration.
@ AA_ODE_RK45_DP
Runge-Kutta 4-5 / Dormand-Prince integration.
@ AA_ODE_RK1
Runge-Kutta 1 integration.
int aa_ode_check(void *cx, double t, double *AA_RESTRICT x, double *AA_RESTRICT y)
Function signature to check whether a differential equation has reached its goal.
AA_API void aa_la_plane_fit(size_t m, size_t n, const double *points, double *plane)
Fit a plane to a set of points.
AA_API void aa_la_np(size_t m, size_t n, const double *A, const double *A_star, double *B)
Computer Nullspace Projection Matrix.
AA_API void aa_la_vdiv(size_t n, const double *x, const double *y, double *r)
Elementwise division.
#define AA_MODULO(a, b)
Fortran modulo, Ada mod.
AA_API double aa_stat_mean(size_t n, const double *x)
Compute mean of vector x.
AA_API void aa_stat_vmean_cov(size_t m, size_t n, const double *X, double *mu, double *E)
Compute sample covariance of vectors.
AA_API void aa_lsim_rk4step(size_t m, size_t n, double dt, const double *AA_RESTRICT A, const double *AA_RESTRICT B, const double *AA_RESTRICT x0, const double *AA_RESTRICT u, double *AA_RESTRICT x1)
Linear simulation step with Runge-Kutta-4 integration.
AA_API double aa_la_norm(size_t n, const double *x)
Euclidean norm of x.
AA_API void aa_odestep_rkbs23(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT k, double *AA_RESTRICT x4, double *AA_RESTRICT x5)
Runge-Kutta-2-3 (Bogacki-Shampine Method) integration.
AA_API void aa_la_dzdpinv(size_t m, size_t n, double s2_min, const double *A, double *A_star)
Deadzone, Damped Pseudo Inverse of A.
AA_API void aa_la_dls(size_t m, size_t n, double k, const double *A, const double *b, double *x)
Damped Least Squares.
AA_API void aa_la_vsub(size_t n, const double *x, const double *y, double *r)
Elementwise subtraction.
static double aa_fmodulo(double a, double b)
Mathematical modulo, Fortran modulo, Ada mod.
AA_API int aa_la_svd(size_t m, size_t n, const double *A, double *U, double *S, double *Vt)
Singular Value Decomposition of A.
static double aa_ang_deg2rad(double deg)
convert radians to degrees
static double aa_ang_delta(double a, double b)
Difference between two angles, interval (-pi,pi)
AA_API void aa_odestep_rk1(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT x1)
Euler / Runge-Kutta-1 integration.
AA_API double aa_frand_minmax(double min, double max)
uniform pseudo-random in [min,max]
static double aa_fremainder(double a, double b)
Mathematical remainder, Fortran mod, Ada rem.
AA_API void aa_odestep_rk2(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT x1)
Runge-Kutta-2 (Heun's Method) integration.
AA_API void aa_la_scal(size_t n, double alpha, double *x)
vector-scalar multiplication.
static long aa_lmodulo(long a, long b)
Fortran modulo, Ada mod.
AA_API void aa_sys_affine(const aa_sys_affine_t *cx, double t, const double *AA_RESTRICT x, double *AA_RESTRICT dx)
Affine system model function.
AA_API void aa_la_inverse3x3_(const double R[9], double S[9])
Inverse of 3x3 matrix R.
static double aa_fclamp(double val, double min, double max)
return val within range (min,max)
AA_API void aa_la_lls(size_t m, size_t n, size_t p, const double *A, const double *b, double *x)
Linear Least Squares.
AA_API double aa_la_trace(size_t n, const double *A)
Trace of a matrix.
AA_API void aa_la_xlsnp(size_t m, size_t n, const double *A, const double *A_star, const double *b, const double *xp, double *x)
Least Squares with Nullspace projection.
AA_API void aa_la_vinc(size_t n, const double *x, double *y)
increment by vector.
AA_API void aa_la_linterp(size_t n, double t0, const double *X0, double t1, const double *X1, double ti, double *Xi)
Linear interpolation.
AA_API void aa_odestep_dorpri45(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT k, double *AA_RESTRICT x4, double *AA_RESTRICT x5)
Runge-Kutta-4-5 (Dormand-Prince Method) integration.
static int64_t aa_imodulo64(int64_t a, int64_t b)
Fortran modulo, Ada mod.
AA_API void aa_la_plane_hessian(size_t n, double *plane)
Convert plane to hessian normal form.
AA_API void aa_la_smul(size_t n, double alpha, const double *x, double *r)
vector-scalar multiplication.
static double aa_fsq(double a)
square
static int aa_imodulo(int a, int b)
Fortran modulo, Ada mod.
static void aa_la_ident(size_t n, double *A)
Set A to the identity matrix.
AA_API double aa_la_wdot(size_t n, const double *x, const double *A, const double *y)
Weighted inner product.
AA_API int aa_la_care_laub(size_t m, size_t n, size_t p, const double *AA_RESTRICT A, const double *AA_RESTRICT B, const double *AA_RESTRICT C, double *AA_RESTRICT X)
Solve the continuous-time Riccati equation.
static int aa_feq(double a, double b, double tol)
Fuzzy equals.
void aa_odestep_adaptive(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT k, double *AA_RESTRICT x_n_1, double *AA_RESTRICT x_n)
Function signature for an adaptive integration step.
static double aa_horner3(double x, double a0, double a1, double a2)
Evaluate three-term polynomial using horner's rule.
AA_API double aa_la_max(size_t n, const double *x)
max of vector
AA_API void aa_la_sdiv(size_t n, double alpha, const double *x, double *r)
vector-scalar division.
static double aa_ang_rad2deg(double rad)
convert radians to degrees
AA_API int aa_isfok(double x)
returns one if x is not infinity or NAN
AA_API double aa_la_min(size_t n, const double *x)
min of vector
AA_API void aa_lsim_dstep(size_t m, size_t n, const double *AA_RESTRICT A, const double *AA_RESTRICT B, const double *AA_RESTRICT x0, const double *AA_RESTRICT u, double *AA_RESTRICT x1)
Linear simulation step in discrete time.
static AA_DEPRECATED double aa_clamp(double val, double level)
force value to be within +/- level
AA_API void aa_stat_vmean(size_t m, size_t n, const double *X, double *mu)
Compute mean of vectors.
AA_API void aa_la_quadterp_dx(size_t n, double t0, const double *X0, double t1, const double *X1, double t2, const double *X2, double ti, double *dXi)
Quadratic interpolation, derivative.
AA_API void aa_vrand(size_t n, double *v)
fills v with random numbers in [0,1.0]
AA_API void aa_la_ssub(size_t n, double alpha, const double *x, double *r)
vector-scalar subtraction.
AA_API int aa_veq(size_t n, const double *a, const double *b, double tol)
Fuzzy equals.
AA_API void aa_la_axpy(size_t n, double alpha, const double *x, double *y)
increment by scale times vector.
static void aa_la_inverse3x3(const double R[9], double S[9])
Inverse of 3x3 matrix R.
static void aa_la_mvmul(size_t m, size_t n, const double *A, const double *x, double *b)
matrix-vector multiplication
AA_API double aa_la_point_plane(size_t n, const double *point, const double *plane)
Point Plane Distance.
AA_API void aa_la_vadd(size_t n, const double *x, const double *y, double *r)
Elementwise addition.
AA_API void aa_la_axpy3(size_t n, double alpha, const double *x, const double *y, double *z)
increment by scale times vector.
AA_API int aa_ode_path(enum aa_ode_integrator integrator, const struct aa_ode_sol_opts *AA_RESTRICT opts, size_t n, aa_sys_fun sys, const void *sys_cx, aa_ode_check check, void *check_cx, double t0, double dt0, const double *AA_RESTRICT x0, struct aa_mem_region *region, size_t *n_points, double **path)
Compute the path followed during integration.
double aa_la_det3x3(const double R[AA_RESTRICT 9])
Determinant of 3x3 matrix R.
static double aa_ang_norm_2pi(double an)
normalize angle on interval [0,2pi)
AA_API size_t aa_stat_excluded_mean_std(size_t n, const double *x, double *pmu, double *psigma, double zmin, double zmax, size_t max_iterations)
Compute mean and standard deviation, excluding outliers.
static void aa_vclamp(size_t n, double *v, double min, double max)
modify each element of v to be within range (min,max)
static double aa_stat_x2z(double x, double mu, double sigma)
Convert x-score to z-score a normal distribution.
AA_API void aa_odestep_rkf45(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT k, double *AA_RESTRICT x4, double *AA_RESTRICT x5)
Runge-Kutta-4-5 (Fehlberg Method) integration.
double aa_stat_mahalanobis(size_t m, const double *x, const double *mu, const double *E_inv)
Mahalanobis distance.
static void aa_sincos(double x, double *s, double *c)
Portable sincos.
AA_API void aa_odestep_rkck45(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT k, double *AA_RESTRICT x4, double *AA_RESTRICT x5)
Runge-Kutta-4-5 (Cash-Karp Method) integration.
AA_API int aa_la_inv(size_t n, double *A)
Inverse of A.
AA_API size_t aa_fminloc(size_t n, double *v)
Returns index of minimum element in array v.
AA_API void aa_la_dlsnp(size_t m, size_t n, double k, const double *A, const double *b, const double *xp, double *x)
Damped Least Squares with Nullspace projection.
static int aa_iremainder(int a, int b)
Fortran mod, Ada rem.
void aa_sys_fun(const void *cx, double t, const double *AA_RESTRICT x, double *AA_RESTRICT y)
A "Signal" function.
AA_API double aa_stat_circ_mean(size_t n, const double *x)
Compute mean of angles.
static double aa_sign(double val)
return the sign of val, one of {-1,0,1}
AA_API void aa_lsim_estep(size_t m, size_t n, double dt, const double *AA_RESTRICT A, const double *AA_RESTRICT B, const double *AA_RESTRICT x0, const double *AA_RESTRICT u, double *AA_RESTRICT x1)
Linear simulation step with euler integration.
void aa_odestep_fixed(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT x1)
Function signature for a fixed integration step.
AA_API double aa_frand()
uniform pseudo-random in [0,1.0]
AA_API void aa_la_dpinv(size_t m, size_t n, double k, const double *A, double *A_star)
Damped Pseudo Inverse of A.
AA_API double aa_stat_std(size_t n, const double *x)
Compute standard deviation of vector x.
AA_API void aa_la_sinc(size_t n, double alpha, double *x)
increment by scalar.
AA_API double aa_la_dot(size_t n, const double *x, const double *y)
Dot product.
AA_API void aa_la_transpose2(size_t m, size_t n, const double *A, double *At)
transpose m*n matrix A into n*m matrix At
AA_API int aa_ode_sol(enum aa_ode_integrator integrator, const struct aa_ode_sol_opts *AA_RESTRICT opts, size_t n, aa_sys_fun sys, const void *sys_cx, aa_ode_check check, void *check_cx, double t0, double dt0, const double *AA_RESTRICT x0, double *AA_RESTRICT x1)
Solve an ordinary differential equation.
AA_API void aa_la_vmul(size_t n, const double *x, const double *y, double *r)
Elementwise multiplication.
AA_API double aa_la_dist(size_t n, const double *x, const double *y)
Euclidean Distance.
static double aa_ang_norm_pi(double an)
normalize angle on interval (-pi,pi)
AA_API void aa_odestep_rk4(size_t n, aa_sys_fun sys, const void *cx, double t0, double dt, const double *AA_RESTRICT x0, double *AA_RESTRICT x1)
Runge-Kutta-4 integration.
AA_API void aa_la_sadd(size_t n, double alpha, const double *x, double *r)
vector-scalar addition.
AA_API void aa_la_quadterp(size_t n, double t0, const double *X0, double t1, const double *X1, double t2, const double *X2, double ti, double *Xi)
Quadratic interpolation.
AA_API double aa_la_ssd(size_t n, const double *x, const double *y)
Sum of Squared Differences.
AA_API void aa_la_normalize(size_t n, double *x)
Make x unit vector.
AA_API void aa_odestep_euler(size_t n, double dt, const double *AA_RESTRICT dx, const double *AA_RESTRICT x0, double *AA_RESTRICT x1)
Euler / Runge-Kutta-1 integration.
static double aa_fdeadzone(double val, double min, double max, double deadval)
apply deadzone to val
AA_API void aa_stat_box_muller(double x1, double x2, double *z1, double *z2)
Generate 2 gaussian random numbers with stddev=1 from two uniform random numbers in interval (0,...
#define AA_MEM_ZERO(dst, n_elem)
Set n_elem elements at dst to zero.
Data Structure for Region-Based memory allocation.
Options for the differential equation solver.
double adapt_factor_inc
Factor to increse step size.
double adapt_tol_dec
Decrease step size if error is greater than tol_shrink.
double adapt_factor_dec
Factor to decrease step size.
double adapt_tol_inc
Increase step size if error is below tol_grow.
Context-struct for function aa_sys_affine.
double * A
state transition
double * D
additive constant