To install the package, type
In [ ]:
Pkg.add("MatrixDepot")
In [1]:
using MatrixDepot
The only function will be exported is matrixdepot.
In [2]:
? matrixdepot
Every matrix in the collection is represented by a string matrix_name, for example, the Cauchy matrix is represented by "cauchy" and the Hilbert matrix is represented by "hilb".
The properties of the matrices in the collection are also symbolized by strings propertry_name. For example, the class of the symmetric matrices is symbolized by "symmetric".
matrixdepot() returns a list of all the matrices in the collection.
matrixdepot(matrix_name, p1, p2, ...) returns a matrix specified by the query string matrix_name. p1, p2, ... are input parameters depending on matrix_name.
matrixdepot(matrix_name) returns the parameter options and the properties of matrix_name.
matrixdepot(property_name) returns a list of matrices with the property property_name.
matrixdepot(prop1, prop2, prop3,...) returns a list of matrices with properties prop1, prop2, prop3 etc.
We can define our own properties using the macro @addproperty
@addproperty property_name = ["matrix1", "matrix2", "matrix3"]and remove a defined property using the macro @rmproperty
@rmproperty property_nameSee below for examples.
To see all the matrices in the collection, type
In [3]:
matrixdepot()
The meanings of the column heading is as follows:
"symmetric": the matrix is symmetric for some parameter values.
"inverse": the inverse of the matrix is known explicitly.
"ill-cond": the matrix is ill-conditioned for some parameter values.
"pos-def": the matrix is symmetric positive definite for some parameter values.
"eigen": the eigensystem of the matrix has some known results (explicit formulas for eigenvalues, eigenvectors, bounds of eigenvalues, etc).
We can generate a Hilbert matrix of size 4 by typing
In [4]:
matrixdepot("hilb", 4)
Out[4]:
and generate a circul matrix of size 5 by
In [5]:
matrixdepot("circul", 5)
Out[5]:
We can type the matrix name to see the parameter options or matrix properties.
In [6]:
matrixdepot("hilb")
In [7]:
matrixdepot("hadamard")
From the information given, we notice that we can create a 4-by-6 rectanglular Hilbert matrix by
In [8]:
matrixdepot("hilb", 4, 6)
Out[8]:
We can aslo specify the data type
In [9]:
matrixdepot("hilb", Float16, 5, 3)
Out[9]:
By inputing a matrix name, we can see what properties that matrix have. Conversely, if we input a property (or properties), we can see all the matrices (in the collection) having that property (or properties).
In [8]:
matrixdepot("symmetric")
Out[8]:
In [11]:
matrixdepot("symmetric", "ill-cond")
Out[11]:
In [10]:
matrixdepot("inverse", "ill-cond", "symmetric")
Out[10]:
Given a property, we can loop through all the matrices having this propery
In [15]:
# Multiply all matrices of the class "symmetric" and "ill-cond" and "inverse"
A = eye(4)
print("Identity matrix")
for mat in intersect(matrixdepot("symmetric"), matrixdepot("ill-cond"), matrixdepot("inverse"))
print(" x $mat matrix")
A = A * full(matrixdepot(mat, 4))
end
println(" =")
A
Out[15]:
The loop above can also be written as
In [16]:
A = eye(4)
print("Identity matrix")
for mat in matrixdepot("symmetric", "ill-cond", "inverse")
print(" x $mat matrix")
A = A * full(matrixdepot(mat, 4))
end
println(" =")
A
Out[16]:
We can define properties in MatrixDepot. Since each property in Matrix Depot is a list of strings, you can simply do, for example,
In [2]:
spd = matrixdepot("symmetric", "pos-def")
Out[2]:
In [5]:
myprop = ["lehmer", "cauchy", "hilb"]
Out[5]:
Then use it in your tests like
In [8]:
for matrix in myprop
A = matrixdepot(matrix, 6)
L, U, p = lu(A) #LU factorization
err = norm(A[p,:] - L*U, 1) # 1-norm error
println("1-norm error for $matrix matrix is ", err)
end
To add a property permanently for future use, we put the macro @addproperty at the beginning.
In [2]:
@addproperty myfav = ["lehmer", "cauchy", "hilb"]
Out[2]:
In [4]:
@addproperty spd = matrixdepot("symmetric", "pos-def")
Out[4]:
We need to restart Julia to see the changes. Type
In [2]:
matrixdepot()
Notice new defined properties have been included. We can use them as
In [3]:
matrixdepot("myfav")
Out[3]:
We can remove a property using the macro @rmproperty. As before, we need to restart Julia to see the changes.
In [4]:
@rmproperty myfav
Out[4]:
In [3]:
matrixdepot()
An interesting test matrix is magic square. It can be generated as
In [16]:
M = matrixdepot("magic", 5)
Out[16]:
In [17]:
sum(M,1)
Out[17]:
In [18]:
sum(M,2)
Out[18]:
In [19]:
sum(diag(M))
Out[19]:
In [20]:
p = [5:-1:1]
sum(diag(M[:,p]))
Out[20]:
Pascal Matrix can be generated as
In [21]:
P = matrixdepot("pascal", 6)
Out[21]:
Notice the Cholesky factor of the Pascal matrix has Pascal's triangle rows.
In [22]:
chol(P)
Out[22]: