Java examples for Collection Framework:Array Algorithm
Calculate Exponentiation for all values in an array of doubles by a given exponent.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { double[] array = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000, 37.1234, 67.2344, 68.34534, 69.87700 }; float exponent = 2.45678f; System.out.println(java.util.Arrays.toString(exponentiateAll(array, exponent)));//from w ww . j ava2s . co m } /** * Exponentiates all values in an array of doubles by a given exponent. * * @param array Array of doubles to be exponentiated. * @param exponent An exponent. * @return Array with exponentiated values. */ public static double[] exponentiateAll(double[] array, float exponent) { for (int i = 0; i < array.length; i++) { array[i] = Math.pow(array[i], exponent); } return array; } /** * Exponentiates all values in an array of integers by a given exponent. * * @param array Array of integers to be exponentiated. * @param exponent An exponent. * @return Array with exponentiated values. */ public static double[] exponentiateAll(int[] array, float exponent) { double[] newArray = new double[array.length]; for (int i = 0; i < array.length; i++) { newArray[i] = Math.pow(array[i], exponent); } return newArray; } /** * Exponentiates all values in an array of floats by a given exponent. * * @param array Array of floats to be exponentiated. * @param exponent An exponent. * @return Array with exponentiated values. */ public static float[] exponentiateAll(float[] array, float exponent) { float[] newArray = new float[array.length]; for (int i = 0; i < array.length; i++) { newArray[i] = (float) Math.pow(array[i], exponent); } return newArray; } /** * Exponentiates all values in an array of longs by a given exponent. * * @param array Array of longs to be exponentiated. * @param exponent An exponent. * @return Array with exponentiated values. */ public static double[] exponentiateAll(long[] array, float exponent) { double[] newArray = new double[array.length]; for (int i = 0; i < array.length; i++) { newArray[i] = Math.pow(array[i], exponent); } return newArray; } /** * Exponentiates all values in an array of bytes by a given exponent. * * @param array Array of bytes to be exponentiated. * @param exponent An exponent. * @return Array with exponentiated values. */ public static float[] exponentiateAll(byte[] array, float exponent) { float[] newArray = new float[array.length]; for (int i = 0; i < array.length; i++) { newArray[i] = (float) Math.pow(array[i], exponent); } return newArray; } /** * Exponentiates all values in a matrix of doubles by a given exponent. * * @param matrix Matrix of doubles to be exponentiated. * @param exponent An exponent. * @return Matrix with exponentiated values. */ public static double[][] exponentiateAll(double[][] matrix, float exponent) { for (double[] column : matrix) { for (int j = 0; j < matrix[0].length; j++) { column[j] = Math.pow(column[j], exponent); } } return matrix; } /** * Exponentiates all values in a matrix of integers by a given exponent. * * @param matrix Matrix of integers to be exponentiated. * @param exponent An exponent. * @return Matrix with exponentiated values. */ public static double[][] exponentiateAll(int[][] matrix, float exponent) { double[][] newMatrix = new double[matrix.length][matrix[0].length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { newMatrix[i][j] = Math.pow(matrix[i][j], exponent); } } return newMatrix; } /** * Exponentiates all values in a matrix of floats by a given exponent. * * @param matrix Matrix of floats to be exponentiated. * @param exponent An exponent. * @return Matrix with exponentiated values. */ public static float[][] exponentiateAll(float[][] matrix, float exponent) { float[][] newMatrix = new float[matrix.length][matrix[0].length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { newMatrix[i][j] = (float) Math.pow(matrix[i][j], exponent); } } return newMatrix; } /** * Exponentiates all values in a matrix of longs by a given exponent. * * @param matrix Matrix of longs to be exponentiated. * @param exponent An exponent. * @return Matrix with exponentiated values. */ public static double[][] exponentiateAll(long[][] matrix, float exponent) { double[][] newMatrix = new double[matrix.length][matrix[0].length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { newMatrix[i][j] = Math.pow(matrix[i][j], exponent); } } return newMatrix; } /** * Exponentiates all values in a matrix of doubles by a given exponent. * * @param matrix Matrix of integers to be exponentiated. * @param exponent An exponent. * @return Matrix with exponentiated values. */ public static float[][] exponentiateAll(byte[][] matrix, float exponent) { float[][] newMatrix = new float[matrix.length][matrix[0].length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { newMatrix[i][j] = (float) Math.pow(matrix[i][j], exponent); } } return newMatrix; } }