Java Array Print formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits)

Here you can find the source of formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits)

Description

format String For Pretty Printing Related Values

License

Apache License

Declaration

public static String formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits) 

Method Source Code


//package com.java2s;
/*/*from w  ww  .ja  v a  2s  . c  om*/
 * Copyright 2015 Lutz Fischer <lfischer at staffmail.ed.ac.uk>.
 *
 * Licensed 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.
 */

import java.util.ArrayList;

public class Main {
    public static String formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits) {

        double diff = minDiff(values, true);

        // turn that into the decimal by using log10 and round that down
        double roundingFactor = Math.round(Math.log(diff) / Math.log(10) - 0.5);

        // we don't want to round away any digits before the dot
        if (roundingFactor > -minDigits)
            roundingFactor = -minDigits;

        double normFactor = Math.pow(10, roundingFactor);
        //if (roundingFactor<0)
        roundingFactor = -roundingFactor;

        return "%." + String.format("%.0f", roundingFactor) + "f";
    }

    public static String formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits, int maxDigits) {

        double[] roundedvalues = new double[values.length];
        double factor = Math.pow(10, maxDigits);

        // make sure that no difference smaller then the number of maximum 
        // digits is still there, by rounding all values to these digits.
        for (int i = 0; i < values.length; i++) {
            roundedvalues[i] = Math.round(values[i] * factor) / factor;
        }

        // get the smallest difference greater than 0
        double diff = minDiff(roundedvalues, true);

        // turn that into the decimal by using log10 and round that down
        double roundingFactor = Math.round(Math.log(diff) / Math.log(10) - 0.5);

        // we don't want to round away any digits before the dot
        if (roundingFactor > -minDigits)
            roundingFactor = -minDigits;

        double normFactor = Math.pow(10, roundingFactor);
        //if (roundingFactor<0)
        roundingFactor = -roundingFactor;

        return "%." + String.format("%.0f", roundingFactor) + "f";
    }

    public static double minDiff(ArrayList<Double> list, boolean gz) {
        int l = list.size();
        double minDiff = Double.POSITIVE_INFINITY;
        if (gz) {
            for (int f = 0; f < l; f++) {
                for (int s = f + 1; s < l; s++) {
                    double diff = Math.abs(list.get(f) - list.get(s));
                    if (diff > 0)
                        minDiff = Math.min(minDiff, diff);
                }
            }
            if (minDiff == Double.POSITIVE_INFINITY)
                return 0;

            return minDiff;
        }
        // we don't care about zeros
        for (int f = 0; f < l; f++) {
            for (int s = f + 1; s < l; s++) {
                double diff = Math.abs(list.get(f) - list.get(s));
                minDiff = Math.min(minDiff, diff);
            }
        }

        return minDiff;
    }

    /**
     * minimum difference between any of the values in the array
     * if 
     * @param list
     * @param greaterZero
     * @return 
     */
    public static double minDiff(double[] list, boolean greaterZero) {
        int l = list.length;
        double minDiff = Double.POSITIVE_INFINITY;
        if (greaterZero) {
            for (int f = 0; f < l; f++) {
                for (int s = f + 1; s < l; s++) {
                    double diff = Math.abs(list[f] - list[s]);
                    if (diff > 0)
                        minDiff = Math.min(minDiff, diff);
                }
            }
            if (minDiff == Double.POSITIVE_INFINITY)
                return 0;

            return minDiff;
        }
        // we don't care about zeros
        for (int f = 0; f < l; f++) {
            for (int s = f + 1; s < l; s++) {
                double diff = Math.abs(list[f] - list[s]);
                minDiff = Math.min(minDiff, diff);
            }
        }

        return minDiff;
    }
}

Related

  1. prettyPrint(double[][] gammas, double[][] thetas, double[][] zprobs)
  2. print(Object[] array)
  3. print(Object[] array)
  4. print(String[] files)