Java Code Examples for org.apache.commons.lang.ArrayUtils.toPrimitive()

The following are Jave code examples for showing how to use toPrimitive() of the org.apache.commons.lang.ArrayUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: DroidSentinel   File: DoubleMovingAverage.java   View Source Code Vote up 6 votes
public List<Double> getAllPredictions(List<Double> data, int T){
    //returns all the predictions for a time serie
    //this is expensive, but helps in testing
    double[] dObs=ArrayUtils.toPrimitive(data.toArray(new Double[data.size()]));
    double[] sol= new double[data.size()+T];
    for(int i=0;i<data.size();i++){
        List<Double> subList = new ArrayList<Double>(data.subList(0, i));
        if(i<T){
            sol[i]=dObs[i];
            sol[i+T]=getPrediction(subList);
        }
        else{
            sol[i+T]=getPrediction(subList);
        }
    }//for

    List<Double> auxSOl=convertDoubletoArrayList(sol);
    return auxSOl;
}
 
Example 2
Project: DroidSentinel   File: DoubleMovingAverage.java   View Source Code Vote up 6 votes
public double getPrediction(List<Double> data){
    //Predict a simple value
    //pred[i]= SMA[i-1]+(O[i-2]-SMA[i-2])
    double pred=0.0;

    double[] dObs=ArrayUtils.toPrimitive(data.toArray(new Double[data.size()]));
    List<Double> SMA_data = get2MA((List) ((ArrayList) data).clone());
    double[] dSMA=ArrayUtils.toPrimitive(SMA_data.toArray(new Double[SMA_data.size()]));

    int i = data.size()-1;
    if(i>2){
        pred=dSMA[i]+(dObs[i-1]-dSMA[i-1]);
        //System.out.println("pred: "+pred+"dSMA[i-1]:" + dSMA[i-1]+" + (dObs[i-2]"+dObs[i-2]+"-dSMA[i-2]): "+dSMA[i-2]+")");
    }
    else{
        pred= 0;
    }
    return pred;
}
 
Example 3
Project: DroidSentinel   File: WeightedMovingAverage.java   View Source Code Vote up 6 votes
public List<Double> getAllPredictions(List<Double> data, int T){
    //returns all the predictions for a time serie
    //this is expensive, but helps in testing
    double[] dObs=ArrayUtils.toPrimitive(data.toArray(new Double[data.size()]));
    double[] sol= new double[data.size()+T];
    for(int i=0;i<data.size();i++){
        List<Double> subList = new ArrayList<Double>(data.subList(0, i));
        if(i<T){
            sol[i]=dObs[i];
            sol[i+T]=getPrediction(subList);
        }
        else{
            sol[i+T]=getPrediction(subList);
        }
    }//for

    List<Double> auxSOl=convertDoubletoArrayList(sol);
    return auxSOl;
}
 
Example 4
Project: DroidSentinel   File: WeightedMovingAverage.java   View Source Code Vote up 6 votes
public double getPrediction(List<Double> data){
    //Predict a simple value
    //pred[i]= SMA[i-1]+(O[i-2]-SMA[i-2])
    double pred=0.0;

    double[] dObs=ArrayUtils.toPrimitive(data.toArray(new Double[data.size()]));
    List<Double> SMA_data = getWMA((List) ((ArrayList) data).clone());
    double[] dSMA=ArrayUtils.toPrimitive(SMA_data.toArray(new Double[SMA_data.size()]));

    int i = data.size()-1;
    if(i>2){
        pred=dSMA[i]+(dObs[i-1]-dSMA[i-1]);
        //System.out.println("pred: "+pred+"dSMA[i-1]:" + dSMA[i-1]+" + (dObs[i-2]"+dObs[i-2]+"-dSMA[i-2]): "+dSMA[i-2]+")");
    }
    else{
        pred= 0;
    }
    return pred;
}
 
Example 5
Project: smaph   File: SmaphUtils.java   View Source Code Vote up 5 votes
/**
 * @param ftrCount
 *            the number of features.
 * @param excludedFeatures
 *            the features that have to be excluded from the feature list.
 * @return a vector containing all feature ids from 1 to ftrCount
 *         (included), except those in excludedFeatures.
 */
public static int[] getAllFtrVect(int ftrCount, int[] excludedFeatures) {
	Arrays.sort(excludedFeatures);
	Vector<Integer> res = new Vector<>();
	for (int i = 1; i < ftrCount + 1; i++)
		if (Arrays.binarySearch(excludedFeatures, i) < 0)
			res.add(i);
	return ArrayUtils.toPrimitive(res.toArray(new Integer[] {}));
}
 
Example 6
Project: alfresco-repository   File: PageCollatorTest.java   View Source Code Vote up 5 votes
private int[] createMergedPage(int skip, int count, List<Integer> s1, int[] s2)
{
    int[] s1Primitive = (s1 == null || s1.isEmpty()) ? new int[] {} : ArrayUtils.toPrimitive((Integer[]) s1
                .toArray());

    return createMergedPage(skip,
                            count,
                            s1Primitive,
                            s2);
}
 
Example 7
Project: ShotgunWSD   File: GeometricMedianComputation.java   View Source Code Vote up 5 votes
/**
 * Computes the Geometric Median of a given array of word embeddings
 * @param senseEmbeddings Array of Word Embeddings
 * @return The vector that represents the geometric median of the given array
 */
