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.math3.optim.nonlinear.vector.jacobian;
19 
20 import java.awt.geom.Point2D;
21 
22 import org.apache.commons.math3.random.RandomGenerator;
23 import org.apache.commons.math3.random.Well44497b;
24 import org.apache.commons.math3.distribution.RealDistribution;
25 import org.apache.commons.math3.distribution.UniformRealDistribution;
26 import org.apache.commons.math3.distribution.NormalDistribution;
27 
28 /**
29  * Factory for generating a cloud of points that approximate a straight line.
30  */
31 @Deprecated
32 public class RandomStraightLinePointGenerator {
33     /** Slope. */
34     private final double slope;
35     /** Intercept. */
36     private final double intercept;
37     /** RNG for the x-coordinate. */
38     private final RealDistribution x;
39     /** RNG for the error on the y-coordinate. */
40     private final RealDistribution error;
41 
42     /**
43      * The generator will create a cloud of points whose x-coordinates
44      * will be randomly sampled between {@code xLo} and {@code xHi}, and
45      * the corresponding y-coordinates will be computed as
46      * <pre><code>
47      *  y = a x + b + N(0, error)
48      * </code></pre>
49      * where {@code N(mean, sigma)} is a Gaussian distribution with the
50      * given mean and standard deviation.
51      *
52      * @param a Slope.
53      * @param b Intercept.
54      * @param sigma Standard deviation on the y-coordinate of the point.
55      * @param lo Lowest value of the x-coordinate.
56      * @param hi Highest value of the x-coordinate.
57      * @param seed RNG seed.
58      */
RandomStraightLinePointGenerator(double a, double b, double sigma, double lo, double hi, long seed)59     public RandomStraightLinePointGenerator(double a,
60                                             double b,
61                                             double sigma,
62                                             double lo,
63                                             double hi,
64                                             long seed) {
65         final RandomGenerator rng = new Well44497b(seed);
66         slope = a;
67         intercept = b;
68         error = new NormalDistribution(rng, 0, sigma,
69                                        NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
70         x = new UniformRealDistribution(rng, lo, hi,
71                                         UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
72     }
73 
74     /**
75      * Point generator.
76      *
77      * @param n Number of points to create.
78      * @return the cloud of {@code n} points.
79      */
generate(int n)80     public Point2D.Double[] generate(int n) {
81         final Point2D.Double[] cloud = new Point2D.Double[n];
82         for (int i = 0; i < n; i++) {
83             cloud[i] = create();
84         }
85         return cloud;
86     }
87 
88     /**
89      * Create one point.
90      *
91      * @return a point.
92      */
create()93     private Point2D.Double create() {
94         final double abscissa = x.sample();
95         final double yModel = slope * abscissa + intercept;
96         final double ordinate = yModel + error.sample();
97 
98         return new Point2D.Double(abscissa, ordinate);
99     }
100 }
101