/** * <p> * Sets each element in the matrix to a value drawn from an uniform distribution from 'min' to 'max' inclusive. * </p> * * @param min The minimum value each element can be. * @param max The maximum value each element can be. * @param mat The matrix who is to be randomized. Modified. * @param randJava.Util.Random number generator used to fill the matrix. */ public static void fillUniform(DMatrixD1 mat, double min, double max, Java.Util.Random rand) { double[] d = mat.getData(); int size = mat.NumElements; double r = max - min; for (int i = 0; i < size; i++) { d[i] = r * rand.NextDouble() + min; } }
/** * <p> * AddsJava.Util.Random values to each element in the matrix from an uniform distribution.<br> * <br> * a<sub>ij</sub> = a<sub>ij</sub> + U(min,max)<br> * </p> * * @param A The matrix who is to be randomized. Modified * @param min The minimum value each element can be. * @param max The maximum value each element can be.. * @param randJava.Util.Random number generator used to fill the matrix. */ public static void addUniform(DMatrixRMaj A, double min, double max, Java.Util.Random rand) { double[] d = A.getData(); int size = A.NumElements; double r = max - min; for (int i = 0; i < size; i++) { d[i] += r * rand.NextDouble() + min; } }
/** * Splatter paint in an area. * * Chooses random vectors describing the flow of paint from a round nozzle * across a range of a few degrees. Then adds this vector to the direction * indicated by the orientation and tilt of the tool and throws paint at * the canvas along that vector. * * Repeats the process until a masterpiece is born. */ private void DrawSplat(Canvas canvas, float x, float y, float orientation, float distance, float tilt, Paint paint) { float z = distance * 2 + 10; // Calculate the center of the spray. float nx = (float)(Math.Sin(orientation) * Math.Sin(tilt)); float ny = (float)(-Math.Cos(orientation) * Math.Sin(tilt)); float nz = (float)Math.Cos(tilt); if (nz < 0.05) { return; } float cd = z / nz; float cx = nx * cd; float cy = ny * cd; for (int i = 0; i < SPLAT_VECTORS; i++) { // Make a random 2D vector that describes the direction of a speck of paint // ejected by the nozzle in the nozzle's plane, assuming the tool is // perpendicular to the surface. double direction = mRandom.NextDouble() * Math.PI * 2; double dispersion = mRandom.NextGaussian() * 0.2; double vx = Math.Cos(direction) * dispersion; double vy = Math.Sin(direction) * dispersion; double vz = 1; // Apply the nozzle tilt angle. double temp = vy; vy = temp * Math.Cos(tilt) - vz * Math.Sin(tilt); vz = temp * Math.Sin(tilt) + vz * Math.Cos(tilt); // Apply the nozzle orientation angle. temp = vx; vx = temp * Math.Cos(orientation) - vy * Math.Sin(orientation); vy = temp * Math.Sin(orientation) + vy * Math.Cos(orientation); // Determine where the paint will hit the surface. if (vz < 0.05) { continue; } float pd = (float)(z / vz); float px = (float)(vx * pd); float py = (float)(vy * pd); // Throw some paint at this location, relative to the center of the spray. mCanvas.DrawCircle(x + px - cx, y + py - cy, 1.0f, paint); } }
/** * Creates aJava.Util.Random vector that is inside the specified span. * * @param span The span theJava.Util.Random vector belongs in. * @param rand RNG * @return AJava.Util.Random vector within the specified span. */ public static DMatrixRMaj insideSpan(DMatrixRMaj[] span, double min, double max, Java.Util.Random rand) { DMatrixRMaj A = new DMatrixRMaj(span.Count(), 1); DMatrixRMaj B = new DMatrixRMaj(span[0].NumElements, 1); for (int i = 0; i < span.Count(); i++) { B.setTo(span[i]); double val = rand.NextDouble() * (max - min) + min; CommonOps_DDRM.scale(val, B); CommonOps_DDRM.add(A, B, A); } return(A); }
/** * Creates aJava.Util.Random symmetric positive definite matrix. * * @param width The width of the square matrix it returns. * @param randJava.Util.Random number generator used to make the matrix. * @return TheJava.Util.Random symmetric positive definite matrix. */ public static DMatrixRMaj symmetricPosDef(int width, Java.Util.Random rand) { // This is not formally proven to work. It just seems to work. DMatrixRMaj a = new DMatrixRMaj(width, 1); DMatrixRMaj b = new DMatrixRMaj(width, width); for (int i = 0; i < width; i++) { a.set(i, 0, rand.NextDouble()); } CommonOps_DDRM.multTransB(a, a, b); for (int i = 0; i < width; i++) { b.add(i, i, 1); } return(b); }
/** * Creates aJava.Util.Random matrix where all elements are zero but diagonal elements. Diagonal elements * randomly drawn from a uniform distribution from min to max, inclusive. * * @param numRows Number of rows in the returned matrix.. * @param numCols Number of columns in the returned matrix. * @param min Minimum value of a diagonal element. * @param max Maximum value of a diagonal element. * @param randJava.Util.Random number generator. * @return AJava.Util.Random diagonal matrix. */ public static DMatrixRMaj diagonal(int numRows, int numCols, double min, double max, Java.Util.Random rand) { if (max < min) { throw new ArgumentException("The max must be >= the min"); } DMatrixRMaj ret = new DMatrixRMaj(numRows, numCols); int N = Math.Min(numRows, numCols); double r = max - min; for (int i = 0; i < N; i++) { ret.set(i, i, rand.NextDouble() * r + min); } return(ret); }
/** * Sets the provided square matrix to be aJava.Util.Random symmetric matrix whose values are selected from an uniform distribution * from min to max, inclusive. * * @param A The matrix that is to be modified. Must be square. Modified. * @param min Minimum value an element can have. * @param max Maximum value an element can have. * @param randJava.Util.Random number generator. */ public static void symmetric(DMatrixRMaj A, double min, double max, Java.Util.Random rand) { if (A.numRows != A.numCols) { throw new ArgumentException("A must be a square matrix"); } double range = max - min; int length = A.numRows; for (int i = 0; i < length; i++) { for (int j = i; j < length; j++) { double val = rand.NextDouble() * range + min; A.set(i, j, val); A.set(j, i, val); } } }
/** * Creates a lower triangular matrix whose values are selected from a uniform distribution. If hessenberg * is greater than zero then a hessenberg matrix of the specified degree is created instead. * * @param dimen Number of rows and columns in the matrix.. * @param hessenberg 0 for triangular matrix and > 0 for hessenberg matrix. * @param min minimum value an element can be. * @param max maximum value an element can be. * @param randJava.Util.Random number generator used. * @return The randomly generated matrix. */ public static DMatrixRMaj triangularLower(int dimen, int hessenberg, double min, double max, Java.Util.Random rand) { if (hessenberg < 0) { throw new SystemException("hessenberg must be more than or equal to 0"); } double range = max - min; DMatrixRMaj A = new DMatrixRMaj(dimen, dimen); for (int i = 0; i < dimen; i++) { int end = Math.Min(dimen, i + hessenberg + 1); for (int j = 0; j < end; j++) { A.set(i, j, rand.NextDouble() * range + min); } } return(A); }
/** * Creates an upper triangular matrix whose values are selected from a uniform distribution. If hessenberg * is greater than zero then a hessenberg matrix of the specified degree is created instead. * * @param dimen Number of rows and columns in the matrix.. * @param hessenberg 0 for triangular matrix and > 0 for hessenberg matrix. * @param min minimum value an element can be. * @param max maximum value an element can be. * @param randJava.Util.Random number generator used. * @return The randomly generated matrix. */ public static DMatrixRMaj triangularUpper(int dimen, int hessenberg, double min, double max, Java.Util.Random rand) { if (hessenberg < 0) { throw new SystemException("hessenberg must be more than or equal to 0"); } double range = max - min; DMatrixRMaj A = new DMatrixRMaj(dimen, dimen); for (int i = 0; i < dimen; i++) { int start = i <= hessenberg ? 0 : i - hessenberg; for (int j = start; j < dimen; j++) { A.set(i, j, rand.NextDouble() * range + min); } } return(A); }