# Random generation of vectors with constraints

Hi all,

I need to generate random vectors with some constraints.
I need to create 2 set of random (latitude, longitude) vectors with the following constraints:

• a first set of vectors such as

latitude0 - alpha <= latitude <= latitude0 + alpha
longitude0 - n * beta <= longitude <= longitude0 + n * beta

• a second set of vectors such as

latitude0 - m * alpha <= latitude <= latitude0 + m* alpha
longitude0 - beta <= longitude <= longitude0 + beta

Moreover the best would be also to switch from one set to the other regularly …
I look at Random Data Generation | Hipparchus::Core
but as I am not familiar with such random generations, I tried the example but I don’t know how to answer my problem …

My main problem is to construct the covariance matrix …

For the record: my need is related to Digital Elevation Model (DEM) for 4 contiguous tiles of DEM, where I need to test the increase in computation time when using the new feature solving the issue in Rugged library.

Thanks

I tried the following that seems to solved my problem (with the Hipparchus example) such as:

``````    double[] mean = {0, 0};
double pixelSizeLat = 2;  (alpha)
double sizeLat = 1;
// The size of an interval is inside +/- 3.5 sigma for a normal distribution (99.7%)
double stdDevLat = (pixelSizeLat*sizeLat)/3.5;

double pixelSizeLon = 2; (beta)
double sizeLon = 5;  (n)
double stdDevLon = (pixelSizeLon*sizeLon)/3.5;

double desiredCov = stdDevLat * stdDevLon * 0.5;
double[][] covAlongParallel = {{stdDevLat*stdDevLat, desiredCov},
{desiredCov, stdDevLon*stdDevLon}};
RealMatrix covariance = MatrixUtils.createRealMatrix(covAlongParallel);

// Create a CorrelatedRandomVectorGenerator using rawGenerator for the components
CorrelatedRandomVectorGenerator generator =
new CorrelatedRandomVectorGenerator(mean, covariance,
1.0e-12 * covariance.getNorm1(),
rawGenerator);
``````

(to be noticed in the example: the method covariance.getNorm() does not exist anymore)

While computing:

``````   // Use the generator to generate correlated vectors
double[] randomVector;
for (int i=0; i < 10000; i++) randomVector  = generator.nextVector();
``````

I found only around 10 vectors out of bound … so it is OK for my tests …

Your method seems correct to me. Be aware that it assumes longitude and latitude represent a flat area here, so it works only on small ranges. For larger ranges, or ranges that contain poles, a completely different method should be used, but this is another story.

1 Like