# DMelt:Numeric/3 Random Matrices

# Random Matrices

The DataMelt contains many high-performance Java packages to construct random matrices.

- jhplot.math.LinearAlgebra - A Native jHPlot Linear Algebra package
- Jama/package-summary - A Java Matrix Package
- ParallelColt - high-performance calculations on multiple cores
- EJML - Efficient Java Matrix Library

## 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:

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

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.

For more information how to generate random matrices, read 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 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).

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

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

More information on this topic is in DMelt books |