# TOMVIEW  
# REGISTER (TOMVIEW)
# LOGIN  
# myTOMVIEW
TOMLAB LOGO

« Previous « Start » Next »

10  Sparse Matrix Handling

These sections outline the functionality available for sparse matrix handling included with TOMVIEW.

10.1  Create

The following functions are available to create sparse matrices.

10.1.1  init

Purpose
Initiate a sparse matrix from three arrays and two integers. One array with matrix element values, one array with row index and the third array with column index. The integers describe the number of rows and number of columns in the matrix.

Description of Inputs
value Array value. The array contains matrix element values.
 
row Array row index. The array contains row index corresponding to the array of values.
 
col Array column index. The array contains column index corresponding to the array of values.
 
m An integer equal to the number of rows in A.
 
n An integer equal to the number of columns in A.
 

Description of Outputs
A A sparse matrix described by the inputs.

Description
The routine sparse_init.vi initiates a sparse matrix from the inputs. The length of all the three input arrays must be equal. The indexing starts with 0 and must be less then the size of the matrix (m resp. n).

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If the minimum value in row or col is less then zero the errorcode 6522 is returned. If the three input vectors differ in length or m or n is less then zero the errorcode 6521 is returned.

10.1.2  empty

Purpose
Initiate an empty sparse matrix.

Description of Inputs
m An integer equal to the number of rows in A.
 
n An integer equal to the number of columns in A.

Description of Outputs
A A sparse matrix with all elements equal to zero.

Description
The routine sparse_empty.vi initiates a sparse matrix from the inputs.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If m or n is less then zero the errorcode 6521 is returned.

10.1.3  fill

Purpose
Initiate a matrix in sparse format with all element equal.

Description of Inputs
m An integer equal to the number of rows in the output matrix A.
 
n An integer equal to the number of columns in the output matrix A.
 
element A real number. element is the value all the elements matrix A will have.

Description of Outputs
A A sparse matrix described by the inputs.

Description
The routine sparse_fill.vi initiates a sparse matrix from the inputs with all elements equal to the input variable element.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If m or n is less or equal to zero the errorcode 6521 is returned.

10.1.4  eye

Purpose
Initiate a sparse identity matrix from two integers. The integers describe the number of rows and number of columns in the identity matrix.

Description of Inputs
m An integer equal to the number of rows in matrix I.
 
n An integer equal to the number of columns in matrix I.

Description of Outputs
I A sparse identity matrix with size described by the inputs.

Description
The routine sparse_eye.vi initiates a sparse identity matrix from the inputs. If only one input is given the function will return a quadratic identity matrix of size as the nonzero input.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If m or n is less or equal to zero the errorcode 6521 is returned.

10.1.5  toTriplet

Purpose
Returns matrix triplet components from a sparse matrix.

Description of Inputs
A A sparse matrix.

Description of Outputs
value Array value. The array contains matrix element values.
 
row Array row index. The array contains row index corresponding to the array of values.
 
col Array column index. The array contains column index corresponding to the array of values.
 
m An integer equal to the number of rows in A.
 
n An integer equal to the number of columns in A.

Description
The routine sparse_toTriplet.vi returns triplet components from a sparse matrix.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. Errorcode 6521 is returned if the matrix is corrupt.

10.2  Modify

The following functions are available to merge and modify existing sparse matrices.

10.2.1  transpose

Purpose
The function returns the transpose of the input matrix.

B = AT
where B ∈ Rn × m, A ∈ Rm × n.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.

Description of Outputs
B Sparse matrix B. The transpose of the input matrix.

Description
The routine sparse_transpose.vi uses optimal data handling and structures to return the transpose of a matrix.

Examples
usersguide_sparse.

10.2.2  concat

Purpose
Concats two sparse matrices either rowwise or columnwise.

C = [
A B
]    (dim = 0)
C = ⎡
⎣
A
B
⎤
⎦
   (dim = 1)


where C ∈ RmA × (nA+nB) or C ∈ R(mA+mB) × nA, A ∈ RmA × nA, B ∈ RmB × nB.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
B Sparse matrix B. Defined by sparse_init or by converting a dense matrix to a sparse.
 
dim Integer dim. Eighter 0 (default) or 1. 0 for rowwise concatation or 1 for columnwise concatation.

