public Matrix Randomize(double lowest = 0.0, double highest = 1.0, EDistribution distribution = EDistribution.Uniform) { var _mat = Duplicate(); _mat.InRandomize(lowest, highest, distribution); return(_mat); }
/// <summary> /// Get a random matrix filled with a custom distribution of random numbers /// </summary> /// <param name="lowest">Lower bound</param> /// <param name="highest">Upper bound</param> /// <param name="distribution">Distribution type</param> /// <returns>A random matrix filled with a custom distribution of random numbers</returns> public Matrix Randomize(double lowest = 0.0, double highest = 1.0, EDistribution distribution = EDistribution.Gaussian) { var mat = Duplicate(); mat.InRandomize(lowest, highest, distribution); return(mat); }
/// <summary> /// Fill the matrix with a custom distribution of random numbers /// </summary> /// <param name="lowest">Lower bound</param> /// <param name="highest">Upper bound</param> /// <param name="distribution">Distribution type</param> public void InRandomize(double lowest = 0.0, double highest = 1.0, EDistribution distribution = EDistribution.Gaussian) { var diff = highest - lowest; switch (distribution) { case EDistribution.Invalid: { throw new InvalidOperationException("Invalid Random Distribution Mode!"); } case EDistribution.Uniform: { var random = new Random(); Parallel.For(0, Rows, row => { for (var col = 0; col < Columns; col++) { _matrix[row, col] = random.NextDouble() * diff + lowest; } }); break; } case EDistribution.Normal: case EDistribution.Gaussian: { var dispersion = diff / 2; var random = new Random(); Parallel.For(0, Rows, row => { for (var col = 0; col < Columns; col++) { var u1 = 1.0 - random.NextDouble(); var u2 = 1.0 - random.NextDouble(); var randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); //random normal(mean,stdDev^2) var randNormal = 0 + dispersion * randStdNormal; _matrix[row, col] = randNormal; } }); break; } default: throw new InvalidOperationException("Invalid Random Distribution Mode!"); } }
public void InRandomize(double lowest = 0.0, double highest = 1.0, EDistribution distribution = EDistribution.Uniform) { double _diff = highest - lowest; if (distribution == EDistribution.Invalid) { throw new InvalidOperationException("Invalid Random Distribution Mode!"); } else if (distribution == EDistribution.Uniform) { Random _random = new Random(); for (int _row = 0; _row < _matrix.GetLength(0); _row++) { for (int _col = 0; _col < _matrix.GetLength(1); _col++) { _matrix[_row, _col] = (_random.NextDouble() * _diff) + lowest; } } } else if (distribution == EDistribution.Gaussian) { double _dispersion = _diff / 2; Random random = new Random(); //reuse this if you are generating many for (int _row = 0; _row < _matrix.GetLength(0); _row++) { for (int _col = 0; _col < _matrix.GetLength(1); _col++) { //uniform(0,1] random doubles double u1 = 1.0 - random.NextDouble(); double u2 = 1.0 - random.NextDouble(); //random normal(0,1) double randStdNormal = System.Math.Sqrt(-2.0 * System.Math.Log(u1)) * System.Math.Sin(2.0 * System.Math.PI * u2); //random normal(mean,stdDev^2) double randNormal = 0 + _dispersion * randStdNormal; _matrix[_row, _col] = randNormal; } } } }
/// <summary> /// The distribution to use for calculating the random matrix. Sparse1 is: /// sqrt(3) * { -1 with prob(1/6), 0 with prob(2/3), +1 with prob(1/6) } Sparse2 /// is: { -1 with prob(1/2), +1 with prob(1/2) } /// </summary> public RandomProjection Distribution(EDistribution newDstr) { Impl.setDistribution(new weka.core.SelectedTag((int)newDstr, weka.filters.unsupervised.attribute.RandomProjection.TAGS_DSTRS_TYPE)); return(this); }