Here you can find the source of normalizeToOne(double[] doubles)
public static double[] normalizeToOne(double[] doubles)
//package com.java2s; /*-/*from www. j a v a 2 s . com*/ * * * Copyright 2015 Skymind,Inc. * * * * 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. * */ public class Main { public static double[] normalizeToOne(double[] doubles) { normalize(doubles, sum(doubles)); return doubles; } /** * Normalize a value * (val - min) / (max - min) * @param val value to normalize * @param max max value * @param min min value * @return the normalized value */ public static double normalize(double val, double min, double max) { if (max < min) throw new IllegalArgumentException("Max must be greater than min"); return (val - min) / (max - min); } /** * Normalizes the doubles in the array using the given value. * * @param doubles the array of double * @param sum the value by which the doubles are to be normalized * @exception IllegalArgumentException if sum is zero or NaN */ public static void normalize(double[] doubles, double sum) { if (Double.isNaN(sum)) { throw new IllegalArgumentException("Can't normalize array. Sum is NaN."); } if (sum == 0) { // Maybe this should just be a return. throw new IllegalArgumentException("Can't normalize array. Sum is zero."); } for (int i = 0; i < doubles.length; i++) { doubles[i] /= sum; } } /** * This returns the sum of the given array. * @param nums the array of numbers to sum * @return the sum of the given array */ public static double sum(double[] nums) { double ret = 0; for (double d : nums) ret += d; return ret; } }