 numEclipse - An Eclipse based workbench for

Numerical Computing

# Linear Algebra

This chapter introduces the linear algebra toolbox. This is the most important toolbox in numEclipse. In fact, it implements the core functionality of the application. All other toolboxes depend on this.  Some of the functions are implemented as part of the core functions and others are implemented as an extension toolbox. This chapter presents all the functions related to numerical linear algebra regardless of where they are implemented. In the first section, 7.1, we present all the basic functions. In the second section, 7.2, we discus all the specialized matrices. Finally, in section 7.3 we look at the advanced linear algebra functions.

## 7.1 Basic Functions

In this section, we look at all the basic functions used in numerical linear algebra.

### Length

Length of a vector or matrix is determined using the following function.

n = length(X)

If X is a row or column vector then, it will return the maximum number of elements.

If X is a matrix then, it will return the maximum number of columns.

### Size

Size of a vector or matrix is determined using the following function.

1. n = size(X)

2. [m, n] = size(X)

3. n = size(X, dim)

The first alternative returns row vector where each element contains the size of corresponding dimension.

> n = size(rand(3, 5))

ans =

3   5

The second form of the function call returns the size of each dimension of the matrix X to a separate variable listed as output argument.

> [m n] = size(rand(3, 5))

m =

3

n =

5

The third form of the function call returns the size of dimension specified in the second argument.

> size(rand(3, 5), 1)

ans =

3

> size(rand(3, 5), 2)

ans =

5

### Number of Dimensions

Number of dimensions of a vector or matrix is determined by the following function.

n = ndims(X)

At this point, numEclipse does not implement multi-dimensional arrays. So the above function call will always return the same value of "2".

### Identity Matrix

The following function returns the identity matrix.

1. I = eye(n)

2. I = eye(m, n)

3. I = eye([m n])

The first function call returns an identity matrix of size n x n. The second and third function call returns an identity matrix of size m x n.

### Ones Matrix

The following function returns a matrix with all 1s.

1. X = ones(n)

2. X = ones(m, n)

3. X = ones([m n])

The first function call returns a matrix of size n x n. The second and third function call returns a 1s matrix of size m x n.

### Zeros Matrix

The following function returns a matrix with all 0s.

1. X = zeros(n)

2. X = zeros(m, n)

3. X = zeros([m n])

The first function call returns a matrix of size n x n. The second and third function call returns a 0s matrix of size m x n.

### Sum

The following function returns the sum of vector or Matrix elements.

1. X = sum(A)

2. X = sum(A, dim)

If A is a row or column vector then the first function call returns the sum of all elements.

If A is a matrix then the first function call returns a row vector such that each row element is a sum of all elements in corresponding column in A.

The second function call returns a row vector as previous function call for dim = 1. It returns a column vector containing sum of rows in matrix A, when dim = 2.

### Product

The following function returns the product of vector or Matrix elements.

1. X = prod(A)

2. X = prod(A, dim)

If A is a row or column vector then the first function call returns the product of all elements.

If A is a matrix then the first function call returns a row vector such that each row element is a product of all elements in corresponding column in A.

The second function call returns a row vector as previous function call for dim = 1. It returns a column vector containing product of rows in matrix A, when dim = 2.

### Diagonal

The following function returns the diagonal matrix.

D = diag(X)

If X is a matrix then the function will return a column vector contain the diagonal elements of matrix X. In case of a vector, it will return a matrix with diagonal elements from X and all other elements will be zero.

### Trace

The following function returns the sum of all the diagonal elements of argument matrix X.

s = trace(X)

Note: This method is adopted from GNU Octave.

### Repeat

The following function repeats and tile a matrix.

1. Y = repmat(X, m, n)

2. Y = repmat(X, [m n])

The above function calls create a bigger matrix Y containing m x n copies of input argument matrix X.

### Reshape

The following function changes the rows and columns size of a matrix.

1. Y = reshape(X, m, n)

2. Y = reshape(X, [m n])

The above function calls reshape the size of input argument matrix X to m x n.

### Real part

The following function returns the real part of the input argument matrix.

R = real(X)

### Imaginary part

The following function returns the imaginary part of the input argument matrix.

I = imag(X)

### Conjugate

The following function determines the complex conjugate of a matrix.

