Java linear algebra libraries results are incorrect

228 Views Asked by At

I have an algorithm implemented in Python uses numpy library for linear algebra. I want to implement it in Java for Android app, I tried many libraries like Jama.

The algorithm for check if a set of 3D points has the same pattern of another set.

this is the implementation in Python that works fine:

def compute_similarity(original_shape, shape, nearest_neighbors= False, debug= False):    
    reference_shape = np.copy(original_shape)
    # calculate rotation matrix 3 x 3
    U, _, V = svd(reference_shape.dot(shape.T))
    rotation_matrix = U.dot(V)
    rotated_shape = np.dot(rotation_matrix, shape)
    # Perform procrustes analysis
    ref_shape_mean_vals = np.mean(reference_shape, axis=1)[:, np.newaxis]
    shape_mean_vals = np.mean(rotated_shape, axis=1)[:, np.newaxis]

    translation = ref_shape_mean_vals - scale * shape_mean_vals
    # Fit shape
    fitted_shape = np.dot(rotation_matrix, shape) + translation
  
    # Euclidean metric
    score = np.linalg.norm(reference_shape-fitted_shape)    
    return score

if the points have the same pattern the score should be a small value smaller than 1.

This is my implementation in Java:

package com.moussa.zomzoom.Player;

import Jama.Matrix;
import Jama.SingularValueDecomposition;

public class Solver {


public static boolean isFit(double[][] _reference_shape,double[][] _shape) {
    boolean result = true;
    double threshold = 50;
    // calculate rotation matrix 3 x 3
    Matrix reference_shape = new Matrix(_reference_shape);
    Matrix shape = new Matrix(_shape);
    Matrix dot = reference_shape.times(shape.transpose());
    SingularValueDecomposition SVD = new SingularValueDecomposition(dot);
    Matrix U = SVD.getU();
    Matrix V = SVD.getV();
    Matrix rotation_matrix = U.times(V);
    Matrix rotated_shape = rotation_matrix.times(shape);

    // Perform procrustes analysis
    double score = Double.MAX_VALUE;
    Matrix reference_shape_mean = mean(reference_shape);
    Matrix shape_mean = mean(rotated_shape);

    // double scale = reference_shape.minus(reference_shape_mean).norm1() / shape.minus(shape_mean).norm1();
    //Matrix translation = reference_shape_mean.minus(shape_mean.times(scale));
    Matrix translation = reference_shape_mean.minus(shape_mean);

    // Fit shape
    //Matrix fitted_shape = rotation_matrix.times(shape).times(scale).plus(translation);
    Matrix fitted_shape = rotated_shape.plus(translation);
    // Euclidean metric
    //double euclidean_dist = fitted_shape. distance2(reference_shape);
    score = reference_shape.minus(fitted_shape).norm1();

    result = score < threshold;
    return result;
}

private static Matrix mean(Matrix matrix){
    double[]mean_values=new double[3];
    for(int i=0;i<matrix.getRowDimension();i++){
        mean_values[0]+=matrix.get(i,0);
        mean_values[1]+=matrix.get(i,1);
        mean_values[2]+=matrix.get(i,2);
    }
    mean_values[0]/=matrix.getRowDimension();
    mean_values[1]/=matrix.getRowDimension();
    mean_values[2]/=matrix.getRowDimension();
    double[][]m=new double[matrix.getRowDimension()][matrix.getColumnDimension()];
    for(int i=0;i<matrix.getRowDimension();i++){
        m[i]=mean_values;
    }
    return new Matrix(m);
}

}

The same test in Java causes score to be very large number > 2000. I noticed that the SVD values in Jama are different from Python.

I tried with many Java linear algebra library like Apache common-maths and get the same result.

0

There are 0 best solutions below