kigubkur.elmat.random

Elementary Matrix: kigubkur© matrix of all random elements [[rij]]m × n

(require '[kigubkur.elmat [random :refer [urand urandi

Examples

Generate a random kigubkur© matrix of desired matrix order

=> (view (urand [1 4]))
[0.6467861286428364 0.7553509935175396 0.3862482094084946 0.9356150748498575]
Order -> 1 x 4
=> (view (urand [4 1]))
[0.6370258106256105]
[0.8512319043211013]
[0.609290157797747]
[0.25561600085811176]
Order -> 4 x 1
=> (view (urand [4 5]))
[0.2094506898774492 0.08549039125684621 0.6684312841858667 0.5568109853986569 0.5406309311128197]
[0.35555386480811046 0.12287461588097848 0.9685059482116815 0.7594858211250237 0.24587919183693252]
[0.4257865118413622 0.19274722184084636 0.09242762596877119 0.39208439550321506 0.7787636348248705]
[0.9493276891266701 0.43333224989814934 0.39051522984650977 0.5648277975875399 0.8449391268737577]
Order -> 4 x 5

Note that urand returns a vector for Order -> 1 x 1, i.e. for single element

=> (urand [1 1])
[[0.07233606262567682]]
=> (urand 1)
[[0.9787671846303916]]

Notice that (urand m) will generate a square matrix.

=> (view (urand 2))
[0.5772541657551494 0.4248419946834543]
[0.738065848008936 0.823655831708688]
Order -> 2 x 2

By DEFAULT the random numbers are generated from the interval [0 1) (0 is the lower bound and 1 is the tight upper bound) and generated numbers are of double type.

Generating random numbers from desired interval

Insert the desired interval [lb tub] (lb as the lower bound and tub as the tight upper bound) as an optional argument.

=> (urand 1 [5 10]) ; lower bound is 5 and tight upper bound is 10
[[8.767246367591731]]
=> (view (urand [4 3] [0 3])) ; lower bound is 0 and tight upper bound is 3
[0.6787402583331696 1.730324389792447 2.0281069109222343]
[1.4635383632074985 1.8785073755135042 1.693957367943494]
[0.04365213075076235 0.18987943644213723 0.5494642122282194]
[1.5430395050710204 1.0259558681118577 0.045118145816113064]
Order -> 4 x 3

Generating random numbers of desired number type

By default the number type for the 1’s generated by the function ones is of int type. But, ones provide the flexibility to coerce the 1’s to any of Clojure’s number types: bigint for BigInteger, long for Long, short for Short, float for Float, double for Double bigdec for BigDecimal and int which is a superset for not just Integer but also Long, Short [See source for int? versus integer?].

For example to coerce (urand [4 5]) to BigDecimal

=> (view (urand [4 5] bigdec))
[0.5900044726398823M 0.8138277965166598M 0.9296083300041081M 0.16785241744483903M 0.7117965998890433M]
[0.9684924607255586M 0.7820877347157771M 0.3215747693796758M 0.0017293313450749137M 0.2587744131036487M]
[0.1122304081918134M 0.8705610964426572M 0.23920305215294413M 0.16702684432517623M 0.9175792177387482M]
[0.8725146774337583M 0.916222875079842M 0.5777314817889722M 0.4756703649380001M 0.346660406517852M]
Order -> 4 x 5
=> (view (urand [7 3] [2 7] int))
[2 5 3]
[5 5 2]
[2 2 6]
[3 5 2]
[3 3 4]
[6 6 4]
[2 2 2]
Order -> 7 x 3

For generating random numbers of the int type one can alternatively use urandi.

=> (view (urandi [7 3] [2 7]))
[3 2 3]
[2 2 6]
[5 2 5]
[5 6 3]
[2 5 3]
[4 2 5]
[2 5 6]
Order -> 7 x 3

urand

(urand mn & args)

Returns a number or a kigubkur© matrix with elements from standard uniform random distribution. given the matrix order as a scalar or clojure vector and optional arguments

[lb tub] for lower bound and tight upper bound such that lb is inclusive and tub is excluded

numb-type for Clojure number type function: int, bigint, long, short, float, double and bigdec.

Syntax Purpose
(urand s) where s > 0 returns a square matrix s × s with entries from [0 1)
(urand [m n]) where m, n > 0 returns a rectangular matrix m × n with entries from [0 1)
(urand s [lb tub]) or (urand [m n] [lb tub]) entries from [lb tub]
(urand s numb-type), (urand [m n] [lb tub] numb-type) entries of desired number type

urandi

(urandi mn & arg)

Returns a kigubkur© matrix with elements of integer values from standard uniform random distribution. given the matrix order as a scalar or clojure vector and an optional argument for the Clojure number type function: int, bigint, long, short, float, double and bigdec.

Syntax Purpose
(urandi s) where s > 0 returns a square matrix s × s with entries from [0 1)
(urandi [m n]) where m, n > 0 returns a rectangular matrix m × n with entries from [0 1)
(urandi s [lb tub]) or (urandi [m n] [lb tub]) entries from [lb tub]