Description of Outputs
C Sparse matrix C.

Description
The routine sparse_concat.vi uses optimal data handling and structures to concate two sparse matrices eighter rowwise or columnwise.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If the two matrices has different number of rows/columns (depending on what dimensions that will be concate) the errorcode 6521 is returned.

10.2.3  concatZeros

Purpose
Add more rows or columns filled with zeros to a sparse matrix.

B = [
A 0
]    (dim = 0 order = 0)
B = [
0 A
]    (dim = 0 order = 1)
B = ⎡
⎣
A
0
⎤
⎦
   (dim = 1 order = 0)
B = ⎡
⎣
0
A
⎤
⎦
   (dim = 1 order = 1)


where B ∈ Rm × (n+n0) or B ∈ R(m+m0) × n, A ∈ Rm × n.

Description of Inputs
A Sparse matrix. Defined by sparse_init or by converting a dense matrix to a sparse.
 
dim An integer. Eighter 0 (default) or 1. 0 for rowwise concatation or 1 for columnwise concatation.
 
order An integer. Eighter 0 (default) or 1. 0 for A to be the first matrix and 1 for the zero matrix to be the first matrix to concat.
 
m0/n0 An integer equal to the number of rows/columns to concat.

Description of Outputs
B Sparse matrix B.

Description
The routine sparse_concateZeros.vi uses optimal data handling and structures to add more rows/columns filled with zeros to a matrix.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If the input matrix has one of its dimension less than zero the errorcode 6521 is returned.

10.2.4  subMatrix

Purpose
Returns a sub matrix of the input matrix.

B=A(:,index)
⎛
⎝
dim=0
⎞
⎠
B=A(index,:)
⎛
⎝
dim=1
⎞
⎠


where B ∈ Rm× length(index) or B ∈ Rlength(index) × n, A ∈ Rm × n.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
index Integer vector with index that will be in the submatrix.
 
dim Integer dim. Eighter 0 (default) or 1. 0 to get columns or 1 for get rows.

Description of Outputs
B Sparse matrix B. A sub matrix to the input matrix A.

Description
The routine sparse_subMatrix.vi uses optimal data handling and structures to return a sub matrix of the input matrix.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If the index in the index vector is out of matrix dimension the errorcode 6522 is returned.

10.2.5  setVal

Purpose
Set a matrix element to a given value.

A(i,j) = val
where A ∈ Rm × n, i integer, j integer, val is a scalar.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
i Integer equal to the row index.
 
j Integer equal to the column index.
 
val The new element in A.

Description of Outputs
A new The modified sparse matrix.

Description
The routine sparse_setVal.vi sets a sparse matrix element value in the i:th row and j:th column to a given value.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If i or j is out of matrix dimension the errorcode 6522 is returned.

10.2.6  setCol

Purpose
Set a matrix column to given values.

A(row,j) = val
where A ∈ Rm × n, row integer vector, j integer, val is a value vector.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
row Integer vector equal to the row indices.
 
j Integer equal to the column index.
 
val A vector with the new elements in A.

Description of Outputs
A new The modified sparse matrix.

Description
The routine sparse_setCol.vi sets row element values in a sparse matrix to given values.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If j or the indices in row is out of matrix dimension the errorcode 6522 is returned. If the two input vectors have different size the errorcode -23036 is returned.

10.2.7  setRow

Purpose
Set a matrix row to given values.

A(i,col) = val
where A ∈ Rm × n, i integer, col integer vector, val is a value vector.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
i Integer equal to the row index.
 
col Integer vector equal to the column indices.
 
val A vector with the new elements in A.

Description of Outputs
A new The modified sparse matrix.

Description
The routine sparse_setRow.vi sets column element values in a sparse matrix to given values.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If i or the indices in col is out of matrix dimension the errorcode 6522 is returned. If the two input vectors have different size the errorcode -23036 is returned.



10.3  Functions

The following functions are available to obtain information about the sparse matrices.

10.3.1  size

Purpose
Get the size of a sparse 2-D matrix.

m =   number of rows in A
n =   number of columns in A
where A ∈ Rm × n.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.

Description of Outputs
m An integer equal to the number of rows in A.
 
n An integer equal to the number of columns in A.

Description
The routine sparse_size.vi gives the size of a sparse matrix.

Examples
usersguide_sparse.

