Java Code Examples for org.ejml.simple.SimpleMatrix#get()

The following examples show how to use org.ejml.simple.SimpleMatrix#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SentimentAnalyzer.java    From hazelcast-jet-demos with Apache License 2.0 6 votes vote down vote up
private double getScore(List<CoreMap> sentences, double overallSentiment) {
    int matrixIndex =
            overallSentiment < -0.5  ? 0  // very negative
            : overallSentiment < 0.0 ? 1  // negative
            : overallSentiment < 0.5 ? 3  // positive
            : 4;                       // very positive
    double sum = 0;
    int numberOfSentences = 0;
    for (CoreMap sentence : sentences) {
        Tree sentiments = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
        int predictedClass = RNNCoreAnnotations.getPredictedClass(sentiments);
        if (predictedClass == 2) { // neutral
            continue;
        }
        SimpleMatrix matrix = RNNCoreAnnotations.getPredictions(sentiments);
        sum += matrix.get(matrixIndex);
        numberOfSentences++;
    }
    return sum / numberOfSentences;
}
 
Example 2
Source File: MatrixOps.java    From okde-java with MIT License 5 votes vote down vote up
public static double maxVectorElement(SimpleMatrix matrix){
	double d = Double.MIN_VALUE;
	for (int i = 0; i < matrix.numRows(); i++) {
			if(matrix.get(i,0)>d)
				d = matrix.get(i,0);
	}
	return d;
}
 
Example 3
Source File: MatrixOps.java    From okde-java with MIT License 5 votes vote down vote up
public static int maxVectorElementIndex(SimpleMatrix matrix){
	double d = Double.MIN_VALUE;
	int row = 0;
	for (int i = 0; i < matrix.numRows(); i++) {
			if(matrix.get(i,0)>d){
				d = matrix.get(i,0);
				row = i;
			}
	}
	return row;
}
 
Example 4
Source File: EphemerisSystemGalileo.java    From GNSS_Compare with Apache License 2.0 4 votes vote down vote up
private SimpleMatrix satellite_motion_diff_eq(SimpleMatrix pos1Array, SimpleMatrix vel1Array, SimpleMatrix accArray,
		long ellAGlo, double gmGlo, double j2Glo, double omegaeDotGlo) {
	// TODO Auto-generated method stub

	/* renaming variables for better readability position */
	double X = pos1Array.get(0);
	double Y = pos1Array.get(1);
	double Z = pos1Array.get(2);

	// System.out.println("X: " + X);
	// System.out.println("Y: " + Y);
	// System.out.println("Z: " + Z);

	/* velocity */
	double Xv = vel1Array.get(0);
	double Yv = vel1Array.get(1);

	// System.out.println("Xv: " + Xv);
	// System.out.println("Yv: " + Yv);

	/* acceleration (i.e. perturbation) */
	double Xa = accArray.get(0);
	double Ya = accArray.get(1);
	double Za = accArray.get(2);

	// System.out.println("Xa: " + Xa);
	// System.out.println("Ya: " + Ya);
	// System.out.println("Za: " + Za);

	/* parameters */
	double r = Math.sqrt(Math.pow(X, 2) + Math.pow(Y, 2) + Math.pow(Z, 2));
	double g = -gmGlo / Math.pow(r, 3);
	double h = j2Glo * 1.5 * Math.pow((ellAGlo / r), 2);
	double k = 5 * Math.pow(Z, 2) / Math.pow(r, 2);

	// System.out.println("r: " + r);
	// System.out.println("g: " + g);
	// System.out.println("h: " + h);
	// System.out.println("k: " + k);

	/* differential velocity */
	double[] vel_dot = new double[3];
	vel_dot[0] = g * X * (1 - h * (k - 1)) + Xa + Math.pow(omegaeDotGlo, 2) * X + 2 * omegaeDotGlo * Yv;
	// System.out.println("vel1: " + vel_dot[0]);

	vel_dot[1] = g * Y * (1 - h * (k - 1)) + Ya + Math.pow(omegaeDotGlo, 2) * Y - 2 * omegaeDotGlo * Xv;
	// System.out.println("vel2: " + vel_dot[1]);

	vel_dot[2] = g * Z * (1 - h * (k - 3)) + Za;
	// System.out.println("vel3: " + vel_dot[2]);

	SimpleMatrix velDotArray = new SimpleMatrix(1, 3, true, vel_dot);
	// velDotArray.print();

	return velDotArray;
}
 
