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 package org.apache.commons.math3.optim.nonlinear.vector.jacobian;
18 
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 
24 import org.apache.commons.math3.util.FastMath;
25 
26 /**
27  * A factory to create instances of {@link StatisticalReferenceDataset} from
28  * available resources.
29  */
30 @Deprecated
31 public class StatisticalReferenceDatasetFactory {
32 
StatisticalReferenceDatasetFactory()33     private StatisticalReferenceDatasetFactory() {
34         // Do nothing
35     }
36 
37     /**
38      * Creates a new buffered reader from the specified resource name.
39      *
40      * @param name the name of the resource
41      * @return a buffered reader
42      * @throws IOException if an I/O error occurred
43      */
createBufferedReaderFromResource(final String name)44     public static BufferedReader createBufferedReaderFromResource(final String name)
45         throws IOException {
46         final InputStream resourceAsStream;
47         resourceAsStream = StatisticalReferenceDatasetFactory.class
48             .getResourceAsStream(name);
49         if (resourceAsStream == null) {
50             throw new IOException("could not find resource " + name);
51         }
52         return new BufferedReader(new InputStreamReader(resourceAsStream));
53     }
54 
createKirby2()55     public static StatisticalReferenceDataset createKirby2()
56         throws IOException {
57         final BufferedReader in = createBufferedReaderFromResource("Kirby2.dat");
58         StatisticalReferenceDataset dataset = null;
59         try {
60             dataset = new StatisticalReferenceDataset(in) {
61 
62                 @Override
63                 public double getModelValue(final double x, final double[] a) {
64                     final double p = a[0] + x * (a[1] + x * a[2]);
65                     final double q = 1.0 + x * (a[3] + x * a[4]);
66                     return p / q;
67                 }
68 
69                 @Override
70                 public double[] getModelDerivatives(final double x,
71                                                     final double[] a) {
72                     final double[] dy = new double[5];
73                     final double p = a[0] + x * (a[1] + x * a[2]);
74                     final double q = 1.0 + x * (a[3] + x * a[4]);
75                     dy[0] = 1.0 / q;
76                     dy[1] = x / q;
77                     dy[2] = x * dy[1];
78                     dy[3] = -x * p / (q * q);
79                     dy[4] = x * dy[3];
80                     return dy;
81                 }
82             };
83         } finally {
84             in.close();
85         }
86         return dataset;
87     }
88 
createHahn1()89     public static StatisticalReferenceDataset createHahn1()
90         throws IOException {
91         final BufferedReader in = createBufferedReaderFromResource("Hahn1.dat");
92         StatisticalReferenceDataset dataset = null;
93         try {
94             dataset = new StatisticalReferenceDataset(in) {
95 
96                 @Override
97                 public double getModelValue(final double x, final double[] a) {
98                     final double p = a[0] + x * (a[1] + x * (a[2] + x * a[3]));
99                     final double q = 1.0 + x * (a[4] + x * (a[5] + x * a[6]));
100                     return p / q;
101                 }
102 
103                 @Override
104                 public double[] getModelDerivatives(final double x,
105                                                     final double[] a) {
106                     final double[] dy = new double[7];
107                     final double p = a[0] + x * (a[1] + x * (a[2] + x * a[3]));
108                     final double q = 1.0 + x * (a[4] + x * (a[5] + x * a[6]));
109                     dy[0] = 1.0 / q;
110                     dy[1] = x * dy[0];
111                     dy[2] = x * dy[1];
112                     dy[3] = x * dy[2];
113                     dy[4] = -x * p / (q * q);
114                     dy[5] = x * dy[4];
115                     dy[6] = x * dy[5];
116                     return dy;
117                 }
118             };
119         } finally {
120             in.close();
121         }
122         return dataset;
123     }
124 
createMGH17()125     public static StatisticalReferenceDataset createMGH17()
126         throws IOException {
127         final BufferedReader in = createBufferedReaderFromResource("MGH17.dat");
128         StatisticalReferenceDataset dataset = null;
129         try {
130             dataset = new StatisticalReferenceDataset(in) {
131 
132                 @Override
133                 public double getModelValue(final double x, final double[] a) {
134                     return a[0] + a[1] * FastMath.exp(-a[3] * x) + a[2] *
135                            FastMath.exp(-a[4] * x);
136                 }
137 
138                 @Override
139                 public double[] getModelDerivatives(final double x,
140                                                     final double[] a) {
141                     final double[] dy = new double[5];
142                     dy[0] = 1.0;
143                     dy[1] = FastMath.exp(-x * a[3]);
144                     dy[2] = FastMath.exp(-x * a[4]);
145                     dy[3] = -x * a[1] * dy[1];
146                     dy[4] = -x * a[2] * dy[2];
147                     return dy;
148                 }
149             };
150         } finally {
151             in.close();
152         }
153         return dataset;
154     }
155 
createLanczos1()156     public static StatisticalReferenceDataset createLanczos1()
157         throws IOException {
158         final BufferedReader in =
159             createBufferedReaderFromResource("Lanczos1.dat");
160         StatisticalReferenceDataset dataset = null;
161         try {
162             dataset = new StatisticalReferenceDataset(in) {
163 
164                 @Override
165                 public double getModelValue(final double x, final double[] a) {
166                     System.out.println(a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]);
167                     return a[0] * FastMath.exp(-a[3] * x) +
168                            a[1] * FastMath.exp(-a[4] * x) +
169                            a[2] * FastMath.exp(-a[5] * x);
170                 }
171 
172                 @Override
173                 public double[] getModelDerivatives(final double x,
174                     final double[] a) {
175                     final double[] dy = new double[6];
176                     dy[0] = FastMath.exp(-x * a[3]);
177                     dy[1] = FastMath.exp(-x * a[4]);
178                     dy[2] = FastMath.exp(-x * a[5]);
179                     dy[3] = -x * a[0] * dy[0];
180                     dy[4] = -x * a[1] * dy[1];
181                     dy[5] = -x * a[2] * dy[2];
182                     return dy;
183                 }
184             };
185         } finally {
186             in.close();
187         }
188         return dataset;
189     }
190 
191     /**
192      * Returns an array with all available reference datasets.
193      *
194      * @return the array of datasets
195      * @throws IOException if an I/O error occurs
196      */
createAll()197     public StatisticalReferenceDataset[] createAll()
198         throws IOException {
199         return new StatisticalReferenceDataset[] {
200             createKirby2(), createMGH17()
201         };
202     }
203 }
204