Je vous présente ci-dessous une petite librairie d'analyse numérique, comprenant les domaines suivants :
- Recherche de racines
- Résolution de sysètmes linéaires
- Interpolation
- Intégration
Le code se veut souple et adaptable, je poste d'abord les sources, les examples d'utilisations suivront, au cas où la javadoc ne suffirait pas.
Voici d'abord quelques classes d'ordre général utilisées en analyse et dans les domaines présentés ci-dessus :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /* * Function.java * * Created on 28 novembre 2006, 10:25 * */ package org.umh.math.analysis; /** * A function is represented by its evaluation method. * @author Absil Romain */ public interface Function { /** * Evaluate the function on the given argument. * @return the evaluation of the function on the given argument. * @param x the number you want the function to be evaluated. */ public double eval(double x); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * Function2D.java * * Created on 16 janvier 2007, 13:42 * */ package org.umh.math.analysis; import java.awt.geom.Point2D; /** * This class models functions from R to R² wich are represented by * their evaluation. * @author Absil Romain */ public class Function2D { private Function fy; private Function fx; /** * Construcs a new Function2D with the functions defining its evaluation. * @param fx the function giving the x coordinate of the evaluations. * @param fy the function giving the y coordinate of the evaluations. **/ public Function2D(Function fx, Function fy) { this.fx = fx; this.fy = fy; } /** * Returns the function giving the y coordinate of the evaluations. * @return the function the y coordinate of the evaluations. **/ public Function getFy() { return fy; } /** * Sets the function giving the y coordinate of the evaluations. * @param fy the function to set. **/ public void setFy(Function fy) { this.fy = fy; } /** * Returns the function giving the x coordinate of the evaluations. * @return the function giving the x coordinate of the evaluations. **/ public Function getFx() { return fx; } /** * Sets the function giving the x coordinate of the evaluations. * @param fx the function to set. **/ public void setFx(Function fx) { this.fx = fx; } /** * Evaluates the function to the given parameter. * @param t the parameter you want the function to be evaluated on. * @return the evaluation of the function to the given parameter. **/ public Point2D.Double eval(double t) { return new Point2D.Double(fx.eval(t),fy.eval(t)); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * Function_1_2.java * * Created on 22 mars 2007, 15:55 * */ package org.umh.math.analysis; import java.awt.geom.Point2D; /** * This class models functions from R² to R. * @author Absil Romain */ public interface Function_2_1 { /** * Evaluates the function to the given parameters x and y. * @param x the first number you want the function to be evaluated on. * @param y the second number you want the function to be evaluated on. * @return the function evaluated to the point (x,y). */ public double eval(double x, double y); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326 /* * Polynome.java * * Created on 22 mars 2007, 16:26 * */ package org.umh.math.analysis; /** * A polynome is a function on wich some other operations are defined. * @author Absil Romain */ public class Polynome implements Function { private int deg; private double[] coffs; /** * Constructs a null polynome of degree n. * @param deg the degree of the polynome. **/ public Polynome(int deg) { if(deg < 0) throw new IllegalArgumentException(); this.deg = deg; this.coffs = new double[deg+1]; } /** * Construcs a new polynome wich the coefficients are given in their * natural order. * <br>e.g. the call <code>new Polynome(1,2,3);</code> will creates the * polynome x² + 2x + 3. * @param coffs the coefficients of the polynome. **/ public Polynome(double ... coffs) { this.deg = coffs.length - 1; this.coffs = new double[coffs.length]; for (int i = 0; i < coffs.length; i++) this.coffs[i] = coffs[coffs.length - 1 - i]; trim(); } /** * Returns the degree of the polynome. * @return the degree of the polynome. **/ public int getDeg() { return deg; } /** * Returns all the coefficients of the polynome. The coefficient indexed * by the integer i is the coefficient of degree i. * @return all the coefficients of the polynome. The coefficient indexed * by the integer i is the coefficient of degree i. **/ public double[] getCoffs() { return coffs; } /** * Returns the coefficient of the given degree. * @param deg the degree of the coefficient you want to return. * @return the coefficient of the given degree. **/ public double getCof(int deg) { return coffs[deg]; } /** * Sets the coefficient of the given degree to the given value. * @param deg the degree of the coefficient yoy want to set. * @param value the coefficient to set. **/ public void setCof(int deg, double value) { coffs[deg] = value; } /** * Sets all the coefficients of the current polynome by other coefficients. * @param coffs the coefficients to set. **/ public void setCoffs(double[] coffs) { this.coffs = coffs; } /** * Returns the String representation of this polynome. * @return the String representation of this polynome. **/ public String toString() { if(deg == 0 ) return "" + coffs[0]; String s = ""; for(int i = deg ; i >= 0 ; i--) { if(i != deg) { if(getCof(i) > 0 && i != 1 && i != 0) s += " + " + getCof(i) + " x^" + i; else if(getCof(i) < 0 && i != 3 && i != 2 && i != 1 && i != 0) s += " - " + Math.abs(getCof(i)) + " x^" + i; else if(getCof(i) > 0 && i == 1) s += " + " + getCof(i) + " x"; else if(getCof(i) > 0 && i == 0) s += " + " + getCof(i); else if(getCof(i) < 0 && i == 1) s += " - " + Math.abs(getCof(i)) + " x"; else if(getCof(i) < 0 && i == 0) s += " - " + Math.abs(getCof(i)); } else if(i == deg) { if(getCof(i) > 0 && i > 1) s += getCof(i) + " x^" + i; else if(getCof(i) < 0 && i > 3) s += Math.abs(getCof(i)) + " x^" + i; else if(getCof(i) > 0 && i == 1) s += getCof(i) + " x"; else if(getCof(i) > 0 && i == 0) s += getCof(i); else if(getCof(i) < 0 && i == 1) s += Math.abs(getCof(i)) + " x"; else if(getCof(i) < 0 && i == 0) s += Math.abs(getCof(i)); else if(getCof(i) == 0) s += ""; } } return s; } /** * Returns true if the given Object is equal to the current polynome, * returns false otherwise. * @return true if the given Object is equal to the current polynome, * returns false otherwise. * @param other the bject you want to know if it is equal to the current Polynome */ public boolean equals(Object other) { if(!(other instanceof Polynome)) return false; Polynome pol = (Polynome)other; pol.trim(); if(pol.getDeg() != deg) return false; for (int i = 0; i < coffs.length; i++) if(coffs[i] != pol.getCof(i)) return false; return true; } /** * Returns a copy of the current polynome. * @return a copy of the current polynome. **/ public Polynome clone() { return new Polynome(coffs); } /** * Deletes all the first null coefficiets and update the degree of the * polynome. **/ public void trim() { int i = deg; for(; i >= 0 ; i--) { if(coffs[i] != 0) break; } if(i != deg) { double[] coffTmp = new double[i+1]; for(int j = 0 ; j <= i ; j++) coffTmp[j] = coffs[j]; deg = i; coffs = coffTmp; } } /** * Returns the image of the polynome evaluated in x. * @param x the value you want to know the image. * @return the image of the polynome evaluated in x. **/ public double eval(double x) { double img = 0; for (int i = deg; i >= 0; i--) img = coffs[i] + x * img; return img; } /** * Adds a polynome to the current polynome. * @param polynome the polynome to add. * @return the result of the addition. **/ public Polynome add(Polynome polynome) { int newDeg = Math.max(deg,polynome.getDeg()); //sets the degree of the 2 polynomes to add Polynome thisTmp = new Polynome(newDeg); for(int i = 0 ; i <= deg ; i++) thisTmp.setCof(i,getCof(i)); Polynome polTmp = new Polynome(newDeg); for(int i = 0 ; i <= polynome.getDeg() ; i++) polTmp.setCof(i,polynome.getCof(i)); //adds the polynomes for(int i = 0 ; i <= newDeg ; i++) thisTmp.setCof(i,thisTmp.getCof(i) + polTmp.getCof(i)); thisTmp.trim(); return thisTmp; } /** * Returns the opposite of the current polynome for the additive law. * @return the opposite of the current polynome for additive law. */ public Polynome getOpposite() { double[] newcofs = new double[coffs.length]; for (int i = 0; i < newcofs.length; i++) newcofs[i] = - coffs[i]; return new Polynome(newcofs); } /** * Substracts a polynome to the current polynome. * @param polynome the polynome to substract. * @return the result of the substraction. **/ public Polynome substract(Polynome polynome) { int newDeg = Math.max(deg,polynome.getDeg()); //sets the degree of the 2 polynomes to add Polynome thisTmp = new Polynome(newDeg); for(int i = 0 ; i <= deg ; i++) thisTmp.setCof(i,getCof(i)); Polynome polTmp = new Polynome(newDeg); for(int i = 0 ; i <= polynome.getDeg() ; i++) polTmp.setCof(i,polynome.getCof(i)); //adds the polynomes for(int i = 0 ; i <= newDeg ; i++) thisTmp.setCof(i,thisTmp.getCof(i) - polTmp.getCof(i)); thisTmp.trim(); return thisTmp; } /** * Multiply the current polynome by a scalar. * @param scalar the scalar wich multiplies the polynome. * @return the resutl of the multiplication. **/ public Polynome multByScal(double scalar) { if(scalar == 0) return new Polynome(0); else if(scalar == 1) return clone(); else { Polynome thisTmp = clone(); for(int i = 0 ; i <= deg ; i ++) thisTmp.setCof(i,getCof(i) * scalar); return thisTmp; } } /** * Multiplies a polynome to the current polynome * @param polynome the polynome to multiply. * @return the result of the multiplication. **/ public Polynome multiply(Polynome polynome) { Polynome pol = polynome.clone(); int newDeg = deg + pol.getDeg(); Polynome thisTmp = new Polynome(newDeg); for(int i = 0 ; i <= deg ; i++) thisTmp.setCof(i, getCof(i)); Polynome polTmp = new Polynome(newDeg); for(int i = 0 ; i <= pol.getDeg() ; i++) polTmp.setCof(i,pol.getCof(i)); int count = newDeg; while(count >= 0) { double sum = 0; for(int i = 0 ; i <= newDeg ; i++) for(int j = 0 ; j <= newDeg ; j++) if(i + j == count) sum += thisTmp.getCof(i) * polTmp.getCof(j); thisTmp.setCof(count,sum); count--; } return thisTmp; } }
La classe Vector sera fournie dans les post suivants.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * RNFunction.java * * Created on 28 novembre 2006, 10:23 * */ package org.umh.math.analysis; import org.umh.math.linear.Vector; /** * A function is represented by its evaluation. * @author Absil Romain */ public interface RNFunction { /** * Evaluate the function on the given argument. * @return the evaluation of the function on the given argument. * @param v the vector you want to function to be evaluated on. */ public Vector eval(Vector v); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /* * Sequence.java * * Created on 30 novembre 2006, 9:38 * */ package org.umh.math.analysis; /** * A seuquence if a function from N to R with is represented by its evaluation. * @author Absil Romain */ public interface Sequence { /** * Returns the sequence evaluated on the natural n. * @param n the natural you want the sequence to be evaluated on. * @return the sequence evaluated on the natural n. */ public double eval(int n); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * Sequence2D.java * * Created on 30 novembre 2006, 9:38 * */ package org.umh.math.analysis; import java.awt.geom.Point2D; /** * This clas models sequences from N to R² wich are rerpesented by * their evaluations. * @author Absil Romain */ public class Sequence2D { private Sequence fx; private Sequence fy; /** * Construcs a new Sequence2D with the sequences defining its evaluation. * @param fx the sequence giving the x coordinate of the evaluations. * @param fy the sequence giving the y coordinate of the evaluations. **/ public Sequence2D(Sequence fx, Sequence fy) { this.setFx(fx); this.setFy(fy); } /** * Evaluates the sequence to the given parameter. * @param t the parameter you want the sequence to be evaluated on. * @return the evaluation of the sequence to the given parameter. **/ public Point2D.Double eval(int t) { return new Point2D.Double(getFx().eval(t), getFy().eval(t)); } /** * Returns the sequence giving the x coordinate of the evaluations. * @return the sequence giving the x coordinate of the evaluations. **/ public Sequence getFx() { return fx; } /** * Sets the sequence giving the x coordinate of the evaluations. * @param fx the sequence to set. **/ public void setFx(Sequence fx) { this.fx = fx; } /** * Returns the sequence giving the y coordinate of the evaluations. * @return the sequence the y coordinate of the evaluations. **/ public Sequence getFy() { return fy; } /** * Sets the sequence giving the y coordinate of the evaluations. * @param fy the sequence to set. **/ public void setFy(Sequence fy) { this.fy = fy; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * Sequence_1_2.java * * Created on 22 mars 2007, 15:56 * */ package org.umh.math.analysis; import java.awt.geom.Point2D; /** * This class models sequences from N² to R. * @author Absil Romain */ public interface Sequence_2_1 { /** * Evaluates the sequence to the given parameters n and m. * @param n the first number you want the function to be evaluated on. * @param m the second number you want the function to be evaluated on. * @return the sequence evaluated on the given point (n, m). */ public double eval(int n, int m); }
Voilà pour les classes d'ordre général ne faisant partie d'aucun des domaines ci-dessus mais utilisés un peu partout
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * ParametricFunction.java * * Created on 22 décembre 2006, 13:42 * */ package org.umh.math.analysis; import org.umh.math.linear.Vector; /** * This class models vectorial functions wich are functions from R to Rn. * @author Absil Romain */ public class VectorialFunction { private Function[] functions; /** * Construc a new vectorial, wich form is * f : R -> Rn : t -> f(t) = ( f1(t) , ... , fn(t) ). * The functions f1, ... , fn are function from R to R wich give the * function evaluated in t. * <p>e.g. to construct the function f(t) = (t, 1/t, t³) call * @param functions the functions f1, ... , fn wich give the function * evaluated in t. **/ public VectorialFunction(Function ... functions) { this.functions = functions; } /** * Returns the function evaluated in the real t. * @return the function evaluated in the real t. * @param t the number you want the function to be evaluated on. */ public Vector eval(double t) { Vector v = new Vector(functions.length); for (int i = 0; i < functions.length; i++) v.setComp(i,functions[i].eval(t)); return v; } }
Partager