DMelt:Numeric/3 Random Matrices

From HandWiki

Random Matrices

A random matrix is a matrix filled with random variables. Read Random_matrix. The DataMelt contains many high-performance Java packages to construct random matrices.

Using EJML

In this example we will illustrate the capabilities of the EJML library. Let us create a 20x20 matrix with random numbers between -10 and 10. The script below generates this matrix and visualize it:

from org.ejml.ops import RandomMatrices
from java.util import Random
from org.ejml.ops import MatrixVisualization

A=RandomMatrices.createSymmetric(20,-10,10,Random())
MatrixVisualization.show(A,"Small Matrix")

This is the generated image. Block means an element is zero. Red positive and blue negative, while more intense the color larger the element's absolute value is.

DMelt example: Random matrix and its visualisation

For more information how to generate random matrices, read RandomMatrices RandomMatrices class.

Using Parallel Colt

Matrix creation and manipulation can be fully multithreaded. In this approach, all processing cores of your computer will be used for calculations (or only a certain number of core as you have specified). Below we give a simple example. In the example below we create a large matrix 2000x2000 and calculate various characteristics of such matrix (cardinality, vectorize). We compare single threaded calculations with multithreaded ones (in this case, we set the number of cores to 2, but feel free to set to a large value).

To build random 2D matrices use DoubleFactory2D DoubleFactory2D class. Here is a short example to create 1000x1000 matrix and fill it with random numbers:

from cern.colt.matrix        import *
from edu.emory.mathcs.utils  import ConcurrencyUtils
ConcurrencyUtils.setNumberOfThreads(4) # set 4 numbers of threads
M=tdouble.DoubleFactory2D.dense.random(1000, 1000) # random matrix

In the example below we create a large matrix 2000x2000 and calculate various characteristics of such matrix (cardinality, vectorize). We compare single threaded calculations with multithreaded ones (in this case, we set the number of cores to 2, but feel free to set to a large value).

# In Parallel Colt threads are used automatically when computations are done on a
# machine with multiple CPUs. 
# Here are some benchmarks of DenseDoubleMatrix2Di done by Piotr Wendykier piotr.wendykier@gmail.com
# http://sites.google.com/site/piotrwendykier/files/BenchmarkDenseDoubleMatrix2D.txt
# This example is written by S.Chekanov

###################### benchmarking multi-threaded colt ##############
from cern.colt.matrix        import *
from edu.emory.mathcs.utils  import ConcurrencyUtils
import time

# do some calculations on DenseDoubleMatrix2D
def process(M):
       M.cardinality()
       M.dctColumns(0)
       M.dctRows(0)
       M.dct2(0)  
       M.dht2()
       M.dhtColumns()
       M.dhtRows() 
       M.dst2(0)
       M.dstColumns(0)
       M.dstRows(0)
       M.vectorize()
       M.zSum()
       M.idct2(0)


################### two threads ###################################
Ncores=2
print " benchmarks of DenseDoubleMatrix2D for "+str(Ncores)+"  CPU core. Wait!"
ConcurrencyUtils.setNumberOfThreads(Ncores)
start = time.clock()
M=tdouble.DoubleFactory2D.dense.random(2000, 2000) # random matrix
process(M)
print ' Multiple CPU time (s)=',time.clock()-start


####################### single core #################################
Ncores=1
print " benchmarks of DenseDoubleMatrix2D for "+str(Ncores)+"  CPU core. wait!"
ConcurrencyUtils.setNumberOfThreads(Ncores)
start = time.clock()
M=tdouble.DoubleFactory2D.dense.random(2000, 2000) # random matrix
process(M)
print ' Multiple CPU time (s)=',time.clock()-start

This example shows the the speed of the calculations is inversely propositional to the number of processing cores.