Defines | Functions

src/mathadd.h File Reference

STATUS: unstable Additional math-functions. More...

#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <string.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_cblas.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "helper.h"
#include "definitions.h"
#include "complex.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define _ISOC99_SOURCE
#define ABS(a)   ( ((a)<0) ? (-1*(a)) : (a) )
 absolute value of a number
#define CHECK_BIT(var, pos)   ((var) & (1<<(pos)))
 check whether bit at position pos is 1.
#define ISODD(x)   ((((x)%2)==0)? (0) : (1))
#define MAX(a, b)   ((a) > (b) ? (a):(b))
 maximum of two elements.
#define MIN(a, b)   ((a) < (b) ? (a):(b))
 minimum of two elements.
#define PI   3.14159265358979323846
#define SQR(a)   ((a)*(a))
 square of a number.

Functions

int abscmp (const void *p1, const void *p2)
int closest_index (const double *v, int n, double c)
int cmpdouble (double d1, double d2, int precision)
double * dblp_complex_to_real (const Complex *vc, double *vr, int n)
double dblp_euclidean_distance (const double *v1, const double *v2, int n)
double * dblp_init (double *v, int n, double val)
double dblp_max (double *v, int n, int *idx)
double dblp_mean (double *v, int n)
double dblp_min (double *v, int n, int *idx)
void dblp_minus_scalar (double *v, int n, double val)
void dblp_print (double *v, int n)
void dblp_print_int (int *v, int n)
void dblp_shuffle_int (int *permut, int n)
void dblpp_add_dblpp (double **m1, const double **m2, int N, int n)
void dblpp_add_scalar (double **m, int N, int n, double s)
void dblpp_copy (const double **src, double **dest, int N, int M)
double ** dblpp_delcol (double **m, int N, int n, int col)
double ** dblpp_delrow (double **m, int N, int n, int row)
void dblpp_divide_scalar (double **m, int N, int n, double s)
void dblpp_dottimes_dblpp (double **m1, const double **m2, int N, int M)
void dblpp_free (double **m, int N)
double ** dblpp_init (int N, int M)
int ** dblpp_init_int (int N, int M)
double dblpp_max (const double **m, int N, int n, int *i1, int *i2)
double dblpp_min (const double **m, int N, int n, int *i1, int *i2)
void dblpp_mul_scalar (double **m, int N, int n, double s)
void dblpp_normalize_by_max (double **m, int M, int N)
void dblpp_print (const double **m, int N, int n)
double ** dblpp_rand (double **m, int N, int M, double lower, double upper)
void dblpp_sub_dblpp (double **dest, const double **src, int N, int n)
double drawsample_nearest_neighbour (const double *v, int n, double x)
Complexexpand_polynomial_from_roots (const Complex *roots, int n, Complex *coeffs)
void fft (double *data, unsigned long nn, int isign)
double * flip_array (double *v, int n)
double gaussian (double x, double sigma, double mu)
double glog (double v, int b)
int iremainder (double x, double y)
double * lininterp (const double *x1, const double *y1, int n1, const double *x2, double *y2, int n2)
int * linspace (int first, int last)
double * linspace_dbl (double first, double last, double step, double *v, int *n)
double mad (const double *data, int n)
double maxel (double *v, int n)
int maxeli (int *v, int n)
int next_pow2 (int n)
double * resample_gsl (const double *s, int n, int newn, double *news, const gsl_interp_type *method)
double * resample_linear (const double *s, int n, int newn, double *news)
double * resample_nearest_neighbour (const double *s, int n, int newn, double *news)
double rmse (const double *r, const double *d, int n)
double * sampled_line (double *ntimes, int n, double start, double end)
void scalar_minus_dblpp (double scalar, double **m, int N, int M)
int sgn (int x)
double * sigext_smooth (double *data, int ns, int n)
double * sigext_sym (double *data, int ns, int n)
double * sigext_zeros (double *data, int ns, int n)
double * sigext_zerosr (double *data, int ns, int n)
double snr (const double *r, const double *d, int n)
void swap2d (double *v1, double *v2)
void swap2i (int *v1, int *v2)
double vnorm (const double *v, int n, int p)
double weighted_median_from_unsorted (const double *d, const double *w, int n)

