nzMatrix using the SQL interface

This example demonstrates using nzMatrix by calling NZPLSQL stored procedures. It illustrates a wide variety of nzMatrix capabilities, ranging from simple matrix addition to complex linear algebra operations such as Singular Value Decomposition (SVD). The example is not intended to solve a specific issue, but rather to illustrate syntax and usage. Refer to the Matrix Engine Reference Guide for additional examples and detailed information on each stored procedure.
Note: nzMatrix does not overwrite existing matrices. Therefore, you may need to delete any pre-existing matrices before running this example and again after the example is complete. The DELETE_ALL_MATRICES() procedure deletes all matrices in the current database and should be used only if you are certain that the database does not contain matrices needed by other users. If the database does contain matrices belonging to another user, use the Delete_MATRIX() procedure to delete matrices by name.

nzMatrix Run from Netezza SQL

This example can be run from the nzsql command line or from any other SQL-based application.

--Note: All procedures return a value of true, except where noted.

--Initialize nzMatrix
CALL NZM..INITIALIZE();

--Uncomment the next line to delete all matrices in the current database
--CALL NZM..DELETE_ALL_MATRICES();

--Generate matrices
CALL NZM..UNIFORM('A', 5, 5); --Uniformly distributed random numbers 
CALL NZM..NORMAL('B', 5, 5); --Normally distributed random numbers 
CALL NZM..CREATE_RANDOM_MATRIX('A53', 5, 3); --Same as UNIFORM 
CALL NZM..CREATE_IDENTITY_MATRIX('A_IDENT', 5); --Identity matrix 
CALL NZM..CREATE_ONES_MATRIX('A_ONES', 5, 5); --Matrix of ones

--Set and Get the value of a matrix element 
CALL NZM..SET_VALUE('A', 3, 2, 0.12345);
CALL NZM..GET_VALUE('A', 3, 2); --Returns 0.12345

--Bulk export of matrix data to a Netezza table 
DROP TABLE MYTABLE; --In case MYTABLE already exists 
CALL NZM..CREATE_TABLE_FROM_MATRIX('A53', 'MYTABLE');
SELECT * FROM MYTABLE ORDER BY ROW, COL; --Confirm contents of MYTABLE

--Create a matrix from a table -bulk import of data
CALL NZM..CREATE_MATRIX_FROM_TABLE('MYTABLE', 'B53', 5, 3);
--Confirm that B53 has been correctly created 
CALL NZM.._TEST_DENSE_VALID('B53'); --Returns true
CALL NZM..LIST_MATRICES(); --Returns current list of matrices
CALL NZM..GET_NUM_ROWS('B53'); --Returns 5
CALL NZM..GET_NUM_COLS('B53'); --Returns 3

--Element-by-element add, subtract, multiply, divide
CALL NZM..ADD('A', 'B', 'A_PLUS_B');
CALL NZM..SUBTRACT('A', 'B', 'A_MINUS_B');
CALL NZM..MULTIPLY_ELEMENTS('A', 'B', 'A_TIMES_ELEM_B');
CALL NZM..DIVIDE_ELEMENTS('A', 'B', 'A_DIV_ELEM_B');

--Element-by-element comparisons
CALL NZM..EQ('A', 'B', 'A_EQ_B'); --Equal 
CALL NZM..NE('A', 'B', 'A_NE_B'); --Not equal 
CALL NZM..GT('A', 'B', 'A_GT_B'); --Greater than
CALL NZM..GE('A', 'B', 'A_GE_B'); --Greater than or equal 
CALL NZM..LT('A', 'B', 'A_LT_B'); --Less than
CALL NZM..LE('A', 'B', 'A_LE_B'); --Less than or equal
CALL NZM..MAX('A', 'B', 'MAX_A_B'); --Pair-wise maximum
CALL NZM..MIN('A', 'B', 'MIN_A_B'); --Pair-wise minimum-

-Functions of matrix elements
CALL NZM..ABS_ELEMENTS('A','A_ABS'); --Absolute value 
CALL NZM..CEIL_ELEMENTS('A','A_CEIL'); --Ceiling 
CALL NZM..FLOOR_ELEMENTS('A','A_FLOOR');--Floor 
CALL NZM..EXP_ELEMENTS('A','A_EXP'); --Exponential 
CALL NZM..LN_ELEMENTS('A','A_LN'); --Natural logarithm 
CALL NZM..LOG_ELEMENTS('A','A_LOG'); --Base 10 logarithm 
CALL NZM..INT_ELEMENTS('A','A_INT'); --Truncate toward zero

--Round to the specified number of decimal digits 
CALL NZM..ROUND_ELEMENTS('A', 'A_ROUND', 2);

--Show the contents of a matrix
CALL NZM..PRINT('A_ROUND', FALSE); --Returns the contents of A_ROUND

--Modulo (remainder) function
CALL NZM..MOD_ELEMENTS('A', 'A_MOD', 3);