Example 5
Source File: EphemerisSystemGps.java    From GNSS_Compare with Apache License 2.0 4 votes vote down vote up
private SimpleMatrix satellite_motion_diff_eq(SimpleMatrix pos1Array,
			SimpleMatrix vel1Array, SimpleMatrix accArray, long ellAGlo,
			double gmGlo, double j2Glo, double omegaeDotGlo) {
		// TODO Auto-generated method stub
		
		/* renaming variables for better readability position */
		double X = pos1Array.get(0);
		double Y = pos1Array.get(1);
		double Z = pos1Array.get(2);
		
//		System.out.println("X: " + X);
//		System.out.println("Y: " + Y);
//		System.out.println("Z: " + Z);
		
		/* velocity */
		double Xv = vel1Array.get(0);
		double Yv = vel1Array.get(1);
		
//		System.out.println("Xv: " + Xv);
//		System.out.println("Yv: " + Yv);
		
		/* acceleration (i.e. perturbation) */
		double Xa = accArray.get(0);
		double Ya = accArray.get(1);
		double Za = accArray.get(2);
		
//		System.out.println("Xa: " + Xa);
//		System.out.println("Ya: " + Ya);
//		System.out.println("Za: " + Za);
		
		/* parameters */
		double r = Math.sqrt(Math.pow(X,2) + Math.pow(Y,2) + Math.pow(Z,2));
		double g = -gmGlo/Math.pow(r,3);
		double h = j2Glo*1.5*Math.pow((ellAGlo/r),2);
		double k = 5*Math.pow(Z,2)/Math.pow(r,2);
		
//		System.out.println("r: " + r);
//		System.out.println("g: " + g);
//		System.out.println("h: " + h);
//		System.out.println("k: " + k);
		
		/* differential velocity */
		double[] vel_dot = new double[3] ;
		vel_dot[0] = g*X*(1 - h*(k - 1)) + Xa + Math.pow(omegaeDotGlo,2)*X + 2*omegaeDotGlo*Yv;
//		System.out.println("vel1: " + vel_dot[0]);
		
		vel_dot[1] = g*Y*(1 - h*(k - 1)) + Ya + Math.pow(omegaeDotGlo,2)*Y - 2*omegaeDotGlo*Xv;
//		System.out.println("vel2: " + vel_dot[1]);
		
		vel_dot[2] = g*Z*(1 - h*(k - 3)) + Za;
//		System.out.println("vel3: " + vel_dot[2]);
		
		SimpleMatrix velDotArray = new SimpleMatrix(1, 3, true, vel_dot);
//		velDotArray.print();
		
		return velDotArray;
	}
 
Example 6
Source File: EphemerisSystem.java    From GNSS_Compare with Apache License 2.0 4 votes vote down vote up
private SimpleMatrix satellite_motion_diff_eq(SimpleMatrix pos1Array,
                                                  SimpleMatrix vel1Array, SimpleMatrix accArray, long ellAGlo,
                                                  double gmGlo, double j2Glo, double omegaeDotGlo) {
        // TODO Auto-generated method stub
		
		/* renaming variables for better readability position */
        double X = pos1Array.get(0);
        double Y = pos1Array.get(1);
        double Z = pos1Array.get(2);

//		System.out.println("X: " + X);
//		System.out.println("Y: " + Y);
//		System.out.println("Z: " + Z);
		
		/* velocity */
        double Xv = vel1Array.get(0);
        double Yv = vel1Array.get(1);

//		System.out.println("Xv: " + Xv);
//		System.out.println("Yv: " + Yv);
		
		/* acceleration (i.e. perturbation) */
        double Xa = accArray.get(0);
        double Ya = accArray.get(1);
        double Za = accArray.get(2);

//		System.out.println("Xa: " + Xa);
//		System.out.println("Ya: " + Ya);
//		System.out.println("Za: " + Za);
		
		/* parameters */
        double r = Math.sqrt(Math.pow(X, 2) + Math.pow(Y, 2) + Math.pow(Z, 2));
        double g = -gmGlo / Math.pow(r, 3);
        double h = j2Glo * 1.5 * Math.pow((ellAGlo / r), 2);
        double k = 5 * Math.pow(Z, 2) / Math.pow(r, 2);

//		System.out.println("r: " + r);
//		System.out.println("g: " + g);
//		System.out.println("h: " + h);
//		System.out.println("k: " + k);
		
		/* differential velocity */
        double[] vel_dot = new double[3];
        vel_dot[0] = g * X * (1 - h * (k - 1)) + Xa + Math.pow(omegaeDotGlo, 2) * X + 2 * omegaeDotGlo * Yv;
//		System.out.println("vel1: " + vel_dot[0]);

        vel_dot[1] = g * Y * (1 - h * (k - 1)) + Ya + Math.pow(omegaeDotGlo, 2) * Y - 2 * omegaeDotGlo * Xv;
//		System.out.println("vel2: " + vel_dot[1]);

        vel_dot[2] = g * Z * (1 - h * (k - 3)) + Za;
//		System.out.println("vel3: " + vel_dot[2]);

        SimpleMatrix velDotArray = new SimpleMatrix(1, 3, true, vel_dot);
//		velDotArray.print();

        return velDotArray;
    }
 