Detailed Description

STATUS: unstable Additional math-functions.

Definition in file mathadd.h.


Define Documentation

#define _ISOC99_SOURCE

Definition at line 29 of file mathadd.h.

#define ABS (   a  )     ( ((a)<0) ? (-1*(a)) : (a) )

absolute value of a number

Definition at line 59 of file mathadd.h.

#define CHECK_BIT (   var,
  pos 
)    ((var) & (1<<(pos)))

check whether bit at position pos is 1.

Definition at line 64 of file mathadd.h.

#define ISODD (   x  )     ((((x)%2)==0)? (0) : (1))

Definition at line 61 of file mathadd.h.

#define MAX (   a,
  b 
)    ((a) > (b) ? (a):(b))

maximum of two elements.

Definition at line 53 of file mathadd.h.

#define MIN (   a,
  b 
)    ((a) < (b) ? (a):(b))

minimum of two elements.

Definition at line 55 of file mathadd.h.

#define PI   3.14159265358979323846

Definition at line 66 of file mathadd.h.

#define SQR (   a  )     ((a)*(a))

square of a number.

Definition at line 57 of file mathadd.h.


Function Documentation

int abscmp ( const void *  p1,
const void *  p2 
)

Definition at line 144 of file mathadd.c.

int closest_index ( const double *  v,
int  n,
double  c 
)

return index in vector that is closest to a value

Parameters:
v vector to search
n length of vector
c value to look for
Returns:
index of the value closest to c in v

Definition at line 185 of file mathadd.c.

int cmpdouble ( double  d1,
double  d2,
int  precision 
)

Compare two doubles to a certain precision. cmpdouble()

Parameters:
precision to which position after comma it is compared
Returns:
  • -1 if d1<d2
  • 0 if d1==d2
  • 1 if d1>d2

Definition at line 105 of file mathadd.c.

double* dblp_complex_to_real ( const Complex vc,
double *  vr,
int  n 
)

return the real part of the complex numbers in vc. If the imaginary part is nonzero, a warning is issued.

Parameters:
vc complex vector
vr real vector of size n, or ALLOC_IN_FCT

Definition at line 1037 of file mathadd.c.

double dblp_euclidean_distance ( const double *  v1,
const double *  v2,
int  n 
)

calculate$ |\vec{v_1} - \vec{v_2}|^2 $

Definition at line 390 of file mathadd.c.

double* dblp_init ( double *  v,
int  n,
double  val 
)
Parameters:
v - if NULL, own memory is allocated
n - length(v)
val - set v[i]=val for all i=0,...,n-1
Returns:
v

Definition at line 376 of file mathadd.c.

double dblp_max ( double *  v,
int  n,
int *  idx 
)

find max element in vector.

Parameters:
v vector of length n
idx the index in v where v[i] is max
Returns:
v[i]

Definition at line 331 of file mathadd.c.

double dblp_mean ( double *  v,
int  n 
)

Definition at line 287 of file mathadd.c.

double dblp_min ( double *  v,
int  n,
int *  idx 
)

find minimum element in vector.

Parameters:
v vector of length n
idx the index in v where v[i] is minimal
Returns:
v[i]

Definition at line 311 of file mathadd.c.

void dblp_minus_scalar ( double *  v,
int  n,
double  val 
)

remove a scalar from all values in a vector.

Definition at line 364 of file mathadd.c.

void dblp_print ( double *  v,
int  n 
)

Definition at line 280 of file mathadd.c.

void dblp_print_int ( int *  v,
int  n 
)

Definition at line 296 of file mathadd.c.

