MAT statements ☆☆

Full BASIC provides MAT statements, which perform matrix operations.

Let A, B and C be arrays, m, n and p be numeric expressions below.

MAT A = B
substitutes B for A, where A and B have the same dimension.

MAT A = ZER
substitutes 0 for all elements of A.

MAT A = ZER(m) when A is 1-dimensional
MAT A = ZER(m, n) when A is 2-dimensional
MAT A = ZER(m, n, p) when A is 3-dimensional
changes the upper bounds of A and substitutes 0 for all elements.
Note that the dimension of an array do not change.

MAT A = CON
substitutes 1 for all elements of A.

MAT A = CON(m)
MAT A = CON(m, n)
MAT A = CON(m, n, p)
changes the upper bounds of A and substitutes 1 for all elements.

MAT A=B+C
MAT A=B-C
MAT A=B*C
Substitutes the sum, difference or product of B and C for A.
In case of the product, dimensions of A, B and C must be one of 1-2-1, 1-1-2, 2-2-2.

MAT A=(numeric_expression)*B
Scalar multiplication.
If numeric_expression is a constant, a function reference or a variable, parentheses can be omitted.
Example. Let x be a numeric variable.
MAT A=x*A
MAT A=(2*x)*B
MAT A=ABS(x+3)*B


MAT A=(numeric_expression)*CON
The value of numeric_expression is assigned to all elements of A.

Let A and B be 2-dimensional arrays.

MAT A = IDN
substitutes the identity matrix for A.
If A is not square, an exception of 6004 shall be raised.

MAT A = IDN(n)
substitutes n×n identity matrix for A.

MAT A=INV(B)
substitutes the inverse matrix of B for A.

MAT A=TRN(B)
substitutes the transposed matrix of B for A.

Notes.

Although a MAT statement may change the size of an array, the lower bounds of an array does not change, and only the upper bounds are adjusted for the new size.
For example, when an array such as A(1)=1,A(2)=2,A(3)=3 is substituted for an array B with the lower bound 0, B becomes as B(0)=1,B(1)=2,B(2)=3.
That is, MAT B=A means
LET B(0)=A(1)
LET B(1)=A(2)
LET B(2)=A(3)

Note.
MAT A=ZER(m TO n) does not make the lower bound of A into m.
This statement keeps the lower bound of A and changes only the size of A to n-m+1.
If you want to change the lower bound of an array, use a MAT READ statement or an original enhancement MAT REDIM.

Note.
If a MAT statement yields that the number of elements of an array exceeds the number of elements declared at first, an exception of extye 5001 shall be raised.

Cross Product(Original Enhancement)
Suppose A, B, C be 1-dimensional numeric arrays.
MAT A=CROSS(B,C) Assignes the cross product (vector product) of B and C to A.
If the number of elemtnts of B, C disagrees with 3, an exception of extype 6001 shall be raised.


Array formula (original ehnhancement)
Suppose A be a one or two dimensinal numeric array. The next style of MAT statements are available.
MAT A=an matrix formula
MAT TRN(A)=an matrix formula
When TRN(A) is written in the lefthand side of the equal sign, A shall be substituted with transposed matrix of the calculation result.
The INV, TRN, and CNJ functions can be used in matrix formulas.
Applying the TRN function to a one-dimensional array results in a two-dimensional array (column vector).
When used in complex number mode, the CNJ function converts all elements of the array to conjugate complex numbers.
Matrix powers are limited to integer powers. Fractional exponents are rounded to integers.
A scalar product can be specified at the beginning of a matrix product. A scalar product can only be specified at the beginning of a product.
Unlike regular mathematical expressions, matrix addition and subtraction cannot begin with a positive or negative sign.
You can change the order of calculations by enclosing the formula in parentheses ( ).
A1-dimensional arrays and a 2-dimensional array that has only one row can be subsituted for another mutulally, in that case dimensions are invariable.

Products of a 2-dimensional array and a 1-dimensional array can be written in the form MAT A=B*C, which Full BASIC standard indicate.
You can also specify formulas of this form in the DET function.

