tico 0.1
Mechanical Watch Terminal Timegrapher
Functions
Mymath Library

Math and matrix utilities. More...

Functions

void transpone (double *arr, unsigned int Nrows, unsigned int Ncols)
 Transposes a matrix in-place. More...
 
void invert (double *arr, unsigned int Nrows, unsigned int Ncols)
 Inverts a square matrix in-place. More...
 
double * mulmat (const double *matrix0, unsigned int Nrows, unsigned int Ncols, const double *matrix1, unsigned int Mrows, unsigned int Mcols)
 Multiplies two matrices and returns the result as a new matrix. More...
 
void matlinreg (double coeffs[2], const double *xmat, unsigned int Nrows, unsigned int Ncols, double *vec, const double *weight)
 Performs linear regression on a dataset with optional weighting. More...
 
void fitNpeaks (double *intercept, double *slope, unsigned int curPos, const struct myarr *maxvals, const struct myarr *maxes, const struct myarr *subpos, unsigned int npeaks, double SDthreshold)
 Fits a line to N peaks in the data, with outlier rejection. More...
 
void fastlinreg (double coeffs[2], const double *xmat, unsigned int Npoints, const double *vec, const double *weightArr)
 Performs fast weighted linear regression. More...
 
size_t getmaxpos (const int *array, size_t ArrayLength)
 Returns the index of the maximum value in an integer array. More...
 
void linreg (const double *xarr, const double *yarr, size_t ArrayLength, double *intercept, double *slope, double *stdev)
 Performs simple linear regression on two arrays. More...
 
int shiftHalf (size_t value, size_t ArrayLength)
 Shifts a value by half the array length, wrapping around. More...
 

Detailed Description

Math and matrix utilities.

Function Documentation

◆ fastlinreg()

void fastlinreg ( double  coeffs[2],
const double *  xmat,
unsigned int  Npoints,
const double *  vec,
const double *  weightArr 
)

Performs fast weighted linear regression.

Parameters
coeffsArray to store the resulting coefficients (intercept and slope).
xmatThe array of x values.
NpointsThe number of points.
vecThe array of y values.
weightArrThe array of weights.

◆ fitNpeaks()

void fitNpeaks ( double *  intercept,
double *  slope,
unsigned int  curPos,
const struct myarr maxvals,
const struct myarr maxes,
const struct myarr subpos,
unsigned int  npeaks,
double  SDthreshold 
)

Fits a line to N peaks in the data, with outlier rejection.

Parameters
interceptPointer to store the intercept.
slopePointer to store the slope.
curPosThe current position in the data.
maxvalsArray of weights.
maxesArray of y values.
subposArray of sub-positions.
npeaksNumber of peaks to fit.
SDthresholdStandard deviation threshold for outlier rejection.

◆ getmaxpos()

size_t getmaxpos ( const int *  array,
size_t  ArrayLength 
)

Returns the index of the maximum value in an integer array.

Parameters
arrayThe array to search.
ArrayLengthThe number of elements in the array.
Returns
The index of the maximum value.

◆ invert()

void invert ( double *  arr,
unsigned int  Nrows,
unsigned int  Ncols 
)

Inverts a square matrix in-place.

Parameters
arrThe square matrix to invert, stored as a 1D array in row-major order. The result will overwrite the original matrix.
NrowsThe number of rows (and columns) in the square matrix.
NcolsThe number of columns (and rows) in the square matrix.

◆ linreg()

void linreg ( const double *  xarr,
const double *  yarr,
size_t  ArrayLength,
double *  intercept,
double *  slope,
double *  stdev 
)

Performs simple linear regression on two arrays.

Parameters
xarrThe array of x values.
yarrThe array of y values.
ArrayLengthThe number of elements in the arrays.
interceptPointer to store the intercept.
slopePointer to store the slope.
stdevPointer to store the standard deviation of the fit.

◆ matlinreg()

void matlinreg ( double  coeffs[2],
const double *  xmat,
unsigned int  Nrows,
unsigned int  Ncols,
double *  vec,
const double *  weight 
)

Performs linear regression on a dataset with optional weighting.

Parameters
coeffsAn array of size 2 where the resulting coefficients (intercept and slope) will be stored.
xmatThe design matrix, stored as a 1D array in row-major order.
NrowsThe number of rows in the design matrix.
NcolsThe number of columns in the design matrix.
vecThe response vector, stored as a 1D array.
weightAn optional array of weights for each data point, stored as a 1D array. If NULL, unweighted regression is performed.

◆ mulmat()

double * mulmat ( const double *  matrix0,
unsigned int  Nrows,
unsigned int  Ncols,
const double *  matrix1,
unsigned int  Mrows,
unsigned int  Mcols 
)

Multiplies two matrices and returns the result as a new matrix.

Parameters
matrix0The first matrix, stored as a 1D array in row-major order.
NrowsThe number of rows in the first matrix.
NcolsThe number of columns in the first matrix.
matrix1The second matrix, stored as a 1D array in row-major order.
MrowsThe number of rows in the second matrix.
McolsThe number of columns in the second matrix.
Returns
A pointer to the resulting matrix, stored as a 1D array in row-major order. The caller is responsible for freeing this memory.

◆ shiftHalf()

int shiftHalf ( size_t  value,
size_t  ArrayLength 
)

Shifts a value by half the array length, wrapping around.

Parameters
valueThe value to shift.
ArrayLengthThe length of the array.
Returns
The shifted value, wrapped around the array length.

◆ transpone()

void transpone ( double *  arr,
unsigned int  Nrows,
unsigned int  Ncols 
)

Transposes a matrix in-place.

Parameters
arrThe matrix to transpose, stored as a 1D array in row-major order.
NrowsThe number of rows in the original matrix.
NcolsThe number of columns in the original matrix.