Java Array Normalize normalizeToSumUpTo(double[] x, double sumUp)

Here you can find the source of normalizeToSumUpTo(double[] x, double sumUp)

Description

normalize To Sum Up To

License

Open Source License

Declaration

public static double[] normalizeToSumUpTo(double[] x, double sumUp) 

Method Source Code

//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;
    }
}

Related

  1. normalizeSpaces(char[] buf, int origStart, int origEnd)
  2. normalizeTo(float[] in, float normsum)
  3. normalizeToInPlace(double[] in, double normsum)
  4. normalizeToLogProbs(double[] x)
  5. normalizeToOne(double[] doubles)
  6. normalizeTrimmedText(char[] ch)
  7. NormalizeVec2D(double[] vec)
  8. normalizeVector(double[] input)
  9. normalizeVector(Double[] vector)