hmatrix examples

hmatrix examples

Most of the functions described in the old tutorial are available in version 0.16. This page shows a few usage examples of selected hmatrix functions and explains the main new features.

(work in progress)

vector

There are several ways to create vectors: fromList, (|>), etc. We have introduced vector to work easily in ghci with Vector Double without type annotations.

u = vector [1..5]

v = vector [10,-3,0,4,5]
u + v
fromList [11.0,-1.0,3.0,8.0,10.0]
u * v
fromList [10.0,-6.0,0.0,16.0,25.0]
u <·> v
45.0
let w = vector [1,0,1,0,1,0,1]
w / scalar (sqrt (w <·> w))
fromList [0.5,0.0,0.5,0.0,0.5,0.0,0.5]
norm_2 w
2.0

Vectors are just Data.Vector.Storable.Vector, and any function from the vector package can be directly used with them:

import qualified Data.Vector.Storable as V
V.backpermute (vector [10,20,30]) (fromList[2,1])
fromList [30.0,20.0]

matrix

We have introduced matrix to easily create real matrices in interactive mode.

tm r c = (r >< c) [1..] ::  Matrix Double

a = tm 5 3

b = matrix 5
    [ 1, 2,  3, 0, -2
    , 0, 7, -2, 4,  1 ]
a
(5><3)
 [  1.0,  2.0,  3.0
 ,  4.0,  5.0,  6.0
 ,  7.0,  8.0,  9.0
 , 10.0, 11.0, 12.0
 , 13.0, 14.0, 15.0 ]
b
(2><5)
 [ 1.0, 2.0,  3.0, 0.0, -2.0
 , 0.0, 7.0, -2.0, 4.0,  1.0 ]

