HelloSuanShu

Wikis > SuanShu > Tutorial > HelloSuanShu

Hello SuanShu

This tutorial walks you through a simple first example on using the SuanShu library. You will need to have a SuanShu project to follow the examples here. For details on how to create or download a SuanShu project, please read the Setup Guide.

We will create some matrices and vectors and do some simple linear algebra. Java is an object-oriented language. So, other than the primitive types, like int and double, all variables are objects, namely, instances of some object definitions called classes. For example, to have a matrix instance, a matrix variable, or simply a matrix, we create an instance of the Matrix class.

 

Matrix A1 = new DenseMatrix(new double[][]{//create a matrix

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

});

We can print them out to check.

System.out.println(A1);

In NetBeans the short cut for System.out.println is to type “sout” (without the “”) and then hit the “Tab” key.

To fix the imports, hit “Ctrl+Shift+I”. There are multiple choices. Choose “com.numericalmethod.suanshu.matrix.doubles.Matrix”.

fix_all_imports_matrix

Hit “F6” to run.

Vola! You will see this output.

 

3×3

[,1] [,2] [,3]

[1,] 1.000000, 2.000000, 3.000000,

[2,] 4.000000, 5.000000, 6.000000,

[3,] 7.000000, 8.000000, 9.000000,

Congratulations! You have just successfully created your first matrix using the SuanShu library. We are now ready to do more.

Let’s check whether this matrix has a full rank. We do

 

int rank1 = MatrixMeasure.rank(A1);//compute A1’s rank

System.out.println(rank1);//rank1 is 2 implies a deficient matrix

The MatrixMeasure.rank method computes the numerical rank. For more information, see Compute the numerical rank of a matrix.

Hit “F6” to run.

We see that the rank of A1 is 2 so A1 is a rank deficient matrix. We can make it a full rank matrix by changing one entry.

A1.set(3, 3, 10);//change A1[3, 3] to 10

Note that SuanShu counts from 1!

Let’s try again.

rank1 = MatrixMeasure.rank(A1);//compute modified A1’s rank

System.out.println(String.format(“rank for the modified A1 = %d”, rank1));

OK. A1 is now a full rank matrix. We can now find its inverse.

Matrix A2 = new Inverse(A1);//compute A1 inverse

System.out.println(A2);

Hit “Ctrl+Shift+I” to fix the imports. Hit “F6” to run. Here is the output.

3×3

[,1] [,2] [,3]

[1,]  -0.666667, -1.333333, 1.000000,

[2,]  -0.666667, 3.666667, -2.000000,

[3,]  1.000000, -2.000000, 1.000000,

It looks good. To make sure, we multiply them together.

 

 

Matrix A3 = A1.multiply(A2);//A3 = A1 %*% A2

/*

* In numerical computing, we are usually satisfied to compare equality up to a precision.

*/

final double precision = 1e-14;//A3 and the 3×3 identity matrix are equal up to 14 decimal points

if (AreMatrices.equal(A3, new DenseMatrix(3, 3).ONE(), precision)) {//compare A3 to the 3×3 identity matrix

System.out.println(“A2 is indeed A1’s inverse”);

}

Hit “Ctrl+Shift+I” to fix the imports. Hit “F6” to run.

OK. It works.

Let’s try a vector. We do

 

Vector y = new DenseVector(new double[]{//create a vector

20, 10, 4

});

Hit “Ctrl+Shift+I” to fix the imports. There are multiple choices. Choose “com.numericalmethod.suanshu.vector.doubles.Vector”.

fix_all_imports_vector2

We solve a linear equation of this form.

Ax = y

We need to set up a linear equation solver.

LinearSystemSolver solver = new LinearSystemSolver(A1);//set up the linear equation solver

Vector x = solver.solve(y);//solve A1 %*% x = y

System.out.println(x);

Hit “Ctrl+Shift+I” to fix the imports. There are multiple choices. Choose “com.numericalmethod.suanshu.matrix.doubles.linearsystem.LinearSystemSolver”.

x is a vector [-22.666667, 15.333333, 4.000000].

The source code can be browsed in our svn repository here.