Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.apache.mahout.df.data;
 
 import java.util.List;
Helper methods that deals with data lists and arrays of values
 
 public class DataUtils {
   private DataUtils() {
   }

  
Computes the sum of the values

Parameters:
values
Returns:
 
   public static int sum(int[] values) {
     int sum = 0;
     for (int value : values) {
       sum += value;
     }
 
     return sum;
   }

  
foreach i : array1[i] += array2[i]

Parameters:
array1
array2
 
   public static void add(int[] array1int[] array2) {
     if (array1.length != array2.length) {
       throw new IllegalArgumentException("array1.length != array2.length");
     }
     
     for (int index = 0; index < array1.lengthindex++) {
       array1[index] += array2[index];
     }
   }

  
foreach i : array1[i] -= array2[i]

Parameters:
array1
array2
 
   public static void dec(int[] array1int[] array2) {
     if (array1.length != array2.length) {
       throw new IllegalArgumentException("array1.length != array2.length");
     }
     
     for (int index = 0; index < array1.lengthindex++) {
       array1[index] -= array2[index];
     }
   }

  
return the index of the maximum of the array, breaking ties randomly

Parameters:
rng used to break ties
values
Returns:
index of the maximum
 
   public static int maxindex(Random rngint[] values) {
     int max = 0;
     List<Integermaxindices = new ArrayList<Integer>();
 
     for (int index = 0; index < values.lengthindex++) {
       if (values[index] > max) {
         max = values[index];
         maxindices.clear();
         maxindices.add(index);
       } else if (values[index] == max) {
         maxindices.add(index);
       }
     }
 
     int bestind;
     if (maxindices.size() > 1) {
       // break ties randomly
      bestind = maxindices.get(rng.nextInt(maxindices.size()));
    } else {
      bestind = maxindices.get(0);
    }
    return bestind;
  }
New to GrepCode? Check out our FAQ X