UnitSphereRandomVectorGenerator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.random;

  22. import org.hipparchus.util.FastMath;


  23. /**
  24.  * Generate random vectors isotropically located on the surface of a sphere.
  25.  */
  26. public class UnitSphereRandomVectorGenerator
  27.     implements RandomVectorGenerator {

  28.     /** RNG used for generating the individual components of the vectors. */
  29.     private final RandomGenerator rand;
  30.     /** Space dimension. */
  31.     private final int dimension;

  32.     /** Simple constructor.
  33.      * @param dimension Space dimension.
  34.      * @param rand RNG for the individual components of the vectors.
  35.      */
  36.     public UnitSphereRandomVectorGenerator(final int dimension,
  37.                                            final RandomGenerator rand) {
  38.         this.dimension = dimension;
  39.         this.rand = rand;
  40.     }

  41.     /**
  42.      * Create an object that will use a default RNG ({@link MersenneTwister}),
  43.      * in order to generate the individual components.
  44.      *
  45.      * @param dimension Space dimension.
  46.      */
  47.     public UnitSphereRandomVectorGenerator(final int dimension) {
  48.         this(dimension, new MersenneTwister());
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public double[] nextVector() {
  53.         final double[] v = new double[dimension];

  54.         // See http://mathworld.wolfram.com/SpherePointPicking.html for example.
  55.         // Pick a point by choosing a standard Gaussian for each element, and then
  56.         // normalizing to unit length.
  57.         double normSq = 0;
  58.         for (int i = 0; i < dimension; i++) {
  59.             final double comp = rand.nextGaussian();
  60.             v[i] = comp;
  61.             normSq += comp * comp;
  62.         }

  63.         final double f = 1 / FastMath.sqrt(normSq);
  64.         for (int i = 0; i < dimension; i++) {
  65.             v[i] *= f;
  66.         }

  67.         return v;
  68.     }
  69. }