Revision for “HelloSuanShu” created on April 14, 2015 @ 07:23:20

TitleContentExcerptRevision Note
<h1 id="HelloSuanShu">Hello <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a></h1>
This tutorial walks you through a simple first example on using the <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> library. You will need to have a <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> project to follow the examples here. For details on how to create or download a <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> project, please read the <a href="http://numericalmethod.com/up/suanshu/tutorial/setupguide/" target="_blank">Setup Guide</a>.

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 <tt>int</tt> and <tt>double</tt>, all variables are objects, namely, instances of some object definitions called <a class="ext-link" href="http://java.sun.com/docs/books/tutorial/java/javaOO/index.html"><span class="icon">​</span>classes</a>. For example, to have a matrix instance, a matrix variable, or simply a matrix, we create an instance of the <tt>Matrix</tt> 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.


In <a href="http://numericalmethod.com/up/netbeans/" target="_blank">NetBeans</a> the short cut for <tt>System.out.println</tt> 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".

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/fix_all_imports_matrix.png"><img class="alignnone size-full wp-image-3558" src="http://numericalmethod.com/wp-content/uploads/2015/04/fix_all_imports_matrix.png" alt="fix_all_imports_matrix" width="803" height="91" /></a>

Hit "F6" to run.

Vola! You will see this output.



[,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 <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> 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 <tt>MatrixMeasure.rank</tt> method computes the numerical rank. For more information, see <a href="http://www.numericalmethod.com/javadoc/suanshu/com/numericalmethod/suanshu/algebra/linear/matrix/doubles/operation/MatrixMeasure.html#rank">Compute the numerical rank of a matrix.</a>

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 <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> 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


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


[,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".

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/fix_all_imports_vector2.png"><img class="alignnone size-full wp-image-3559" src="http://numericalmethod.com/wp-content/uploads/2015/04/fix_all_imports_vector2.png" alt="fix_all_imports_vector2" width="779" height="93" /></a>

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


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

x is a vector <tt>[-22.666667, 15.333333, 4.000000]</tt>.

<a href="http://redmine.numericalmethod.com/projects/public/repository/show/Tutorials" target="_blank">The source code can be browsed in our svn repository here</a>.

Old New Date Created Author Actions
April 14, 2015 @ 07:23:20 sanjay
April 13, 2015 @ 11:20:23 sanjay
April 13, 2015 @ 11:20:01 [Autosave] sanjay
April 13, 2015 @ 09:01:15 sanjay