Salut à tous ,
J'ai besoin de convertir un code C++ en Java. Voici le code C++ :Il y a deux trois méthodes que j'ai enlevé par rapport au code de départ, car elle ne me servaient pas, par exemple usage().(Il faut enlever les appels à cette méthode dans ce code.)
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 #define MAXSTP 10000 #define TINY 1.e-30 void initialize(int argc, char *argv[], Point *point, int *N_crossing, int *n_default, double *PAS_TEMPS_AFFICHAGE); void affiche_parametres(Point point, int N_crossing); void odeint(Point &, double delta_x, double eps, double h1, double hmin); void rkqs(Point & y, double &x, double htry, double eps, Point yscal, double *hdid, double *hnext) ; void rkck(Point y, double h, Point & yout, Point & yerr) ; void odeint(Point &ystart, double delta_x, double eps, double h1, double hmin) { int nstp, i; double x, hnext, hdid, h; Point y, dydx, yscal; x = 0.; h = h1; if (h>delta_x) h=delta_x; y = ystart; for (nstp=1;nstp<=MAXSTP;nstp++) { //Take at most MAXSTP steps. dydx = y.derivee_t(); yscal = fabs(y) + h*fabs(dydx) + TINY; // Scaling used to monitor accuracy. This general-purpose choice can be modifed if need be. if ((x+h-delta_x)*(x+h) > 0.0) h=delta_x-x; // If stepsize can overshoot, decrease. rkqs(y,x,h,eps, yscal, &hdid,&hnext); if ((x-delta_x)*delta_x >= 0.0) //Are we done? { ystart = y; return; //Normal exit. } if (fabs(hnext) <= hmin) std::cout << "Step size too small in odeint" << std::endl; h=hnext; } std::cout << "Too many steps in routine odeint" << std::endl; } #define SAFETY 0.9 #define PGROW -0.2 #define PSHRNK -0.25 #define ERRCON 1.89e-4 //The value ERRCON equals (5/SAFETY) raised to the power (1/PGROW), see use below. void rkqs(Point &y, double & x, double htry, double eps, Point yscal, double *hdid, double *hnext) { double errmax,h,htemp; Point ytemp,yerr; h=htry; // Set stepsize to the initial trial value. for (;;) { rkck(y,h,ytemp,yerr); // Take a step. errmax=0.0; Evaluate accuracy. errmax = fabs(yerr/yscal).Max() / eps; // Scale relative to required tolerance. if (errmax <= 1.0) break; // Step succeeded. Compute size of next step. htemp=SAFETY*h*pow(errmax,PSHRNK); // Truncation error too large, reduce stepsize. //h=(h >= 0.0 ? FMAX(htemp,0.1*h) : FMIN(htemp,0.1*h)); // No more than a factor of 10. h = htemp; if ((h>=0.) && (htemp < 0.1*h)) h = 0.1*h; if ((h<0.) && (htemp > 0.1*h)) h = 0.1*h; } if (errmax > ERRCON) *hnext=SAFETY*h*pow(errmax,PGROW); else *hnext=5.0*h; //No more than a factor of 5 increase. x += h; y = ytemp; } void rkck(Point y, double h, Point & yout, Point & yerr) { static double // a2=0.2,a3=0.3,a4=0.6,a5=1.0,a6=0.875, b21=0.2, b31=3.0/40.0,b32=9.0/40.0,b41=0.3,b42 = -0.9,b43=1.2, b51 = -11.0/54.0, b52=2.5,b53 = -70.0/27.0,b54=35.0/27.0, b61=1631.0/55296.0,b62=175.0/512.0, b63=575.0/13824.0, b64=44275.0/110592.0,b65=253.0/4096.0, c1=37.0/378.0, c3=250.0/621.0,c4=125.0/594.0,c6=512.0/1771.0, dc5 = -277.00/14336.0; double dc1=c1-2825.0/27648.0,dc3=c3-18575.0/48384.0, dc4=c4-13525.0/55296.0,dc6=c6-0.25; Point dydx, ak2,ak3,ak4,ak5,ak6,ytemp; dydx = y.derivee_t(); ytemp = y + b21*h*dydx; // First step. ak2 = ytemp.derivee_t(); //Second step. ytemp = y + h*(b31*dydx+b32*ak2); ak3 = ytemp.derivee_t(); //Third step. ytemp = y + h*(b41*dydx+b42*ak2+b43*ak3); ak4 = ytemp.derivee_t(); //Fourth step. ytemp = y + h*(b51*dydx+b52*ak2+b53*ak3+b54*ak4); ak5 = ytemp.derivee_t(); //Fifth step. ytemp = y + h*(b61*dydx + b62*ak2 + b63*ak3+b64*ak4+b65*ak5); ak6 = ytemp.derivee_t(); //Sixth step. yout = y + h*(c1*dydx + c3*ak3 + c4*ak4 + c6*ak6); yerr = h*(dc1*dydx + dc3*ak3 + dc4*ak4 + dc5*ak5 + dc6*ak6); } double compute_y_from_energy(double E, double x_dep, double v_dep) { double erreur=1000., y_test=0.01; Point test; test.SetCoordinatesCart(x_dep,y_test, 0., v_dep); cout << "Energie demandee : " << E << ", energie possible a la position voulue : " << test.Energie() << endl; if ( test.Energie()>E) return -1.; int Ntest=0; while (fabs(erreur-1.) > 0.0001) { ++Ntest; erreur = E/test.Energie(); //cout << y_test << " " << erreur << endl; y_test/= pow(erreur,2.); test.SetCoordinatesCart(x_dep,y_test, 0., v_dep); cout << Ntest << " : " << "erreur : " << erreur << endl; } return y_test; } double compute_v_from_energy(double E, double x_dep, double y_dep, double v_dep) { double r1 = sqrt(pow(x_dep+1.,2)+pow(y_dep,2)); double r2 = sqrt(pow(x_dep-1.,2)+pow(y_dep,2)); cout << "Energie demandee : " << E << ", energie possible a la position voulue : " << 1./r1+1./r2 << endl; if (E+1./r1+1./r2 < 0.) return -1.; else return sqrt((E+1./r1+1./r2)*2.); } void initialize(int argc, char *argv[], Point *point, int *N_crossing, int *n_default, double *PAS_TEMPS_AFFICHAGE) { double v_dep , x_dep, y_dep; bool set_x=false, set_y=false, set_v=false, set_E=false, set_N=false, set_d=false; *PAS_TEMPS_AFFICHAGE = 0.1; *N_crossing = 10; *n_default = 5; if (argc == 0) cout << "valeurs par defaut" << endl; else for (int i = 1; i < argc; i++) { char *arg = argv[i]; if (arg[0] != '-') { cout << " do not understand " << arg << endl; cout << " default values used " << endl; break; } switch (arg[1]) { case 'h' : usage(); break; case 'x' : i++; x_dep = atof(argv[i]); set_x=true; break; case 'y' : i++; y_dep = atof(argv[i]); set_y=true; break; case 'v' : i++; v_dep = atof(argv[i]); set_v=true; break; case 'p' : i++; *PAS_TEMPS_AFFICHAGE = atof(argv[i]); break; case 'N' : i++; *N_crossing = atoi(argv[i]); set_N=true; break; case 'd' : i++; *n_default = atoi(argv[i]); set_d=true; break; case 'E' : i++; Etot = -atof(argv[i]); set_E=true; // calculer y a partir de x, avec vy=vx=0 //default: // cout << "default values" << endl; } } bool erreur=false; if (set_E) { if (set_v) { y_dep = compute_y_from_energy(Etot,x_dep, v_dep); } else if (set_y) { v_dep = compute_v_from_energy(Etot,x_dep, y_dep, v_dep); } else { cout << "vous devez utiliser une option -v ou -y avec -E" << endl; erreur = true; } if (y_dep < 0.) erreur = true; if (erreur) cout << "probleme, retour aux valeurs par defaut" << endl; else cout << "energie calculee : " << Etot << ", (x,y) = " << x_dep << " " << y_dep <<endl; } if (erreur) point->set_default(1); else if (set_d) point->set_default(*n_default); else point->SetCoordinatesCart(x_dep, y_dep, 0., v_dep); return; } void affiche_parametres(Point point, int N_crossing) { cout << "x=" << point.X() << ", y=" << point.Y() << ", v=" << point.Vy() << ", N=" << N_crossing << ", E=" << point.Energie() << endl; return; }
Je suis débutant en Java, j'ai JBuilder, et j'ai commencé à "traduire" grossièrement, mais j'ai encore plein d'erreur. Voila mon code :
Pouvez-vous m'aider en me signalant les principales erreurs, et en me corrigeant?
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 public class Ode { public double MAXSTP = 10000 ; public double TINY = 1.e-30; void odeint(Point ystart, double delta_x, double eps, double h1, double hmin){ int nstp, i; double x, hnext, hdid, h; Point y, dydx, yscal; x = 0.; h = h1; if (h>delta_x) h=delta_x; y = ystart; for (nstp=1;nstp<=MAXSTP;nstp++) { //Take at most MAXSTP steps. dydx = y.derivee_t(); yscal = fabs(y) + h*fabs(dydx) + TINY; // Scaling used to monitor accuracy. This general-purpose choice can be modifed if need be. if ((x+h-delta_x)*(x+h) > 0.0) h=delta_x-x; // If stepsize can overshoot, decrease. rkqs(y,x,h,eps, yscal, hdid,hnext); if ((x-delta_x)*delta_x >= 0.0) //Are we done? { ystart = y; return; //Normal exit. } if (fabs(hnext) <= hmin)System.out.println("Step size too small in odeint"); h=hnext; } System.out.println("Too many steps in routine odeint"); } public double SAFETY = 0.9 ; public double PGROW = -0.2 ; public double PSHRNK = -0.25 ; public double ERRCON = 1.89e-4; //The value ERRCON equals (5/SAFETY) raised to the power (1/PGROW), see use below. void rkqs(Point y, double x, double htry, double eps, Point yscal, double hdid, double hnext) { double errmax,h,htemp; Point ytemp,yerr; h=htry; // Set stepsize to the initial trial value. for (;;) { rkck(y,h,ytemp,yerr); // Take a step. errmax=0.0; Evaluate accuracy. errmax = fabs(yerr/yscal).Max() / eps; // Scale relative to required tolerance. if (errmax <= 1.0) break; // Step succeeded. Compute size of next step. htemp=SAFETY*h*Math.pow(errmax,PSHRNK); // Truncation error too large, reduce stepsize. //h=(h >= 0.0 ? FMAX(htemp,0.1*h) : FMIN(htemp,0.1*h)); // No more than a factor of 10. h = htemp; if ((h>=0.) && (htemp < 0.1*h)) h = 0.1*h; if ((h<0.) && (htemp > 0.1*h)) h = 0.1*h; } if (errmax > ERRCON) hnext=SAFETY*h*Math.pow(errmax,PGROW); else hnext=5.0*h; //No more than a factor of 5 increase. x += h; y = ytemp; } void rkck(Point y, double h, Point yout, Point yerr) { double // a2=0.2,a3=0.3,a4=0.6,a5=1.0,a6=0.875, b21=0.2, b31=3.0/40.0,b32=9.0/40.0,b41=0.3,b42 = -0.9,b43=1.2, b51 = -11.0/54.0, b52=2.5,b53 = -70.0/27.0,b54=35.0/27.0, b61=1631.0/55296.0,b62=175.0/512.0, b63=575.0/13824.0, b64=44275.0/110592.0,b65=253.0/4096.0, c1=37.0/378.0, c3=250.0/621.0,c4=125.0/594.0,c6=512.0/1771.0, dc5 = -277.00/14336.0; double dc1=c1-2825.0/27648.0,dc3=c3-18575.0/48384.0, dc4=c4-13525.0/55296.0,dc6=c6-0.25; Point dydx, ak2,ak3,ak4,ak5,ak6,ytemp; // Ici les derivees sont toutes egales, non ? // Pas de dependance explicite en t ! dydx = y.derivee_t(); ytemp = y + b21*h*dydx; // First step. // et je remplace les ak par dxdy ?? ak2 = ytemp.derivee_t(); //Second step. ytemp = y + h*(b31*dydx+b32*ak2); ak3 = ytemp.derivee_t(); //Third step. ytemp = y + h*(b41*dydx+b42*ak2+b43*ak3); ak4 = ytemp.derivee_t(); //Fourth step. ytemp = y + h*(b51*dydx+b52*ak2+b53*ak3+b54*ak4); ak5 = ytemp.derivee_t(); //Fifth step. ytemp = y + h*(b61*dydx + b62*ak2 + b63*ak3+b64*ak4+b65*ak5); ak6 = ytemp.derivee_t(); //Sixth step. yout = y + h*(c1*dydx + c3*ak3 + c4*ak4 + c6*ak6); yerr = h*(dc1*dydx + dc3*ak3 + dc4*ak4 + dc5*ak5 + dc6*ak6); } double compute_y_from_energy(double E, double x_dep, double v_dep) { double erreur=1000., y_test=0.01; // si l'energie est trop grande, elle n'est pas atteignable au x voulu. On retourne une valeur negative // qui sera interpretee comme une erreur plus loin dans le main Point test; test.SetCoordinatesCart(x_dep,y_test, 0., v_dep); System.out.println("Energie demandee : "+ E + ", energie possible a la position voulue : " + test.Energie() ); if ( test.Energie()>E) return -1.; int Ntest=0; while (fabs(erreur-1.) > 0.0001) { ++Ntest; erreur = E/test.Energie(); //cout << y_test << " " << erreur << endl; y_test/= Math.pow(erreur,2.); test.SetCoordinatesCart(x_dep,y_test, 0., v_dep); System.out.println(Ntest +" : " + "erreur : " + erreur ); } return y_test; } double compute_v_from_energy(double E, double x_dep, double y_dep, double v_dep) { double r1 = Math.sqrt(Math.pow(x_dep+1.,2)+Math.pow(y_dep,2)); double r2 = Math.sqrt(Math.pow(x_dep-1.,2)+Math.pow(y_dep,2)); System.out.println("Energie demandee : " + E + ", energie possible a la position voulue : " + 1./r1+1./r2 ); if (E+1./r1+1./r2 < 0.) return -1.; else return Math.sqrt((E+1./r1+1./r2)*2.); } void initialize(int argc, char argv[], Point point, int N_crossing, int n_default, double PAS_TEMPS_AFFICHAGE) { double v_dep , x_dep, y_dep; boolean set_x=false, set_y=false, set_v=false, set_E=false, set_N=false, set_d=false; PAS_TEMPS_AFFICHAGE = 0.1; N_crossing = 10; n_default = 5; if (argc == 0) System.out.println("valeurs par defaut"); else for (int i = 1; i < argc; i++) { char arg = argv[i]; if (arg[0] != '-') { System.out.println(" do not understand " + arg); System.out.println(" default values used " ); break; } switch (arg[1]) { case 'h' : usage(); break; case 'x' : i++; x_dep = (double)(argv[i]); set_x=true; break; case 'y' : i++; y_dep = (double)(argv[i]); set_y=true; break; case 'v' : i++; v_dep = (double)(argv[i]); set_v=true; break; case 'p' : i++; PAS_TEMPS_AFFICHAGE = (double)(argv[i]); break; case 'N' : i++; N_crossing = (char)(argv[i]); set_N=true; break; case 'd' : i++; n_default = (char)(argv[i]); set_d=true; break; case 'E' : i++; Etot = -(double)(argv[i]); set_E=true; // calculer y a partir de x, avec vy=vx=0 //default: // cout << "default values" << endl; } } boolean erreur=false; if (set_E) { if (set_v) { y_dep = compute_y_from_energy(Etot,x_dep, v_dep); } else if (set_y) { v_dep = compute_v_from_energy(Etot,x_dep, y_dep, v_dep); } else { System.out.println("vous devez utiliser une option -v ou -y avec -E" ); erreur = true; } if (y_dep < 0.) erreur = true; if (erreur) System.out.println("probleme, retour aux valeurs par defaut" ); else System.out.println("energie calculee : " + Etot + ", (x,y) = "+ x_dep + " " + y_dep ); } if (erreur) point.set_default(1); else if (set_d) point.set_default(n_default); else point.SetCoordinatesCart(x_dep, y_dep, 0., v_dep); return; } void affiche_parametres(Point point, int N_crossing) { System.out.println("x=" + point.X() + ", y=" << point.Y() + ", v=" << point.Vy() + ", N=" << N_crossing + ", E=" + point.Energie() ); return; } }
Merci d'avance
Bien cordialement
Marc
Partager