1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.optim.nonlinear.vector.leastsquares;
23
24 import java.util.ArrayList;
25
26 import org.hipparchus.analysis.MultivariateMatrixFunction;
27 import org.hipparchus.analysis.MultivariateVectorFunction;
28 import org.hipparchus.geometry.euclidean.twod.Vector2D;
29
30
31
32
33 class CircleVectorial {
34 private ArrayList<Vector2D> points;
35
36 public CircleVectorial() {
37 points = new ArrayList<Vector2D>();
38 }
39
40 public void addPoint(double px, double py) {
41 points.add(new Vector2D(px, py));
42 }
43
44 public int getN() {
45 return points.size();
46 }
47
48 public double getRadius(Vector2D center) {
49 double r = 0;
50 for (Vector2D point : points) {
51 r += point.distance(center);
52 }
53 return r / points.size();
54 }
55
56 public MultivariateVectorFunction getModelFunction() {
57 return new MultivariateVectorFunction() {
58 public double[] value(double[] params) {
59 Vector2D center = new Vector2D(params[0], params[1]);
60 double radius = getRadius(center);
61 double[] residuals = new double[points.size()];
62 for (int i = 0; i < residuals.length; i++) {
63 residuals[i] = points.get(i).distance(center) - radius;
64 }
65
66 return residuals;
67 }
68 };
69 }
70
71 public MultivariateMatrixFunction getModelFunctionJacobian() {
72 return new MultivariateMatrixFunction() {
73 public double[][] value(double[] params) {
74 final int n = points.size();
75 final Vector2D center = new Vector2D(params[0], params[1]);
76
77 double dRdX = 0;
78 double dRdY = 0;
79 for (Vector2D pk : points) {
80 double dk = pk.distance(center);
81 dRdX += (center.getX() - pk.getX()) / dk;
82 dRdY += (center.getY() - pk.getY()) / dk;
83 }
84 dRdX /= n;
85 dRdY /= n;
86
87
88 double[][] jacobian = new double[n][2];
89 for (int i = 0; i < n; i++) {
90 final Vector2D pi = points.get(i);
91 final double di = pi.distance(center);
92 jacobian[i][0] = (center.getX() - pi.getX()) / di - dRdX;
93 jacobian[i][1] = (center.getY() - pi.getY()) / di - dRdY;
94 }
95
96 return jacobian;
97 }
98 };
99 }
100 }