Previous Page Next Page Contents

linalg::randomMatrix -- generate a random matrix

Introduction

linalg::randomMatrix(m, n) returns an m x n matrix with random components.

Call(s)

linalg::randomMatrix(m, n <, R>)
linalg::randomMatrix(m, n <, R> <, bound>, Diagonal)
linalg::randomMatrix(m, n <, R> <, bound>, Unimodular)

Parameters

m, n - positive integers
R - the component ring, i.e., a domain of category Cat::Rng; default: Dom::ExpressionField()
bound - an arithmetical expression

Options

Diagonal - creates a random m x n diagonal matrix over R.
Unimodular - creates a random m x n unimodular matrix over R.

Returns

a matrix of the domain Dom::Matrix(R).

Related Functions

random, Dom::Matrix

Details

Option: Unimodular

Option: Diagonal

Example 1

We create a random square matrix over the integers. Because the matrix is random the created matrix can vary:

>> linalg::randomMatrix(2, 2, Dom::Integer)
                             +-            -+
                             |   824,  -65  |
                             |              |
                             |  -814, -741  |
                             +-            -+

If you want to bound the size of its components, say between -2 and 2, enter:

>> linalg::randomMatrix(2, 2, Dom::Integer, -2..2)
                                +-       -+
                                |  -1, 1  |
                                |         |
                                |  -2, 1  |
                                +-       -+

Example 2

The following input creates a random vector over the default component ring Dom::ExpressionField(). Because the vector is random the created vector can vary:

>> v := linalg::randomMatrix(1, 2)
               array(1..1, 1..2,
                                     3         5
                               470 R1  - 494 R1  - 246
                 (1, 1) = ---------------------------------,
                                         2          3
                          381 R1 + 747 R1  - 1150 R1  - 535
                                          3         5
                          1169 R1 - 977 R1  + 932 R1  - 781
                 (1, 2) = ----------------------------------
                                          2          3
                          - 214 R1 + 10 R1  - 1240 R1  + 712
               )
>> domtype(v)
                               Dom::Matrix()

The components of this matrix are random univariate polynomials created by the function polylib::randpoly. See the method "random" of the domain constructor Dom::ExpressionField for details.

Example 3

To create a random diagonal matrix over the rationals we enter, for example:

>> linalg::randomMatrix(3, 3, Dom::Rational, Diagonal)
                      +-                           -+
                      |  -64/305,    0,       0     |
                      |                             |
                      |     0,    41/617,     0     |
                      |                             |
                      |     0,       0,   -167/509  |
                      +-                           -+

Example 4

The following command creates a random unimodular matrix over the integers so that its determinant is either 1 or -1:

>> A := linalg::randomMatrix(3, 3, Dom::Integer, Unimodular)
                              +-           -+
                              |   9, 2,  8  |
                              |             |
                              |   4, 1,  0  |
                              |             |
                              |  -1, 0, -7  |
                              +-           -+
>> linalg::det(A)
                                     1

We can bound the size of the components. The following input returns a unimodular matrix A = (a[i,j]) with abs(a[i,j]) <= 2 for i,j=1..3:

>> A := linalg::randomMatrix(3, 3, 2, Unimodular)
                             +-            -+
                             |  -1,  0, -2  |
                             |              |
                             |   1,  1,  0  |
                             |              |
                             |  -2, -1, -1  |
                             +-            -+

Since we did not specifiy the component ring, the matrix is defined over the standard component ring for matrices (the domain Dom::ExpressionField()):

>> domtype(A)
                               Dom::Matrix()

Background

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000