Java examples for java.lang:Math Geometry
Compute the x and y configured norms of all other points from the data points at time step t.
/*//from w w w . java 2 s. c o m * Java Information Dynamics Toolkit (JIDT) * Copyright (C) 2012, Joseph T. Lizier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; public class Main { public static final int NORM_EUCLIDEAN = 0; public static final int NORM_EUCLIDEAN_NORMALISED = 1; public static final int NORM_MAX_NORM = 2; public static final int NORM_EUCLIDEAN_SQUARED = 3; private int normToUse = 0; /** * Compute the x and y configured norms of all other points from * the data points at time step t. * Puts norms of t from itself as infinity, which is useful * when counting the number of points closer than epsilon say. * * @param mvTimeSeries1 * @param mvTimeSeries2 * @return */ public double[][] computeNorms(double[][] mvTimeSeries1, double[][] mvTimeSeries2, int t) { int timeSteps = mvTimeSeries1.length; double[][] norms = new double[timeSteps][2]; for (int t2 = 0; t2 < timeSteps; t2++) { if (t2 == t) { norms[t2][0] = Double.POSITIVE_INFINITY; norms[t2][1] = Double.POSITIVE_INFINITY; continue; } // Compute norm in first direction norms[t2][0] = norm(mvTimeSeries1[t], mvTimeSeries1[t2]); // Compute norm in second direction norms[t2][1] = norm(mvTimeSeries2[t], mvTimeSeries2[t2]); } return norms; } /** * Compute the x, y and z norms of all other points from * the data points at time step t. * Puts norms of t from itself as infinity, which is useful * when counting the number of points closer than epsilon say. * * @param mvTimeSeries1 * @param mvTimeSeries2 * @param mvTimeSeries3 * @return */ public double[][] computeNorms(double[][] mvTimeSeries1, double[][] mvTimeSeries2, double[][] mvTimeSeries3, int t) { int timeSteps = mvTimeSeries1.length; double[][] norms = new double[timeSteps][3]; for (int t2 = 0; t2 < timeSteps; t2++) { if (t2 == t) { norms[t2][0] = Double.POSITIVE_INFINITY; norms[t2][1] = Double.POSITIVE_INFINITY; norms[t2][2] = Double.POSITIVE_INFINITY; continue; } // Compute norm in first direction norms[t2][0] = norm(mvTimeSeries1[t], mvTimeSeries1[t2]); // Compute norm in second direction norms[t2][1] = norm(mvTimeSeries2[t], mvTimeSeries2[t2]); // Compute norm in third direction norms[t2][2] = norm(mvTimeSeries3[t], mvTimeSeries3[t2]); } return norms; } /** * Compute the norms for each marginal variable for all other points from * the data points at time step t. * Puts norms of t from itself as infinity, which is useful * when counting the number of points closer than epsilon say. * * @param mvTimeSeries * @return */ public static double[][] computeNorms(double[][] mvTimeSeries, int t) { int timeSteps = mvTimeSeries.length; int variables = mvTimeSeries[0].length; double[][] norms = new double[timeSteps][variables]; for (int t2 = 0; t2 < timeSteps; t2++) { if (t2 == t) { for (int v = 0; v < variables; v++) { norms[t2][v] = Double.POSITIVE_INFINITY; } continue; } for (int v = 0; v < variables; v++) { norms[t2][v] = Math.abs(mvTimeSeries[t][v] - mvTimeSeries[t2][v]); } } return norms; } /** * Computing the configured norm between vectors x1 and x2. * * @param x1 * @param x2 * @return */ public double norm(double[] x1, double[] x2) { switch (normToUse) { case NORM_EUCLIDEAN_NORMALISED: return euclideanNorm(x1, x2) / Math.sqrt(x1.length); case NORM_MAX_NORM: return maxNorm(x1, x2); case NORM_EUCLIDEAN_SQUARED: return euclideanNormSquared(x1, x2); case NORM_EUCLIDEAN: default: return euclideanNorm(x1, x2); } } /** * Computing the norm as the Euclidean norm. * * @param x1 * @param x2 * @return */ public static double euclideanNorm(double[] x1, double[] x2) { double distance = 0.0; for (int d = 0; d < x1.length; d++) { double difference = x1[d] - x2[d]; distance += difference * difference; } return Math.sqrt(distance); } /** * Computing the norm as the Max norm. * * @param x1 * @param x2 * @return */ public static double maxNorm(double[] x1, double[] x2) { double distance = 0.0; for (int d = 0; d < x1.length; d++) { double difference = x1[d] - x2[d]; // Take the abs if (difference < 0) { difference = -difference; } if (difference > distance) { distance = difference; } } return distance; } /** * Computing the norm as the Euclidean norm squared * (i.e. avoids taking the square root). * * @param x1 * @param x2 * @return */ public static double euclideanNormSquared(double[] x1, double[] x2) { double distance = 0.0; for (int d = 0; d < x1.length; d++) { double difference = x1[d] - x2[d]; distance += difference * difference; } return distance; } }