PolyMath icon indicating copy to clipboard operation
PolyMath copied to clipboard

A better API is needed to built random matrices

Open SergeStinckwich opened this issue 6 years ago • 2 comments

At the moment, we can only built a random matrix with:

PMMatrix class>>rows: rows columns: columns random: aMaxNumber
	"Answer a new Matrix of the given dimensions filled with random numbers"
	|a b|
	a:=	(1 to: rows) collect: [:row |b:=PMVector new:columns .
		1 to: columns do: [:column |
			b  at: column put: (aMaxNumber random)].
		b].
	^PMMatrix rows: a

i.e with only integers between 0 and aMaxNumber-1. We need to be able to choose the min and the max value and also to use a different distribution law (for example normal distribution).

In Numpy, they have : Numpy.random.randn for example to generate random numbers following a normal distribution probability: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.randn.html#numpy.random.randn

SergeStinckwich avatar Jan 08 '18 15:01 SergeStinckwich

Hey ! I would like to take up this issue.

  • For user to choose min and max value I'm thinking something like :
rows: rows columns: columns lowerLimit:x  upperLimit:y
	"Answer a new Matrix of the given dimensions filled with random numbers between 'x' and 'y'"
	|a b|
	a:= (1 to: rows) collect: [:row |b:=PMVector new:columns .
	    1 to: columns do: [:column |
			b  at: column put: ((x to: y) atRandom)].
		b].
	^PMMatrix rows: a
  • For normal distribution I'm think of using PMGaussianGenerator to generate random values of the normal distribution. Something like :
rows: rows columns: columns mean: mean standardDeviation: stdiv
	|a b randomGenerator|
	randomGenerator := PMGaussianGenerator new .
	randomGenerator initialize .
	a:= (1 to: rows) collect: [:row |b:=PMVector new:columns .
	     1 to: columns do: [:column |
			b  at: column put: ((randomGenerator next)*stdiv + mean)].
		b].
	^PMMatrix rows: a

I would like to know your opinion on these.

kausthubtm avatar Dec 14 '21 14:12 kausthubtm

Instead of hardcoding PMGaussianGenerator it would be nice to have a configurable RNG randomGenerator method:

rows: rows columns: columns mean: mean standardDeviation: stdiv
	| a b |

	a := (1 to: rows) collect: [:row |
             b := PMVector new: columns .
	     1 to: columns do: [:column |
			b  at: column put: ((self randomGenerator next) * stdiv + mean)].
		b ].
	^ PMMatrix rows: a

hernanmd avatar Feb 03 '22 02:02 hernanmd