HTBasic Help
×
Menu
Index

MAT

Specifies an array operation.
 
 MAT string-array$ = string-array$ | (string-expression)
 MAT numeric-array = numeric-array [operator numeric-array]
 MAT numeric-array = (numeric-expression) [operator numeric-array]
 MAT numeric-array = numeric-array operator (numeric-expression)
 MAT vector = RSUM(matrix) | CSUM(matrix)
 MAT matrix = INV(matrix) | TRN(matrix) | IDN
 MAT array-name [sub-array] = array-name [sub-array]
 
Where:
operator = + | - | . | / | < | <= | = | <> | >= | > | *
sub-array = ( {range | subscript} [, {range | subscript}...] )
range = * | lower-bound : upper-bound
 
Usage:
MAT A=A*(Pny*6)
MAT A=B+C
MAT A=C>=(1)
MAT A=(4)
MAT A=CSUM(C)
MAT A=RSUM(D)
MAT A=IDN
MAT A=INV(B)
MAT Destination(3,*,*)=Source(*,2,*)
 
Example:           MAT.BAS
 
Description:
MAT initializes and performs operations on string and numeric arrays. MAT operations can copy a string or numeric expression or array into an array, add or subtract an array or numeric expression to an array or numeric expression, multiply or divide an array or numeric expression by an array or numeric expression, compare arrays and numeric expressions or perform an identity (IDN), inverse (INV), sum (CSUM or RSUM) or transpose (TRN) of rows and columns of a matrix. MAT operations can also be used to assign a sub-array to another array or sub-array.
 
The REAL, IMAG, ARG, ABS, CONJG and CMPLX functions operate the same with arrays as with scalar numbers.
 
Size and Shape Requirements
In general, a matrix must meet certain size and shape requirements for each matrix operation. If it does not, in certain operations it makes sense to automatically redimension it. If it can't be redimensioned, an error is given.
 
Sub-array assignments require that the number of ranges specified in the source match the number of ranges specified in the destination. If a complete array is specified, the number of ranges equals the rank of the array. In corresponding ranges of the source and destination, the number of elements must be the same. The following examples will help you visualize these rules:
 
DIM X(1:3),Y(1:10)
DIM D(3,4,5),S(4,2,5)
MAT X=Y(2:4)          ! One range, three elements
MAT D(3,*,*)=S(*,2,*) ! Range 1 has 5 elements,2 has 6
MAT Y(1:6)=S(0,0,*)   ! One range, 6 elements
 
For the list of operators above, the target array must be the same size and shape as the source array because numeric operations are performed one array element at a time and the result is returned to the corresponding element in the target array.
 

Matrix Multiply

The asterisk "*" operator performs a matrix multiplication when it is between two matrices. If it is between an array and a numeric expression each element of the array is multiplied by the value of the expression. The period "." operator is used between two arrays to perform an element by element multiply. Vectors can be used in a matrix multiplication as if they were two-dimensional matrices. If used as the first matrix, a vector is treated as a 1 by N matrix. If used as the second matrix, a vector is treated as an N by 1 matrix.
 

Sum Columns, Rows

The CSUM and RSUM matrix functions sum the columns and rows, respectively, of a matrix and return the result into a target vector array.
 

Identity

The IDN matrix function initializes a square matrix to an identity matrix. An identity matrix has zeros in all elements but the diagonal elements, which have the value one.
 

Invert

The INV matrix function returns the inverse of a square matrix. It also calculates the DET value. If the matrix has no inverse, the DET is set to zero, but no error is returned. If the DET is very small in relation to values of the array, numerical methods for inverting the array fail. Thus, the DET should be checked after using INV.
 

Transpose

The TRN matrix function returns the transpose of the source matrix by exchanging rows for columns and columns for rows.
See Also: