Demo


In [2]:
using MatrixDepot
using PyPlot

Basic

display all the matrices in the collection


In [3]:
matrixdepot()


Matrices:
   1) baart            2) binomial         3) cauchy           4) chebspec      
   5) chow             6) circul           7) clement          8) deriv2        
   9) dingdong        10) fiedler         11) forsythe        12) foxgood       
  13) frank           14) grcar           15) hadamard        16) heat          
  17) hilb            18) invhilb         19) invol           20) kahan         
  21) kms             22) lehmer          23) lotkin          24) magic         
  25) minij           26) moler           27) neumann         28) oscillate     
  29) parter          30) pascal          31) pei             32) phillips      
  33) poisson         34) prolate         35) randcorr        36) rando         
  37) randsvd         38) rohess          39) rosser          40) sampling      
  41) shaw            42) toeplitz        43) tridiag         44) triw          
  45) vand            46) wathen          47) wilkinson       48) wing          

Groups:
  data          eigen         ill-cond      inverse     
  pos-def       random        regprob       sparse      
  symmetric   

show all the symmetric and pos-def matrices in the collection


In [4]:
matrixdepot("symmetric", "pos-def")


Out[4]:
14-element Array{ASCIIString,1}:
 "hilb"     
 "cauchy"   
 "circul"   
 "invhilb"  
 "moler"    
 "pascal"   
 "pei"      
 "minij"    
 "tridiag"  
 "lehmer"   
 "poisson"  
 "kms"      
 "wathen"   
 "oscillate"

In [2]:
matrixdepot("magic")


Magic square matrix:
             
 Input options:
             
 [type,] dim: the dimension of the matrix.
             
 ['inverse']

In [17]:
A = matrixdepot("magic", 5)


Out[17]:
5x5 Array{Int64,2}:
 17  24   1   8  15
 23   5   7  14  16
  4   6  13  20  22
 10  12  19  21   3
 11  18  25   2   9

In [27]:
matrixdepot("magic", Float32, 5)


Out[27]:
5x5 Array{Float32,2}:
 17.0  24.0   1.0   8.0  15.0
 23.0   5.0   7.0  14.0  16.0
  4.0   6.0  13.0  20.0  22.0
 10.0  12.0  19.0  21.0   3.0
 11.0  18.0  25.0   2.0   9.0

A magic square is an arrangement of n^2 distinct numbers in a n-by-n square matrix such that the numbers in each row, each column, and the numbers on the diagonal and anti-diagonal all add up to the same number.


In [16]:
sum(A,1)


Out[16]:
1x5 Array{Int64,2}:
 65  65  65  65  65

In [17]:
sum(A,2)


Out[17]:
5x1 Array{Int64,2}:
 65
 65
 65
 65
 65

In [19]:
sum(diag(A))


Out[19]:
65

In [22]:
p = [5:-1:1]
sum(diag(A[:,p]))


Out[22]:
65

In [7]:
matshow(A)


Out[7]:
PyObject <matplotlib.image.AxesImage object at 0xce28d10>

In [9]:
B = matrixdepot("wilkinson", 12)


Out[9]:
12x12 Tridiagonal{Float64}:
 5.5  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  4.5  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  1.0  3.5  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  1.0  2.5  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  1.0  1.5  1.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  1.0  0.5  1.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  1.0  0.5  1.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  1.0  1.5  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  2.5  1.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  3.5  1.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  4.5  1.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  5.5

In [10]:
matshow(full(B))


Out[10]:
PyObject <matplotlib.image.AxesImage object at 0xd475e10>

In [16]:
matrixdepot("rando", Bool, 6)


Out[16]:
6x6 Array{Bool,2}:
  true  false  false  false   true   true
 false  false  false  false  false  false
  true   true   true   true  false  false
 false  false  false   true  false   true
  true   true  false  false   true   true
  true  false   true   true  false   true

We set the precision of BigFloat and generate matrices of arbitrary precision.


In [26]:
set_bigfloat_precision(200)
H = matrixdepot("hilb", BigFloat, 5)


Out[26]:
5x5 Array{BigFloat,2}:
 1e+00                                                                …  2.0000000000000000000000000000000000000000000000000000000000003e-01
 5e-01                                                                   1.6666666666666666666666666666666666666666666666666666666666672e-01
 3.3333333333333333333333333333333333333333333333333333333333344e-01     1.428571428571428571428571428571428571428571428571428571428571e-01 
 2.5e-01                                                                 1.25e-01                                                           
 2.0000000000000000000000000000000000000000000000000000000000003e-01     1.1111111111111111111111111111111111111111111111111111111111115e-01

Polar Decomposition

PolarFact is a Julia package for computing the matrix polar decomposition. It includes algorithms like Newton's method (:newton), the QDWH method (:qdwh), a hybrid Newton's method (:hybrid) and the Halley method (:halley).

We can use Matrix Depot to compare the performance of these algorithms.


In [11]:
Pkg.add("PolarFact")


INFO: Cloning cache of PolarFact from git://github.com/weijianzhang/PolarFact.jl.git
INFO: Installing PolarFact v0.0.5
INFO: Package database updated

In [10]:
using PolarFact

Compare the number of iterations of four polar decomposition algorithms


