Back to Smile

Linear Algebra

studio/src/universal/notebooks/matrix.ipynb

6.1.05.0 KB
Original Source

Linear Algebra

Smile provides an MATLAB like environment. In the simplest case, you can use it as a calculator. Besides java.lang.Math functions, Smile provides many other important mathematical functions such as logistic, factorial, choose, etc. in smile.math.MathEx class.

scala
import scala.language.implicitConversions
import smile.math.*
import smile.tensor.*

Special Functions

Smile implements beta, erf, gamma and their related functions. Special functions are particular mathematical functions which have more or less established names and notations due to their importance in mathematical analysis, functional analysis, physics, or other applications. Many special functions appear as solutions of differential equations or integrals of elementary functions. For example, the error function erf (also called the Gauss error function) is a special function of sigmoid shape which occurs in probability, statistics, materials science, and partial differential equations. The complementary error function, denoted erfc, is defined as erfc(x) = 1 - erf(x). The error function and complementary error function are special cases of the incomplete gamma function.

scala
erf(1.0)
digamma(1.0)

Vector

Common arithmetic operations on vectors and scalars are similar as in R and Matlab.

scala
val x = c(1.0, 2.0, 3.0)
val y = c(4.0, 3.0, 2.0)

x + y

1.5 * x - 3.0 * y

Note that these operations are lazy. The computation is only performed when the results are needed, e.g. when the expression is used where a vector is expected.

For a vector, there are multiple functions to calculate its norm such as norm (L2 norm), norm1 (L1 norm), norm2 (L2 norm), normInf (infinity norm), normFro (Frobenius norm). We can also standardize a vector to mean 0 and variance 1, unitize it so that L2 norm be 1, or unitize1 it so that L1 norm be 1.

scala
norm(x)

unitize(y)

For a pair of vectors, we can calculate the dot product, distance, divergence, covariance, and correlations with dot, distance, kld (Kullback-Leibler Divergence), jsd (Jensen-Shannon Divergence), cov, cor (Pearson Correlation), spearman (Spearman Rank Correlation Coefficient), kendall (Kendall Tau Rank Correlation Coefficient).

scala
dot(x, y)

cov(x, y)

Matrix

Like Matlab, we can use eye, zeros and ones to create identity, zero, or all-ones matrix, respectively. To create a matrix from 2-dimensional array, we can use the constructor matrix or the ~ operator. The ~ operator can be applied to 1-dimensional array too, which creates a single column matrix.

scala
val a = DenseMatrix.of(Array(
        Array(0.7220180, 0.07121225, 0.6881997),
        Array(-0.2648886, -0.89044952, 0.3700456),
        Array(-0.6391588, 0.44947578, 0.6240573))
    )

val b = DenseMatrix.of(Array(
        Array(0.6881997, -0.07121225, 0.7220180),
        Array(0.3700456, 0.89044952, -0.2648886),
        Array(0.6240573, -0.44947578, -0.6391588))
    )

val c = DenseMatrix.of(Array(
        Array(0.9527204, -0.2973347, 0.06257778),
        Array(-0.2808735, -0.9403636, -0.19190231),
        Array(0.1159052, 0.1652528, -0.97941688))
    )

Matrix-vector operations are just like in math formula.

scala
a * x + y * 1.5

Similarly for matrix-matrix operations:

scala
a + b

Note that a * b are element-wise:

scala
a * b

For matrix multiplication, the operator is %*%, same as in R

scala
a %*% b %*% c

The method DenseMatrix.transpose returns the transpose of matrix, which executes immediately. However, the method t is preferred on MatrixExpression as it is lazy.

scala
a %*% b.t %*% c

Smile has runtime optimization for matrix multiplication chain, which can greatly improve the performance. In the below we generate several random matrices and multiply them together.

scala
val a = randn( 300,  900)
val b = randn( 900,  150)
val c = randn( 150, 1800)
val d = randn(1800,   30)

(a %*% b %*% c %*% d).toMatrix

where randn() creates a matrix of normally distributed random numbers. Smile tries to load machine optimized BLAS/LAPACK native libraries for most matrix computation. If BLAS/LAPACK is not available, Smile will fall back to pure Java implementation.

In linear algebra, a matrix decomposition or matrix factorization is a factorization of a matrix into a product of matrices. There are many different matrix decompositions. In Smile, we provide LU, QR, Cholesky, eigen, and SVD decomposition by functions lu, qr, cholesky, eigen, and svd, respectively.

With these decompositions, many important linear algebra operations can be performed such as calculating matrix rank, determinant, solving linear systems, computing inverse matrix, etc. In fact, Smile has functions det, rank, inv and operator \ for these common computation.

scala
val a = DenseMatrix.of(Array(
  Array(0.7220180, 0.07121225, 0.6881997),
  Array(-0.2648886, -0.89044952, 0.3700456),
  Array(-0.6391588, 0.44947578, 0.6240573))
)

val x = Array(1.0, 2.0, 3.0)

a \ x

inv(a)