Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.opencloudengine.flamingo.mapreduce.util; import com.google.common.base.Joiner; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import java.math.BigDecimal; import java.util.LinkedList; import java.util.List; /** * Mathmatics Utility. * * @author Edward KIM * @author Seo Ji Hye * @since 0.1 */ public class MathUtils { public static final int FLOAT_POSITION = 6; /** * Scientific Notation? Standard Notation . * * @param value Double? Scientific Notation * @return Double? Standard Notation */ public static String convertScientificToStandard(double value) { return (new BigDecimal(Double.toString(value))).toPlainString(); } /** * Scientific Notation? Standard Notation . * * @param value Float? Scientific Notation * @return Float? Standard Notation */ public static String convertScientificToStandard(float value) { return (new BigDecimal(Float.toString(value))).toPlainString(); } /** * ? ? ? . * * @param value * @return ? */ public static double pow(double value) { return Math.pow(value, 2); } /** * ? ? . * * @param values * @return */ public static double min(double[] values) { double min = values[0]; for (double value : values) { if (value < min) min = value; } return min; } /** * ? ? . * * @param values * @return */ public static double max(double[] values) { double max = values[0]; for (double value : values) { if (value > max) max = value; } return max; } /** * ? ?? ?? ?. * * @param source ? * @param values ? * @return ?? <tt>true</tt> */ public static boolean in(long source, long[] values) { for (long value : values) { if (value == source) { return true; } } return false; } /** * ?? ? ?? ?? ?. * * @param source ? * @param values ? * @return ?? <tt>true</tt> */ public static boolean in(String source, String[] values) { for (String value : values) { if (value.equals(source)) { return true; } } return false; } /** * ? ? . * * @param value * @return */ public static double sqrt(double value) { return Math.sqrt(value); } /** * . * * @param value * @return e? value */ public static double exp(double value) { return Math.exp(value); } /** * ? . * * @param value * @return log(value) */ public static double log(double value) { return Math.log(value); } /** * ? ? . * * @param value1 * @param value2 * @return value1? value2 */ public static double pow(double value1, double value2) { return Math.pow(value1, value2); } /** * . * * @param value * @return Value? */ public static double round(double value) { return Math.round(value); } /** * BigDecimal? ? . * * @param bigDecimal Big Decimal * @return ? */ public static String toBeforeDecimalPointString(BigDecimal bigDecimal) { String value = bigDecimal.toPlainString(); if (!value.contains(".")) { return bigDecimal.toPlainString(); } String[] strings = value.split("\\."); return strings[0]; } /** * Double? ? . * * @param value Double * @return ? */ public static String toBeforeDecimalPointString(double value) { return toBeforeDecimalPointString(new BigDecimal(value)); } /** * ? 0 . * * @param count 0? * @return 0 ? ? */ public static String fillZero(int count) { String zero = ""; for (int index = 0; index < count; index++) { zero += "0"; } return zero; } /** * ?? {@link #FLOAT_POSITION} ?? 0? . * <ul> * <li>0.5 -> 0.500000</li> * <li>0.50 -> 0.500000</li> * <li>0.500 -> 0.500000</li> * </ul> * * @param floatValue ? ? * @return ?? 0? ? ? */ public static String correctFloatValueForECMiner(String floatValue) { // ??? . . String[] strings = floatValue.split("\\."); // . ? 1? ? . ? . if (strings.length == 1) { return floatValue; } // ??? . ?? ? ? ? ? ? . if (strings[1].length() > FLOAT_POSITION) { return strings[0] + "." + strings[1].substring(0, FLOAT_POSITION + 1); } // ??? . ?? ? ? ? . if (strings[1].length() == FLOAT_POSITION) { return strings[0] + "." + strings[1]; } // ??? . ?? ? ? ? . if (strings[1].length() < FLOAT_POSITION) { int diff = FLOAT_POSITION - strings[1].length(); StringBuilder builder = new StringBuilder(); builder.append(strings[0]); builder.append("."); builder.append(strings[1]); for (int index = 0; index < diff; index++) { builder.append("0"); } return builder.toString(); } return floatValue; } /** * ? ? ? 0? 0 ? . * * @param value ?? * @return 0? */ public static String deleteZero(String value) { String[] strings = value.split("\\."); if (strings.length > 1 && Integer.parseInt(strings[1]) == 0) { return strings[0]; } return value; } /** * ? . * * @param n ?? * @param m ? * @return ? */ public static long combination(int n, int m) { long result = 1; for (int i = 0; i < m; i++) { result *= n - i; } for (int i = 0; i < m; i++) { result /= i + 1; } return result; } /** * Percentile? . * * @param values ? * @param quantile ? Quantile * @return Percentile */ public static double percentile(double[] values, double quantile) { Percentile percentile = new Percentile(); return percentile.evaluate(values, quantile); } /** * Percentile? . * * @param values ? * @param quantiles ? Quantile * @return Percentile */ public static List<Double> percentile(double[] values, double quantiles[]) { List<Double> percentiles = new LinkedList<Double>(); for (double quantile : quantiles) { percentiles.add(percentile(values, quantile)); } return percentiles; } /** * Percentile? . * * @param values ? * @param quantiles ? Quantile * @param joinChar ? quantile? ? * @return Percentile */ public static String percentile(double[] values, double quantiles[], String joinChar) { List<Double> percentiles = percentile(values, quantiles); return Joiner.on(joinChar).join(percentiles); } }