--ReductionsCALL NZM..RED_MAX('A'); --Returns the maximum value in A
CALL NZM..RED_MIN('A'); --Returns the minimum value in A
CALL NZM..RED_MAX_ABS('A'); --Returns the maximum absolute value in A
CALL NZM..RED_MIN_ABS('A'); --Returns the minimum absolute value in A
CALL NZM..RED_SUM('A'); --Returns the sum of the values in A
CALL NZM..RED_SSQ('A'); --Returns the sum of the squares of the values in A
CALL NZM..RED_TRACE('A'); --Returns the trace of A (the sum of the diagonal elements)
CALL NZM..ANY_NONZERO('A'); --Returns one if any of A's values are nonzero. Otherwise, returns zero.
CALL NZM..ALL_NONZERO('A'); --Returns one if all of A's values are nonzero. Otherwise, returns zero.

--Copy a submatrix
CALL NZM..COPY_SUBMATRIX('A', 'A_SUB', 2, 3, 1, 4);
CALL NZM..GET_NUM_ROWS('A_SUB'); --Returns 2
CALL NZM..GET_NUM_COLS('A_SUB'); --Returns 4

--Transpose (interchange rows and columns) 
CALL NZM..TRANSPOSE('A', 'A_TRANS');

--Concatenate vertically (number of columns stays the same) 
CALL NZM..CONCAT('A', 'B', 'A_B_CONCAT_V', 'V');
CALL NZM..GET_NUM_ROWS('A_B_CONCAT_V'); --Returns 10 
CALL NZM..GET_NUM_COLS('A_B_CONCAT_V'); --Returns 5

--Concatenate horizontally (number of rows stays the same) 
CALL NZM..CONCAT('A', 'B', 'A_B_CONCAT_H', 'H');
CALL NZM..GET_NUM_ROWS('A_B_CONCAT_H'); --Returns 5 
CALL NZM..GET_NUM_COLS('A_B_CONCAT_H'); --Returns 10

--Kronecker product
CALL NZM..KRONECKER('A', 'B', 'A_B_KRON');
CALL NZM..GET_NUM_ROWS('A_B_KRON'); --Returns 25
CALL NZM..GET_NUM_COLS('A_B_KRON'); --Returns 25

--Housekeeping
CALL NZM..MATRIX_EXISTS('A');
CALL NZM..DELETE_MATRIX('A');
CALL NZM..MATRIX_EXISTS('A'); --Returns false

--Linear algebra

CALL NZM..CREATE_RANDOM_MATRIX('A', 1000, 1000);

--Matrix inversion
CALL NZM..INVERSE('A', 'A_INV');

--Matrix multiplication
CALL NZM..GEMM('A', 'A_INV', 'A_A_INV');
--Verify that A_INV is the inverse of A, within floating point precision 
CALL NZM..CREATE_IDENTITY_MATRIX('I', 1000);
CALL NZM..SUBTRACT('A_A_INV', 'I', 'A_A_INV_MINUS_I');
CALL NZM..RED_MAX_ABS('A_A_INV_MINUS_I'); --Returns a value close to zero

--Linear Equation Solving
--Solve A X = RHS for X
CALL NZM..NORMAL('RHS', 1000, 10);
CALL NZM..SOLVE('A', 'RHS', 'X');

--Verify that A X = RHS, within floating point precision 
CALL NZM..GEMM('A', 'X', 'A_X');
CALL NZM..SUBTRACT('A_X', 'RHS', 'A_X_MINUS_RHS');
CALL NZM..RED_MAX_ABS('A_X_MINUS_RHS'); --Returns a value close to zero

--Linear Least Squares
--For over/underdetermined systems A X_LLS = RHS,
--find linear least squares solution X_LLS
CALL NZM..SOLVE_LINEAR_LEAST_SQUARES('A', 'RHS', 'X_LLS');

--Singular Value Decomposition 
CALL NZM..SVD('A', 'U', 'S', 'VT');

--Create a diagonal matrix from the one-column matrix S 
CALL NZM..VEC_TO_DIAG('S', 'SIGMA');

--Verify that A = U SIGMA VT, within floating point precision 
CALL NZM..GEMM('U', 'SIGMA', 'U_SIGMA');
CALL NZM..GEMM('U_SIGMA', 'VT', 'U_SIGMA_VT');
CALL NZM..SUBTRACT('A', 'U_SIGMA_VT', 'A_MINUS_U_SIGMA_VT');
CALL NZM..RED_MAX_ABS('A_MINUS_U_SIGMA_VT'); --Returns a value close to zero

--Eigenvalues and eigenvectors of a symmetric matrix
--Create a symmetric matrix
CALL NZM..TRANSPOSE('A', 'A_TRANSP');
CALL NZM..ADD('A', 'A_TRANSP', 'A_SYMM');

--Compute Eigenvalues W and Eigenvectors Z 
CALL NZM..EIGEN('A_SYMM', 'W', 'Z');
Note:

In actual operation, the CALL NZM.._TEST_DENSE_VALID(); command used in the example outputs notice messages that provide some informational text on the tests it is performing. The call ultimately returns TRUE or FALSE, depending on whether the matrix passes all of the validity tests. The notice text was omitted from the example for clarity.