10.3.2  nnz

Purpose
Get the number of non zeros in a sparse 2-D matrix.

nnz=  number of nonzeros in A
where A ∈ Rm × n.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.

Description of Outputs
nnz An integer equal to the number of non zeros in A.

Description
The routine sparse_nnz.vi gives the number of non zeros in a sparse matrix.

Examples
usersguide_sparse.

10.3.3  getVal

Purpose
Get a matrix element value from a sparse 2-D matrix.

val = A(i,j)
where val is a scalar, A ∈ Rm × n, i integer, j integer.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
i Integer equal to the row index.
 
j Integer equal to the column index.

Description of Outputs
val The element in A in the i:th row and j:th column.

Description
The routine sparse_getVal.vi gives the matrix element value in the i:th row and j:th column of the sparse matrix A.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If i or j is out of matrix dimension the errorcode 6522 is returned.

10.3.4  getCol

Purpose
Get a matrix column from a sparse 2-D matrix.

col = A(:,j)
where col ∈ Rm × 1 , A ∈ Rm × n, j integer.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
j Integer equal to the column index.

Description of Outputs
col The j:th column in A.

Description
The routine sparse_getCol.vi gives an array equal to the j:th column of the sparse matrix A.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If j is out of matrix dimension the errorcode 6522 is returned.

10.3.5  getRow

Purpose
Get a matrix row from a sparse 2-D matrix.

row = A(i,:)
where row ∈ R1 × n , A ∈ Rm × n, i integer.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
i Integer equal to the row index.

Description of Outputs
row The i:th row in A.

Description
The routine sparse_getRow.vi gives an array equal to the i:th row of the sparse matrix A.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If i is out of matrix dimension the errorcode 6522 is returned.

10.4  Operators

The following functions are available to perform calculations with sparse matrices.

10.4.1  cA

Purpose
Mutliplies a sparse 2-D matrix with a scalar c.

B = cA
where B ∈ Rm × n, A ∈ Rm × n, and c is scalar.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
c A scalar c.

Description of Outputs
B A sparse matrix containing each element in A multiplied by c.

Description
The routine sparse_cA.vi uses optimal data handling and structures to multiply a sparse LabVIEW matrix by a scalar.

Examples
usersguide_sparse.

10.4.2  Ax

Purpose
Mutliplies a sparse 2-D matrix with a dense vector x.

y = Ax
where y ∈ Rm × 1, A ∈ Rm × n, and x ∈ Rn × 1.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
x A dense vector (array) x.

Description of Outputs
y An array containing each row in A multiplied by x.

Description
The routine sparse_Ax.vi uses optimal data handling and structures to multiply a sparse LabVIEW matrix by a dense array.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If length of x is not equal to the number of columns in A the errorcode -20039 is returned. If the dimensions of all the input variables is equal to zero the errorcode -20003 is returned.

10.4.3  ATx

Purpose
Mutliplies the transpose of a sparse 2-D matrix with a dense vector x.

y = ATx
where y ∈ Rn × 1, A ∈ Rm × n, and x ∈ Rm × 1.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
x A dense vector (array) x.

Description of Outputs
y An array containing each column in A multiplied by x.

Description
The routine sparse_ATx.vi uses optimal data handling and structures to multiply a sparse LabVIEW matrix by a dense array.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If length of x is not equal to the number of rows in A the errorcode -20039 is returned. If the dimensions of all the input variables is equal to zero the errorcode -20003 is returned.

10.4.4  mult

Purpose
Mutliplies two sparse 2-D matrices.

C = AB
where C ∈ RmA × nB, A ∈ RmA × nA and B ∈ RmB × nB.

Description of Inputs
A Sparse matrix A. Defined by sparse_init or by converting a dense matrix to a sparse.
 
B Sparse matrix B. Defined by sparse_init or by converting a dense matrix to a sparse.

Description of Outputs
C Sparse matrix C.

Description
The routine sparse_mult.vi uses optimal data handling and structures to multiply two sparse LabVIEW matrices.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If the number of columns in A is different from the number of rows in B the errorcode -20039 is returned. If some of the dimensions to the input variables is equal to zero the errorcode -20003 is returned.

10.4.5  mathFunc

Purpose
Returns the function value of each nonzero element in a sparse 2-D matrix.