In [3]:
n = length(matrixdepot("inverse", "symmetric"))
niters = zeros(4, n)
for (i,mat) in enumerate(matrixdepot("inverse", "symmetric"))
    A = matrixdepot(mat, Float64, 12)
    A = full(A)
    r_newton = polarfact(A, alg = :newton)
    r_qdwh = polarfact(A, alg = :qdwh)
    r_hybird = polarfact(A, alg = :hybrid)
    r_halley = polarfact(A, alg = :halley)
    niters[1, i] = r_newton.niters
    niters[2, i] = r_qdwh.niters
    niters[3, i] = r_hybird.niters
    niters[4, i] = r_halley.niters
end

In [5]:
plt.figure()
for i in 1:4
    x = [1:n]
    y = niters[i,:][:]
    plt.plot(x,y, ".-")
    plt.hold("on")
end
plt.xlabel("test matrices")
plt.ylabel("number of iterations")
plt.legend(["newton", "qdwh", "hybird", "halley"])


Figure(PyObject <matplotlib.figure.Figure object at 0xbea2c90>)
Out[5]:
PyObject <matplotlib.legend.Legend object at 0xd52f350>

Interface to UF Sparse Matrix Collection and Matrix Market

We first run MatrixDepot.update(), which download two html matrix name lists.


In [2]:
MatrixDepot.update()


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1808k    0 1808k    0     0   521k      0 --:--:--  0:00:03 --:--:--  521k

Matrix Depot first check if the matrix is on the matrix data list before downloading it.


In [5]:
matrixdepot("Collection/somematrix", :get)


LoadError: can not find Collection/somematrix in UF sparse matrix collection
while loading In[5], in expression starting on line 1

 in error at ./error.jl:21
 in get at /home/weijian/.julia/v0.4/MatrixDepot/src/download.jl:149
 in matrixdepot at /home/weijian/.julia/v0.4/MatrixDepot/src/common.jl:227

HB/1138_bus (undirected graph drawing, UF Sparse Matrix Collection, Tim Davis and Yifan Hu)


In [6]:
matrixdepot("HB/1138_bus", :get)


download:/home/weijian/.julia/v0.4/MatrixDepot/data/uf/HB/1138_bus.tar.gz
1138_bus/1138_bus.mtx

In [7]:
matrixdepot()


Matrices:
   1) baart            2) binomial         3) cauchy           4) chebspec      
   5) chow             6) circul           7) clement          8) deriv2        
   9) dingdong        10) fiedler         11) forsythe        12) foxgood       
  13) frank           14) grcar           15) hadamard        16) heat          
  17) hilb            18) invhilb         19) invol           20) kahan         
  21) kms             22) lehmer          23) lotkin          24) magic         
  25) minij           26) moler           27) neumann         28) oscillate     
  29) parter          30) pascal          31) pei             32) phillips      
  33) poisson         34) prolate         35) randcorr        36) rando         
  37) randsvd         38) rohess          39) rosser          40) sampling      
  41) shaw            42) toeplitz        43) tridiag         44) triw          
  45) vand            46) wathen          47) wilkinson       48) wing          
  49) HB/1138_bus   
Groups:
  data          eigen         ill-cond      inverse     
  pos-def       random        regprob       sparse      
  symmetric   

In [8]:
matrixdepot("HB/1138_bus")


%%MatrixMarket matrix coordinate real symmetric
%-------------------------------------------------------------------------------
% UF Sparse Matrix Collection, Tim Davis
% http://www.cise.ufl.edu/research/sparse/matrices/HB/1138_bus
% name: HB/1138_bus
% [S ADMITTANCE MATRIX 1138 BUS POWER SYSTEM, D.J.TYLAVSKY, JULY 1985.]
% id: 1
% date: 1985
% author: D. Tylavsky
% ed: I. Duff, R. Grimes, J. Lewis
% fields: title A name id date author ed kind
% kind: power network problem
%-------------------------------------------------------------------------------

use matrixdepot("HB/1138_bus", :read) to read the data

In [6]:
A = matrixdepot("HB/1138_bus", :r)


Out[6]:
1138x1138 sparse matrix with 4054 Float64 entries:
	[1   ,    1]  =  1474.78
	[5   ,    1]  =  -9.01713
	[563 ,    1]  =  -5.73066
	[2   ,    2]  =  9.13665
	[10  ,    2]  =  -3.40599
	[563 ,    2]  =  -5.73066
	[3   ,    3]  =  69.6147
	[11  ,    3]  =  -8.81057
	[34  ,    3]  =  -31.1527
	[35  ,    3]  =  -16.0668
	⋮
	[1134, 1134]  =  10000.0
	[1124, 1135]  =  -10.7411
	[1129, 1135]  =  -15.8228
	[1135, 1135]  =  26.5639
	[1122, 1136]  =  -21.7865
	[1131, 1136]  =  -24.3902
	[1136, 1136]  =  46.1767
	[802 , 1137]  =  -10000.0
	[1137, 1137]  =  10000.0
	[805 , 1138]  =  -117.647
	[1138, 1138]  =  117.647

In [23]:
spy(A)


Out[23]:
PyObject <matplotlib.image.AxesImage object at 0x7f30ca832150>

show all the downloaded matrices


In [9]:
matrixdepot("data")


Out[9]:
1-element Array{AbstractString,1}:
 "HB/1138_bus"

Define a new group for a paper


In [10]:
@addgroup paper1_test = ["HB/1138_bus", "hilb", "cauchy", "pei"]


Out[10]:
99

In [2]:
matrixdepot("paper1_test")


Out[2]:
4-element Array{ASCIIString,1}:
 "HB/1138_bus"
 "hilb"       
 "cauchy"     
 "pei"