View Javadoc
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    *      http://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  package org.apache.commons.rng.sampling;
19  
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler;
22  import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
23  
24  /**
25   * Generate vectors <a href="http://mathworld.wolfram.com/SpherePointPicking.html">
26   * isotropically located on the surface of a sphere</a>.
27   *
28   * @since 1.1
29   */
30  public class UnitSphereSampler {
31      /** Sampler used for generating the individual components of the vectors. */
32      private final NormalizedGaussianSampler sampler;
33      /** Space dimension. */
34      private final int dimension;
35  
36      /**
37       * @param dimension Space dimension.
38       * @param rng Generator for the individual components of the vectors.
39       * A shallow copy will be stored in this instance.
40       */
41      public UnitSphereSampler(int dimension,
42                               UniformRandomProvider rng) {
43          if (dimension <= 0) {
44              throw new IllegalArgumentException("Dimension must be strictly positive");
45          }
46  
47          this.dimension = dimension;
48          sampler = new ZigguratNormalizedGaussianSampler(rng);
49      }
50  
51      /**
52       * @return a random normalized Cartesian vector.
53       */
54      public double[] nextVector() {
55          final double[] v = new double[dimension];
56  
57          // Pick a point by choosing a standard Gaussian for each element,
58          // and then normalize to unit length.
59          double normSq = 0;
60          for (int i = 0; i < dimension; i++) {
61              final double comp = sampler.sample();
62              v[i] = comp;
63              normSq += comp * comp;
64          }
65  
66          if (normSq == 0) {
67              // Zero-norm vector is discarded.
68              // Using recursion as it is highly unlikely to generate more
69              // than a few such vectors. It also protects against infinite
70              // loop (in case a buggy generator is used), by eventually
71              // raising a "StackOverflowError".
72              return nextVector();
73          }
74  
75          final double f = 1 / Math.sqrt(normSq);
76          for (int i = 0; i < dimension; i++) {
77              v[i] *= f;
78          }
79  
80          return v;
81      }
82  }