1

In Mablab/Octave, I can create a new vector with range-style, for example:
v = [1:10];

However, when I put it into ejml equation (Java), it doesn't work:
eq.process("v = [1:10]");

It only works for explicit initialization like:
eq.process("v = [1 2 ]");

Can anyone do that, or suggest me a workaround for it? Thanks.

Dude
  • 692
  • 1
  • 4
  • 25
lenhhoxung
  • 2,530
  • 2
  • 30
  • 61

2 Answers2

1

To work with matrix and vectors you can use normal java operators and loops ans det it here is a example solving a linear system, and adding elements and then setting a Range vector column and row

I hope this helps you

import org.ejml.factory.SingularMatrixException;
import org.ejml.simple.SimpleMatrix;

/**
 * Created by anquegi on 15/05/15.
 */
public class TestEjml {



    public static void main(String args[]){

        //Solving a system
        SimpleMatrix A = new SimpleMatrix(2,2);
        SimpleMatrix b = new SimpleMatrix(2,1);
        SimpleMatrix x;


        // Can assign values the usual way

        A.set(0,0,1);
        A.set(0,1,4);
        A.set(1,0,1);
        A.set(1,1,1);

        b.set(0,0,28);
        b.set(1,0,10);


        try {
            x = A.solve(b);
            System.out.println(x);
        } catch ( SingularMatrixException e ) {
            e.printStackTrace();
        }

        // So to do a Range
        SimpleMatrix my_range_v = new SimpleMatrix(10,1);

        for (int i = 0; i < my_range_v.numRows(); i++) {
            my_range_v.set(i,i); // you can set also wit set(row,col,value)
        }

        // So to do a Range
        SimpleMatrix my_range_h = new SimpleMatrix(1,10);



        for (int i = 0; i < my_range_h.numCols(); i++) {
            my_range_h.set(i,i); // you can set also wit set(row,col,value)
        }

        System.out.println(my_range_v);
        System.out.println(my_range_h);



    }

}

with the result:

Enter number: 2

[info] Running ejml.TestEjml 
Type = dense real , numRows = 2 , numCols = 1
 4,000  
 6,000  

Type = dense real , numRows = 10 , numCols = 1
 0,000  
 1,000  
 2,000  
 3,000  
 4,000  
 5,000  
 6,000  
 7,000  
 8,000  
 9,000  

Type = dense real , numRows = 1 , numCols = 10
 0,000   1,000   2,000   3,000   4,000   5,000   6,000   7,000   8,000   9,000  

[success] Total time: 2 s, completed 15/05/2015 12:16:20
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
anquegi
  • 11,125
  • 4
  • 51
  • 67
  • Thanks for the workaround, it actually works and i, but what I prefer is a Matlab/Octive-like expression inside double qoutes " ". – lenhhoxung May 15 '15 at 10:35
  • 1
    I think that at the moment this is not possible because in the javadoc there isn't a reference to that http://ejml.org/javadoc/org/ejml/equation/Equation.html – anquegi May 15 '15 at 11:04
  • And also in the examples it doesn't use it http://ejml.org/wiki/index.php?title=Equations, I will consider to add this conclusion to the answer – anquegi May 15 '15 at 11:10
  • Can you also help with this question?http://stackoverflow.com/questions/30268878/error-when-assigning-value-for-matrix-element-with-equation – lenhhoxung May 15 '15 at 21:21
1

Actually this code

eq.process("A = [1:5]");
DMatrixRMaj A = new DMatrixRMaj(eq.lookupMatrix("A"));

gives back

Type = dense64 real , numRows = 1 , numCols = 5
 1,000  2,000  3,000  4,000  5,000 
Lore
  • 1,286
  • 1
  • 22
  • 57