Linear Algebra Functions

Read(1980) Label: linear algebra function,

In esProc, functions for performing matrix operations, such as multiplication of matrices and calculating the determinatnt of a matrix, are directly available, and the to-be-computed matrices and the result matrix are represented by two-dimensional sequences.

2.8.1 Vectors and matrices

esProc uses sequences and two-dimensional sequences to represent vectors and matrices respectively:

 

A

1

[-5, 8, 11]

2

=A1(3)

3

[[-5, 8, 11]]

4

[[1,3,5],[2,4,7]]

5

=A4(2)(1)

6

=transpose(A4)

A1 directly defines a vector using a sequence. The vector is a single-column column vector, which is displayed as it is defined:

We can get the number of a certain row from a column vector according to the ordinal number, as we do with a sequence. A2 gets the number of the third row:

There is also the row vector with only one row. Some linear algebra functions can automatically convert a column vector to row vector, or vice versa, as needed. A3 provides a method to define a row vector directly, which is equivalent to defining a matrix having only one row:

The definition of a matrix is a two-dimensional sequence. Each member of the sequence represents a row in the matrix. We can get an element of a matrix through its row and column numbers. A5 gets the number at the second row and in the first column. Below are results of A4 and A5 respectively:

 

We use transpose() function to transform rows to column, or columns to rows, in a matrix. A6 transposes a 2*3 matrix to a 3*2 one:

 

A

1

[[1,3,5],[2,4]]

2

=A1(2)

3

=A1.(~(2))

4

=A1.len()

5

=A1.max(~.len())

A row of a matrix can be directly obtained according to its row number, such as A2 that gets the second row. But getting a column of a matrix involves a loop operation on the sequence, as A3 get the second column. The first result below is the intitial sequence A1 generates.

   

When a matix has blank elements, it is error-prone to get a column from it. It is recommended that all elements in a matrix be defined even an element is blank. A1’s definition can be modified as [[1,3,5],[2,4,]].

The length of a matrix is its number of rows, which can be directly obtained using A.len() function. The length of each row in a matrix is known as its number of columns, which needs to be obtained using A5’s method that gets the maximum number of columns for the row when not all elements in the matrix are defined. Below are results of A4 and A5:

 

It would be the best to define the same number of columns for every row to avoid calculating the maximum number.

Basic matrix operations

We can perform the four arithmertic operations on vectors or matrices having the same number of rows and columns in alignment using sequences’ alignment operators. For example:

 

A

B

1

[1,1,0,0]

[1,2,3,4]

2

[[1,3,5],[2,4,7]]

[[-5, 8, 11], [3, 9, 21]]

3

=A1++B1

=A1--B1

4

