Hello NM Dev

This tutorial walks you through a simple first example on using the NM Dev library. You will need to have a NM Dev project to follow the examples here. For details on how to create or download a NM Dev 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.

[pastacode lang=”java” manual=”Matrix%20A1%20%3D%20new%20DenseMatrix(%0A%20%20new%20double%5B%5D%5B%5D%7B%20%2F%2F%20create%20a%20matrix%0A%20%20%20%20%7B1%2C%202%2C%203%7D%2C%0A%20%20%20%20%7B4%2C%205%2C%206%7D%2C%0A%20%20%20%20%7B7%2C%208%2C%209%7D%0A%7D)%3B” message=”Java matrix” highlight=”” provider=”manual”/]

We can print out the Java matrix to check.

[pastacode lang=”java” manual=”System.out.println(A1)%3B” message=”” highlight=”” provider=”manual”/]

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.


Hit “F6″ to run.

Vola! You will see this output.

[pastacode lang=”bash” manual=”%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%0A%5B1%2C%5D%201.000000%2C%202.000000%2C%203.000000%2C%0A%5B2%2C%5D%204.000000%2C%205.000000%2C%206.000000%2C%0A%5B3%2C%5D%207.000000%2C%208.000000%2C%209.000000%2C” message=”” highlight=”” provider=”manual”/]

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

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

[pastacode lang=”java” manual=”int%20rank1%20%3D%20MatrixMeasure.rank(A1)%3B%20%2F%2F%20compute%20A1’s%20rank%0ASystem.out.println(rank1)%3B%20%2F%2F%20rank1%20is%202%20implies%20a%20deficient%20matrix” message=”” highlight=”” provider=”manual”/]

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.

[pastacode lang=”java” manual=”A1.set(3%2C%203%2C%2010)%3B%20%2F%2F%20change%20A1%5B3%2C%203%5D%20to%2010″ message=”” highlight=”” provider=”manual”/]

Note that NM Dev counts from 1!

Let’s try again.

[pastacode lang=”java” manual=”rank1%20%3D%20MatrixMeasure.rank(A1)%3B%20%2F%2F%20compute%20modified%20A1’s%20rank%0ASystem.out.println(String.format(%22rank%20for%20the%20modified%20A1%20%3D%20%25d%22%2C%20rank1))%3B” message=”” highlight=”” provider=”manual”/]

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

[pastacode lang=”java” manual=”Matrix%20A2%20%3D%20new%20Inverse(A1)%3B%20%2F%2F%20compute%20A1%20inverse%0ASystem.out.println(A2)%3B” message=”” highlight=”” provider=”manual”/]

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

[pastacode lang=”bash” manual=”3×3%0A%0A%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%0A%5B1%2C%5D%20%C2%A0-0.666667%2C%20-1.333333%2C%201.000000%2C%0A%5B2%2C%5D%20%C2%A0-0.666667%2C%203.666667%2C%20-2.000000%2C%0A%5B3%2C%5D%20%C2%A01.000000%2C%20-2.000000%2C%201.000000%2C” message=”” highlight=”” provider=”manual”/]

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

[pastacode lang=”java” manual=”Matrix%20A3%20%3D%20A1.multiply(A2)%3B%20%2F%2F%20A3%20%3D%20A1%20%25*%25%20A2%0A%2F**%20In%20numerical%20computing%2C%20we%20are%20usually%20satisfied%20to%20compare%20equality%20up%20to%20a%20precision.*%2F%0Afinal%20double%20precision%20%3D%201e-14%3B%20%2F%2F%20A3%20and%20the%203×3%20identity%20matrix%20are%20equal%20up%20to%2014%20decimal%20points%0Aif%20(AreMatrices.equal(A3%2C%20new%20DenseMatrix(3%2C%203).ONE()%2C%20precision))%20%7B%20%2F%2F%20compare%20A3%20to%20the%203×3%20identity%20matrix%0ASystem.out.println(%22A2%20is%20indeed%20A1’s%20inverse%22)%3B” message=”” highlight=”” provider=”manual”/]

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

OK. It works.

Let’s try a vector. We do

[pastacode lang=”java” manual=”Vector%20y%20%3D%20new%20DenseVector(new%20double%5B%5D%7B%20%2F%2F%20create%20a%20vector%0A20%2C%2010%2C%204%0A%7D)%3B” message=”” highlight=”” provider=”manual”/]

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


We solve a linear equation of this form.

Ax = y

We need to set up a linear equation solver.

[pastacode lang=”java” manual=”LinearSystemSolver%20solver%20%3D%20new%20LinearSystemSolver(A1)%3B%20%2F%2F%20set%20up%20the%20linear%20equation%20solver%0AVector%20x%20%3D%20solver.solve(y)%3B%20%2F%2F%20solve%20A1%20%25*%25%20x%20%3D%20y%0ASystem.out.println(x)%3B” message=”” highlight=”” provider=”manual”/]

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

x is a vector

[pastacode lang=”bash” manual=”%5B-22.666667%2C%2015.333333%2C%204.000000%5D” message=”” highlight=”” provider=”manual”/]

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


The NM Dev Tutorials

The NM Dev tutorials are practical guides for those who would like to create mathematics applications using the NM Dev library. They include lessons with many complete and working examples. Groups of related lessons are organized into “trails”.

For the most up-to-date information on using the NM Dev library, please consult the Javadoc. Please check out the NM Forum, the place to discuss the NM Dev library, mathematics and programming. You are also encouraged to post your questions there to get help beyond documents.

Before you start these lessons, please make sure you have set up the development environment properly. You can find the details in the Setup Guide.

Basic Trails

Specialized Trails


Time Series Analysis

  • various moving average filters
  • univariate/multivariate time series analysis
    • ARIMA modeling, simulation, fitting, prediction
    • GARCH modeling, simulation, fitting, prediction
    • Auto-Covariance computation

Linear Regression

  • OLS regression
  • logistic regression
  • GLM regression
    • family: Binomial, Gamma, Gaussian, Inverse Gaussian, Poisson
    • quasi-family: Binomial, Gamma, Gaussian, Inverse Gaussian, Poisson
  • GLM model selection

Hypothesis Testing

  • Kolmogorov-Smirnov
  • D’Agostino
  • Jarque-Bera
  • Lilliefors
  • Shapiro-Wilk
  • One-way ANOVA
  • T
  • Kruskal-Wallis
  • Siegel-Tukey
  • Van der Waerden
  • Wilcoxon rank sum
  • Wilcoxon signed rank
  • Breusch-Pagan
  • Glejser
  • Harvey-Godfrey
  • White
  • ADF
  • Bartlett
  • Brown-Forsythe
  • F
  • Levene


This is a collection of advanced statistics algorithms.

  • cointegration
  • covariance selection
  • factor analysis
  • hidden Markov model (Rabiner)
  • hidden Markov model (mixture models: Beta, Binomial, Exponential, Gamma, Log-Normal, Normal, Poisson)
  • hypothesis testing
  • Kalman filter
  • LASSO (Least Absolute Shrinkage and Selection Operator)
  • linear regression
  • principal component analysis
  • stochastic process simulation
  • time series analysis


This is a collection of the advanced optimization algorithms.

  • linear programming (LP)
  • quadratic programming (QP)
  • second order conic programming (SOCP)
  • semi-definite programming (SDP)
  • sequential quadratic programming (SQP)
  • mixed linear integer programming (MIP)
  • genetic algorithms (GA)
  • differential evolution optimization (DE)


This is a collection of numerical algorithms to solve ordinary and partial differential equation problems.

  • Ordinary Differential Equation (ODE) solvers for initial value problem (IVP):
    • Euler’s method
    • Runge Kutta
      • 1st order Runge Kutta
      • 2nd order Runge Kutta
      • 3rd order Runge Kutta
      • 4th order Runge Kutta
      • 5th order Runge Kutta
      • 6th order Runge Kutta
      • 7th order Runge Kutta
      • 8th order Runge Kutta
      • 10th order Runge Kutta
    • Runge-Kutta-Fehlberg (RKF45) (adaptive step-size control)
    • Adams-Bashforth-Moulton predictor-corrector multi-step method
      • 1st order
      • 2nd order
      • 3rd order
      • 4th order
      • 5th order
    • solvers based on Richardson extrapolation
      • Burlisch-Stoer extrapolation
      • semi-implicit extrapolation (suitable for stiff systems)
    • first order system of ODEs
    • conversion from high order ODE to first order ODE system
  • Partial Differential Equation (PDE) solvers
    • finite difference methods:
      • elliptic problem:
        • iterative central difference method (for Poisson’s equation)
      • 1D hyperbolic problem:
        • explicit central difference method (for 1D wave equation)
      • 2D hyperbolic problem:
        • explicit central difference method (for 2D wave equation)
      • 1D parabolic problem:
        • Crank-Nicolson method (for 1D heat or diffusion equation)
      • 2D parabolic problem:
        • alternating direction implicit (ADI) method (for 2D heat or diffusion equation)


NM Dev is a large collection of numerical algorithms covering many topics in mathematics. The following is a summary of our packages. For a complete listing, please refer to the Javadoc.

  • BASIC: basic numerical analysis algorithms
  • OPTIM: advanced optimization algorithms
  • STATS: advanced statistics algorithms
  • O/PDE: ordinary and partial differential equations
  • DSP: digital signal processing
  • EVT: extreme value theory
  • many more……

All our code is well tested. We have thousands of test cases that run daily.

Disclaimer: For each subject/topic heading above, there may or may not be features you expected. You will have to download the trial version to experiment with the library to decide whether it is suitable for your purpose, or whether the features you are looking for are included in the library or a particular version of the library. For example, we have listed “OLS regression” in the feature list, but not all possible mathematics, subjects, or topics related to OLS regression are found in the NM Dev library.



This is a collection of algorithms for Extreme Value Theory.

  • univariate distributions:
    • Gumbel
    • Frechet
    • Weibull
    • Generalized EV (GEVD)
    • Generalized Pareto (GPD)
    • maxima
    • minima
    • order statistics
  • bivariate distributions:
    • asymmetric logic
    • asymmetric mixed
    • asymmetric negative logistic
    • bilogistic
    • Coles-Tawn
    • Husler-Reiss
    • logistic
    • negative bilogistic
    • negative logistic
  • return level and return period
  • extremal index estimation
    • Ferro-Seegers algorithm
  • cluster analysis
  • extreme value markov chain
  • extreme value time series, i.e., MARMA(p,q) model
  • fitting GEVD by maximum likelihood estimation
  • fitting GPD by Peaks-over-Threshold (POT) method
  • fitting Gumbel type by Average Conditional Exceedance Rate (ACER) method