C = conj(X)

### All non-zero

The following function determines if the input argument matrix has all non-zero elements.

1. Y = all(X)

2. Y = all(X, dim)

If X is a row or column vector then the first function call will return a 1 if all elements are greater than zero otherwise it will return 0.

In case of a matrix argument it will return a row vector such that each element of the vector will be 1 if the corresponding column in input matrix is greater than zero otherwise the output vector element be 0.

The second function call works similar to the above function when the second argument dim = 1. In case of dim = 2, it will return a column vector such that each element of the vector will be 1 if corresponding row in the input argument matrix X is greater than zero otherwise the output vector element be 0.

### Any non-zero

The following function determines if the input argument matrix has any non-zero elements.

1. Y = any(X)

2. Y = any(X, dim)

If X is a row or column vector then the first function call will return a 1 if any element is greater than zero otherwise it will return 0.

In case of a matrix argument it will return a row vector such that each element of the vector will be 1 if the corresponding column in input matrix has at least one element greater than zero otherwise the output vector element be 0.

The second function call works similar to the above function when the second argument dim = 1. In case of dim = 2, it will return a column vector such that each element of the vector will be 1 if corresponding row in the input argument matrix X has at least one element greater than zero otherwise the output vector element be 0.

### Test Real Matrix

The following function determines if the input argument matrix is a real matrix.

b = isreal(X)

The function call returns 1 when X is a real matrix otherwise it returns 0.

### Number of Elements

The following function call returns the total number of elements in a matrix.

n = numel(X)

### Sort

The following function sort vector and matrix.

1. Y = sort(X)

2. Y = sort(X, dim)

If X is a vector then the first function call will sort the vector in ascending order.

If X is a matrix then the function call will sort each column of the input argument matrix.

The second function call works similar to last function call when dim = 1. It will sort along the rows when d = 2.

### Flip left-right

The following function flips columns of the given matrix in the left-right direction.

Y = fliplr(X)

Note: Implementation of the function is adopted from GNU Octave.

### Flip up-down

The following function flips rows of the given matrix in the up-down direction.

Y = flipud(X)

Note: Implementation of the function is adopted from GNU Octave.

### Lower Triangular Matrix

The following function return the lower triangular matrix for a given matrix.

1. L = tril(X)

2. L = tril(X, d)

The first function call returns the lower triangular matrix of X, i.e., all the elements for which i < j are zeroed out in the given matrix X.

The second function call is similar to the first except that the diagonal is determined using the second argument. The d = 0 corresponds to the main diagonal, d = 1 is first diagonal above the main diagonal and d = -1 is first diagonal below the main diagonal.

### Upper Triangular Matrix

The following function return the upper triangular matrix for a given matrix.

1. U = triu(X)

2. U = triu(X, d)

The first function call returns the upper triangular matrix of X, i.e., all the elements for which i > j are zeroed out in the given matrix X.

The second function call is similar to the first except that the diagonal is determined using the second argument. The d = 0 corresponds to the main diagonal, d = 1 is first diagonal above the main diagonal and d = -1 is first diagonal below the main diagonal.

Above functions are considered basic functions by the author since they are utility functions used to build more complex linear algebra functions. In the next section, we will discuss some specialized matrices.

## 7.2 Special Matrices

### Hessenberg Matrix

The following function call creates a hessenberg matrix form for the input argument matrix

1. H = hess(X)

2. [H P] = hess(X)

The first function call finds the hessenberg matrix form H for the input matrix X.

The second function call returns the hessenberg matrix form and unitary matrix P such that

X = P * H * P'. Note that the hessenberg form for a matrix is not unique.

Note: Implementation of the function is adopted from jampack library.

### Hankel Matrix

The following function creates a hankel matrix.

1. H = hankel(c, r)

2. H = hankel(c)

The above function call returns a hankel matrix.

Note: Implementation of the function is adopted from GNU Octave.

### Hilbert Matrix

The following function creates a hilbert matrix.

H = hilb(n)

The above function call returns a hilbert matrix of order n × n.

Note: Implementation of the function is adopted from GNU Octave.

### Magic Matrix

The following function creates a magic matrix of order n × n.

M = magic(n)

Note: Implementation of the function is adopted from GNU Octave.

