Here you can find the source of normalizeToSumUpTo(double[] x, double sumUp)
public static double[] normalizeToSumUpTo(double[] x, double sumUp)
//package com.java2s; /**/*w ww . j a v a2 s . c o m*/ * Copyright 2004-2006 DFKI GmbH. * All Rights Reserved. Use is subject to license terms. * * This file is part of MARY TTS. * * MARY TTS is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, version 3 of the License. * * 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ public class Main { public static double[] normalizeToSumUpTo(double[] x, double sumUp) { return normalizeToSumUpTo(x, x.length, sumUp); } public static double[] normalizeToSumUpTo(double[] x, int len, double sumUp) { if (len > x.length) len = x.length; double[] y = new double[len]; double total = 0.0; int i; for (i = 0; i < len; i++) total += x[i]; if (total > 0.0) { for (i = 0; i < len; i++) y[i] = sumUp * (x[i] / total); } else { for (i = 0; i < len; i++) y[i] = 1.0 / len; } return y; } }