first commit
[synclust.git] / src / sources / utils / algebra.c
1 #include "sources/utils/algebra.h"
2 #include <stdlib.h>
3 #include <math.h>
4
5 // small useful function to transform a matrix as given by R
6 // into a easier-to-handle one.
7 double* transpose(double* M, int nrow, int ncol)
8 {
9 double* Mtr = (double*)malloc(nrow*ncol*sizeof(double));
10 for (int i=0; i<nrow; i++)
11 {
12 for (int j=0; j<ncol; j++)
13 Mtr[i*ncol+j] = M[i+nrow*j];
14 }
15 return Mtr;
16 }
17
18 // auxiliary to compute euclidian norm
19 double norm2(double* v, int length)
20 {
21 double norm = 0.0;
22 for (int j=0; j<length; j++)
23 norm += v[j]*v[j];
24 return sqrt(norm);
25 }
26
27 // auxiliary to compute euclidian distance
28 double distance2(double* v1, double* v2, int length)
29 {
30 double distance = 0.0, diff;
31 for (int j=0; j<length; j++)
32 {
33 diff = v1[j]-v2[j];
34 distance += diff*diff;
35 }
36 return sqrt(distance);
37 }