Demo


In [1]:
using MatrixDepot
using PyPlot


INFO: Loading help data...

Basic

display all the matrices in the collection


In [2]:
matrixdepot()


            | symmetric |  inverse  | ill-cond  |  pos-def  |   eigen   |
    binomial|           |           |           |           |           |
      cauchy|     *     |     *     |     *     |     *     |           |
    chebspec|           |           |           |           |     *     |
        chow|           |           |           |           |     *     |
      circul|     *     |           |           |     *     |     *     |
     clement|     *     |     *     |           |           |     *     |
    dingdong|     *     |           |           |           |     *     |
     fiedler|     *     |     *     |           |           |     *     |
    forsythe|           |     *     |     *     |           |     *     |
       frank|           |           |     *     |           |     *     |
       grcar|           |           |           |           |     *     |
    hadamard|           |     *     |           |           |     *     |
        hilb|     *     |     *     |     *     |     *     |           |
     invhilb|     *     |     *     |     *     |     *     |           |
       invol|           |     *     |     *     |           |     *     |
       kahan|           |     *     |     *     |           |           |
         kms|     *     |     *     |     *     |     *     |           |
      lehmer|     *     |     *     |           |     *     |           |
      lotkin|           |     *     |     *     |           |     *     |
       magic|           |     *     |           |           |           |
       minij|     *     |     *     |           |     *     |     *     |
       moler|     *     |     *     |     *     |     *     |           |
     neumann|           |           |           |           |     *     |
      parter|           |           |           |           |     *     |
      pascal|     *     |     *     |     *     |     *     |     *     |
         pei|     *     |     *     |     *     |     *     |           |
     poisson|     *     |     *     |           |     *     |     *     |
    randcorr|     *     |           |           |           |           |
       rando|           |           |           |           |           |
     randsvd|           |           |     *     |           |           |
      rohess|           |           |           |           |           |
      rosser|           |           |     *     |           |     *     |
    sampling|           |           |           |           |     *     |
     tridiag|     *     |     *     |     *     |     *     |     *     |
        triw|           |     *     |     *     |           |           |
        vand|           |     *     |     *     |           |           |
      wathen|     *     |           |           |     *     |     *     |
   wilkinson|     *     |           |           |           |     *     |

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


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


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

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 [20]:
MatrixDepot.get("Collection/somematrix")


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

 in error at error.jl:21
 in get at /home/w/.julia/v0.3/MatrixDepot/src/download.jl:108
 in get at /home/w/.julia/v0.3/MatrixDepot/src/download.jl:151

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


In [3]:
MatrixDepot.get("HB/1138_bus")


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 19829  100 19829    0     0  45212      0 --:--:-- --:--:-- --:--:-- 45271
download:/Users/weijian/.julia/v0.3/MatrixDepot/data/uf/HB/1138_bus.tar.gz
x 1138_bus/1138_bus.mtx

In [4]:
matrixdepot()


            | symmetric |  inverse  | ill-cond  |  pos-def  |   eigen   |
    binomial|           |           |           |           |           |
      cauchy|     *     |     *     |     *     |     *     |           |
    chebspec|           |           |           |           |     *     |
        chow|           |           |           |           |     *     |
      circul|     *     |           |           |     *     |     *     |
     clement|     *     |     *     |           |           |     *     |
    dingdong|     *     |           |           |           |     *     |
     fiedler|     *     |     *     |           |           |     *     |
    forsythe|           |     *     |     *     |           |     *     |
       frank|           |           |     *     |           |     *     |
       grcar|           |           |           |           |     *     |
    hadamard|           |     *     |           |           |     *     |
        hilb|     *     |     *     |     *     |     *     |           |
     invhilb|     *     |     *     |     *     |     *     |           |
       invol|           |     *     |     *     |           |     *     |
       kahan|           |     *     |     *     |           |           |
         kms|     *     |     *     |     *     |     *     |           |
      lehmer|     *     |     *     |           |     *     |           |
      lotkin|           |     *     |     *     |           |     *     |
       magic|           |     *     |           |           |           |
       minij|     *     |     *     |           |     *     |     *     |
       moler|     *     |     *     |     *     |     *     |           |
     neumann|           |           |           |           |     *     |
      parter|           |           |           |           |     *     |
      pascal|     *     |     *     |     *     |     *     |     *     |
         pei|     *     |     *     |     *     |     *     |           |
     poisson|     *     |     *     |           |     *     |     *     |
    randcorr|     *     |           |           |           |           |
       rando|           |           |           |           |           |
     randsvd|           |           |     *     |           |           |
      rohess|           |           |           |           |           |
      rosser|           |           |     *     |           |     *     |
    sampling|           |           |           |           |     *     |
     tridiag|     *     |     *     |     *     |     *     |     *     |
        triw|           |     *     |     *     |           |           |
        vand|           |     *     |     *     |           |           |
      wathen|     *     |           |           |     *     |     *     |
   wilkinson|     *     |           |           |           |     *     |

         HB/1138_bus|  UF sparse matrix

In [5]:
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
%-------------------------------------------------------------------------------


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 [3]:
matrixdepot("data")


Out[3]:
18-element Array{Any,1}:
 "1138_bus"      
 "ca-HepTh"      
 "California"    
 "ch7-8-b3"      
 "circuit_2"     
 "crystm03"      
 "dtoc"          
 "europe_osm"    
 "G66"           
 "hi2010"        
 "hvdc1"         
 "illc1850"      
 "lp_cre_d"      
 "lp_nug20"      
 "pcrystk03"     
 "rajat09"       
 "wb-cs-stanford"
 "685_bus"       

Add a property for all the matrix representation of graphs


In [2]:
@addproperty graphs = ["California", "ca-HepTh", "wb-cs-stanford", "G66"]


Out[2]:
106

In [2]:
matrixdepot("graphs")


Out[2]:
4-element Array{ASCIIString,1}:
 "California"    
 "ca-HepTh"      
 "wb-cs-stanford"
 "G66"