From 19d893c4554f7f2cc9a75111cec40712c698e7e2 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 16 Dec 2019 18:40:30 +0100
Subject: [PATCH] Remove debug traces, inline matrix index function

---
 pkg/R/optimParams.R |  4 +--
 pkg/src/functions.c | 87 +++++++--------------------------------------
 2 files changed, 15 insertions(+), 76 deletions(-)

diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R
index f06581e..c1d7fe8 100644
--- a/pkg/R/optimParams.R
+++ b/pkg/R/optimParams.R
@@ -267,8 +267,8 @@ setRefClass(
           ci=c(-1,rep(0,K-1)) )
         if (loop < loopMax) #avoid computing an extra W
           W <<- computeW(expArgs(op_res$par))
-        print(op_res$value) #debug
-        print(expArgs(op_res$par)) #debug
+        #print(op_res$value) #debug
+        #print(expArgs(op_res$par)) #debug
       }
 
       expArgs(op_res$par)
diff --git a/pkg/src/functions.c b/pkg/src/functions.c
index feea3ad..1c7295a 100644
--- a/pkg/src/functions.c
+++ b/pkg/src/functions.c
@@ -1,13 +1,13 @@
 #include <stdlib.h>
 
 // Index matrix (by columns)
-int mi(int i, int j, int d1, int d2)
+int inline mi(int i, int j, int d1, int d2)
 {
   return j*d1 + i;
 }
 
 // Index 3-tensor (by columns, matrices ordered by last dim)
-int ti(int i, int j, int k, int d1, int d2, int d3)
+int inline ti(int i, int j, int k, int d1, int d2, int d3)
 {
   return k*d1*d2 + j*d1 + i;
 }
@@ -54,62 +54,8 @@ void Moments_M3(double* X, double* Y, int* pn, int* pd, double* M3)
   }
 }
 
-#include <stdio.h>
-
 // W = 1/N sum( t(g(Zi,theta)) g(Zi,theta) )
 // with g(Zi, theta) = i-th contribution to all moments (size dim) - real moments
-//void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W)
-//{
-//  int n=*pn, d=*pd;
-//  int dim = d + d*d + d*d*d;
-//  //double* W = (double*)malloc(dim*dim*sizeof(double));
-//
-//  // (Re)Initialize W:
-//  for (int j=0; j<dim; j++)
-//  {
-//    for (int k=0; k<dim; k++)
-//      W[j*dim+k] = 0.0;
-//  }
-//  double* g = (double*)malloc(dim*sizeof(double));
-//  for (int i=0; i<n; i++)
-//  {
-//    // g == gi:
-//    for (int j=0; j<d; j++)
-//      g[j] = Y[i] * X[mi(i,j,n,d)] - M[j];
-//    for (int j=d; j<d+(d*d); j++)
-//    {
-//      int idx1 = (j-d) % d; //num row
-//      int idx2 = ((j-d) - idx1) / d; //num col
-//      g[j] = 0.0;
-//      if (idx1 == idx2)
-//        g[j] -= Y[i];
-//      g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j];
-//    }
-//    for (int j=d+d*d; j<dim; j++)
-//    {
-//      int idx1 = (j-d-d*d) % d; //num row
-//      int idx2 = ((j-d-d*d - idx1) / d) %d; //num col
-//      int idx3 = (((j-d-d*d - idx1) / d) - idx2) / d; //num "depth"
-//      g[j] = 0.0;
-//      if (idx1 == idx2)
-//        g[j] -= Y[i] * X[mi(i,idx3,n,d)];
-//      if (idx1 == idx3)
-//        g[j] -= Y[i] * X[mi(i,idx2,n,d)];
-//      if (idx2 == idx3)
-//        g[j] -= Y[i] * X[mi(i,idx1,n,d)];
-//      g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j];
-//    }
-//    // Add 1/n t(gi) %*% gi to W
-//    for (int j=0; j<dim; j++)
-//    {
-//      for (int k=0; k<dim; k++)
-//        W[j*dim+k] += g[j] * g[k] / n;
-//    }
-//  }
-//  free(g);
-//}
-
-// Optimisation attempt:
 void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W)
 {
   int n=*pn, d=*pd;
@@ -125,21 +71,17 @@ void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W)
   double* g = (double*)malloc(dim*sizeof(double));
   for (int i=0; i<n; i++)
   {
-    printf("i: %i\n",i);
     // g == gi:
     for (int j=0; j<d; j++)
-      g[j] = (Y[i] ? X[mi(i,j,n,d)] - M[j] : 0.0);
+      g[j] = Y[i] * X[mi(i,j,n,d)] - M[j];
     for (int j=d; j<d+(d*d); j++)
     {
       int idx1 = (j-d) % d; //num row
       int idx2 = ((j-d) - idx1) / d; //num col
       g[j] = 0.0;
-      if (Y[i])
-      {
-        if (idx1 == idx2)
-          g[j]--;
-        g[j] += X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j];
-      }
+      if (idx1 == idx2)
+        g[j] -= Y[i];
+      g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j];
     }
     for (int j=d+d*d; j<dim; j++)
     {
@@ -147,16 +89,13 @@ void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W)
       int idx2 = ((j-d-d*d - idx1) / d) %d; //num col
       int idx3 = (((j-d-d*d - idx1) / d) - idx2) / d; //num "depth"
       g[j] = 0.0;
-      if (Y[i])
-      {
-        if (idx1 == idx2)
-          g[j] -= X[mi(i,idx3,n,d)];
-        if (idx1 == idx3)
-          g[j] -= X[mi(i,idx2,n,d)];
-        if (idx2 == idx3)
-          g[j] -= X[mi(i,idx1,n,d)];
-        g[j] += X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j];
-      }
+      if (idx1 == idx2)
+        g[j] -= Y[i] * X[mi(i,idx3,n,d)];
+      if (idx1 == idx3)
+        g[j] -= Y[i] * X[mi(i,idx2,n,d)];
+      if (idx2 == idx3)
+        g[j] -= Y[i] * X[mi(i,idx1,n,d)];
+      g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j];
     }
     // Add 1/n t(gi) %*% gi to W
     for (int j=0; j<dim; j++)
-- 
2.44.0