basic_algebra.h
Go to the documentation of this file.
1 #ifndef BASICALGEBRAH
2 
16 #include "defines.h"
17 #include "boolean.h"
18 
19 #include <stdio.h>
20 
21 #if !defined(_LAPACK) && !defined(_GSL)
22  #error "CuikSuite requires LAPACK or GSL (LAPACK preferred)"
23 #endif
24 
25 #ifdef _CBLAS
26  #ifdef _GSL_CBLAS
27  /* Use the CBLAS included in the GSL */
28  #include <gsl/gsl_cblas.h>
29  #else
30  /* Use the system CBLAS */
31  #include <cblas.h>
32  #endif
33 
40  #define CBLAS_INLINE inline
41 #else
42 
48  #define CBLAS_INLINE
49 #endif
50 
51 #ifdef _GSL
52 
58  #define ROW_MAJOR 1
59 #endif
60 
61 #ifdef _LAPACK
62 
68  #define ROW_MAJOR 0
69 #endif
70 
81 #define RC2INDEX(i,j,nr,nc) (ROW_MAJOR?(i)*(nc)+(j):(j)*(nr)+(i))
82 
83 
84 
85 /*******************************************************************************/
86 /* The functions below have a naif implementation and a more efficient one */
87 /* based on blas */
88 
100 double GeneralDotProduct(unsigned int s,double *v1,double *v2);
101 
111 void ScaleVector(double f,unsigned int s,double *v);
112 
124 void ScaleVector2(double f,unsigned int s,double *v,double *vout);
125 
135 void AccumulateVector(unsigned int s,double *v1,double *v2);
136 
147 void SumVector(unsigned int s,double *v1,double *v2,double *v);
148 
161 void SumVectorScale(unsigned int s,double *v1,double w,double *v2,double *v);
162 
172 void CosVector(unsigned int s,double *v,double *co);
173 
183 void SinVector(unsigned int s,double *v,double *si);
184 
196 unsigned int MaxVectorElement(unsigned int m,double *v);
197 
209 double MaxVector(unsigned int m,double *v);
210 
222 unsigned int MinVectorElement(unsigned int m,double *v);
223 
235 double MinVector(unsigned int m,double *v);
236 
253 void Vector2Range(double l,double u,double mode,
254  unsigned int m,double *v);
255 
265 void SubtractVector(unsigned int s,double *v1,double *v2);
266 
277 void DifferenceVector(unsigned int s,double *v1,double *v2,double *v);
278 
291 void DifferenceVectorSubset(unsigned int s,
292  boolean *subset,
293  double *v1,double *v2,double *v);
306 void DifferenceVectorTopology(unsigned int s,unsigned int *tp,
307  double *v1,double *v2,double *v);
308 
326 void DifferenceVectorTopologySubset(unsigned int s,unsigned int *tp,
327  boolean *subset,
328  double *v1,double *v2,double *v);
329 
340 double Norm(unsigned int s,double *v);
341 
355 double NormWithStride(unsigned int s,unsigned int st,double *v);
356 
369 double SquaredDistance(unsigned int s,double *v1,double *v2);
370 
384 double SquaredDistanceSubset(unsigned int s,boolean *subset,
385  double *v1,double *v2);
386 
399 double Distance(unsigned int s,double *v1,double *v2);
400 
415 double SquaredDistanceTopology(unsigned int s,unsigned int *tp,double *v1,double *v2);
416 
435 double SquaredDistanceTopologyMin(double t2,unsigned int s,unsigned int *tp,
436  double *v1,double *v2);
437 
452 double DistanceTopology(unsigned int s,unsigned int *tp,
453  double *v1,double *v2);
454 
472 double DistanceTopologySubset(unsigned int s,unsigned int *tp,
473  boolean *subset,
474  double *v1,double *v2);
475 
496 double DistanceTopologyMin(double t,unsigned int s,unsigned int *tp,
497  double *v1,double *v2);
498 
513 boolean CrossTopologyBorder(unsigned int s,unsigned int *tp,double *v1,double *v2);
514 
523 void Normalize(unsigned int s,double *v);
524 
535 double Mean(unsigned int s,double *v);
536 
548 double StdDev(unsigned int s,double m,double *v);
549 
560 void ArrayPi2Pi(unsigned int n,unsigned int *t,double *a);
561 
573 void GetRow(unsigned int k,unsigned int r,unsigned int c,double *m,double *v);
574 
586 void GetColumn(unsigned int k,unsigned int r,unsigned int c,double *m,double *v);
587 
599 void SetRow(double *v,unsigned int k,unsigned int r,unsigned int c,double *m);
600 
612 void SetColumn(double *v,unsigned int k,unsigned int r,unsigned int c,double *m);
613 
626 double RowSquaredNorm(unsigned int k,unsigned int r,unsigned int c,double *m);
627 
640 double ColumnSquaredNorm(unsigned int k,unsigned int r,unsigned int c,double *m);
641 
657 void MatrixVectorProduct(unsigned int r,unsigned int c,double *A,double *b,double *o);
658 
674 void TMatrixVectorProduct(unsigned int r,unsigned int c,double *A,double *b,double *o);
675 
694 void TMatrixVectorStrideProduct(unsigned int r,unsigned int c,double *A,unsigned int s,double *b,double *o);
695 
710 void MatrixMatrixProduct(unsigned int ra,unsigned int ca,double *A,
711  unsigned int cb,double *B,double *C);
726 void TMatrixMatrixProduct(unsigned int ra,unsigned int ca,double *A,
727  unsigned int cb,double *B,double *C);
728 
751 double MinCosinusBetweenSubSpaces(unsigned int m,unsigned int k,double *T1,double *T2);
752 
767 void SubMatrixFromMatrix(unsigned int nr1,unsigned int nc1,double *m1,
768  unsigned int nri,unsigned int nci,
769  unsigned int nr,unsigned int nc,double *m);
770 
785 void SubMatrixFromTMatrix(unsigned int nr1,unsigned int nc1,double *m1,
786  unsigned int nri,unsigned int nci,
787  unsigned int nr,unsigned int nc,double *m);
788 
799 void PrintVector(FILE *f,char *label,unsigned int n,double *v);
800 
810 void SaveVector(FILE *f,unsigned int n,double *v);
811 
823 void PrintMatrix(FILE *f,char *label,unsigned int r,unsigned int c,double *m);
824 
836 void PrintTMatrix(FILE *f,char *label,unsigned int r,unsigned int c,double *m);
837 
838 #endif
Definition of the boolean type.
void DifferenceVector(unsigned int s, double *v1, double *v2, double *v)
Substracts two vectors.
void MatrixMatrixProduct(unsigned int ra, unsigned int ca, double *A, unsigned int cb, double *B, double *C)
C = A * B.
void PrintVector(FILE *f, char *label, unsigned int n, double *v)
Prints a vector.
double MaxVector(unsigned int m, double *v)
Value of the maximum element of a vector.
double ColumnSquaredNorm(unsigned int k, unsigned int r, unsigned int c, double *m)
Computes the squared norm of a column of a matrix.
void CosVector(unsigned int s, double *v, double *co)
Cosine on a vector.
double DistanceTopologySubset(unsigned int s, unsigned int *tp, boolean *subset, double *v1, double *v2)
Computes the distance of two points along a subset of components.
double SquaredDistanceTopology(unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the squared distance of two points.
void PrintMatrix(FILE *f, char *label, unsigned int r, unsigned int c, double *m)
Prints a matrix.
void SubtractVector(unsigned int s, double *v1, double *v2)
Substracts a vector from another vector.
unsigned int MaxVectorElement(unsigned int m, double *v)
Index of the maximum element of a vector.
double StdDev(unsigned int s, double m, double *v)
Computes the standard deviation.
void ScaleVector2(double f, unsigned int s, double *v, double *vout)
Scales a vector.
Definition: basic_algebra.c:42
double DistanceTopologyMin(double t, unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the distance of two points, if it is below a given threshold.
double Norm(unsigned int s, double *v)
Computes the norm of a vector.
boolean CrossTopologyBorder(unsigned int s, unsigned int *tp, double *v1, double *v2)
Determines if the line between two points crosses the topology boder.
double RowSquaredNorm(unsigned int k, unsigned int r, unsigned int c, double *m)
Computes the squared norm of a row of a matrix.
void MatrixVectorProduct(unsigned int r, unsigned int c, double *A, double *b, double *o)
Product of a matrix and a vector.
void Normalize(unsigned int s, double *v)
Normalizes a vector.
double DistanceTopology(unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the distance of two points.
void AccumulateVector(unsigned int s, double *v1, double *v2)
Adds a vector to another vectors.
Definition: basic_algebra.c:55
void TMatrixMatrixProduct(unsigned int ra, unsigned int ca, double *A, unsigned int cb, double *B, double *C)
C = A^t * B.
unsigned int MinVectorElement(unsigned int m, double *v)
Index of the minimum element of a vector.
void SaveVector(FILE *f, unsigned int n, double *v)
Saves a vector.
void SubMatrixFromMatrix(unsigned int nr1, unsigned int nc1, double *m1, unsigned int nri, unsigned int nci, unsigned int nr, unsigned int nc, double *m)
Defines a submatrix in a matrix.
void SumVector(unsigned int s, double *v1, double *v2, double *v)
Adds two vectors.
Definition: basic_algebra.c:67
void TMatrixVectorStrideProduct(unsigned int r, unsigned int c, double *A, unsigned int s, double *b, double *o)
Product of a transposed matrix and a vector.
double Mean(unsigned int s, double *v)
Computes the mean.
void SinVector(unsigned int s, double *v, double *si)
Sine on a vector.
Definitions of constants and macros used in several parts of the cuik library.
void SetColumn(double *v, unsigned int k, unsigned int r, unsigned int c, double *m)
Sets a column of a matrix.
void SubMatrixFromTMatrix(unsigned int nr1, unsigned int nc1, double *m1, unsigned int nri, unsigned int nci, unsigned int nr, unsigned int nc, double *m)
Defines a submatrix in a matrix.
double MinCosinusBetweenSubSpaces(unsigned int m, unsigned int k, double *T1, double *T2)
Computes the cosinus of the maximum angle between two lineal sub-spaces.
void DifferenceVectorTopologySubset(unsigned int s, unsigned int *tp, boolean *subset, double *v1, double *v2, double *v)
Substracts two vectors along some of its components.
void SetRow(double *v, unsigned int k, unsigned int r, unsigned int c, double *m)
Sets a row of a matrix.
double Distance(unsigned int s, double *v1, double *v2)
Computes the distance of two points.
double SquaredDistanceTopologyMin(double t2, unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the squared distance of two points.
void DifferenceVectorSubset(unsigned int s, boolean *subset, double *v1, double *v2, double *v)
Substracts two vectors along some of its components.
void ArrayPi2Pi(unsigned int n, unsigned int *t, double *a)
Applies PI2PI to an array.
void GetRow(unsigned int k, unsigned int r, unsigned int c, double *m, double *v)
Gets a row from a matrix.
void ScaleVector(double f, unsigned int s, double *v)
Scales a vector.
Definition: basic_algebra.c:30
double NormWithStride(unsigned int s, unsigned int st, double *v)
Computes the norm of a vector.
void SumVectorScale(unsigned int s, double *v1, double w, double *v2, double *v)
Adds two vectors with a scale.
Definition: basic_algebra.c:86
double MinVector(unsigned int m, double *v)
Value of the minimum element of a vector.
double SquaredDistanceSubset(unsigned int s, boolean *subset, double *v1, double *v2)
Computes the squared distance of two points along some components.
double GeneralDotProduct(unsigned int s, double *v1, double *v2)
Computes the dot product of two general vectors.
Definition: basic_algebra.c:15
void GetColumn(unsigned int k, unsigned int r, unsigned int c, double *m, double *v)
Gets a column from a matrix.
double SquaredDistance(unsigned int s, double *v1, double *v2)
Computes the squared distance of two points.
void Vector2Range(double l, double u, double mode, unsigned int m, double *v)
Scales a vector to a given range.
void PrintTMatrix(FILE *f, char *label, unsigned int r, unsigned int c, double *m)
Prints a transposed matrix.
void TMatrixVectorProduct(unsigned int r, unsigned int c, double *A, double *b, double *o)
Product of a transposed matrix and a vector.
void DifferenceVectorTopology(unsigned int s, unsigned int *tp, double *v1, double *v2, double *v)
Substracts two vectors.