}
private static void ComputeConstrainedSparseCode(double[] desc, ArrayList<Integer> cl, double[][] centers){
// Matrix S : initial SIFT vector to approximate : S = C*Alpha + epsilon
Matrix S = new Matrix(desc, desc.length);
// Matrix S : Matrix containing coordinates of the k-nn Centers from S
double [][] ClosestCenters = new double[centers[0].length][knn];
for(int i=0;i<knn;i++){
for(int j=0;j<centers[0].length;j++){
ClosestCenters[j][i] = centers[cl.get(i)][j];
}
}
Matrix C = new Matrix(ClosestCenters);
// Contraint R*Alpha=r - Here we choose the constraint Sum Alpha = 1
// R : row vector (1,...,1) - r scalar value (1)
double [] constraints = new double[knn];
for(int i=0;i<knn;i++){
constraints[i] = 1.0;
}
Matrix R = new Matrix(constraints, 1);
Matrix Rt = R.transpose();
Matrix r = new Matrix(1, 1);
r.set(0, 0,1.0);
// Least-square Optimisation
Matrix Ct = C.transpose();
Matrix Gamma = (Ct.times(C)).inverse();
// Least square solution without constraints
Matrix SC = (Gamma.times(Ct)).times(S);
// Least square solution with constraint R*Alpha=r
Matrix SCc = SC.minus( (Gamma.times(Rt)).times( (R.times(Gamma).times(Rt) ).inverse() ).times((R.times(SC)).minus(r)) ) ;
// Sparse code : abs(Alpha)
SparseCode = new double[knn];
for(int i=0;i<knn;i++){
SparseCode[i] = Math.abs(SCc.get(i,0));
}
// Debog
//System.out.println("********************* Constrained Sparse code computation ********************");
// Matrix Src = C.times(SCc);