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 
237 
247 void SubtractVector(unsigned int s,double *v1,double *v2);
248 
259 void DifferenceVector(unsigned int s,double *v1,double *v2,double *v);
260 
273 void DifferenceVectorTopology(unsigned int s,unsigned int *tp,
274  double *v1,double *v2,double *v);
275 
286 double Norm(unsigned int s,double *v);
287 
301 double NormWithStride(unsigned int s,unsigned int st,double *v);
302 
315 double SquaredDistance(unsigned int s,double *v1,double *v2);
316 
329 double Distance(unsigned int s,double *v1,double *v2);
330 
345 double SquaredDistanceTopology(unsigned int s,unsigned int *tp,double *v1,double *v2);
346 
365 double SquaredDistanceTopologyMin(double t2,unsigned int s,unsigned int *tp,
366  double *v1,double *v2);
367 
382 double DistanceTopology(unsigned int s,unsigned int *tp,
383  double *v1,double *v2);
384 
405 double DistanceTopologyMin(double t,unsigned int s,unsigned int *tp,
406  double *v1,double *v2);
407 
422 boolean CrossTopologyBorder(unsigned int s,unsigned int *tp,double *v1,double *v2);
423 
432 void Normalize(unsigned int s,double *v);
433 
444 double Mean(unsigned int s,double *v);
445 
457 double StdDev(unsigned int s,double m,double *v);
458 
469 void ArrayPi2Pi(unsigned int n,unsigned int *t,double *a);
470 
482 void GetRow(unsigned int k,unsigned int r,unsigned int c,double *m,double *v);
483 
495 void GetColumn(unsigned int k,unsigned int r,unsigned int c,double *m,double *v);
496 
508 void SetRow(double *v,unsigned int k,unsigned int r,unsigned int c,double *m);
509 
521 void SetColumn(double *v,unsigned int k,unsigned int r,unsigned int c,double *m);
522 
535 double RowSquaredNorm(unsigned int k,unsigned int r,unsigned int c,double *m);
536 
549 double ColumnSquaredNorm(unsigned int k,unsigned int r,unsigned int c,double *m);
550 
566 void MatrixVectorProduct(unsigned int r,unsigned int c,double *A,double *b,double *o);
567 
583 void TMatrixVectorProduct(unsigned int r,unsigned int c,double *A,double *b,double *o);
584 
603 void TMatrixVectorStrideProduct(unsigned int r,unsigned int c,double *A,unsigned int s,double *b,double *o);
604 
619 void MatrixMatrixProduct(unsigned int ra,unsigned int ca,double *A,
620  unsigned int cb,double *B,double *C);
635 void TMatrixMatrixProduct(unsigned int ra,unsigned int ca,double *A,
636  unsigned int cb,double *B,double *C);
637 
660 double MinCosinusBetweenSubSpaces(unsigned int m,unsigned int k,double *T1,double *T2);
661 
676 void SubMatrixFromMatrix(unsigned int nr1,unsigned int nc1,double *m1,
677  unsigned int nri,unsigned int nci,
678  unsigned int nr,unsigned int nc,double *m);
679 
694 void SubMatrixFromTMatrix(unsigned int nr1,unsigned int nc1,double *m1,
695  unsigned int nri,unsigned int nci,
696  unsigned int nr,unsigned int nc,double *m);
697 
708 void PrintVector(FILE *f,char *label,unsigned int n,double *v);
709 
721 void PrintMatrix(FILE *f,char *label,unsigned int r,unsigned int c,double *m);
722 
734 void PrintTMatrix(FILE *f,char *label,unsigned int r,unsigned int c,double *m);
735 
736 #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 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 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 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 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 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 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.