B = func(A)
where B ∈ Rm × n, A ∈ Rm × n, and func is a mathematical function given as a number in the input.

Description of Inputs
A A sparse matrix. Defined by sparse_init or by converting a dense matrix to a sparse.
 
func An integer describing what matematical function to be used (defalult =1) (see list below).
 
n (optional) An integer used in some matematical function (default=3).



Table 10: The different types of optimization problems defined in TOMVIEW.

func   function
1   cos
2   sin
3   tan
4   acos
5   asin
6   atan
7   cosh
8   sinh
9   tanh
10   acosh
11   asinh
12   atanh
13   ln
14   exp
15   log10
16   power of 10
17   log2
18   power of 2
19   power of n
20   abs
21   round to nearest
22   round to −∞
23   round to +∞
24   sqrt
25   to the power of 2
26   to the power of n

Description of Outputs
B A sparse matrix containing the function value of each nonzero element.

Description
The routine sparse_mathFunc.vi uses optimal data handling and structures to return the function value of each nonzero element.

Examples
usersguide_sparse.

Error codes
An integer error code is returned for various scenarios. If the function number (func) is not included in the list above the errorcode -23095 is returned.



10.5  Examples

In the following sub-sections are some examples of how to use the sparse matrix library included with TOMVIEW.

10.5.1  init

The first exemplified VI is sparse_init.vi. It initiates a sparse matrix in TOMVIEW using values and row/column position. The following figure illustrates a Front Panel where a small sparse matrix is created.



Figure 11: Creating a sparse matrix.


pngs/tomview010.png

The values 3, 1.9, -5.7, 0.8 are inserted into a matrix in positions (2,1), (0,0), (1,2), (2,2). The indices start with 0. m (number of rows) and n (number of columns) are not given. They will get the default value 3, which is the largest value in row and col plus one. The output is a matrix in sparse form. It is possible to view the dense version of the matrix and the sparse matrix cluster component vectors pr, ir and jc.

Next figure displays how the block sparse_init.vi is called.



Figure 12: Block Diagram for creating a sparse matrix.


pngs/tomview011.png

The inputs are inserted into the block. The output from sparse_init.vi is a sparse matrix (as a cluster). The cluster components are unbundled as seen in Front Panel figure.

10.5.2  concat

The VI sparse_concat.vi concatenates two sparse matrices either row or column wise. When dim is set to 0 the VI concatenates row wise and column wise for 1. The following two figures illustrates the difference.



Figure 13: Front Panel illustrating a row wise concatenation.


pngs/tomview012.png




Figure 14: Front Panel illustrating a column wise concatenation.


pngs/tomview013.png

The input matrices are viewed in dense form to get an better overview. The output is viewed in both dense and sparse formats.

The block diagram shows how to connect the inputs to the sparse_concat block.



Figure 15: Block Diagram illustrating sparse_concat.


pngs/tomview014.png

The dense matrices are first converted to the sparse format. The output from concat is also a sparse matrix. In this example the output cluster is unbundled into its components. Also a dense version of the output is displayed (as seen on the Front Panel).

10.5.3  Ax

sparse_Ax.vi is a function which multiplies a sparse matrix with a vector (in LabVIEW an array). The front panel is shown in the next figure.



Figure 16: Front Panel illustrating a sparse matrix vector multiplication.


pngs/tomview015.png

The matrix is displayed in a dense format to get a better overview. The VI sparse_Ax.vi accepts a sparse matrix as input which can be seen in the Block Diagram.

The Block Diagram shows how to connect the input to sparse_Ax.vi.



Figure 17: Block Diagram for illustration of sparse matrix vector multiplication.


pngs/tomview016.png

10.5.4  example

sparse_example.vi is an example VI where all the sparse functions are examplified. A sparse matrix is initiated by sparse_init which is used in many of the examples. In the Block Diagram all the sparse functions are called. The next two figures show how to call the sparse functions.



Figure 18: Block Diagram for illustration of the functions in the sparse library.


pngs/tomview017.png




Figure 19: Block Diagram for illustration of the functions in the sparse library.


pngs/tomview018.png

The inputs and the outputs are dispayed in the Front Panel which is shown in the next figure.



Figure 20: Front Panel to sparse_example. All inputs and outputs are displayed.


pngs/tomview019.png

« Previous « Start » Next »