### Pascal Matrix

The following function creates a pascal matrix.

H = pascal(n)

The above function call returns a pascal matrix of order n × n.

Note: Implementation of the function is adopted from GNU Octave.

### Toeplitz Matrix

The following function creates a toeplitz matrix.

1. T = toeplitz(c, r)

2. T = toeplitz(r)

The above function call returns a toeplitz matrix.

Note: Implementation of the function is adopted from GNU Octave.

### Vandermonde Matrix

The following function creates a pascal matrix.

V = vander(x)

The above function call returns a vandermonde matrix.

Note: Implementation of the function is adopted from GNU Octave.

## 7.3 Advance Functions

In this section we look at the numerical linear algebra functions.

### Cholesky Factorization

The following function determines the cholesky factor matrix of the input argument matrix.

C = chol(X)

The input argument matrix must be positive definite matrix, otherwise an error message is returned. The returned matrix is upper triangular matrix such that C' * C = X.

Note: Implementation of this function is adopted from jampack library.

### Condition Number

The following function determines the 2-norm condition number of the input argument matrix.

C = cond(X)

Note: Implementation of the function is adopted from GNU Octave.

### Determinant

The following function determines the determinant of given matrix.

d = det(X)

Note: Implementation of the function is adopted from GNU Octave.

### Differences

The following function determines the difference matrix for a given input argument matrix.

1. D = diff(X)

2. D = diff(X, n)

3. D = diff(X, n, dim)

The first function call calculates the first order differences between adjacent matrix elements.

The second function call calculates the n-th order differences between adjacent matrix elements.

The third function call calculates the n-th order differences between adjacent matrix elements along matrix dimension dim.

Note: Implementation of the function is adopted from GNU Octave.

### Eigen Values

The following function determines the eigen values and eigen vectors of a given matrix.

1. D = eig(X)

2. [U D] = eig(X)

The first function call returns a column vector containing the eigen values of matrix X.

The second function call returns a matrix U containing columns of eigen vectors and matrix D a diagonal matrix with eigen values.

Note: Implementation of the function is adopted from jampack library.

### Functions

The following functions determine the exp, log and sqrt of the given matrix respectively.

1. Y = expm(X)

2. Y = logm(X)

3. Y = sqrtm(X)

Note: Implementation of the function is adopted from GNU Octave.

### Inverse

The following function determines the inverse of a matrix.

Y = inv(X)

Note: Implementation of the function is adopted from jampack library.

### LU Decomposition

The following function determines the LU-decomposition of a given matrix.

1. L = lu(X)

2. [L U] = lu(X)

3. [L U P] = lu(X)

The above function calls decompose the given matrix X into lower, upper and permutation matrix such that P' * L * U = X.

Note: Implementation of the function is adopted from jampack library.

### Norm

The following function determines the norm of a vector or matrix.

1. n = norm(X)

2. n = norm(X, d)

The first function call returns the 2-norm whereas the second function call returns d-norm. Here d could be 1, 2, or inf.

### QR Decomposition

The following function determines the QR-decomposition of a given matrix.

1. Q = qr(X)

2. [Q R] = qr(X)

The above function calls decompose the given matrix X into an orthonormal and upper triangular matrix such that Q * R = X.

Note: Implementation of the function is adopted from jampack library.

### Rank

The following function determines the rank of a matrix.

1. r = rank(X)

2. r = rank(X, tol)

In the second function call argument tol is used to specify the tolerance to number of singular values.

Note: Implementation of the function is adopted from GNU Octave.

### Reduced Echelon Form

The following function determines the reduced row echelon form of a given matrix

1. R = rref(X)

2. R = rref(X, tol)

In the second function call argument tol is used to specify the tolerance.

Note: Implementation of the function is adopted from GNU Octave.

### Schur Decomposition

The following function determines the schur form of a given matrix.

1. T = schur(X)

2. [U T] = schur(X)

The above function calls decompose the given matrix X such that U * T * U' = X.

Note: Implementation of the function is adopted from jampack library.

### SVD Decomposition

The following function determines the singular value decomposition of a given matrix.

1. S = svd(X)

2. [U S V] = svd(X)

The above function calls decompose the given matrix X such that U * S * V' = X.

Note: Implementation of the function is adopted from jampack library.