forked from Apress/numerical-methods-using-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter4.java
More file actions
180 lines (157 loc) · 6.73 KB
/
Chapter4.java
File metadata and controls
180 lines (157 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (c) NM LTD.
* https://nm.dev/
*
* THIS SOFTWARE IS LICENSED, NOT SOLD.
*
* YOU MAY USE THIS SOFTWARE ONLY AS DESCRIBED IN THE LICENSE.
* IF YOU ARE NOT AWARE OF AND/OR DO NOT AGREE TO THE TERMS OF THE LICENSE,
* DO NOT USE THIS SOFTWARE.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITH NO WARRANTY WHATSOEVER,
* EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
* ANY WARRANTIES OF ACCURACY, ACCESSIBILITY, COMPLETENESS,
* FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, NON-INFRINGEMENT,
* TITLE AND USEFULNESS.
*
* IN NO EVENT AND UNDER NO LEGAL THEORY,
* WHETHER IN ACTION, CONTRACT, NEGLIGENCE, TORT, OR OTHERWISE,
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIMS, DAMAGES OR OTHER LIABILITIES,
* ARISING AS A RESULT OF USING OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.nm.nmj;
import dev.nm.algebra.linear.vector.doubles.Vector;
import dev.nm.algebra.linear.vector.doubles.dense.DenseVector;
import dev.nm.analysis.function.rn2r1.AbstractBivariateRealFunction;
import dev.nm.analysis.function.rn2r1.AbstractRealScalarFunction;
import dev.nm.analysis.function.rn2r1.AbstractTrivariateRealFunction;
import dev.nm.analysis.function.rn2r1.BivariateRealFunction;
import dev.nm.analysis.function.rn2r1.RealScalarFunction;
import dev.nm.analysis.function.rn2r1.TrivariateRealFunction;
import dev.nm.analysis.function.rn2rm.AbstractRealVectorFunction;
import dev.nm.analysis.function.rn2rm.RealVectorFunction;
import dev.nm.analysis.root.multivariate.NewtonSystemRoot;
import dev.nm.analysis.root.univariate.NoRootFoundException;
import java.util.Arrays;
/**
* Numerical Methods Using Java: For Data Science, Analysis, and Engineering
*
* @author haksunli
* @see
* https://www.amazon.com/Numerical-Methods-Using-Java-Engineering/dp/1484267966
* https://nm.dev/
*/
public class Chapter4 {
public static void main(String[] args) throws Exception {
System.out.println("Chapter 4 demos");
Chapter4 chapter4 = new Chapter4();
chapter4.define_multivariate_functions();
chapter4.define_multivariate_vector_function();
chapter4.solve_system_of_two_equations();
chapter4.solve_system_of_equations();
}
public void define_multivariate_functions() {
System.out.println("define multivariate functions");
RealScalarFunction f1 = new AbstractRealScalarFunction(3) {
@Override
public Double evaluate(Vector x) {
double x1 = x.get(1);
double x2 = x.get(2);
double x3 = x.get(3);
return 2 * x1 * x1 + x2 * x2 - x3;
}
};
System.out.println("f1(1,2,3) = " + f1.evaluate(new DenseVector(1, 2, 3)));
TrivariateRealFunction f2 = new AbstractTrivariateRealFunction() {
@Override
public double evaluate(double x1, double x2, double x3) {
return 2 * x1 * x1 + x2 * x2 - x3;
}
};
System.out.println("f2(1,2,3) = " + f2.evaluate(new DenseVector(1, 2, 3)));
}
public void define_multivariate_vector_function() {
System.out.println("define multivariate vector function");
TrivariateRealFunction f1 = new AbstractTrivariateRealFunction() {
@Override
public double evaluate(double x, double y, double z) {
return Math.pow(x, 2) + Math.pow(y, 3) - z - 6;
}
};
TrivariateRealFunction f2 = new AbstractTrivariateRealFunction() {
@Override
public double evaluate(double x, double y, double z) {
return 2 * x + 9 * y - z - 17;
}
};
TrivariateRealFunction f3 = new AbstractTrivariateRealFunction() {
@Override
public double evaluate(double x, double y, double z) {
return Math.pow(x, 4) + 5 * y + 6 * z - 29;
}
};
RealScalarFunction[] F = new TrivariateRealFunction[]{f1, f2, f3};
Vector x = new DenseVector(1.5, 2.5, 3.5);
double f1_x = F[0].evaluate(x);
double f2_x = F[1].evaluate(x);
double f3_x = F[2].evaluate(x);
double[] F_x = new double[]{f1_x, f2_x, f3_x};
System.out.println("F(x) = " + Arrays.toString(F_x));
RealVectorFunction G = new AbstractRealVectorFunction(3, 3) {
@Override
public Vector evaluate(Vector v) {
double x = v.get(1);
double y = v.get(2);
double z = v.get(3);
double g1 = Math.pow(x, 2) + Math.pow(y, 3) - z - 6;
double g2 = 2 * x + 9 * y - z - 17;
double g3 = Math.pow(x, 4) + 5 * y + 6 * z - 29;
Vector g = new DenseVector(g1, g2, g3);
return g;
}
};
Vector Gx = G.evaluate(x);
System.out.println("G(x) = " + Gx);
}
public void solve_system_of_two_equations() throws NoRootFoundException {
System.out.println("solve a system of two equations");
BivariateRealFunction f1 = new AbstractBivariateRealFunction() {
@Override
public double evaluate(double x, double y) {
return 3 * x + y * y - 12;
}
};
BivariateRealFunction f2 = new AbstractBivariateRealFunction() {
@Override
public double evaluate(double x, double y) {
return x * x + y - 4;
}
};
BivariateRealFunction[] F = new BivariateRealFunction[]{f1, f2};
NewtonSystemRoot solver = new NewtonSystemRoot(1e-8, 10);
Vector initial = new DenseVector(new double[]{0, 0}); // (0, 0)
Vector root = solver.solve(F, initial);
System.out.println(String.format("f(%s) = (%f, %f)", root.toString(), f1.evaluate(root), f2.evaluate(root)));
}
public void solve_system_of_equations() throws NoRootFoundException {
System.out.println("solve a system of equations");
RealVectorFunction G = new AbstractRealVectorFunction(3, 3) {
@Override
public Vector evaluate(Vector v) {
double x = v.get(1);
double y = v.get(2);
double z = v.get(3);
double g1 = Math.pow(x, 2) + Math.pow(y, 3) - z - 6;
double g2 = 2 * x + 9 * y - z - 17;
double g3 = Math.pow(x, 4) + 5 * y + 6 * z - 29;
Vector g = new DenseVector(g1, g2, g3);
return g;
}
};
NewtonSystemRoot solver = new NewtonSystemRoot(1e-8, 15);
Vector initial = new DenseVector(new double[]{0, 0, 0}); // (0, 0, 0)
Vector root = solver.solve(G, initial);
System.out.println(String.format("f(%s) = %s", root.toString(), G.evaluate(root).toString()));
}
}