Fix code which is now running, but computations are probably wrong
[morpheus.git] / pkg / src / functions.c
CommitLineData
cbd88fe5
BA
1#include <stdlib.h>
2
d08fef42 3// Index matrix (by columns)
cbd88fe5
BA
4int mi(int i, int j, int d1, int d2)
5{
d08fef42 6 return j*d1 + i;
cbd88fe5
BA
7}
8
d08fef42 9// Index 3-tensor (by columns, matrices ordered by last dim)
cbd88fe5
BA
10int ti(int i, int j, int k, int d1, int d2, int d3)
11{
12 return k*d1*d2 + j*d1 + i;
13}
14
d08fef42 15// Empirical cross-moment of order 2 between X size nxd and Y size n
cbd88fe5
BA
16void Moments_M2(double* X, double* Y, int* pn, int* pd, double* M2)
17{
18 int n=*pn, d=*pd;
19 //double* M2 = (double*)calloc(d*d,sizeof(double));
20
21 // M2 = E[Y*X^*2] - E[Y*e^*2] = E[Y (X^*2 - I)]
22 for (int j=0; j<d; j++)
23 {
24 for (int i=0; i<n; i++)
25 {
26 M2[mi(j,j,d,d)] -= Y[i] / n;
27 for (int k=0; k<d; k++)
28 M2[mi(j,k,d,d)] += Y[i] * X[mi(i,j,n,d)]*X[mi(i,k,n,d)] / n;
29 }
30 }
31}
32
d08fef42 33// Empirical cross-moment of order 3 between X size nxd and Y size n
cbd88fe5
BA
34void Moments_M3(double* X, double* Y, int* pn, int* pd, double* M3)
35{
36 int n=*pn, d=*pd;
37 //double* M3 = (double*)calloc(d*d*d,sizeof(double));
38
39 // M3 = E[Y*X^*3] - E[Y*e*X*e] - E[Y*e*e*X] - E[Y*X*e*e]
40 for (int j=0; j<d; j++)
41 {
42 for (int k=0; k<d; k++)
43 {
44 for (int i=0; i<n; i++)
45 {
46 double tensor_elt = Y[i]*X[mi(i,k,n,d)] / n;
47 M3[ti(j,k,j,d,d,d)] -= tensor_elt;
48 M3[ti(j,j,k,d,d,d)] -= tensor_elt;
49 M3[ti(k,j,j,d,d,d)] -= tensor_elt;
50 for (int o=0; o<d; o++)
51 M3[ti(j,k,o,d,d,d)] += Y[i] * X[mi(i,j,n,d)]*X[mi(i,k,n,d)]*X[mi(i,o,n,d)] / n;
52 }
53 }
54 }
55}
4263503b 56
4bf8494d
BA
57#include <stdio.h>
58
d08fef42
BA
59// W = 1/N sum( t(g(Zi,theta)) g(Zi,theta) )
60// with g(Zi, theta) = i-th contribution to all moments (size dim) - real moments
7737c2fa 61void Compute_Omega(double* X, double* Y, double* M, int* pn, int* pd, double* W)
4263503b 62{
7737c2fa 63 int n=*pn, d=*pd;
b389a46a 64 int dim = d + d*d + d*d*d;
4bf8494d
BA
65
66//printf("X: \n");
67//for (int kk=0; kk<d*n; kk++) printf("%f ",X[kk]);
68//printf("\n");
69//printf("Y: \n");
70//for (int kk=0; kk<n; kk++) printf("%f ",Y[kk]);
71//printf("\n");
72//printf("M: \n");
73//for (int kk=0; kk<dim; kk++) printf("%f ",M[kk]);
74//printf("\n");
75
76 // (Re)Initialize W:
77 for (int j=0; j<dim; j++)
78 {
79 for (int k=0; k<dim; k++)
80 W[j*dim+k] = 0.0;
81 }
82
d08fef42 83 //double* W = (double*)calloc(dim*dim,sizeof(double));
4bf8494d 84 double* g = (double*)malloc(dim*sizeof(double));
7737c2fa
BA
85 for (int i=0; i<n; i++)
86 {
d08fef42
BA
87 // Fill gi:
88 for (int j=0; j<d; j++)
4bf8494d 89 g[j] = Y[i] * X[mi(i,j,n,d)] - M[j];
d08fef42
BA
90 for (int j=d; j<d+(d*d); j++)
91 {
92 int idx1 = (j-d) % d; //num row
93 int idx2 = ((j-d) - idx1) / d; //num col
94 g[j] = 0.0;
95 if (idx1 == idx2)
96 g[j] -= Y[i];
4bf8494d 97 g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j];
d08fef42
BA
98 }
99 for (int j=d+d*d; j<dim; j++)
100 {
101 int idx1 = (j-d-d*d) % d; //num row
102 int idx2 = ((j-d-d*d - idx1) / d) %d; //num col
103 int idx3 = (((j-d-d*d - idx1) / d) - idx2) / d; //num "depth"
104 g[j] = 0.0;
d08fef42
BA
105 if (idx1 == idx2)
106 g[j] -= Y[i] * X[mi(i,idx3,n,d)];
107 if (idx1 == idx3)
108 g[j] -= Y[i] * X[mi(i,idx2,n,d)];
109 if (idx2 == idx3)
110 g[j] -= Y[i] * X[mi(i,idx1,n,d)];
4bf8494d 111 g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j];
d08fef42 112 }
4bf8494d
BA
113
114//printf("i=%i, g=: \n", i);
115//for (int kk=0; kk<d; kk++) printf("%f ",g[kk]);
116//printf("\n");
117
d08fef42
BA
118 // Add 1/n t(gi) %*% gi to W
119 for (int j=0; j<dim; j++)
120 {
121 for (int k=0; k<dim; k++)
122 W[j*dim+k] += g[j] * g[k] / n;
123 }
7737c2fa 124 }
d08fef42 125 free(g);
4bf8494d
BA
126
127// for (int j=0; j<dim; j++)
128// {
129// printf("\n");
130// for (int k=0; k<dim; k++)
131// printf("%f ",W[j*dim+k]);
132// }
4263503b 133}