=A2.(~++B2(#))

=A2.(~--B2(#))

A3 and B3 calculate the alignment addition of two vectors and alignment subtraction of them alrespectively and get the following results:

 

A4 and B4 calculate the alignment addition of two matrices and alignment subtraction of them alrespectively and get the following results:

 

The more common operation is to multiply two vectors or matrices. For instance:

 

A

B

1

[1,1,0]

[1,2,3]

2

=mul(A1,B1)

=mul([A1],B1)

A2 uses mul() function to calculate multiplication of two vectors. By default, the function calculates the wedge product and uses double-precision format to achieve high precision. A2 calculates wedge product of two 3-row vectors while automatically transposing the second column vector to a row vector, and gets a matrix whose wedge product is 3*3, as shown below:

To calculate inner product of two vectors, the first vector should be a row vector. B2 gets a result that is a scalar value:

We also use mul() function to calculate multiplication of matrices. For instance:

 

A

B

1

[[1,3,5],[2,4,7]]

[[-5, 8, 11], [3, 9, 21], [4, , 8]

2

[8, 9, 0]

 

3

=mul(A1,B1)

=mul([A1(2)], B1.(~(2)))

4

=mul(A1, A2)

 

To successfully perform the multiplication, the number of columns of the first matrix should be equal to the number of rows of the second matrix. Below is A3’s result of calculating A1’s matrix and B1’s matrix:

In a result of performing mul(A, B) function, every member on the mth row and in the nth column is the inner product of the member on the mth row of matrix A and the member in the nth column of matrix B. B3 gets the value on the 2nd row and in the 2nd column:

The mul() function can also be used to calculate multiplication of a matrix and a vector. Below is A4’s result of performing such an operation:

esProc also offers functions for calculating the determinant of a matrix, the rank of a matrix, and matrix inverse. For instance:

 

A

B

1

[[1, 0, 2], [-1, 5, 0], [0, 3, -9]]

[[3, 2, 4], [-1, 1, 2], [9, 5, 10]]

2

=det(A1)

=det(B1)

3

=rankm(A1)

=rankm(B1)

4

=inverse(A1)

 

det() function calculates the determinant of a matrix. If a matrix is incomplete, the function will return 0. Below are results of A2 and B2 respectively:

 

rankm() function calculates the rank of a matrix. Below are results of A3 and B3 respectively that calculate matrix ranks:

 

A matrix with the same number of rows and columns is a square matrix. esProc has inverse() function to calculate the inverse of an n*n matrix. The function requires that the matrix for which the inverse is calculated must be invertible. An invertible matrix is one whose rank is equal to n or whose determinant is not equal to zero. A4 calculates the inverse of A1’s matrix and gets the following result:

Mathematical operations

esProc offers matrix- and vector-related mathematical functions for calculating variance, standard deviation, and so on. For instance:

 

A

B

1

[4, 1, 10]

[-7, 4, 7]

2

=var(A1)

=var@s(A1)

3

=mse(A1, B1)

=mae(A1, B1)

4

=dis(A1, B1)

 

5

=dis@a(A1, B1)

=dis@m(A1, B1)

var() function calculates the total variance of a vector. It works with @s option to calculate the sample variance frequently used in statistical analysis. Here are results of A2 and B2 respectively:

 

mse() function and mae() function respectively calculate the standard deviation and absolute deviation of two vectors. A3 and B3 perform the calculations and get the following results:

 

dis() function calculates the Euclidean distance between two vectors. It works with @a option to calculate the sum of absolute values of Euclidean distances, and adds @m option to calculate the average distance according to the lengths of vectors. Below are results of A4, B5 and B5:

   

 

pearson() function and spearman() function respectively calculate Pearson correlation coefficient and Spearman’s rank-order correlation coefficient:

 

A

B

1

[4, 1, 10]

[-7, 4, 7]

2

=pearson(A1, B1)

=spearman(A1, B1)

3

=pearson@r(A1, B1)

=pearson@a(A1, B1; 2)

A2 gets the Pearson c orrelation coefficient and A3 gets the Spearman’s correlation:

 

pearson() function uses @r option to first equalize the vector becore calculating the pearson correlation coefficient, and works with @a option to set the degrees of freedome for the calculation. Below are results of A3 and B3:

 

There are related esProc functions to perform data processing on vectors and matrices. For instance:

 

A

B

1

[4, 1, 10]

[[8, 1, 6], [3, 5, 7], [4, 9, 2]]

2

=norm(A1)

=norm@0(A1)

3

=norm@s(A1)

=norm(B1)

norm() function normalizes a vector. It cooperates with @0 option to only make a linear adjustment to obtain norm 0, and uses @s option to adjust the vector to obtain norm 0 with standard deviation 1. Below are results of A2, B2 and C2:

Besides, the norm() function can normalize each row of a matrix using the matrix as the parameter. Here is B3’s normalization result:

linefit(A, Y) function performs linear fittting using the least squares method. Parameter A is a coefficient matrix and parameter Y is a constant matrix. Both matrices have same number of rows. When A is an n*n matrix and Y is a vector, the calculation is equivalent to solving matrix equation AX=Y. For instance:

 

A

B

1

[[1,-2, 3],[2,3,4],[3,5,7]]

[-10,5,7]

2

=linefit(A1,B1)

 

A2’s result is as follows:

polyfit(A, B, n) function fits together two vectors using a polynomial of degree n and returns a sequence of coefficients ordered from low order to high order. For instance:

 

A

B

1

[1,2,3,5]

[2,13,38,155]

2

=polyfit(A1,B1,1)

=polyfit(A1,B1,2)

3

=polyfit(A1,B1,3)

 

A2, B2 and A3 use different degrees to fit together the same two vectors, and respecrively get results below:

   

A3’s fitting result, for instance, is 1.041667x3+0.75x2+1.458333x-1.25.