Example 7
Source File: SentimentDetector.java    From AIBlueprints with MIT License 4 votes vote down vote up
public void detectSentiment(String msgId, String txt, String source,
                            boolean useCoreNLP, boolean saveDb) {
    Annotation annotation = new Annotation(txt);
    pipeline.annotate(annotation);
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    if (sentences != null) {
        int sentNum = 0;
        for (CoreMap sentence : sentences) {
            sentNum++;
            String sentStr = sentence.toString();
            String sentiment = "Neutral";
            int sentiment_num = 2;
            double score = 0.0;
            if(useCoreNLP) {
                sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
                Tree sentimentTree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
                // 0 = very negative, 1 = negative, 2 = neutral, 3 = positive, and 4 = very positive
                Integer predictedClass = RNNCoreAnnotations.getPredictedClass(sentimentTree);
                SimpleMatrix scoreMatrix = RNNCoreAnnotations.getPredictions(sentimentTree);
                score = scoreMatrix.get(predictedClass.intValue(), 0);
                sentiment_num = predictedClass.intValue();
            } else {
                double sentimentValue = 0.0;
                int adjectivesFound = 0;
                // check every adjective to see if it appears in sentence
                for(String adj : adjectives.keySet()) {
                    if(sentStr.matches(".*\\b" + adj + "\\b.*")) {
                        sentimentValue += adjectives.get(adj);
                        adjectivesFound++;
                    }
                }
                if(adjectivesFound > 0) {
                    sentimentValue /= adjectivesFound;
                }
                if(sentimentValue < -2) {
                    sentiment = "Very Negative";
                    sentiment_num = 0;
                } else if(sentimentValue < -0.5) {
                    sentiment = "Negative";
                    sentiment_num = 1;
                } else if(sentimentValue < 0.5) {
                    sentiment = "Neutral";
                    sentiment_num = 2;
                } else if(sentimentValue < 2) {
                    sentiment = "Positive";
                    sentiment_num = 3;
                } else {
                    sentiment = "Very Positive";
                    sentiment_num = 4;
                }
                if(adjectivesFound > 0)
                    score = 1.0;
                else
                    score = 0.0;
            }

            logger.log(Level.INFO, source + " "
                    + msgId + " - "
                    + sentiment + " (" + score + ") "
                    + sentence.toString());

            // Only save somewhat confident, non-neutral results
            if(score > 0.3 && sentiment_num != 2 && saveDb) {
                try {
                    // check if
                    String sql = "INSERT INTO sentiment"
                            + "(id,source,msg,sentiment,sentiment_num,score)"
                            + " VALUES(?,?,?,?,?,?)";
                    PreparedStatement pstmt = db.prepareStatement(sql);
                    pstmt.setString(1, msgId + "-" + sentNum);
                    pstmt.setString(2, source);
                    pstmt.setString(3, sentStr);
                    pstmt.setString(4, sentiment);
                    pstmt.setInt(5, sentiment_num);
                    pstmt.setDouble(6, score);
                    pstmt.executeUpdate();
                } catch (SQLException e) {
                    logger.log(Level.SEVERE, e.getMessage());
                }
            }
        }
    }
}
 
Example 8
Source File: LSPI.java    From burlap with Apache License 2.0 2 votes vote down vote up
/**
 * Runs LSTDQ on this object's current {@link SARSData} dataset.
 * @return the new weight matrix as a {@link SimpleMatrix} object.
 */
public SimpleMatrix LSTDQ(){
	
	//set our policy
	Policy p = new GreedyQPolicy(this);
	
	//first we want to get all the features for all of our states in our data set; this is important if our feature database generates new features on the fly
	List<SSFeatures> features = new ArrayList<LSPI.SSFeatures>(this.dataset.size());
	int nf = 0;
	for(SARS sars : this.dataset.dataset){
		SSFeatures transitionFeatures = new SSFeatures(this.saFeatures.features(sars.s, sars.a), this.saFeatures.features(sars.sp, p.action(sars.sp)));
		features.add(transitionFeatures);
		nf = Math.max(nf, transitionFeatures.sActionFeatures.length);
	}

	SimpleMatrix B = SimpleMatrix.identity(nf).scale(this.identityScalar);
	SimpleMatrix b = new SimpleMatrix(nf, 1);
	
	
	
	for(int i = 0; i < features.size(); i++){

		SimpleMatrix phi = this.phiConstructor(features.get(i).sActionFeatures, nf);
		SimpleMatrix phiPrime = this.phiConstructor(features.get(i).sPrimeActionFeatures, nf);
		double r = this.dataset.get(i).r;
		

		SimpleMatrix numerator = B.mult(phi).mult(phi.minus(phiPrime.scale(gamma)).transpose()).mult(B);
		SimpleMatrix denomenatorM = phi.minus(phiPrime.scale(this.gamma)).transpose().mult(B).mult(phi);
		double denomenator = denomenatorM.get(0) + 1;
		
		B = B.minus(numerator.scale(1./denomenator));
		b = b.plus(phi.scale(r));
		
		//DPrint.cl(0, "updated matrix for row " + i + "/" + features.size());
		
	}
	
	
	SimpleMatrix w = B.mult(b);
	
	this.vfa = this.vfa.copy();
	for(int i = 0; i < nf; i++){
		this.vfa.setParameter(i, w.get(i, 0));
	}
	
	return w;
	
	
}