COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
mathlib.h
Go to the documentation of this file.
1 /********************************************************************
2 * Copyright (C) 2015 by Interstel Technologies, Inc.
3 * and Hawaii Space Flight Laboratory.
4 *
5 * This file is part of the COSMOS/core that is the central
6 * module for COSMOS. For more information on COSMOS go to
7 * <http://cosmos-project.com>
8 *
9 * The COSMOS/core software is licenced under the
10 * GNU Lesser General Public License (LGPL) version 3 licence.
11 *
12 * You should have received a copy of the
13 * GNU Lesser General Public License
14 * If not, go to <http://www.gnu.org/licenses/>
15 *
16 * COSMOS/core is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public License
18 * as published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * COSMOS/core is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * Refer to the "licences" folder for further information on the
27 * condititons and terms to use this software.
28 ********************************************************************/
29 
30 #ifndef _MATHLIB_H
31 #define _MATHLIB_H 1
32 
37 
106 #include "support/configCosmos.h"
107 #include "support/cosmos-errno.h"
108 
109 // include all math modules
110 #include "constants.h"
111 #include "vector.h"
112 #include "matrix.h"
113 #include "rotation.h"
114 
115 #include <cmath>
116 #include <iostream>
117 
118 //#define true 1
119 //#define false 0
120 
121 
125 
127 #define CRC16CCITT 0x1021
128 #define CRC16CCITTMSB 0x1021
129 #define CRC16CCITTR 0x8408
131 #define CRC16CCITTLSB 0x8408
132 #define CRC16CCITTRR 0x8810
134 
135 
137 
140 // TODO: explain why this union is used for
141 typedef union
142 {
143  double d;
144  float f;
145  int32_t i32;
146  int16_t i16;
147  uint32_t u32;
148  uint16_t u16;
149 } utype;
150 
152 
154 // TODO: explain why this union is used for
155 typedef union
156 {
167  double a4[4];
168 } uvector;
169 
170 
172 //typedef double*** matrix3d;
173 
175 
177 // TODO: create seperate class (and file) for gauss jackson functions
178 struct gj_kernel
179 {
180  int32_t order;
181  int32_t horder;
182  double dvi;
183  double dvi2;
184  int32_t **binom;
185  double **alpha;
186  double **beta;
187  double *c;
188  double *gam;
189  double *q;
190  double *lam;
191 } ;
192 
194 
196 struct gj_step
197 {
199  double vd0;
201  double vd1;
203  double vd2;
204  double *a;
205  double *b;
207  double s;
208  double ss;
209  double sa;
210  double sb;
211 } ;
212 
214 
217 {
221  int32_t axes;
225  double *vi;
227  void (*calc_vd2)(double vi, double *vd0, double *vd2, int32_t axes);
228 } ;
229 
231 
234 {
244  double *vi;
246  double (*calc_vd2)(double vi, double vdx0, double vdy0, double vdz0);
247 } ;
248 
250 
255 {
256  double value[3];
257  double error[3];
258  vector<double> a;
259  vector<double> x;
260  vector<double> y;
261 };
262 
264 
271 {
272  vector<estimatorstruc> r;
273  int32_t index;
274  uint32_t size;
275  uint32_t degree;
276  double xbase;
277  double ybase;
278 } ;
279 
281 
285 
286 double gaussian_random(double mean, double stdev);
287 
288 double distance_rv(rvector p0, rvector p1, rvector p2);
289 //double distance_rv_1(rvector p0, rvector p1, rvector p2);
290 double area_rv(rvector p0, rvector p1, rvector p2);
291 double evaluate_poly(double x, rvector parms);
292 double evaluate_poly_slope(double x, rvector parms);
293 double evaluate_poly_accel(double x, rvector parms);
294 double evaluate_poly_jerk(double x, rvector parms);
295 
296 gvector gv_evaluate_poly(double x, vector< vector<double> > parms);
297 gvector gv_evaluate_poly_slope(double x, vector< vector<double> > parms);
298 gvector gv_evaluate_poly_accel(double x, vector< vector<double> > parms);
299 gvector gv_evaluate_poly_jerk(double x, vector< vector<double> > parms);
300 
301 rvector rv_evaluate_poly(double x, vector< vector<double> > parms);
302 rvector rv_evaluate_poly_slope(double x, vector< vector<double> > parms);
303 rvector rv_evaluate_poly_accel(double x, vector< vector<double> > parms);
304 rvector rv_evaluate_poly_jerk(double x, vector< vector<double> > parms);
305 
306 // TODO: move these functions to quaternion ???
310 
311 // Moving to new naming convention: drotate is Direct Rotation of vector, irotate is rotation of coordinate axes
316 
317 
320 vector<double> polyfit(vector<double> &x, vector<double> &y);
321 void multisolve(vector< vector<double> > x, vector<double> y, vector<double>& a);
322 void open_estimate(estimatorhandle *estimate, uint32_t size, uint32_t degree);
323 int16_t set_estimate(estimatorhandle *estimate, double independent, double dependent);
324 estimatorstruc get_estimate(estimatorhandle *estimate, double independent);
325 
326 
327 
329 
330 uint16_t uint16from(uint8_t *pointer, ByteOrder order);
331 int16_t int16from(uint8_t *pointer, ByteOrder order);
332 uint32_t uint32from(uint8_t *pointer, ByteOrder order);
333 int32_t int32from(uint8_t *pointer, ByteOrder order);
334 float floatfrom(uint8_t *pointer, ByteOrder order);
335 double doublefrom(uint8_t *pointer, ByteOrder order);
336 void uint32to(uint32_t value, uint8_t *pointer, ByteOrder order);
337 void int32to(int32_t value, uint8_t *pointer, ByteOrder order);
338 void uint16to(uint16_t value, uint8_t *pointer, ByteOrder order);
339 void int16to(int16_t value, uint8_t *pointer, ByteOrder order);
340 void floatto(float value, uint8_t *pointer, ByteOrder order);
341 void doubleto(double value, uint8_t *pointer, ByteOrder order);
342 
343 // Gauss-Jackson routines
344 gj_kernel *gauss_jackson_kernel(int32_t order, double dvi);
346 gj_instance *gauss_jackson_instance(gj_kernel *gjk,int32_t axes,void (*calc_vd2)(double vi, double *vd0, double *vd2, int32_t axes));
347 //gj_instance3d *gauss_jackson_instance3d(gj_kernel *gjk,double (*calc_vd2)(double vi, double vdx0, double vdy0, double vdz0));
348 int gauss_jackson_setstep(gj_instance *gji, double vi, double *vd0, double *vd1, double *vd2, int32_t istep);
349 int gauss_jackson_getstep(gj_instance *gji, double *vi, double *vd0, double *vd1, double *vd2, int32_t istep);
351 void gauss_jackson_destep(gj_kernel *kern, gj_step *step);
353 void gauss_jackson_extrapolate(gj_instance *gji, double target);
354 
355 double fixangle(double angle);
356 double actan(double y, double x);
357 double fixprecision(double number, double precision);
358 uint16_t calc_crc16ccitt(uint8_t *buf, int size, bool lsb=true);
359 uint16_t calc_crc16ccitt_lsb(vector<uint8_t>buf);
360 uint16_t calc_crc16ccitt_msb(vector<uint8_t>buf);
361 class LsFit
362 {
363 private:
365 
368  struct fitelement
369  {
370  // Independent variable
371  double x;
372  // Double, rvector or quaternion dependent variable
374  } ;
375 
377 
380  // Number of elements in fit
381  uint16_t element_cnt;
382  // Number of axes (double, rvector, quaternion)
383  uint16_t depth;
384  // Order of fit
385  uint32_t order;
386  // base level subtracted from independent variable before fitting
387  double basex;
388  // Of size element_cnt
389  deque<fitelement> var;
390 
391  vector< vector<double> > parms;
392 
393  void fit();
394 
395 public:
396  double meanx;
397  uvector meany{};
398  double stdevx;
399  uvector stdevy{};
400  // Minimum reasonable step in dependent variable
401  double resolution;
402 
403  // constructors
404 // LsFit();
405 // LsFit(uint16_t element_cnt);
406 // LsFit(uint16_t element_cnt, uint16_t ord);
407  LsFit(uint16_t cnt=10, uint16_t ord=2);
408 
409  void initialize(uint16_t cnt=10, uint16_t ord=2);
410  void update(double x, double y);
411  void update(double x, rvector y);
412  void update(double x, gvector y);
413  void update(double x, quaternion y);
414  void update(fitelement cfit, uint16_t dep);
415  double lastx();
416  double firstx();
417  size_t size();
418  double eval(double x);
419  rvector evalrvector(double x);
420  gvector evalgvector(double x);
421  quaternion evalquaternion(double x);
422  double slope(double x);
423  rvector slopervector(double x);
424  gvector slopegvector(double x);
425  quaternion slopequaternion(double x);
426  double accel(double x);
427  rvector accelrvector(double x);
428  gvector accelgvector(double x);
429  quaternion accelquaternion(double x);
430  double jerk(double x);
431  rvector jerkrvector(double x);
432  gvector jerkgvector(double x);
433  quaternion jerkquaternion(double x);
434  vector < vector <double> > getparms();
435  double getbasex();
436 };
437 
438 
440 
441 #endif
Gauss-Jackson Integration Instance.
Definition: mathlib.h:216
double dvi
Definition: mathlib.h:182
float floatfrom(uint8_t *pointer, ByteOrder order)
Memory to 32 bit float.
Definition: mathlib.cpp:491
void doubleto(double value, uint8_t *pointer, ByteOrder order)
64 bit floating point to memory
Definition: mathlib.cpp:702
rvector transform_q(quaternion q, rvector v)
Definition: mathlib.cpp:2317
vector< vector< double > > parms
Definition: mathlib.h:391
vector< estimatorstruc > r
Definition: mathlib.h:272
Quaternion/Rvector Union.
Definition: mathlib.h:155
double xbase
Definition: mathlib.h:276
double d
Definition: mathlib.h:143
double dvi2
Definition: mathlib.h:183
int16_t int16from(uint8_t *pointer, ByteOrder order)
Memory to 16 bit signed integer.
Definition: mathlib.cpp:424
Quaternion, scalar first using vector elements.
Definition: vector.h:562
void gauss_jackson_preset(gj_instance *gji)
Converge all axes of a Gauss-Jackson integrator prior to propagation.
Definition: mathlib.cpp:1974
gj_kernel * kern
Kernel Pointer.
Definition: mathlib.h:236
double ybase
Definition: mathlib.h:277
y
Definition: inputfile.py:6
rvector r
Definition: mathlib.h:161
double evaluate_poly_slope(double x, rvector parms)
void multisolve(vector< vector< double > > x, vector< double > y, vector< double > &a)
Perform N equation solution.
Definition: mathlib.cpp:900
Gauss-Jackson 3D Integration Instance.
Definition: mathlib.h:233
3 element generic row vector
Definition: vector.h:53
int32_t ** binom
Definition: mathlib.h:184
Quaternion, scalar last, using vector elements.
Definition: vector.h:506
static antstruc target
Definition: agent_antenna.cpp:160
float f
Definition: mathlib.h:144
double evaluate_poly_jerk(double x, rvector parms)
int32_t i32
Definition: mathlib.h:145
double basex
Definition: mathlib.h:387
void int32to(int32_t value, uint8_t *pointer, ByteOrder order)
32 bit signed integer to memory
Definition: mathlib.cpp:582
Quaternion, scalar last, using x, y, z.
Definition: vector.h:402
void uint16to(uint16_t value, uint8_t *pointer, ByteOrder order)
16 bit unsigned integer to memory
Definition: mathlib.cpp:613
rvector rv_evaluate_poly(double x, vector< vector< double > > parms)
Evaluate vector polynomial.
Definition: mathlib.cpp:1059
Scalar value type Union.
Definition: mathlib.h:141
void floatto(float value, uint8_t *pointer, ByteOrder order)
32 bit floating point to memory
Definition: mathlib.cpp:671
qlast ql
Definition: mathlib.h:160
vector< double > a
Definition: mathlib.h:258
matrix1d m1
Definition: mathlib.h:165
void open_estimate(estimatorhandle *estimate, uint32_t size, uint32_t degree)
Initialize estimator.
Definition: mathlib.cpp:736
double vd1
Dependent variable 1st derivative.
Definition: mathlib.h:201
double sb
Definition: mathlib.h:210
quaternion q
Definition: mathlib.h:157
double * vi
Pointer to array of independent variables; order+2.
Definition: mathlib.h:225
void gauss_jackson_extrapolate(gj_instance *gji, double target)
Propagate Gauss-Jackson integration.
Definition: mathlib.cpp:2079
int32_t index
Definition: mathlib.h:273
Estimator handle.
Definition: mathlib.h:270
double actan(double y, double x)
ArcTan, limited to range 0-2PI.
Definition: mathlib.cpp:2174
gvector gv_evaluate_poly_slope(double x, vector< vector< double > > parms)
Evaluate vector polynomial slope.
Definition: mathlib.cpp:1214
Estimator structure.
Definition: mathlib.h:254
uint16_t calc_crc16ccitt_lsb(vector< uint8_t >buf)
Definition: mathlib.cpp:3019
void gauss_jackson_dekernel(gj_kernel *gjk)
Free Gauss-Jackson Integration Kernel.
Definition: mathlib.cpp:1711
pxnxm element cube
Definition: mathlib.h:178
estimatorstruc get_estimate(estimatorhandle *estimate, double x)
Get estimate.
Definition: mathlib.cpp:812
vector< double > polyfit(vector< double > &x, vector< double > &y)
Perform general order polynomial fit.
Definition: mathlib.cpp:1434
Definition: mathlib.h:361
double * q
Definition: mathlib.h:189
void uint32to(uint32_t value, uint8_t *pointer, ByteOrder order)
32 bit unsigned integer to memory
Definition: mathlib.cpp:551
double gaussian_random(double mean, double stdev)
Normal Distribution random number.
Definition: mathlib.cpp:52
double vd0
Dependent variable.
Definition: mathlib.h:199
double * c
Definition: mathlib.h:187
deque< fitelement > var
Definition: mathlib.h:389
int32_t order
Definition: mathlib.h:180
double meanx
Definition: mathlib.h:396
uint32_t u32
Definition: mathlib.h:147
ByteOrder local_byte_order()
Determine local byte order.
Definition: mathlib.cpp:375
3 element attitude vector.
Definition: vector.h:277
uint16_t depth
Definition: mathlib.h:383
double x
Definition: mathlib.h:371
3 element spherical vector
Definition: vector.h:167
double area_rv(rvector p0, rvector p1, rvector p2)
Area of a triangle.
Definition: mathlib.cpp:336
Least Squares Fit Element.
Definition: mathlib.h:368
void int16to(int16_t value, uint8_t *pointer, ByteOrder order)
16 bit signed integer to memory
Definition: mathlib.cpp:642
uint32_t size
Definition: mathlib.h:274
Headers and definitions common to all COSMOS.
rvector rv_quaternion2axis(quaternion q)
Quaternion to row vector axis and angle.
Definition: mathlib.cpp:194
gj_instance * gauss_jackson_instance(gj_kernel *kern, int32_t axes, void(*calc_vd2)(double vi, double *vd0, double *vd2, int32_t axes))
Initialize an Instance of a Gauss-Jackson Integrator.
Definition: mathlib.cpp:1774
double evaluate_poly_accel(double x, rvector parms)
double evaluate_poly(double x, rvector parms)
uint16_t u16
Definition: mathlib.h:148
double fixangle(double angle)
Limit angle to range 0-2PI.
Definition: mathlib.cpp:2159
gj_kernel * gauss_jackson_kernel(int32_t order, double dvi)
Create Gauss-Jackson Integration Kernel.
Definition: mathlib.cpp:1543
COSMOS Error Codes.
uint32_t degree
Definition: mathlib.h:275
gj_step * stepx
Pointer to array of x steps.
Definition: mathlib.h:238
x
Definition: inputfile.py:6
uint32_t order
Definition: mathlib.h:385
rvector rv_evaluate_poly_jerk(double x, vector< vector< double > > parms)
Evaluate vector polynomial jerk.
Definition: mathlib.cpp:1152
gvector gv_evaluate_poly_jerk(double x, vector< vector< double > > parms)
Evaluate vector polynomial jerk.
Definition: mathlib.cpp:1276
qcomplex qc
Definition: mathlib.h:158
uint32_t uint32from(uint8_t *pointer, ByteOrder order)
Memory to 32 bit unsigned integer.
Definition: mathlib.cpp:442
Definition: eci2kep_test.cpp:33
uint16_t calc_crc16ccitt_msb(vector< uint8_t >buf)
Definition: mathlib.cpp:3039
double s
Current guess of dependent variable.
Definition: mathlib.h:207
n element row matrix
Definition: matrix.h:144
svector s
Definition: mathlib.h:163
int32_t axes
Number of axes of integration.
Definition: mathlib.h:221
double * gam
Definition: mathlib.h:188
rvector rv_evaluate_poly_accel(double x, vector< vector< double > > parms)
Evaluate vector polynomial acceleration.
Definition: mathlib.cpp:1121
gvector gv_evaluate_poly(double x, vector< vector< double > > parms)
Evaluate vector polynomial.
Definition: mathlib.cpp:1183
gj_step * stepz
Pointer to array of z steps.
Definition: mathlib.h:242
int16_t set_estimate(estimatorhandle *estimate, double independent, double dependent)
Set estimator.
Definition: mathlib.cpp:755
gj_step * gauss_jackson_step(gj_kernel *kern)
Initialize a Step of a Gauss-Jackson integrator.
Definition: mathlib.cpp:1833
int gauss_jackson_setstep(gj_instance *gji, double vi, double *vd0, double *vd1, double *vd2, int32_t istep)
Set Independent and Dependent variables for Gauss-Jackson step.
Definition: mathlib.cpp:1920
double * vi
Pointer to array of independent variables.
Definition: mathlib.h:244
double distance_rv(rvector p0, rvector p1, rvector p2)
Distance from a line.
Definition: mathlib.cpp:291
double sa
Definition: mathlib.h:209
rvector rotate_q(quaternion q, rvector v)
Definition: mathlib.cpp:2272
rvector irotate(quaternion q, rvector v)
Indirectly rotate a row vector using a quaternion.
Definition: mathlib.cpp:2308
ByteOrder
Enumeration of possible byte orders.
Definition: math/constants.h:48
avector a
Definition: mathlib.h:166
qfirst qf
Definition: mathlib.h:159
double * lam
Definition: mathlib.h:190
vector< double > y
Definition: mathlib.h:260
char buf[128]
Definition: rw_test.cpp:40
gj_step * stepy
Pointer to array of y steps.
Definition: mathlib.h:240
double doublefrom(uint8_t *pointer, ByteOrder order)
Memory to 64 bit float.
Definition: mathlib.cpp:519
rvector rv_evaluate_poly_slope(double x, vector< vector< double > > parms)
Evaluate vector polynomial slope.
Definition: mathlib.cpp:1090
uvector rv_fitpoly(uvector x, uvector y, uint32_t order)
Perform nth order polynomial fit.
Definition: mathlib.cpp:1507
int16_t i16
Definition: mathlib.h:146
gvector gv_evaluate_poly_accel(double x, vector< vector< double > > parms)
Evaluate vector polynomial acceleration.
Definition: mathlib.cpp:1245
cvector c
Definition: mathlib.h:162
double ** alpha
Definition: mathlib.h:185
double resolution
Definition: mathlib.h:401
static double lastx
Definition: physicslib.cpp:41
3 element geodetic vector
Definition: vector.h:222
double ss
Definition: mathlib.h:208
void gauss_jackson_destep(gj_kernel *kern, gj_step *step)
Destroy a Step of a Gauss-Jackson integrator.
Definition: mathlib.cpp:1888
double vd2
Dependent variable 2nd derivative.
Definition: mathlib.h:203
Quaternion, scalar last, using imaginary elements.
Definition: vector.h:450
uint16_t uint16from(uint8_t *pointer, ByteOrder order)
Memory to 16 bit unsigned integer.
Definition: mathlib.cpp:395
gj_kernel * kern
Kernel Pointer.
Definition: mathlib.h:219
uint16_t calc_crc16ccitt(uint8_t *buf, int size, bool lsb)
Calculate CRC-16-CCITT.
Definition: mathlib.cpp:2206
gvector g
Definition: mathlib.h:164
int32_t int32from(uint8_t *pointer, ByteOrder order)
Memory to 32 bit signed integer.
Definition: mathlib.cpp:473
double fixprecision(double number, double prec)
Limit precision.
Definition: mathlib.cpp:2191
gj_step ** steps
Pointer to array of steps; one for each axis, order+2 for the orders.
Definition: mathlib.h:223
Gauss-Jackson Integration Step.
Definition: mathlib.h:196
double ** beta
Definition: mathlib.h:186
double * b
Definition: mathlib.h:205
double * a
Definition: mathlib.h:204
int gauss_jackson_getstep(gj_instance *gji, double *vi, double *vd0, double *vd1, double *vd2, int32_t istep)
Get Independent and Dependent variables for Gauss-Jackson step.
Definition: mathlib.cpp:1951
3 element cartesian vector
Definition: vector.h:107
int32_t horder
Definition: mathlib.h:181
rvector drotate(quaternion q, rvector v)
Rotate a row vector using a quaternion.
Definition: mathlib.cpp:2261
uint16_t element_cnt
Least Squares Fit Structure.
Definition: mathlib.h:381
double stdevx
Definition: mathlib.h:398
vector< double > x
Definition: mathlib.h:259