void dblp_shuffle_int ( int *  permut,
int  n 
)

create a random permutation of the elements in permut. This is crude and simple, the function grabs n pairs of indices and swaps them.

Definition at line 351 of file mathadd.c.

void dblpp_add_dblpp ( double **  m1,
const double **  m2,
int  N,
int  n 
)

add matrix m2 to matrix m1 (must be of equal dimensions

Definition at line 577 of file mathadd.c.

void dblpp_add_scalar ( double **  m,
int  N,
int  n,
double  s 
)

Definition at line 639 of file mathadd.c.

void dblpp_copy ( const double **  src,
double **  dest,
int  N,
int  M 
)

copy matrix src to dest with dimensions N,M

Definition at line 619 of file mathadd.c.

double** dblpp_delcol ( double **  m,
int  N,
int  n,
int  col 
)

Delete a column in a matrix. Memory remains allocated and everything is moved (deleted column sits at index n). Index runs from 0,...,n-1

Parameters:
m - matrix
N - num of rows
n - num of cols
col- num of col to delete

Definition at line 492 of file mathadd.c.

double** dblpp_delrow ( double **  m,
int  N,
int  n,
int  row 
)

Delete a row in a matrix. Memory remains allocated and the row pointer is moved to the end of the matrix. Index runs from 0,...,N-1

Parameters:
m - matrix
N - num of rows
n - num of cols
row- num of row to delete

Definition at line 439 of file mathadd.c.

void dblpp_divide_scalar ( double **  m,
int  N,
int  n,
double  s 
)

divide all entries in m by s

Definition at line 630 of file mathadd.c.

void dblpp_dottimes_dblpp ( double **  m1,
const double **  m2,
int  N,
int  M 
)

MatLab's M1.*M2, which is M1[i][j] = M1[i][j]*M2[i][j]; That is, m1 is overwritten with the result from the operation

Parameters:
m1,m2 
N,M dimensions of m1 and m2

Definition at line 607 of file mathadd.c.

void dblpp_free ( double **  m,
int  N 
)

free matrix memory

Definition at line 662 of file mathadd.c.

double** dblpp_init ( int  N,
int  M 
)

return a 0-initialized matrix of dimension NxM

Definition at line 453 of file mathadd.c.

int** dblpp_init_int ( int  N,
int  M 
)

return a 0-initialized integer matrix of dimension NxM

Definition at line 468 of file mathadd.c.

double dblpp_max ( const double **  m,
int  N,
int  n,
int *  i1,
int *  i2 
)

Get maximum entry from matrix. Set indices accordingly.

Parameters:
m,N,n - Nxn matrix
i1,i2 - pointer to indices of max-element. If NULL, ignored.

Definition at line 536 of file mathadd.c.

double dblpp_min ( const double **  m,
int  N,
int  n,
int *  i1,
int *  i2 
)

Get minimum entry from matrix. Set indices accordingly.

Parameters:
m,N,n - Nxn matrix
i1,i2 - pointer to indices of min-element. If NULL, ignored.

Definition at line 510 of file mathadd.c.

void dblpp_mul_scalar ( double **  m,
int  N,
int  n,
double  s 
)

multiply all entries in m by s

Definition at line 650 of file mathadd.c.

void dblpp_normalize_by_max ( double **  m,
int  M,
int  N 
)

Normalizes matrix by its maximum value:

\[ \hat{M} = \frac{1}{\mbox{max}(M)} M \]

Definition at line 425 of file mathadd.c.

void dblpp_print ( const double **  m,
int  N,
int  n 
)

print a matrix.

Definition at line 561 of file mathadd.c.

double** dblpp_rand ( double **  m,
int  N,
int  M,
double  lower,
double  upper 
)

fills m with uniformely drawn random values from [lower, upper].

Definition at line 405 of file mathadd.c.

void dblpp_sub_dblpp ( double **  dest,
const double **  src,
int  N,
int  n 
)

subtract matrix src from matrix dest (must be of equal dimensions). result is written to dest.

Definition at line 590 of file mathadd.c.

double drawsample_nearest_neighbour ( const double *  v,
int  n,
double  x 
)

Draw a sample from a discrete distribution (nearest neighbour).

Parameters:
v,n the distribution as a funciton of [0,...,n]
x a real value from the interval [0,...,n]
Returns:
v(x) by nn-interpolation

Definition at line 701 of file mathadd.c.

Complex* expand_polynomial_from_roots ( const Complex roots,
int  n,
Complex coeffs 
)

expand a polynomial given in root-form

\[ p(x) = x_0(x-x_1)(x-x_2)\cdots (x-x_n) \]

to find the coefficients a_0, ..., a_n of

\[ p(x) = a_0 + a_1 x_1 + \cdots + a_n x_n \]

Parameters:
roots x_0,...,x_n
coeffs n+1 numbers to hold the resulting coefficients or ALLOC_IN_FCT

Definition at line 1006 of file mathadd.c.

void fft ( double *  data,
unsigned long  nn,
int  isign 
)

1D Fourier transform (FFT). Taken from Numerical Recipes in C.

Parameters:
data - array to be tranformed (input/output); for input, is assumed to be complex, i.e. [ real, imag, real, imag, ... ] data contains nn complex numbers and 2*nn entries.
nn - must be a power of two, not checked for!; length(data)=2*nn
isign - 1: forward; -1: ifft;
Returns:
data is now the FT in complex numbers

Definition at line 827 of file mathadd.c.

double* flip_array ( double *  v,
int  n 
)

Definition at line 804 of file mathadd.c.

double gaussian ( double  x,
double  sigma,
double  mu 
)

$ G(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp{\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)} $

Definition at line 975 of file mathadd.c.

double glog ( double  v,
int  b 
)

Definition at line 116 of file mathadd.c.

int iremainder ( double  x,
double  y 
)

computes remainder of x/y using $ r(x,y) = \left\lfloor x - y\left\lfloor \frac{x}{y}\right\rfloor \right\rfloor $

Definition at line 958 of file mathadd.c.

double* lininterp ( const double *  x1,
const double *  y1,
int  n1,
const double *  x2,
double *  y2,
int  n2 
)

performs linear interpolation for the values x2, given x1 and y1. A pointer to the interpolated values y2 is returned. The x2 must ly within the x1, the x1 must be increasing in magnitude.

Definition at line 254 of file mathadd.c.

int* linspace ( int  first,
int  last 
)

same as first:last in matlab. Data ist allocated in the function and needs to be freed by the caller.

Definition at line 236 of file mathadd.c.

double* linspace_dbl ( double  first,
double  last,
double  step,
double *  v,
int *  n 
)

same as first:step:last in matlab. A sequence first, first+step, first+(2*step), ... ,last is computed. The length of this sequence is $ n=\lfloor \frac{last-first}{step} \rfloor+1 $

Parameters:
v either enough free allocated space, or ALLOC_IN_FCT (NULL)

  • data ist allocated in the function and needs to be freed by the caller.
n - number of doubles in v (return value)

Definition at line 218 of file mathadd.c.

double mad ( const double *  data,
int  n 
)

Definition at line 121 of file mathadd.c.

double maxel ( double *  v,
int  n 
)

Definition at line 163 of file mathadd.c.

int maxeli ( int *  v,
int  n 
)

Definition at line 171 of file mathadd.c.

int next_pow2 ( int  n  ) 

return min_k( 2^k | 2^k>n ).

Definition at line 947 of file mathadd.c.

double* resample_gsl ( const double *  s,
int  n,
int  newn,
double *  news,
const gsl_interp_type *  method 
)

Resample a vector to match a new length using GSL's interpolation options:

  1. gsl_interp_linear
  2. gsl_interp_polynomial
  3. gsl_interp_cspline
  4. gsl_interp_cspline_periodic
  5. gsl_interp_akima
  6. gsl_interp_akima_periodic

Assume that s is from [0,...,n] and resample, such that the new x is running from [0,...,newn] where y[newn]=y_old[n].

Parameters:
s,n the original vector
newn the new length of the vector
news caller-allocated memory of at least length newn (if NULL, the function allocates own memory)
method one of GSL interpolation types (see above)
Returns:
resampled vector in news

Definition at line 774 of file mathadd.c.

double* resample_linear ( const double *  s,
int  n,
int  newn,
double *  news 
)

Resample a vector to match a new length (linear interpolation).

Parameters:
s,n the original vector
newn the new length of the vector
news caller-allocated memory of at least length newn (if NULL, the function allocates own memory)
Returns:
resampled vector in news
See also:
drawsample_linear()

Definition at line 752 of file mathadd.c.

double* resample_nearest_neighbour ( const double *  s,
int  n,
int  newn,
double *  news 
)

Resample a vector to match a new length (NN-interpolation). Assume that s is from [0,...,n] and resample, such that the new x is running from [0,...,newn] where y[newn]=y_old[n].

Parameters:
s,n the original vector
newn the new length of the vector
news caller-allocated memory of at least length newn (if NULL, the function allocates own memory)
Returns:
resampled vector in news
See also:
drawsample_nearest_neighbour()

Definition at line 728 of file mathadd.c.

double rmse ( const double *  r,
const double *  d,
int  n 
)

$ RMSE = \sqrt{ \frac{1}{\#r} \sum{ (r-d)^2)}} $

Definition at line 74 of file mathadd.c.

double* sampled_line ( double *  ntimes,
int  n,
double  start,
double  end 
)

Definition at line 198 of file mathadd.c.

void scalar_minus_dblpp ( double  scalar,
double **  m,
int  N,
int  M 
)

Computes: m[i][j] = scalar-m[i][j]

Definition at line 983 of file mathadd.c.

int sgn ( int  x  ) 

signum function

Definition at line 675 of file mathadd.c.

double* sigext_smooth ( double *  data,
int  ns,
int  n 
)
 [1 2 3 - - - - -] -> [1 1 1 2 3 3 3 3] 

Definition at line 930 of file mathadd.c.

double* sigext_sym ( double *  data,
int  ns,
int  n 
)
 [1 2 3 - - - - -] -> [2 1 1 2 3 3 2 1] 

Definition at line 914 of file mathadd.c.

double* sigext_zeros ( double *  data,
int  ns,
int  n 
)
 [1 2 3 - - -] -> [0 1 2 3 0 0] 

Definition at line 889 of file mathadd.c.

double* sigext_zerosr ( double *  data,
int  ns,
int  n 
)
 [1 2 3 - - -] -> [1 2 3 0 0 0] 

Definition at line 906 of file mathadd.c.

double snr ( const double *  r,
const double *  d,
int  n 
)

$ SNR = 10\log_{10} \frac{\sum{r^2}}{\sum{(r-d)^2}} $

Definition at line 85 of file mathadd.c.

void swap2d ( double *  v1,
double *  v2 
)

Definition at line 687 of file mathadd.c.

void swap2i ( int *  v1,
int *  v2 
)

Definition at line 680 of file mathadd.c.

double vnorm ( const double *  v,
int  n,
int  p 
)

Definition at line 153 of file mathadd.c.

double weighted_median_from_unsorted ( const double *  d,
const double *  w,
int  n 
)

Weighted Median computation. Formula:

\[ WM = y_{(k)}\qquad k = \max\left\{h:\sum_{i=h}^n w_{(i)} \ge \frac{1}{2}\sum_{i=1}^{n} w_i\right\} \]

Parameters:
d - data
w - corresponding weights (>=0)

Definition at line 33 of file mathadd.c.