The multiplication operators are now right associative and we have separated matrix-matrix multiplication (<>) and matrix-vector multiplication (#>) to make code easier to read.

b <> a
(2><3)
 [  4.0,  8.0, 12.0
 , 67.0, 77.0, 87.0 ]
b #> u
fromList [4.0,29.0]
diagl [1..4]
(4><4)
 [ 1.0, 0.0, 0.0, 0.0
 , 0.0, 2.0, 0.0, 0.0
 , 0.0, 0.0, 3.0, 0.0
 , 0.0, 0.0, 0.0, 4.0 ]
u <·> diagl [1..5] #> v
187.0
tr (b/3)
(5><2)
 [  0.3333333333333333,                 0.0
 ,  0.6666666666666666,   2.333333333333333
 ,                 1.0, -0.6666666666666666
 ,                 0.0,  1.3333333333333333
 , -0.6666666666666666,  0.3333333333333333 ]
disp 2 (b/3)
2x5
0.33  0.67   1.00  0.00  -0.67
0.00  2.33  -0.67  1.33   0.33
disp 2 b
2x5
1  2   3  0  -2
0  7  -2  4   1
ident 3 :: Matrix (Complex Double)
(3><3)
 [ 1.0 :+ 0.0, 0.0 :+ 0.0, 0.0 :+ 0.0
 , 0.0 :+ 0.0, 1.0 :+ 0.0, 0.0 :+ 0.0
 , 0.0 :+ 0.0, 0.0 :+ 0.0, 1.0 :+ 0.0 ]
putStr . dispcf 2 . diag . fromList $ [1,iC,3-iC]
3x3
1  0    0
0  i    0
0  0  3-i
vector [1,2,3] `outer` vector [10,20,30,40]
3x4
10  20  30   40
20  40  60   80
30  60  90  120
diagl [1,20] `kronecker` b
4x10
1  2   3  0  -2   0    0    0   0    0
0  7  -2  4   1   0    0    0   0    0
0  0   0  0   0  20   40   60   0  -40
0  0   0  0   0   0  140  -40  80   20

block matrix

ident 3 ¦ 1
(3><4)
 [ 1.0, 0.0, 0.0, 1.0
 , 0.0, 1.0, 0.0, 1.0
 , 0.0, 0.0, 1.0, 1.0 ]
konst 5 (3,4) ¦ row [1,2,3]
3x7
5  5  5  5  1  2  3
5  5  5  5  1  2  3
5  5  5  5  1  2  3
col [10,20,30] ¦ konst 5 (3,4)
3x5
10  5  5  5  5
20  5  5  5  5
30  5  5  5  5
row [1,1,1] ¦ row [2,2] —— row [3,3] ¦ row [4,4,4]
2x5
1  1  1  2  2
3  3  4  4  4
fromBlocks [[konst 7 (2,5), 0],[0,1]]
3x6
7  7  7  7  7  0
7  7  7  7  7  0
0  0  0  0  0  1
diagBlock [konst 7 (2,3), 1]
3x4
7  7  7  0
7  7  7  0
0  0  0  1

sort

z <- flatten <$> randn 200 1




 samples 

































 

 






























































































































































































































 0 
 20 
 40 
 60 
 80 
 100 
 120 
 140 
 160 
 180 
 200 

 k 




 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 







 value 












































 

 


















 b   









 ordered samples 

































 

 






























































































































































































































 0 
 20 
 40 
 60 
 80 
 100 
 120 
 140 
 160 
 180 
 200 

 k 




 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 







 value 












































 

 


















 b   









 empirical cumulative distribution 



























 

 






























































































































































































































 -3 
 -2 
 -1 
 0 
 1 
 2 

 x 




 0.0 
 0.2 
 0.4 
 0.6 
 0.8 
 1.0 







 F(x) 






































 

 


















 b   





display

let z = conv2 (konst (1) (3,3)) (diagl [1..10])
z
(12><12)
 [ 1.0, 1.0, 1.0, 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
 , 1.0, 3.0, 3.0, 2.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
 , 1.0, 3.0, 6.0, 5.0,  3.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
 , 0.0, 2.0, 5.0, 9.0,  7.0,  4.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
 , 0.0, 0.0, 3.0, 7.0, 12.0,  9.0,  5.0,  0.0,  0.0,  0.0,  0.0,  0.0
 , 0.0, 0.0, 0.0, 4.0,  9.0, 15.0, 11.0,  6.0,  0.0,  0.0,  0.0,  0.0
 , 0.0, 0.0, 0.0, 0.0,  5.0, 11.0, 18.0, 13.0,  7.0,  0.0,  0.0,  0.0
 , 0.0, 0.0, 0.0, 0.0,  0.0,  6.0, 13.0, 21.0, 15.0,  8.0,  0.0,  0.0
 , 0.0, 0.0, 0.0, 0.0,  0.0,  0.0,  7.0, 15.0, 24.0, 17.0,  9.0,  0.0
 , 0.0, 0.0, 0.0, 0.0,  0.0,  0.0,  0.0,  8.0, 17.0, 27.0, 19.0, 10.0
 , 0.0, 0.0, 0.0, 0.0,  0.0,  0.0,  0.0,  0.0,  9.0, 19.0, 19.0, 10.0
 , 0.0, 0.0, 0.0, 0.0,  0.0,  0.0,  0.0,  0.0,  0.0, 10.0, 10.0, 10.0 ]
disp 2 z
12x12
1  1  1  0   0   0   0   0   0   0   0   0
1  3  3  2   0   0   0   0   0   0   0   0
1  3  6  5   3   0   0   0   0   0   0   0
0  2  5  9   7   4   0   0   0   0   0   0
0  0  3  7  12   9   5   0   0   0   0   0
0  0  0  4   9  15  11   6   0   0   0   0
0  0  0  0   5  11  18  13   7   0   0   0
0  0  0  0   0   6  13  21  15   8   0   0
0  0  0  0   0   0   7  15  24  17   9   0
0  0  0  0   0   0   0   8  17  27  19  10
0  0  0  0   0   0   0   0   9  19  19  10
0  0  0  0   0   0   0   0   0  10  10  10
dispDots 1 z
1  1  1  .   .   .   .   .   .   .   .   .
1  3  3  2   .   .   .   .   .   .   .   .
1  3  6  5   3   .   .   .   .   .   .   .
.  2  5  9   7   4   .   .   .   .   .   .
.  .  3  7  12   9   5   .   .   .   .   .
.  .  .  4   9  15  11   6   .   .   .   .
.  .  .  .   5  11  18  13   7   .   .   .
.  .  .  .   .   6  13  21  15   8   .   .
.  .  .  .   .   .   7  15  24  17   9   .
.  .  .  .   .   .   .   8  17  27  19  10
.  .  .  .   .   .   .   .   9  19  19  10
.  .  .  .   .   .   .   .   .  10  10  10
dispDots 2 (z/9)
0.11  0.11  0.11   .     .     .     .     .     .     .     .     .  
0.11  0.33  0.33  0.22   .     .     .     .     .     .     .     .  
0.11  0.33  0.67  0.56  0.33   .     .     .     .     .     .     .  
 .    0.22  0.56  1.    0.78  0.44   .     .     .     .     .     .  
 .     .    0.33  0.78  1.33  1.    0.56   .     .     .     .     .  
 .     .     .    0.44  1.    1.67  1.22  0.67   .     .     .     .  
 .     .     .     .    0.56  1.22  2.    1.44  0.78   .     .     .  
 .     .     .     .     .    0.67  1.44  2.33  1.67  0.89   .     .  
 .     .     .     .     .     .    0.78  1.67  2.67  1.89  1.     .  
 .     .     .     .     .     .     .    0.89  1.89  3.    2.11  1.11
 .     .     .     .     .     .     .     .    1.    2.11  2.11  1.11
 .     .     .     .     .     .     .     .     .    1.11  1.11  1.11
dispBlanks 2 (z/1000)
0.00  0.00  0.00                                                      
0.00  0.00  0.00  0.00                                                
0.00  0.00  0.01  0.00  0.00                                          
      0.00  0.00  0.01  0.01  0.00                                    
            0.00  0.01  0.01  0.01  0.00                              
                  0.00  0.01  0.02  0.01  0.01                        
                        0.00  0.01  0.02  0.01  0.01                  
                              0.01  0.01  0.02  0.02  0.01            
                                    0.01  0.02  0.02  0.02  0.01      
                                          0.01  0.02  0.03  0.02  0.01
                                                0.01  0.02  0.02  0.01
                                                      0.01  0.01  0.01
dispDots 2 (diagl [1..15])
1  .  .  .  .  .  .  .  .   .   .   .   .   .   .
.  2  .  .  .  .  .  .  .   .   .   .   .   .   .
.  .  3  .  .  .  .  .  .   .   .   .   .   .   .
.  .  .  4  .  .  .  .  .   .   .   .   .   .   .
.  .  .  .  5  .  .  .  .   .   .   .   .   .   .
.  .  .  .  .  6  .  .  .   .   .   .   .   .   .
.  .  .  .  .  .  7  .  .   .   .   .   .   .   .
.  .  .  .  .  .  .  8  .   .   .   .   .   .   .
.  .  .  .  .  .  .  .  9   .   .   .   .   .   .
.  .  .  .  .  .  .  .  .  10   .   .   .   .   .
.  .  .  .  .  .  .  .  .   .  11   .   .   .   .
.  .  .  .  .  .  .  .  .   .   .  12   .   .   .
.  .  .  .  .  .  .  .  .   .   .   .  13   .   .
.  .  .  .  .  .  .  .  .   .   .   .   .  14   .
.  .  .  .  .  .  .  .  .   .   .   .   .   .  15
dispShort 10 10 1 (matrix 30 [1..30*30])
30x30
  1.0    2.0    3.0    4.0    5.0    6.0    7.0  ..  29.0   30.0
 31.0   32.0   33.0   34.0   35.0   36.0   37.0  ..  59.0   60.0
 61.0   62.0   63.0   64.0   65.0   66.0   67.0  ..  89.0   90.0
 91.0   92.0   93.0   94.0   95.0   96.0   97.0  .. 119.0  120.0
121.0  122.0  123.0  124.0  125.0  126.0  127.0  .. 149.0  150.0
151.0  152.0  153.0  154.0  155.0  156.0  157.0  .. 179.0  180.0
181.0  182.0  183.0  184.0  185.0  186.0  187.0  .. 209.0  210.0
   :      :      :      :      :      :      :   ::    :      : 
841.0  842.0  843.0  844.0  845.0  846.0  847.0  .. 869.0  870.0
871.0  872.0  873.0  874.0  875.0  876.0  877.0  .. 899.0  900.0
dispShort 10 10 2 =<< randn 1000 5
1000x5
-0.52   0.98   0.65   1.58   0.70
-0.99   0.12  -1.45   0.28   0.12
-0.08   1.68   0.75  -1.03  -0.01
-1.58  -0.41  -1.56   0.16   0.14
 0.29   0.36  -0.03  -1.24  -1.28
-0.64  -1.23   1.07   0.96  -0.18
-0.49  -0.87   0.57   1.14   0.70
  :      :      :      :      :  
-0.38  -0.71   0.64  -0.27  -0.95
-0.67  -0.67   1.64   0.32  -0.02

A Ruiz / 2014-07-01
powered by hmatrix