XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ MMRead()

Sub MMRead ( Fname As  String,
Nrow As  Long,
Ncol As  Long,
Nnz As  Long,
Val() As  Double,
Ptr() As  Long,
Ind() As  Long,
DataType As  Long,
MatShape As  Long,
MatForm As  Long,
Optional Info As  Long,
Optional Skip As  Long = 0,
Optional Base As  Long = 0,
Optional Format As  Long = 0,
Optional Sort As  Long = 0 
)

Read a matrix from the Matrix Market file

Purpose
This routine reads a sparse or dense matrix from the Matrix Market file. A sparse matrix is stored in CSR, CSC or COO format.
Parameters
[in]FnameInput file name.
[out]NrowNumber of rows of the matrix.
[out]NcolNumber of columns of the matrix.
[out]NnzNumber of nonzero elements of the matrix (for sparse matrix only).
[out]Val()Array Val(LVal - 1) (LVal is as follows)
Storage     Data type                 Matrix shape    LVal (min. value)
----------  ------------------------  --------------  -----------------
0 (sparse)  0 (real) or 3 (integer)                   Nnz
1 (dense)   0 (real) or 3 (integer)   0 (gegeral)     Nrow*Ncol
                                      1 (symmetric)   Ncol*(Ncol + 1)/2
                                      2 (skew)        Ncol*(Ncol - 1)/2
Nonzero elements of sparse matrix, or, all elements of dense matrix.
Not referenced if data type = 2 (pattern).
[out]Ptr()Array Ptr(LPtr - 1) (LPtr >= Ncol + 1 (if matrix is stored in CSC format), LPtr >= Nrow + 1 (if matrix is stored in CSR format) or LPtr >= Nnz (if matrix is stored in COO format))
Row pointers (if matrix is stored in CSR format), column pointers (if matrix is stored in CSC format) or row indices (if matrix is stored in COO format) of nonzero elements.
Not referenced if storage format = 1 (dense).
[out]Ind()Array Ind(LInd - 1) (LInd >= Nnz)
Row indices (if matrix is stored in CSC format) or column indices (if matrix is stored in CSR or COO format) of nonzero elements.
Not referenced if storage format = 1 (dense).
[out]DataTypeData type of the matrix.
= 0: Real.
= 2: Pattern.
= 3: Integer.
[out]MatShapeMatrix shape.
= 0: General.
= 1: Symmetric.
= 2: Skew-symmetric.
[out]MatFormStorage format of the matrix.
= 0: Sparse.
= 1: Dense.
[out]Info(Optional)
= 0: Successful exit.
= i < 0: The (-i)-th argument is invalid.
= 1: Failed to open file.
= 3: File read error.
= 4: Invalid type.
[in]Skip(Optional)
Flag to skip to read data. (default = 0)
= 0: Read all data.
= 1: Read header and size only (matrix data is not read).
[in]Base(Optional)
Index style of Ptr and Ind. (default = 0)
= 0: Zero-based (C style) indexing: Starting index is 0.
= 1: One-based (Fortran style) indexing: Starting index is 1.
[in]Format(Optional)
Sparse matrix format when stored. (default = 0)
= 0: CSR format.
= 1: CSC format.
= 2: COO format.
[in]Sort(Optional)
Specify whether to sort elements of sparse matrix in ascending order of column number within each row (if CSR) or row number within each column (if CSC). (default = 0)
= 0: Do not sort elements.
= 1: Sort elements.
Example Program
Reads the following file (Test_MMWrite.mtx) in Matrix Market format.
%%MatrixMarket matrix coordinate real general
3 3 9
1 1 2.000000000000000e-01
1 2 -1.100000000000000e-01
1 3 -9.300000000000000e-01
2 1 -3.200000000000000e-01
2 2 8.100000000000001e-01
2 3 3.700000000000000e-01
3 1 -8.000000000000000e-01
3 2 -9.200000000000000e-01
3 3 -2.900000000000000e-01
Sub Ex_MMRead()
Dim M As Long, N As Long, Nnz As Long, DataType As Long, MatShape As Long, MatForm As Long
Dim A(8) As Double, Ia(3) As Long, Ja(8) As Long
Dim Info As Long
Call MMRead("Test_MMWrite.mtx", M, N, Nnz, A(), Ia(), Ja(), DataType, MatShape, MatForm, Info)
Debug.Print "MMRead: Info =" + Str(Info)
Debug.Print "M =" + Str(M) + ", N =" + Str(N) + ", Nnz =" + Str(Nnz)
Debug.Print "DataType =" + Str(DataType) + ", MatShape =" + Str(MatShape) + ", MatForm =" + Str(MatForm)
Debug.Print A(0), A(1), A(2), A(3), A(4), A(5), A(6), A(7), A(8)
Debug.Print Ia(0), Ia(1), Ia(2), Ia(3)
Debug.Print Ja(0), Ja(1), Ja(2), Ja(3), Ja(4), Ja(5), Ja(6), Ja(7), Ja(8)
End Sub
Sub MMRead(Fname As String, Nrow As Long, Ncol As Long, Nnz As Long, Val() As Double, Ptr() As Long, Ind() As Long, DataType As Long, MatShape As Long, MatForm As Long, Optional Info As Long, Optional Skip As Long=0, Optional Base As Long=0, Optional Format As Long=0, Optional Sort As Long=0)
Read a matrix from the Matrix Market file
Example Results
MMRead: Info = 0
M = 3, N = 3, Nnz = 9
DataType = 0, MatShape = 0, MatForm = 0
0.2 -0.11 -0.93 -0.32 0.81 0.37 -0.8 -0.92 -0.29
0 3 6 9
0 1 2 0 1 2 0 1 2