All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.TKnudsen.ComplexDataObject.data.distanceMatrix.PairwiseDistancesMatrix Maven / Gradle / Ivy

Go to download

A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.

The newest version!
package com.github.TKnudsen.ComplexDataObject.data.distanceMatrix;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 

* Distance matrix implementation that accepts a matrix of pairwise distances * from an external source. * * Assumes that the given pairwise distances are symmetric. *

* * * *

* Copyright: (c) 2016-2017 Juergen Bernard, https://github.com/TKnudsen/DMandML *

* * @author Christian Ritter, Juergen Bernard * @version 1.05 */ public class PairwiseDistancesMatrix implements IDistanceMatrix { private double[][] distanceMatrix; private Map objectMapping; private List elements; public PairwiseDistancesMatrix(List elements, double[][] pairwiseDistances) { this.distanceMatrix = pairwiseDistances; this.elements = elements; initializeObjectMapping(); } private void initializeObjectMapping() { objectMapping = new HashMap<>(); int i = 0; for (T t : getElements()) { objectMapping.put(t, i++); } } @Override public double getDistance(T o1, T o2) { Integer i = objectMapping.get(o1); Integer j = objectMapping.get(o2); if (i == null || j == null) { return Double.NaN; } return getDistanceMatrix()[i][j]; } @Override public boolean isSymmetric() { return true; } @Override public double applyAsDouble(T t, T u) { return getDistance(t, u); } @Override public String getName() { return "PairwiseDistancesMatrix"; } @Override public String getDescription() { return "IDistanceMatrix wrapper for pairwise distances already represented as 2D double array"; } @Override public double[][] getDistanceMatrix() { return distanceMatrix; } @Override public List getElements() { return elements; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy