Jave distance matrix

60 Jave code examples are found related to " distance matrix". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: CosineDistanceMatrix.java    From sax-vsm_classic with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a distance matrix.
 * 
 * @param tfidf The data to use.
 */
public CosineDistanceMatrix(HashMap<String, HashMap<String, Double>> tfidf) {

  Locale.setDefault(Locale.US);

  rows = tfidf.keySet().toArray(new String[0]);

  Arrays.sort(rows);

  distances = new double[rows.length][rows.length];

  for (int i = 0; i < rows.length; i++) {
    keysToIndex.put(rows[i], i);
    for (int j = 0; j < i; j++) {
      HashMap<String, Double> vectorA = tfidf.get(rows[i]);
      HashMap<String, Double> vectorB = tfidf.get(rows[j]);
      double distance = tp.cosineDistance(vectorA, vectorB);
      distances[i][j] = distance;
    }
  }
}
 
Example 2
Source File: CosineDistanceMatrix.java    From sax-vsm_classic with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prints matrix.
 */
@Override
public String toString() {

  StringBuffer sb = new StringBuffer();

  sb.append("\"\",");
  for (String s : rows) {
    sb.append("\"").append(s).append("\"").append(COMMA);
  }
  sb.delete(sb.length() - 1, sb.length()).append(CR);

  for (int i = 0; i < rows.length; i++) {
    sb.append("\"").append(rows[i]).append("\",");
    for (int j = 0; j < rows.length; j++) {
      sb.append(df.format(distances[i][j])).append(COMMA);
    }
    sb.delete(sb.length() - 1, sb.length()).append(CR);
  }

  return sb.toString();
}
 
Example 3
Source File: DistanceMatrixByClass.java    From KEEL with GNU General Public License v3.0 6 votes vote down vote up
public double get(int i, int j)
{
    double value = 0.0; 
    if(j > i)
        value = matrix.get(i).get(j);
    else if(j == i)
        value = 0.0;
    else if(j < i+1)
    {
        int tmp = j;
        j = i;
        i = tmp;
        value = matrix.get(i).get(j);
    }
    return value;
}
 
Example 4
Source File: MdsArranger.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * This version calculates distances based on graph pathlength weighting
 * edges by the sum of the extents of the vertices they join.
 * <p>
 * Usual edge weights, multiple edges, etc, are not considered.
 */
private static float[][] calcDistanceMatrixByExtent(final GraphWriteMethods graph, final BitSet verticesToArrange, final float scaleFactor) {
    final float minRadius = 1.5f * ArrangementUtilities.FUNDAMENTAL_SIZE;

    // Record distances here.
    final float[][] distanceMatrix = new float[graph.getVertexCapacity()][graph.getVertexCapacity()];

    // Loop through each arrange vertex, recording distances to each influence vertex.
    for (int vxId = verticesToArrange.nextSetBit(0); vxId >= 0; vxId = verticesToArrange.nextSetBit(vxId + 1)) {
        final float[] distancesFromVertex = ArrangementUtilities.getMinDistancesToReachableVertices(graph, vxId, true, true, minRadius);

        // Loop through each influence vertex.
        for (int inflVxId = verticesToArrange.nextSetBit(0); inflVxId >= 0; inflVxId = verticesToArrange.nextSetBit(inflVxId + 1)) {
            distanceMatrix[vxId][inflVxId] = scaleFactor * distancesFromVertex[inflVxId] * EXTENTS_SIZE_INFLATION;
        }
    }

    return distanceMatrix;
}
 
Example 5
Source File: CAST.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
private void normaliseDistanceMatrix(){
    double maxDist = 0;
    double minDist = Double.MAX_VALUE;

    for (int i = 0; i < distanceMatrix.length; i++){
        for (int n = 0; n < i; n++){
            if (distanceMatrix[i][n] > maxDist){
                maxDist = distanceMatrix[i][n];
            }
            else if (distanceMatrix[i][n] < minDist){
                minDist = distanceMatrix[i][n];
            }
        }
    }

    for (int i = 0; i < distanceMatrix.length; i++){
        for (int n = 0; n < i; n++){
            distanceMatrix[i][n] = (distanceMatrix[i][n] - minDist)/(maxDist - minDist);
        }
    }
}
 
Example 6
Source File: DiscreteRatePriorGenerator.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void printFullDistanceMatrix(String name, boolean locationNames) {
    try {
        PrintWriter outFile = new PrintWriter(new FileWriter(name), true);

        outFile.print("location");
        for (int a = 0; a < locations.length; a++) {
            outFile.print(locations[a]+"\t");
            for (int b = 0; b < locations.length; b++) {
                double lat1 = latitudes[a];
                double lat2 = latitudes[b];
                double long1 = longitudes[a];
                double long2 = longitudes[b];
                double distance = getKilometerGreatCircleDistance(lat1,long1,lat2,long2);
                outFile.print(distance+"\t");
            }
            outFile.print("\r");
        }
        outFile.close();

    } catch(IOException io) {
       System.err.print("Error writing to file: " + name);
    }

}
 
Example 7
Source File: JukesCantorDistanceMatrix.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Calculate a pairwise distance
 */
protected double calculatePairwiseDistance(int i, int j) {
	final double obsDist = super.calculatePairwiseDistance(i, j);
	
	if (obsDist == 0.0) return 0.0;

	if (obsDist >= const1) {
		return MAX_DISTANCE;
	} 
       
	final double expDist = -const1 * Math.log(1.0 - (const2 * obsDist));

	if (expDist < MAX_DISTANCE) {
		return expDist;
	} else {
		return MAX_DISTANCE;
	}
}
 
Example 8
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/**
 * Test the language parameter.
 *
 * <p>Sample request: <a
 * href="http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR">
 * origins: Vancouver BC|Seattle, destinations: San Francisco|Victoria BC, mode: bicycling,
 * language: french</a>.
 */
@Test
public void testLanguageParameter() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    String[] origins = new String[] {"Vancouver BC", "Seattle"};
    String[] destinations = new String[] {"San Francisco", "Victoria BC"};
    DistanceMatrixApi.newRequest(sc.context)
        .origins(origins)
        .destinations(destinations)
        .mode(TravelMode.BICYCLING)
        .language("fr-FR")
        .await();

    sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
    sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
    sc.assertParamValue(TravelMode.BICYCLING.toUrlValue(), "mode");
    sc.assertParamValue("fr-FR", "language");
  }
}
 
Example 9
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Test duration in traffic with traffic model set. */
@Test
public void testDurationInTrafficWithTrafficModel() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    final long ONE_HOUR_MILLIS = 60 * 60 * 1000;
    DistanceMatrixApi.newRequest(sc.context)
        .origins("Fisherman's Wharf, San Francisco")
        .destinations("San Francisco International Airport, San Francisco, CA")
        .mode(TravelMode.DRIVING)
        .trafficModel(TrafficModel.PESSIMISTIC)
        .departureTime(Instant.ofEpochMilli(System.currentTimeMillis() + ONE_HOUR_MILLIS))
        .await();

    sc.assertParamValue("Fisherman's Wharf, San Francisco", "origins");
    sc.assertParamValue("San Francisco International Airport, San Francisco, CA", "destinations");
    sc.assertParamValue(TravelMode.DRIVING.toUrlValue(), "mode");
    sc.assertParamValue(TrafficModel.PESSIMISTIC.toUrlValue(), "traffic_model");
  }
}
 
Example 10
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Test transit without arrival or departure times specified. */
@Test
public void testTransitWithoutSpecifyingTime() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    String[] origins =
        new String[] {"Fisherman's Wharf, San Francisco", "Union Square, San Francisco"};
    String[] destinations =
        new String[] {"Mikkeller Bar, San Francisco", "Moscone Center, San Francisco"};
    DistanceMatrixApi.newRequest(sc.context)
        .origins(origins)
        .destinations(destinations)
        .mode(TravelMode.TRANSIT)
        .await();

    sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
    sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
    sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");
  }
}
 
Example 11
Source File: DistanceMatrix.java    From JDeodorant with MIT License 6 votes vote down vote up
public double[][] getJaccardDistanceMatrix(MyClass sourceClass) {
	ArrayList<Entity> entities = new ArrayList<Entity>();
	entities.addAll(sourceClass.getAttributeList());
	entities.addAll(sourceClass.getMethodList());
	double[][] jaccardDistanceMatrix = new double[entities.size()][entities.size()];
	for(int i=0; i<jaccardDistanceMatrix.length; i++) {
		for(int j=0; j<jaccardDistanceMatrix.length; j++) {
			if(i != j) {
				jaccardDistanceMatrix[i][j] = DistanceCalculator.getDistance(entities.get(i).getFullEntitySet(), entities.get(j).getFullEntitySet());
			}
			else {
				jaccardDistanceMatrix[i][j] = 0.0;
			}
		}
	}
	return jaccardDistanceMatrix;
}
 
Example 12
Source File: GodClassDistanceMatrixTest.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
public void testSeparateBlocks() {
    String classFileName = "testSeparateBlocks.java";

    ExtractClassCandidateGroup group = getExractClassCandidateGroup(classFileName);
    assertNotNull(group);

    for (int i = 0; i < 2; i++) {
        List<String> expectedFields;
        List<String> expectedMethods;

        if (i == 0) {
            expectedFields = Arrays.asList("a", "b", "c");
            expectedMethods = Collections.singletonList("fun1");
        } else {
            expectedFields = Arrays.asList("d", "e");
            expectedMethods = Collections.singletonList("fun2");
        }

        compareExtractClassCandidateRefactoringContains(group, i, expectedFields, expectedMethods);
    }
}
 
Example 13
Source File: GodClassDistanceMatrixTest.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
@Nullable
private ExtractClassCandidateGroup getExractClassCandidateGroup(@NotNull String classFileName) {
    myFixture.setTestDataPath(PATH_TO_TESTDATA);
    myFixture.configureByFile(PATH_TO_TESTS + classFileName);
    Project project = myFixture.getProject();
    PsiFile psiFile = FilenameIndex.getFilesByName(project, classFileName, GlobalSearchScope.allScope(project))[0];
    ProjectInfo projectInfo = new ProjectInfo(new AnalysisScope(project), false);

    Set<ExtractClassCandidateGroup> set = getExtractClassRefactoringOpportunities(projectInfo, new ProgressIndicatorBase());

    if (set.isEmpty()) {
        return null;
    }

    return set.iterator().next();
}
 
Example 14
Source File: TaxaDistFromMatrixSrc.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
public TaxaDistance getTaxaDistance(Taxa taxa){
	if (matrixSourceTask!=null)
		observedStates =matrixSourceTask.getCurrentMatrix(taxa);
	else if (matrixSourceObedTask!=null)
		observedStates =matrixSourceObedTask.getMatrix(taxa, currentMatrix);
	currentTaxa = taxa;
	if (observedStates==null) {
		MesquiteMessage.warnProgrammer("Observed states null in " + getName() + " for taxa " + taxa); 
		return null;
	}
	if (observedStates.getStateClass() !=currentStateClass){
		currentStateClass = observedStates.getStateClass();
		mss.setListableFilter(currentStateClass);
		//mss.setListableFilter(null);
		resetContainingMenuBar();
	}
	return distanceTask.getTaxaDistance(taxa, observedStates);
}
 
Example 15
Source File: JukesCantorDistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * compute jukes-cantor corrected distances
 *
 * @param dist distance matrix
 * @param numStates number of states of underlying data
 */
public JukesCantorDistanceMatrix(DistanceMatrix dist, int numStates)
{
	numSeqs = dist.numSeqs;
	idGroup = dist.getIdGroup();
	distance = new double[numSeqs][numSeqs];
	
	obsDistance = dist.distance;
	
	double n = numStates;

	const1 = (n-1)/n;
	const2 = n/(n-1);

	computeDistances();
}
 
Example 16
Source File: TaxaDistFromMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
public boolean getDistanceOptions() {
	if (MesquiteThread.isScripting() || !optionsAdded())
		return true;
	MesquiteInteger buttonPressed = new MesquiteInteger(1);
	ExtensibleDialog dialog = new ExtensibleDialog(containerOfModule(), "Distance Options",buttonPressed);  //MesquiteTrunk.mesquiteTrunk.containerOfModule()
	dialog.addLabel("Distance Options for " + getName());

	addOptions(dialog);

	dialog.completeAndShowDialog(true);
	if (buttonPressed.getValue()==0)  {
		processOptions(dialog);
	}
	dialog.dispose();
	return (buttonPressed.getValue()==0) ;

}
 
Example 17
Source File: TreeDistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * compute induced distance matrix
 *
 * @param idGroup  sequence order for the matrix
 * @param t tree
 * @param countEdges boolean variable deciding whether the actual
 *                   branch lengths are used in computing the distance
 *                   or whether simply all edges larger or equal a certain
 *                   threshold length are counted (each with weight 1.0)
 * @param epsilon    minimum branch length for a which an edge is counted
 */
public TreeDistanceMatrix(IdGroup idGroup, Tree t, boolean countEdges, double epsilon)
{
	numSeqs = idGroup.getIdCount();
	this.idGroup = idGroup;
	tree = t;
	
	distance = new double[numSeqs][numSeqs];
	
	alias = TreeUtils.mapExternalIdentifiers(idGroup, tree);
	
	dist = new double[tree.getExternalNodeCount()];
	idist = new double[tree.getInternalNodeCount()];
	
	computeDistances(countEdges, epsilon);
}
 
Example 18
Source File: JukesCantorDistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
private double jccorrection(double obsdist)
{
	if (obsdist == 0.0) return 0.0;

	if (obsdist >= const1)
	{
		return BranchLimits.MAXARC;
	} 
       
	double expDist = -const1 * Math.log(1.0 - (const2 * obsdist));

	if (expDist < BranchLimits.MAXARC)
	{
		return expDist;
	}
	else
	{
		return BranchLimits.MAXARC;
	}
}
 
Example 19
Source File: DistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * constructor that takes a distance matrix and clones the distances,
 * of a the identifiers in idGroup.
 */
public DistanceMatrix(DistanceMatrix dm, IdGroup subset) {
	
	int index1, index2;
	
	distance = new double[subset.getIdCount()][subset.getIdCount()];
	for (int i = 0; i < distance.length; i++) {
		index1 = dm.whichIdNumber(subset.getIdentifier(i).getName());
			
		for (int j = 0; j < i; j++) {
			index2 = dm.whichIdNumber(subset.getIdentifier(j).getName());	
			distance[i][j] = dm.distance[index1][index2];
			distance[j][i] = distance[i][j];
		}
	}
	numSeqs = distance.length;
	format = dm.format;
	idGroup = subset;
}
 
Example 20
Source File: TreeDistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** recompute distances
 * @param countEdges boolean variable deciding whether the actual
 *                   branch lengths are used in computing the distance
 *                   or whether simply all edges larger or equal a certain
 *                   threshold length are counted (each with weight 1.0)
 * @param epsilon    minimum branch length for a which an edge is counted
 */	
public void computeDistances(boolean countEdges, double epsilon)
{		
	// fast O(n^2) computation of induced distance matrix
	for (int i = 0; i < tree.getExternalNodeCount(); i++)
	{
		TreeUtils.computeAllDistances(tree, i, dist, idist, countEdges, epsilon);
		int ai = alias[i];
		
		for (int j = 0; j < tree.getExternalNodeCount(); j++)
		{
			distance[ai][alias[j]] = dist[j];				
		}
	}

}
 
Example 21
Source File: DistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * test whether this matrix is a symmetric distance matrix
 *
 */
public boolean isSymmetric()
{
	for (int i = 0; i < distance.length; i++)
	{
		if (distance[i][i] != 0) return false;
	}
	for (int i = 0; i < distance.length-1; i++)
	{
		for (int j = i+1; j < distance.length; j++)
		{
			if (distance[i][j] != distance[j][i]) return false;
		}
	}
	return true;
}
 
Example 22
Source File: FastDistance.java    From Alink with Apache License 2.0 6 votes vote down vote up
private List<FastDistanceData> prepareDenseMatrixData(Tuple2<Vector, Row> tuple,
                                                      Iterator<Tuple2<Vector, Row>> iterator,
                                                      int vectorSize) {
    final int rowNumber = Math.min(SIZE / 8 / vectorSize, MAX_ROW_NUMBER);
    List<FastDistanceData> res = new ArrayList<>();
    while (null != tuple) {
        int index = 0;
        DenseMatrix matrix = new DenseMatrix(vectorSize, rowNumber);
        Row[] rows = new Row[rowNumber];
        while (index < rowNumber && null != tuple) {
            Preconditions.checkState(tuple.f0 instanceof DenseVector, "Inputs should be the same vector type!");
            rows[index] = tuple.f1;
            MatVecOp.appendVectorToMatrix(matrix, false, index++, tuple.f0);
            tuple = iterator.hasNext() ? iterator.next() : null;
        }
        FastDistanceData data = index == rowNumber ? new FastDistanceMatrixData(matrix, rows) :
            new FastDistanceMatrixData(
                new DenseMatrix(vectorSize, index, Arrays.copyOf(matrix.getData(), index * vectorSize)),
                Arrays.copyOf(rows, index));
        updateLabel(data);
        res.add(data);
    }
    return res;
}
 
Example 23
Source File: FastDistance.java    From Alink with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare the FastDistanceData. If the vector is dense, we organize <code>rowNumber = SIZE / 8 / vectorSize</code>
 * vectors into a matrix. If the number of remaining vectors is n ,which is less than rowNumber, then the last
 * matrix only contains n columns.
 * <p>
 * If the vector is sparse, we deal with the inputs row by row and returns a list of FastDistanceVectorData.
 *
 * @param tuples support vector and row input.
 * @return a list of FastDistanceData.
 */
public List<FastDistanceData> prepareMatrixData(Iterable<Tuple2<Vector, Row>> tuples) {
    Iterator<Tuple2<Vector, Row>> iterator = tuples.iterator();
    Tuple2<Vector, Row> row = null;
    int vectorSize = 0;
    boolean isDense = false;
    if (iterator.hasNext()) {
        row = iterator.next();
        if (row.f0 instanceof DenseVector) {
            isDense = true;
            vectorSize = row.f0.size();
        }
    }
    if (isDense) {
        return prepareDenseMatrixData(row, iterator, vectorSize);
    } else {
        return prepareSparseMatrixData(row, iterator);
    }
}
 
Example 24
Source File: ClusterCalculatorsTest.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Before
public void testCreateAndStoreDistanceMatrix() {
	artifactRepository.deleteAll();
	githubUserRepository.deleteAll();
	relationRepository.deleteAll();
	clusterRepository.deleteAll();
	clusterizationRepository.deleteAll();
	try {
		ObjectMapper mapper = new ObjectMapper();
		Resource resource = new ClassPathResource("artifacts.json");
		InputStream resourceInputStream = resource.getInputStream();
		artifacts = mapper.readValue(resourceInputStream, new TypeReference<List<Artifact>>(){});
		artifactRepository.save(artifacts);
		for (Artifact artifact : artifacts) {
			ossmeterImporter.storeGithubUser(artifact.getStarred(), artifact.getFullName());
			ossmeterImporter.storeGithubUserCommitter(artifact.getCommitteers(), artifact.getFullName());
		} 
		resourceInputStream.close();
		
		similarityManager.createAndStoreDistanceMatrix(simDependencyCalculator);
		assertEquals(((artifacts.size() * (artifacts.size() -1))/2), 
				relationRepository.findAllByTypeName(simDependencyCalculator.getSimilarityName()).size());
	} catch (IOException e) {
		logger.error(e.getMessage());
	}
}
 
Example 25
Source File: TestNormalAlphabet.java    From SAX with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetDistanceMatrix() {

  try {
    for (int i = 2; i < a.getMaxSize(); i++) {
      assertTrue(a.getDistanceMatrix(i).length > 0);
    }
  }
  catch (SAXException e) {
    fail("Should not throw exception");
  }

  try {
    @SuppressWarnings("unused")
    double[][] dd = a.getDistanceMatrix(1);
    fail("Should throw an exception");
  }
  catch (SAXException e) {
    assert true;
  }

  try {
    @SuppressWarnings("unused")
    double[][] dd = a.getDistanceMatrix(21);
    fail("Should throw an exception");
  }
  catch (SAXException e) {
    assert true;
  }

}
 
Example 26
Source File: DistanceMatrixByClass.java    From KEEL with GNU General Public License v3.0 5 votes vote down vote up
public ArrayList<Double> labels()
{
    ArrayList<Double> presLabels = new ArrayList<Double>();
    ArrayList<Double> labels = new ArrayList<Double>(matrix.keySet());
    for(double label : labels)
        if(containsLabel(label))
            presLabels.add(label);
    return presLabels;
}
 
Example 27
Source File: KilometricService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Get JSON response from Google Maps Distance Matrix API.
 *
 * @param origins
 * @param destinations
 * @param language
 * @return
 * @throws URISyntaxException
 * @throws IOException
 * @throws JSONException
 * @throws AxelorException
 */
protected JSONObject getGoogleMapsDistanceMatrixResponse(
    String origins, String destinations, String language)
    throws URISyntaxException, IOException, JSONException, AxelorException {

  URIBuilder ub = new URIBuilder("https://maps.googleapis.com/maps/api/distancematrix/json");
  ub.addParameter("origins", origins);
  ub.addParameter("destinations", destinations);
  ub.addParameter("language", language);
  ub.addParameter("key", mapService.getGoogleMapsApiKey());

  return this.getApiResponse(
      ub.toString(), IExceptionMessage.KILOMETRIC_ALLOWANCE_GOOGLE_MAPS_ERROR);
}
 
Example 28
Source File: MultipleAlignmentEnsembleImpl.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Force recalculation of the distance matrices.
 */
public void updateDistanceMatrix() {

	// Reset the distance Matrix variable
	distanceMatrix = new ArrayList<Matrix>();

	for (int s = 0; s < size(); s++) {
		Atom[] ca = atomArrays.get(s);
		Matrix distMat = AlignUtils.getDistanceMatrix(ca, ca);
		distanceMatrix.add(distMat);
	}
}
 
Example 29
Source File: AlternativeAlignmentFrame.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void showDistanceMatrix(int position){
	if ( position > aligs.length){
		return;
	}
	AlternativeAlignment alig = aligs[position];
	logger.info("display distance matrix for alternative alignment " + (position +1));

	ScaleableMatrixPanel smp = new ScaleableMatrixPanel();
	JFrame frame = new JFrame();
	frame.setTitle("Alt. Alig [" + position+"] - Distance Matrix & path");

	frame.addWindowListener(new WindowAdapter(){
		@Override
		public void windowClosing(WindowEvent e){
			JFrame f = (JFrame) e.getSource();
			f.setVisible(false);
			f.dispose();
		}



	});

	smp.setMatrix(alig.getDistanceMatrix());
	smp.setAlternativeAligs(new AlternativeAlignment[]{alig});

	frame.getContentPane().add(smp);

	frame.pack();
	frame.setVisible(true);

}
 
Example 30
Source File: GuideTreeTest.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testGetDistanceMatrix() {
	assertArrayEquals(tree.getDistanceMatrix(), new double[][] {
			{0.0, 0.0, 1.0, 0.4},
			{0.0, 0.0, 1.0, 0.4},
			{1.0, 1.0, 0.0, 1.0},
			{0.4, 0.4, 1.0, 0.0}});
}
 
Example 31
Source File: DistanceMatrix.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * constructor taking a dimension
 */
public DistanceMatrix(TaxonList taxa) {
    super();
    this.taxa = taxa;
    dimension = taxa.getTaxonCount();
    distances = new double[dimension][dimension];
    distancesKnown = true;
}
 
Example 32
Source File: DistanceMatrix.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * set an element - this overwrites any existing elements
 */
public void setElement(int row, int column, double value) {

    if (!distancesKnown) {
        calculateDistances();
    }

    distances[row][column] = value;
}
 
Example 33
Source File: DiscreteRatePriorGenerator.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
private double[] getFullDistanceMatrix(Double[] latitudes, Double[] longitudes) {

        double[] distances = new double[latitudes.length * latitudes.length];

        int distanceCounter = 0;
        for (int a = 0; a < latitudes.length; a++) {
            //resultsStream.print(locations[a]+"\t");
            for (int b = 0; b < latitudes.length; b++) {
                distances[distanceCounter] = getKilometerGreatCircleDistance(latitudes[a],longitudes[a],latitudes[b],longitudes[b]);
                distanceCounter++;
            }
        }
        return distances;
    }
 
Example 34
Source File: AbstractClusterDissimilarity.java    From JSAT with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates an upper triangular matrix containing the distance between all
 * points in the data set. The main diagonal will contain all zeros, since
 * the distance between a point and itself is always zero. This main
 * diagonal is not stored, and is implicit <br> To save space, the matrix is
 * staggered, and is of a size such that all elements to the left of the
 * main diagonal are not present. <br> To compute the index into the
 * returned array for the index [i][j], the values should be switched such
 * that i &ge; j, and accessed as [i][j-i-1]
 *
 * @param dataSet the data set to create distance matrix for
 * @param cd the cluster dissimilarity measure to use
 * @return a upper triangular distance matrix
 */
public static double[][] createDistanceMatrix(DataSet dataSet, ClusterDissimilarity cd)
{
    double[][] distances = new double[dataSet.size()][];


    for (int i = 0; i < distances.length; i++)
    {
        distances[i] = new double[dataSet.size() - i - 1];
        for (int j = i + 1; j < distances.length; j++)
            distances[i][j - i - 1] = cd.distance(dataSet.getDataPoint(i), dataSet.getDataPoint(j));
    }

    return distances;
}
 
Example 35
Source File: DistanceFunction.java    From ensemble-clustering with MIT License 5 votes vote down vote up
private double[][] createCoverMatrix(Collection<T> x, Collection<T> y) {
	double cover[][] = new double[x.size()][y.size()];
	
	int i = 0;
	for (T itemx : x) {
		int j = 0;
		for (T itemy : y) {
			cover[i][j] = distance(itemx, itemy);
			j++;
		}
		i++;
	}
	return cover;
}
 
Example 36
Source File: PearsonDistance.java    From megan-ce with GNU General Public License v3.0 5 votes vote down vote up
/**
 * computes the pearson distances
 *
 * @param rank
 * @param vectors
 * @param distances
 */
private static void computeCorrelationMatrix(int rank, double[][] vectors, Distances distances) {
    // compute mean for each row
    double[] mean = new double[rank];
    for (double[] row : vectors) {
        for (int col = 0; col < rank; col++) {
            mean[col] += row[col];
        }
    }
    for (int col = 0; col < rank; col++) {
        mean[col] /= vectors.length;
    }
    double[] stddev = new double[rank];
    for (double[] row : vectors) {
        for (int col = 0; col < rank; col++) {
            stddev[col] += (row[col] - mean[col]) * (row[col] - mean[col]);
        }
    }
    for (int col = 0; col < rank; col++) {
        stddev[col] = Math.sqrt(stddev[col] / vectors.length);
    }

    for (int di = 0; di < rank; di++) {
        distances.set(di + 1, di + 1, 0);
        for (int dj = di + 1; dj < rank; dj++) {
            double cor = 0;
            for (double[] row : vectors) {
                cor += (row[di] - mean[di]) * (row[dj] - mean[dj]) / (stddev[di] * stddev[dj]);
            }
            cor /= vectors.length;
            distances.set(di + 1, dj + 1, cor);
        }
    }
}
 
Example 37
Source File: SymmetricShortDistanceMatrix.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
private long getElement(int x, int y) {
    if (x>y) {
        return elementIndex[y] + (long) x;
    } else {
        return elementIndex[x] + (long) y;
    }
}
 
Example 38
Source File: SymmetricShortDistanceMatrix.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
/** Creates a new instance of SymmetricShortDistancaMatrix
 *
 * Defines a new Symmetric matrix, with a predefined size
 *
 * Initially all values will be Short.MAX_Value (65535)
 *
 * All data is stored in memory efficient one dimensional array, which costs size * (size + 1) bytes
 *
 */
public SymmetricShortDistanceMatrix(int size) {

    this.size = size;
    long arraySize = ((long) size * (long) (size + 1)) / 2l;
    matrix = new short[(int) arraySize];
    elementIndex = new long[size]; for (int x=0; x<size; x++) elementIndex[x] = (long) x * (long) size - ((long) x * (long)(x+1)) / 2l;
    setMaxDistance();
    
}
 
Example 39
Source File: SymmetricFloatDistanceMatrix.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new instance of SymmetricShortDistancaMatrix
 * <p>
 * Defines a new Symmetric matrix, with a predefined size
 * <p>
 * Initially all values will be Short.MAX_Value (65535)
 * <p>
 * All data is stored in memory efficient one dimensional array, which costs ((size * (size + 1)) / 2) * 4 bytes (float)
 */
public SymmetricFloatDistanceMatrix(int size) {

    this.size = size;
    long arraySize = ((long) size * (long) (size + 1)) / 2l;
    matrix = new float[(int) arraySize];
    elementIndex = new long[size];
    for (int x = 0; x < size; x++) elementIndex[x] = (long) x * (long) size - ((long) x * (long) (x + 1)) / 2l;
    setDefaultValue();
}
 
Example 40
Source File: SymmetricShortDistanceMatrix.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
public double getCorrelationFromDistance(int distance){
    double correlation;
    if (distance > -Short.MIN_VALUE){
        correlation = (((double)distance + (double) Short.MIN_VALUE)/(double)Short.MAX_VALUE);
    }else{
        correlation = (((double)distance + (double) Short.MIN_VALUE)/(-(double)Short.MIN_VALUE));
    }
    return correlation;
}
 
Example 41
Source File: DistanceMatrixTriangular1D2D.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
static public long getListIndex(int row, int column) { // Symmetrical

    if (row > column)
      return sumFormula(row) + (long) column;
    else
      return sumFormula(column) + (long) row;
  }
 
Example 42
Source File: DistanceMatrixApiRequest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
/**
 * Specifies the mode of transport to use when calculating directions.
 *
 * <p>Note that Distance Matrix requests only support {@link TravelMode#DRIVING}, {@link
 * TravelMode#WALKING} and {@link TravelMode#BICYCLING}.
 *
 * @param mode One of the travel modes supported by the Distance Matrix API.
 * @return Returns this {@code DistanceMatrixApiRequest} for call chaining.
 */
public DistanceMatrixApiRequest mode(TravelMode mode) {
  if (TravelMode.DRIVING.equals(mode)
      || TravelMode.WALKING.equals(mode)
      || TravelMode.BICYCLING.equals(mode)
      || TravelMode.TRANSIT.equals(mode)) {
    return param("mode", mode);
  }
  throw new IllegalArgumentException(
      "Distance Matrix API travel modes must be Driving, Transit, Walking or Bicycling");
}
 
Example 43
Source File: DistanceMatrix.java    From JDeodorant with MIT License 5 votes vote down vote up
private Map<String, ArrayList<String>> computeAccessMap(Set<String> entitySetI) {
	//ArrayList<String> contains the accessed entities per target class (key)
	Map<String, ArrayList<String>> accessMap = new LinkedHashMap<String, ArrayList<String>>();
	for(String e : entitySetI) {
		String[] tokens = e.split("::");
		String classOrigin = tokens[0];
		String entityName = tokens[1];
		if(accessMap.containsKey(classOrigin)) {
			ArrayList<String> list = accessMap.get(classOrigin);
			list.add(entityName);
		}
		else {
			ArrayList<String> list = new ArrayList<String>();
			list.add(entityName);
			accessMap.put(classOrigin, list);
		}
	}
	for(String key1 : accessMap.keySet()) {
		ClassObject classObject = ASTReader.getSystemObject().getClassObject(key1);
		if(classObject != null && classObject.getSuperclass() != null) {
			for(String key2 : accessMap.keySet()) {
				if(classObject.getSuperclass().getClassType().equals(key2)) {
					ArrayList<String> list = accessMap.get(key1);
					list.addAll(accessMap.get(key2));
				}
			}
		}
	}
	return accessMap;
}
 
Example 44
Source File: DistanceMatrix.java    From JDeodorant with MIT License 5 votes vote down vote up
public DistanceMatrix(MySystem system) {
      this.system = system;
      entityIndexMap = new LinkedHashMap<String,Integer>();
      classIndexMap = new LinkedHashMap<String,Integer>();
      entityList = new ArrayList<Entity>();
      classList = new ArrayList<MyClass>();
      entityMap = new LinkedHashMap<String,Set<String>>();
      classMap = new LinkedHashMap<String,Set<String>>();
      IPreferenceStore store = Activator.getDefault().getPreferenceStore();
this.maximumNumberOfSourceClassMembersAccessedByMoveMethodCandidate = store.getInt(PreferenceConstants.P_MAXIMUM_NUMBER_OF_SOURCE_CLASS_MEMBERS_ACCESSED_BY_MOVE_METHOD_CANDIDATE);
this.maximumNumberOfSourceClassMembersAccessedByExtractClassCandidate = store.getInt(PreferenceConstants.P_MAXIMUM_NUMBER_OF_SOURCE_CLASS_MEMBERS_ACCESSED_BY_EXTRACT_CLASS_CANDIDATE);
generateDistances();
  }
 
Example 45
Source File: DistanceMatrix.java    From JDeodorant with MIT License 5 votes vote down vote up
private void generateDistances() {
	Iterator<MyClass> classIt = system.getClassIterator();
    while(classIt.hasNext()) {
        MyClass myClass = classIt.next();
        ListIterator<MyAttribute> attributeIterator = myClass.getAttributeIterator();
        while(attributeIterator.hasNext()) {
            MyAttribute attribute = attributeIterator.next();
            if(!attribute.isReference()) {
                entityList.add(attribute);
                entityMap.put(attribute.toString(),attribute.getEntitySet());
            }
        }
        ListIterator<MyMethod> methodIterator = myClass.getMethodIterator();
        while(methodIterator.hasNext()) {
            MyMethod method = methodIterator.next();
            entityList.add(method);
            entityMap.put(method.toString(),method.getEntitySet());
        }
        classList.add(myClass);
        classMap.put(myClass.getName(),myClass.getEntitySet());
    }

    String[] entityNames = new String[entityList.size()];
    String[] classNames = new String[classList.size()];
    
    int i = 0;
    for(Entity entity : entityList) {
        entityNames[i] = entity.toString();
        entityIndexMap.put(entityNames[i],i);
        int j = 0;
        for(MyClass myClass : classList) {
            classNames[j] = myClass.getName();
            if(!classIndexMap.containsKey(classNames[j]))
                classIndexMap.put(classNames[j],j);
            j++;
        }
        i++;
    }
}
 
Example 46
Source File: DistanceMatrix.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
private boolean targetClassInheritedByAnotherCandidateTargetClass(String targetClass, Set<String> candidateTargetClasses) {
    for (String candidateTargetClass : candidateTargetClasses) {
        if (!candidateTargetClass.equals(targetClass)) {
            MyClass currentSuperclass = classList.get(classIndexMap.get(candidateTargetClass));
            String superclass;
            while ((superclass = currentSuperclass.getSuperclass()) != null) {
                if (superclass.equals(targetClass))
                    return true;
                currentSuperclass = classList.get(classIndexMap.get(superclass));
            }
        }
    }
    return false;
}
 
Example 47
Source File: DistanceMatrix.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
private Map<String, ArrayList<String>> computeAccessMap(Set<String> entitySetI) {
    Map<String, ArrayList<String>> accessMap = new LinkedHashMap<>();
    for (String e : entitySetI) {
        String[] tokens = e.split("::");
        String classOrigin = tokens[0];
        String entityName = tokens[1];
        if (accessMap.containsKey(classOrigin)) {
            ArrayList<String> list = accessMap.get(classOrigin);
            list.add(entityName);
        } else {
            ArrayList<String> list = new ArrayList<>();
            list.add(entityName);
            if (classMap.containsKey(classOrigin)) accessMap.put(classOrigin, list);
        }
    }

    for (String key1 : accessMap.keySet()) {
        ClassObject classObject = ASTReader.getSystemObject().getClassObject(key1);
        if (classObject != null && classObject.getSuperclass() != null) {
            for (String key2 : accessMap.keySet()) {
                if (classObject.getSuperclass().getClassType().equals(key2)) {
                    ArrayList<String> list = accessMap.get(key1);
                    list.addAll(accessMap.get(key2));
                }
            }
        }
    }
    return accessMap;
}
 
Example 48
Source File: DistanceMatrix.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
public DistanceMatrix(MySystem system) {
    this.system = system;
    entityIndexMap = new LinkedHashMap<>();
    classIndexMap = new LinkedHashMap<>();
    entityList = new ArrayList<>();
    classList = new ArrayList<>();
    entityMap = new LinkedHashMap<>();
    classMap = new LinkedHashMap<>();
    generateDistances();
}
 
Example 49
Source File: GodClassDistanceMatrixTest.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
public void testManySeparatesBlocks() {
    String classFileName = "testSeparateBlocksWithStrictOrder.java";

    ExtractClassCandidateGroup group = getExractClassCandidateGroup(classFileName);
    assertNotNull(group);

    for (int i = 0; i < 6; i++) {
        List<String> expectedFields;
        List<String> expectedMethods;

        if (i == 0) {
            expectedFields = Arrays.asList("aa", "ab", "ac", "ad", "ae", "af", "ag");
            expectedMethods = Collections.singletonList("fun1");
        } else if (i == 1) {
            expectedFields = Arrays.asList("ba", "bb", "bc", "bd", "be", "bf");
            expectedMethods = Collections.singletonList("fun2");
        } else if (i == 2) {
            expectedFields = Arrays.asList("ca", "cb", "cc", "cd", "ce");
            expectedMethods = Collections.singletonList("fun3");
        } else if (i == 3) {
            expectedFields = Arrays.asList("da", "db", "dc", "dd");
            expectedMethods = Collections.singletonList("fun4");
        } else if (i == 4) {
            expectedFields = Arrays.asList("ea", "eb", "ec");
            expectedMethods = Collections.singletonList("fun5");
        } else {
            expectedFields = Arrays.asList("fa", "fb");
            expectedMethods = Collections.singletonList("fun6");
        }

        compareExtractClassCandidateRefactoringContains(group, i, expectedFields, expectedMethods);
    }
}
 
Example 50
Source File: SimilarityMatrix.java    From ASTRAL with Apache License 2.0 5 votes vote down vote up
private List<TreeSet<Integer>> sortByDistance(float[][] refMatrix) {
	List<TreeSet<Integer>> ret = new ArrayList<TreeSet<Integer>>(n);
	List<Integer> range = Utils.getRange(n);
	for (int i = 0; i < n; i++) {
		final float[] js = refMatrix[i];
		TreeSet<Integer> indices = sortColumn(range, js);
		ret.add(indices);
	}
	return ret;
}
 
Example 51
Source File: ReferenceAnnotation.java    From EventCoreference with Apache License 2.0 5 votes vote down vote up
/**
 * For each word we make a comparison to all other words using WN SIM
 * This generates a Sim vector for the length of the array
 * For each word, we calculate a centrality score by taking the dot product of the vector
 * @param phraseCountArrayList
 */
static ArrayList<Double> getDistanceMatrix (SortedSet<PhraseCount> phraseCountArrayList) {
    ArrayList<Double> matrix = new ArrayList<Double>();
   /* if (phraseCountArrayList.size()==1) {
        matrix.add(new Double(1));
    }*/
    // For each phrase we create a vector comparison to the others
    for (PhraseCount pcount1 : phraseCountArrayList) {
        Double simScore = 1.0;
        for (PhraseCount pcount2 : phraseCountArrayList) {
                ArrayList<SimilarityPair> similarityPairArrayList = vu.wntools.wnsimilarity.WordnetSimilarityApi.wordLeacockChodorowSimilarity(wordnetLmfSaxParser.wordnetData, pcount1.getPhrase(), pcount2.getPhrase());
                SimilarityPair similarityPair = vu.wntools.wnsimilarity.WordnetSimilarityApi.getTopScoringSimilarityPair(similarityPairArrayList);
                simScore += similarityPair.getScore();
        }
        /// average similarity score is added to the matrix
        matrix.add(simScore/(phraseCountArrayList.size()));

    }

   /* Double maxSimScore = 0.0;

    for (int i = 0; i < matrix.size(); i++) {
        Double aDouble = matrix.get(i);
        if (aDouble>maxSimScore) {
            maxSimScore = aDouble;
        }
    }
    for (int i = 0; i < matrix.size(); i++) {
        Double aDouble = matrix.get(i);
        aDouble = aDouble/maxSimScore;
        System.out.println("aDouble = " + aDouble);
        matrix.set(i,aDouble);
    }*/
    return matrix;
}
 
Example 52
Source File: Residue.java    From OSPREY3 with GNU General Public License v2.0 5 votes vote down vote up
public double[][] atomDistanceMatrix(){
    //matrix of distances between all atoms
    int numAtoms = atoms.size();
    double[][] ans = new double[numAtoms][numAtoms];
    
    for(int atNum1=0; atNum1<numAtoms; atNum1++){
        for(int atNum2=0; atNum2<numAtoms; atNum2++){
            
            double dist = VectorAlgebra.distance(coords,atNum1,coords,atNum2);
            ans[atNum1][atNum2] = dist;
        }
    }
    
    return ans;
}
 
Example 53
Source File: GamaSpatialMatrix.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int manhattanDistanceBetween(final IShape g1, final IShape g2) {

	IGridAgent s1 = g1.getAgent() != null && g1.getAgent().getSpecies() == this.getCellSpecies()
			? (IGridAgent) g1.getAgent() : null;
	IGridAgent s2 = g2.getAgent() != null && g2.getAgent().getSpecies() == this.getCellSpecies()
			? (IGridAgent) g2.getAgent() : null;

	if (s1 == null || s2 == null) {
		ILocation p1 = g1.isPoint() ? g1.getLocation() : null;
		ILocation p2 = g2.isPoint() ? g2.getLocation() : null;
		if (s1 == null) {
			s1 = (IGridAgent) this.getPlaceAt(g1.getLocation());
			if (!s1.covers(g1)) {
				s1 = null;
			}
		}
		if (s2 == null) {
			s2 = (IGridAgent) this.getPlaceAt(g2.getLocation());
			if (!s2.covers(g2)) {
				s2 = null;
			}
		}
		final Coordinate[] coord = new DistanceOp(g1.getInnerGeometry(), g2.getInnerGeometry()).nearestPoints();
		if (s1 == null) {
			p1 = new GamaPoint(coord[0]);
			s1 = (IGridAgent) this.getPlaceAt(p1);
		}
		if (s2 == null) {
			p2 = new GamaPoint(coord[1]);
			s2 = (IGridAgent) this.getPlaceAt(p2);
		}
	}

	final int dx = Math.abs(s1.getX() - s2.getX());
	final int dy = Math.abs(s1.getY() - s2.getY());
	if (usesVN) { return dx + dy; }
	return Math.max(dx, dy);
}
 
Example 54
Source File: DistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** print alignment (PHYLIP format) */
public void printPHYLIP(PrintWriter out)
{
		// PHYLIP header line
	out.println("  " + numSeqs);

	for (int i = 0; i < numSeqs; i++)
	{
		format.displayLabel(out, 
			idGroup.getIdentifier(i).getName(), 10);
		out.print("      ");

		for (int j = 0; j < numSeqs; j++)
		{
			// Chunks of 6 blocks each
			if (j % 6 == 0 && j != 0)
			{
				out.println();
				out.print("                ");
			}

			out.print("  ");
			format.displayDecimal(out, distance[i][j], 5);
		}
		out.println();
	}
}
 
Example 55
Source File: DistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the mean pairwise distance of this matrix
 */
public double meanDistance() {
	double dist = 0.0;
	int count = 0;
	for (int i = 0; i < distance.length; i++) {
		for (int j = 0; j < distance[i].length; j++) {
			if (i != j) {
				dist += distance[i][j];
				count += 1;
			}
		}
	}
	return dist / (double)count;
}
 
Example 56
Source File: ReadDistanceMatrix.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** read from file */
public ReadDistanceMatrix(String file)
	throws DistanceParseException, IOException
{
	PushbackReader input = InputSource.openFile(file);
	readSquare(input);
	input.close();
}
 
Example 57
Source File: CosineDistanceTest.java    From Alink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCalDistanceMatrixMatrix() {
    FastDistanceMatrixData matrixData = initMatrixData();
    DenseMatrix res = distance.calc(matrixData, matrixData);
    double[] expect = new double[] {0.0, 0.2852, 0.2852, 0.0};
    double[] predict = res.getData();
    for (int i = 0; i < expect.length; i++) {
        Assert.assertEquals(expect[i], predict[i], 0.01);
    }
}
 
Example 58
Source File: SymmetricShortDistanceMatrix.java    From depict with GNU General Public License v3.0 5 votes vote down vote up
private long getElement(int x, int y) {
    if (x>y) {
        return elementIndex[y] + (long) x;
    } else {
        return elementIndex[x] + (long) y;
    }
}
 
Example 59
Source File: SymmetricShortDistanceMatrix.java    From depict with GNU General Public License v3.0 5 votes vote down vote up
/** Creates a new instance of SymmetricShortDistancaMatrix
 *
 * Defines a new Symmetric matrix, with a predefined size
 *
 * Initially all values will be Short.MAX_Value (65535)
 *
 * All data is stored in memory efficient one dimensional array, which costs size * (size + 1) bytes
 *
 */
public SymmetricShortDistanceMatrix(int size) {

    this.size = size;
    long arraySize = ((long) size * (long) (size + 1)) / 2l;
    matrix = new short[(int) arraySize];
    elementIndex = new long[size]; for (int x=0; x<size; x++) elementIndex[x] = (long) x * (long) size - ((long) x * (long)(x+1)) / 2l;
    setMaxDistance();
    
}
 
Example 60
Source File: SymmetricShortDistanceMatrix.java    From depict with GNU General Public License v3.0 5 votes vote down vote up
public void setAllElements(int value) {
    for (int x=0; x<size; x++) {
        for (int y = x; y<size; y++) {
            matrix[(int) getElement(x,y)] = (short) (value - 32768);
        }
    }
}