public double[] compute(ArrayList<Double[]> senseEmbeddings) {
    Double[] senseEmbedding = new Double[300];
    Double[] sum1;
    double sum2;

    int iterations = 10;
    double norm;

    for (int i = 0; i < 300; i++) {
        senseEmbedding[i] = 0d;
    }

    for (int i = 0; i < iterations; i++) {
        sum1 = new Double[300]; sum2 = 0;

        for (int j = 0; j < 300; j++) {
            sum1[j] = 0d;
        }

        for(Double[] tmpSenseEmbedding : senseEmbeddings) {
            // this is x
            norm = MathUtil.computeNorm(tmpSenseEmbedding, senseEmbedding);

            sum1 = MathUtil.sumVectors(sum1, MathUtil.divideVectors(tmpSenseEmbedding, norm));
            sum2 += 1 / norm;
        }

        senseEmbedding = MathUtil.divideVectors(sum1, sum2);
    }

    return ArrayUtils.toPrimitive(senseEmbedding);
}
 
Example 8
Project: ShotgunWSD   File: SenseEmbedding.java   View Source Code Vote up 5 votes
/**
 *
 * @param wordVector The Word Embeddings dictionary
 * @param synset The synset ...
 * @param word The word ...
 * @return The sense embedding of a synset
 */
public static double[] getSenseEmbedding(WordVectors wordVector, Synset synset, String word, SenseComputation senseComputation) {
    if(wordEmbeddings.containsKey(synset)){
        return ArrayUtils.toPrimitive(wordEmbeddings.get(synset));
    }

    String[] words = getSenseBag(synset, word);

    double[] senseEmbedding, tmpEmbedding;
    Double[] tmpEmbedding2, tmpSenseEmbedding;
    ArrayList<Double[]> senseEmbeddings = new ArrayList<>();

    // For each word in the sense bag, get the coresponding word embeddings and store them in an array
    for (String w : words) {
        if (w != null) {
            if (wordVector.hasWord(w)) {
                tmpEmbedding = wordVector.getWordVector(w);

                tmpEmbedding2 = new Double[tmpEmbedding.length];
                for (int i = 0; i < tmpEmbedding.length; i++) {
                    tmpEmbedding2[i] = tmpEmbedding[i];
                }
                senseEmbeddings.add(tmpEmbedding2);
            }
        }
    }

    senseEmbedding = senseComputation.compute(senseEmbeddings);

    tmpSenseEmbedding = new Double[senseEmbedding.length];
    for (int i = 0; i < tmpSenseEmbedding.length; i++) {
        tmpSenseEmbedding[i] = senseEmbedding[i];
    }
    wordEmbeddings.put(synset, tmpSenseEmbedding);

    return senseEmbedding;
}
 
Example 9
Project: flume-release-1.7.0   File: FlumeEventQueue.java   View Source Code Vote up 4 votes
/**
 * Serialize the set of in flights into a byte longBuffer.
 *
 * @return Returns the checksum of the buffer that is being
 * asynchronously written to disk.
 */
public void serializeAndWrite() throws Exception {
  Collection<Long> values = inflightEvents.values();
  if (!fileChannel.isOpen()) {
    file = new RandomAccessFile(inflightEventsFile, "rw");
    fileChannel = file.getChannel();
  }
  if (values.isEmpty()) {
    file.setLength(0L);
  }
  //What is written out?
  //Checksum - 16 bytes
  //and then each key-value pair from the map:
  //transactionid numberofeventsforthistxn listofeventpointers

  try {
    int expectedFileSize = (((inflightEvents.keySet().size() * 2) //for transactionIDs and
                                                                  //events per txn ID
        + values.size()) * 8) //Event pointers
        + 16; //Checksum
    //There is no real need of filling the channel with 0s, since we
    //will write the exact number of bytes as expected file size.
    file.setLength(expectedFileSize);
    Preconditions.checkState(file.length() == expectedFileSize,
        "Expected File size of inflight events file does not match the "
            + "current file size. Checkpoint is incomplete.");
    file.seek(0);
    final ByteBuffer buffer = ByteBuffer.allocate(expectedFileSize);
    LongBuffer longBuffer = buffer.asLongBuffer();
    for (Long txnID : inflightEvents.keySet()) {
      Set<Long> pointers = inflightEvents.get(txnID);
      longBuffer.put(txnID);
      longBuffer.put((long) pointers.size());
      LOG.debug("Number of events inserted into "
          + "inflights file: " + String.valueOf(pointers.size())
          + " file: " + inflightEventsFile.getCanonicalPath());
      long[] written = ArrayUtils.toPrimitive(
          pointers.toArray(new Long[0]));
      longBuffer.put(written);
    }
    byte[] checksum = digest.digest(buffer.array());
    file.write(checksum);
    buffer.position(0);
    fileChannel.write(buffer);
    fileChannel.force(true);
    syncRequired = false;
  } catch (IOException ex) {
    LOG.error("Error while writing checkpoint to disk.", ex);
    throw ex;
  }
}
 
Example 10
Project: calendar-based-microsim   File: ArrayHandler.java   View Source Code Vote up 2 votes
/**
 * 
 * @param myList
 * @return
 */
public static int[] toInt(List<Integer> myList) {
	return ArrayUtils.toPrimitive(myList.toArray(new Integer[myList.size()]));
}