Instant Vector (proprietary Extension)
In the MAT statement, you can write numeric variables on the left side of the equal sign, separated by commas and enclosed in [ ], to function as a one-dimensional array.
In a matrix formula, you can write numeric expressions separated by commas and enclosed in [ ] to function as a one-dimensional array.
Example.
DIM A(3,3)
MAT [x,y,z]=[1,2,3]*A
MAT TRN([x,y,z])=A*TRN([1,2,3])

Rows and Columns

Row or Column Extraction (Proprietary Extension)
Let A be a two-dimensional numeric array (calculations not allowed), and m and n be numeric expressions.
The following functions can be used in matrix calculations.

ROW(A,n) one-dimensional array obtained by extracting the n-th row of A.
COLUMN(A,n) one-dimensional array obtained by extracting the n-th column of A.
ROW(A,m:n) two-dimensional array obtained by extracting rows m through n of A.
COLUMN(A,m:n) two-dimensional array obtained by extracting columns m through n of A.

Note:
The one-dimensional array obtained by COLUMN(A,n) is semantically a column vector, but is syntactically indistinguishable from a normal one-dimensional vector.

Partial Assignment to a Row or Column (Proprietary Extension)
Let A be a two-dimensional numeric array (calculations not allowed), and m and n are numeric expressions.

MAT ROW(A,n) = Matrix formula (1-dimensional)
 Replaces the nth row of A with the value on the right-hand side.

MAT COLUMN(A,n) = Matrix formula (1-dimensional)
 Replaces the nth column of A with the value on the right-hand side.

MAT ROW(A,m:n) = Matrix formula (2-dimensional)
 Replaces the m-th through n-th rows of A with the value on the right-hand side.

MAT COLUMN(A,m:n) = Matrix formula (2-dimensional)
 Replaces the m-th through n-th rows of A with the value on the right-hand side.

When assigning to a row, the number of columns must match.
When assigning to a column, the number of rows must match.
Note:
When n-m+1 in MAT ROW(A,m:n) differs from the number of rows calculated on the right-hand side, the number of rows in A will increase or decrease.
When MAT ROW(A,n+1:n) = Matrix Calculation Formula (2-Dimensional), the matrix calculated on the right-hand side is inserted immediately after the n-th row.
To execute a MAT statement that increases the number of rows in A, declare A to be large, then reduce it using a MAT ZER statement or similar.
Example

DIM A(4,2),B(2,2)
MAT A=ZER(2,2)
MAT ROW(A,3:2)=B


Row-delete and culmun-delete
In MAT ROW(A,m:n) =B, B can have zero rows, so it can be used to delete rows. In this case, the number of columns in B must match A.
Example.

DIM A(3,3),B(3,3)
MAT B=ZER(0,3)
MAT ROW(A,2:2)=B

When MAT ROW(A,2:2)=B is executed, the second row of A is deleted.
The following subprogram delete i-th row.

EXTERNAL SUB RowDelete(a(,),i)
DIM b(0,ubound(a,2))
MAT ROW(a,i:i)=b
END SUB

The following subprogram delete j-th column.

EXTERNAL SUB ColumnDelete(a(,),j)
DIM b(ubound(a,1),0)
MAT Column(a,j:j)=b
END SUB 


Row Swapping
Rows can be swapped (exchanged) using the following subprogram:

DIM A(3,4)
CALL SwapRow(A,2,3)
END
EXTERNAL SUB SwapRow(A(,),i,j) ! Swaps row i and row j of A.
DIM u(lbound(A,2) to ubound(A,2))
DIM v(lbound(A,2) to ubound(A,2))
MAT u=row(A,i)
MAT v=row(A,j)
MAT row(A,i)=v
MAT row(A,j)=u
END SUB


Notes on the MAT statement in general.
The execution of the MAT statement does not change the array dimensions, but it does change the upper bounds of the subscripts.
Example

DIM A(1,3), B(3,3)
MAT A=COLUMN(B,2:2)

Executing this statement results in UBOUND(A,1)=3 and UBOUND(A,2)=1.

Note.
Transformation MAT Statement
Matrix multiplications involving TRANSFORM, SCALE(), SHIFT(), ROTATE(), and SHEAR() are Transform Assignment.