Java examples for java.lang:Math Algorithm
Discretizes using even bin sizes
/*// w ww . ja va 2s . 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 { /** * Discretizes using even bin sizes * * @param data * @param numBins * @return */ public static int[] discretise(double data[], int numBins) { int[] discretised = new int[data.length]; double min = min(data); double max = max(data); double binInterval = (max - min) / numBins; for (int t = 0; t < data.length; t++) { discretised[t] = (int) ((data[t] - min) / binInterval); if (discretised[t] == numBins) { // This occurs for the maximum value; put it in the largest bin (base - 1) discretised[t]--; } } return discretised; } public static double min(double[][] matrix) { // double min = 0.0; double min = matrix[0][0]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (Double.isNaN(min) || (matrix[i][j] < min)) { min = matrix[i][j]; } } } return min; } public static int min(int[][] matrix) { // int min = 0; int min = matrix[0][0]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (matrix[i][j] < min) { min = matrix[i][j]; } } } return min; } public static double min(double[] array) { return minStartFromIndex(array, 0); } public static int min(int[] array) { // int min = 0; int min = array[0]; for (int i = 0; i < array.length; i++) { if (array[i] < min) { min = array[i]; } } return min; } /** * Works out the minimum value in the matrix in a given column * * @param matrix * @param column * @return */ public static double min(double[][] matrix, int column) { // double min = 0.0; // Allow ArrayIndexOutOfBoundsException if matrix is size 0 double min = matrix[0][column]; for (int i = 1; i < matrix.length; i++) { if (Double.isNaN(min) || (matrix[i][column] < min)) { min = matrix[i][column]; } } return min; } /** * Works out the minimum value in the matrix in a given column * * @param matrix * @param column * @return */ public static int min(int[][] matrix, int column) { // double min = 0.0; // Allow ArrayIndexOutOfBoundsException if matrix is size 0 int min = matrix[0][column]; for (int i = 1; i < matrix.length; i++) { if (matrix[i][column] < min) { min = matrix[i][column]; } } return min; } public static double max(double[][][] matrix) { // double max = 0.0; double max = matrix[0][0][0]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { for (int k = 0; k < matrix[i][j].length; k++) { if (matrix[i][j][k] > max) { max = matrix[i][j][k]; } } } } return max; } public static double max(double[][] matrix) { // double max = 0.0; double max = matrix[0][0]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (Double.isNaN(max) || (matrix[i][j] > max)) { max = matrix[i][j]; } } } return max; } public static int max(int[][] matrix) { // int max = 0; int max = matrix[0][0]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (matrix[i][j] > max) { max = matrix[i][j]; } } } return max; } public static double max(double[] array) { return maxStartFromIndex(array, 0); } public static int max(int[] array) { // int max = 0; int max = array[0]; for (int i = 0; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; } /** * Works out the maximum value in the matrix in a given column * * @param matrix * @param column * @return */ public static double max(double[][] matrix, int column) { // double max = 0.0; // Allow ArrayIndexOutOfBoundsException if matrix is size 0 double max = matrix[0][column]; for (int i = 1; i < matrix.length; i++) { if (Double.isNaN(max) || (matrix[i][column] > max)) { max = matrix[i][column]; } } return max; } /** * Works out the maximum value in the matrix in a given column * * @param matrix * @param column * @return */ public static int max(int[][] matrix, int column) { // double max = 0.0; // Allow ArrayIndexOutOfBoundsException if matrix is size 0 int max = matrix[0][column]; for (int i = 1; i < matrix.length; i++) { if (matrix[i][column] > max) { max = matrix[i][column]; } } return max; } public static double minStartFromIndex(double[] array, int startFromIndex) { // double min = 0.0; double min = array[startFromIndex]; for (int i = startFromIndex; i < array.length; i++) { if (Double.isNaN(min) || (array[i] < min)) { min = array[i]; } } return min; } public static double maxStartFromIndex(double[] array, int startFromIndex) { // double max = 0.0; double max = array[startFromIndex]; for (int i = startFromIndex; i < array.length; i++) { // TODO Check where we used this and if it's still // the approach we want to take if (Double.isNaN(max) || (array[i] > max)) { max = array[i]; } } return max; } }