| 1 |
/***************************************************************************** |
| 2 |
* Free42 -- an HP-42S calculator simulator |
| 3 |
* Copyright (C) 2004-2026 Thomas Okken |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License, version 2, |
| 7 |
* as published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, see http://www.gnu.org/licenses/. |
| 16 |
*****************************************************************************/ |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
#include "core_math1.h" |
| 21 |
#include "core_commands2.h" |
| 22 |
#include "core_display.h" |
| 23 |
#include "core_globals.h" |
| 24 |
#include "core_helpers.h" |
| 25 |
#include "core_main.h" |
| 26 |
#include "core_variables.h" |
| 27 |
#include "shell.h" |
| 28 |
|
| 29 |
#define SOLVE_VERSION 4 |
| 30 |
#define INTEG_VERSION 3 |
| 31 |
#define NUM_SHADOWS 10 |
| 32 |
|
| 33 |
/* Solver */ |
| 34 |
struct solve_state { |
| 35 |
int version; |
| 36 |
char prgm_name[7]; |
| 37 |
int prgm_length; |
| 38 |
char active_prgm_name[7]; |
| 39 |
int active_prgm_length; |
| 40 |
char var_name[7]; |
| 41 |
int var_length; |
| 42 |
int keep_running; |
| 43 |
int prev_prgm; |
| 44 |
int4 prev_pc; |
| 45 |
int state; |
| 46 |
int which; |
| 47 |
int toggle; |
| 48 |
int retry_counter; |
| 49 |
int secant_impatience; |
| 50 |
phloat retry_value; |
| 51 |
phloat x1, x2, x3; |
| 52 |
phloat fx1, fx2; |
| 53 |
phloat prev_x, curr_x, curr_f; |
| 54 |
phloat xm, fxm; |
| 55 |
phloat best_f, best_x, second_f, second_x; |
| 56 |
char shadow_name[NUM_SHADOWS][7]; |
| 57 |
int shadow_length[NUM_SHADOWS]; |
| 58 |
phloat shadow_value[NUM_SHADOWS]; |
| 59 |
uint4 last_disp_time; |
| 60 |
int prev_sp; |
| 61 |
phloat f_gap; |
| 62 |
int f_gap_worsening_counter; |
| 63 |
}; |
| 64 |
|
| 65 |
static solve_state solve; |
| 66 |
|
| 67 |
#define ROMB_K 5 |
| 68 |
// 1/2 million evals max! |
| 69 |
#define ROMB_MAX 20 |
| 70 |
|
| 71 |
/* Integrator */ |
| 72 |
struct integ_state { |
| 73 |
int version; |
| 74 |
char prgm_name[7]; |
| 75 |
int prgm_length; |
| 76 |
char active_prgm_name[7]; |
| 77 |
int active_prgm_length; |
| 78 |
char var_name[7]; |
| 79 |
int var_length; |
| 80 |
int keep_running; |
| 81 |
int prev_prgm; |
| 82 |
int4 prev_pc; |
| 83 |
int state; |
| 84 |
phloat llim, ulim, acc; |
| 85 |
phloat a, b, eps; |
| 86 |
int n, m, i, k; |
| 87 |
phloat h, sum; |
| 88 |
phloat c[ROMB_K]; |
| 89 |
phloat s[ROMB_K+1]; |
| 90 |
int nsteps; |
| 91 |
phloat p; |
| 92 |
phloat t, u; |
| 93 |
phloat prev_int; |
| 94 |
phloat prev_res; |
| 95 |
int prev_sp; |
| 96 |
}; |
| 97 |
|
| 98 |
static integ_state integ; |
| 99 |
|
| 100 |
|
| 101 |
static void reset_solve(); |
| 102 |
static void reset_integ(); |
| 103 |
|
| 104 |
|
| 105 |
bool persist_math() { |
| 106 |
if (!write_int(solve.version)) return false; |
| 107 |
if (fwrite(solve.prgm_name, 1, 7, gfile) != 7) return false; |
| 108 |
if (!write_int(solve.prgm_length)) return false; |
| 109 |
if (fwrite(solve.active_prgm_name, 1, 7, gfile) != 7) return false; |
| 110 |
if (!write_int(solve.active_prgm_length)) return false; |
| 111 |
if (fwrite(solve.var_name, 1, 7, gfile) != 7) return false; |
| 112 |
if (!write_int(solve.var_length)) return false; |
| 113 |
if (!write_int(solve.keep_running)) return false; |
| 114 |
if (solve_active()) { |
| 115 |
if (!write_int(solve.prev_prgm)) return false; |
| 116 |
if (!write_int4(global_pc2line(solve.prev_prgm, solve.prev_pc))) return false; |
| 117 |
} else { |
| 118 |
if (!write_int(0)) return false; |
| 119 |
if (!write_int4(0)) return false; |
| 120 |
} |
| 121 |
if (!write_int(solve.state)) return false; |
| 122 |
if (!write_int(solve.which)) return false; |
| 123 |
if (!write_int(solve.toggle)) return false; |
| 124 |
if (!write_int(solve.retry_counter)) return false; |
| 125 |
if (!write_int(solve.secant_impatience)) return false; |
| 126 |
if (!write_phloat(solve.retry_value)) return false; |
| 127 |
if (!write_phloat(solve.x1)) return false; |
| 128 |
if (!write_phloat(solve.x2)) return false; |
| 129 |
if (!write_phloat(solve.x3)) return false; |
| 130 |
if (!write_phloat(solve.fx1)) return false; |
| 131 |
if (!write_phloat(solve.fx2)) return false; |
| 132 |
if (!write_phloat(solve.prev_x)) return false; |
| 133 |
if (!write_phloat(solve.curr_x)) return false; |
| 134 |
if (!write_phloat(solve.curr_f)) return false; |
| 135 |
if (!write_phloat(solve.xm)) return false; |
| 136 |
if (!write_phloat(solve.fxm)) return false; |
| 137 |
if (!write_phloat(solve.best_f)) return false; |
| 138 |
if (!write_phloat(solve.best_x)) return false; |
| 139 |
if (!write_phloat(solve.second_f)) return false; |
| 140 |
if (!write_phloat(solve.second_x)) return false; |
| 141 |
for (int i = 0; i < NUM_SHADOWS; i++) { |
| 142 |
if (fwrite(solve.shadow_name[i], 1, 7, gfile) != 7) return false; |
| 143 |
if (!write_int(solve.shadow_length[i])) return false; |
| 144 |
if (!write_phloat(solve.shadow_value[i])) return false; |
| 145 |
} |
| 146 |
if (!write_int4(solve.last_disp_time)) return false; |
| 147 |
if (!write_int(solve.prev_sp)) return false; |
| 148 |
|
| 149 |
if (!write_int(integ.version)) return false; |
| 150 |
if (fwrite(integ.prgm_name, 1, 7, gfile) != 7) return false; |
| 151 |
if (!write_int(integ.prgm_length)) return false; |
| 152 |
if (fwrite(integ.active_prgm_name, 1, 7, gfile) != 7) return false; |
| 153 |
if (!write_int(integ.active_prgm_length)) return false; |
| 154 |
if (fwrite(integ.var_name, 1, 7, gfile) != 7) return false; |
| 155 |
if (!write_int(integ.var_length)) return false; |
| 156 |
if (!write_int(integ.keep_running)) return false; |
| 157 |
if (integ_active()) { |
| 158 |
if (!write_int(integ.prev_prgm)) return false; |
| 159 |
if (!write_int4(global_pc2line(integ.prev_prgm, integ.prev_pc))) return false; |
| 160 |
} else { |
| 161 |
if (!write_int(0)) return false; |
| 162 |
if (!write_int4(0)) return false; |
| 163 |
} |
| 164 |
if (!write_int(integ.state)) return false; |
| 165 |
if (!write_phloat(integ.llim)) return false; |
| 166 |
if (!write_phloat(integ.ulim)) return false; |
| 167 |
if (!write_phloat(integ.acc)) return false; |
| 168 |
if (!write_phloat(integ.a)) return false; |
| 169 |
if (!write_phloat(integ.b)) return false; |
| 170 |
if (!write_phloat(integ.eps)) return false; |
| 171 |
if (!write_int(integ.n)) return false; |
| 172 |
if (!write_int(integ.m)) return false; |
| 173 |
if (!write_int(integ.i)) return false; |
| 174 |
if (!write_int(integ.k)) return false; |
| 175 |
if (!write_phloat(integ.h)) return false; |
| 176 |
if (!write_phloat(integ.sum)) return false; |
| 177 |
for (int i = 0; i < ROMB_K; i++) |
| 178 |
if (!write_phloat(integ.c[i])) return false; |
| 179 |
for (int i = 0; i <= ROMB_K; i++) |
| 180 |
if (!write_phloat(integ.s[i])) return false; |
| 181 |
if (!write_int(integ.nsteps)) return false; |
| 182 |
if (!write_phloat(integ.p)) return false; |
| 183 |
if (!write_phloat(integ.t)) return false; |
| 184 |
if (!write_phloat(integ.u)) return false; |
| 185 |
if (!write_phloat(integ.prev_int)) return false; |
| 186 |
if (!write_phloat(integ.prev_res)) return false; |
| 187 |
if (!write_int(integ.prev_sp)) return false; |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
bool unpersist_math(int ver) { |
| 192 |
if (!read_int(&solve.version)) return false; |
| 193 |
if (fread(solve.prgm_name, 1, 7, gfile) != 7) return false; |
| 194 |
if (!read_int(&solve.prgm_length)) return false; |
| 195 |
if (fread(solve.active_prgm_name, 1, 7, gfile) != 7) return false; |
| 196 |
if (!read_int(&solve.active_prgm_length)) return false; |
| 197 |
if (fread(solve.var_name, 1, 7, gfile) != 7) return false; |
| 198 |
if (!read_int(&solve.var_length)) return false; |
| 199 |
if (!read_int(&solve.keep_running)) return false; |
| 200 |
if (!read_int(&solve.prev_prgm)) return false; |
| 201 |
if (!read_int4(&solve.prev_pc)) return false; |
| 202 |
if (solve_active()) |
| 203 |
solve.prev_pc = global_line2pc(solve.prev_prgm, solve.prev_pc); |
| 204 |
if (!read_int(&solve.state)) return false; |
| 205 |
if (!read_int(&solve.which)) return false; |
| 206 |
if (!read_int(&solve.toggle)) return false; |
| 207 |
if (!read_int(&solve.retry_counter)) return false; |
| 208 |
if (ver < 45) { |
| 209 |
solve.secant_impatience = 0; |
| 210 |
} else { |
| 211 |
if (!read_int(&solve.secant_impatience)) return false; |
| 212 |
} |
| 213 |
if (!read_phloat(&solve.retry_value)) return false; |
| 214 |
if (!read_phloat(&solve.x1)) return false; |
| 215 |
if (!read_phloat(&solve.x2)) return false; |
| 216 |
if (!read_phloat(&solve.x3)) return false; |
| 217 |
if (!read_phloat(&solve.fx1)) return false; |
| 218 |
if (!read_phloat(&solve.fx2)) return false; |
| 219 |
if (!read_phloat(&solve.prev_x)) return false; |
| 220 |
if (!read_phloat(&solve.curr_x)) return false; |
| 221 |
if (!read_phloat(&solve.curr_f)) return false; |
| 222 |
if (!read_phloat(&solve.xm)) return false; |
| 223 |
if (!read_phloat(&solve.fxm)) return false; |
| 224 |
if (ver >= 29) { |
| 225 |
if (!read_phloat(&solve.best_f)) return false; |
| 226 |
if (!read_phloat(&solve.best_x)) return false; |
| 227 |
if (!read_phloat(&solve.second_f)) return false; |
| 228 |
if (!read_phloat(&solve.second_x)) return false; |
| 229 |
} else { |
| 230 |
solve.best_f = solve.second_f = POS_HUGE_PHLOAT; |
| 231 |
solve.best_x = solve.second_x = 0; |
| 232 |
} |
| 233 |
for (int i = 0; i < NUM_SHADOWS; i++) { |
| 234 |
if (fread(solve.shadow_name[i], 1, 7, gfile) != 7) return false; |
| 235 |
if (!read_int(&solve.shadow_length[i])) return false; |
| 236 |
if (!read_phloat(&solve.shadow_value[i])) return false; |
| 237 |
} |
| 238 |
if (!read_int4((int4 *) &solve.last_disp_time)) return false; |
| 239 |
if (ver >= 33) { |
| 240 |
if (!read_int(&solve.prev_sp)) return false; |
| 241 |
} else { |
| 242 |
solve.prev_sp = -2; |
| 243 |
} |
| 244 |
|
| 245 |
if (!read_int(&integ.version)) return false; |
| 246 |
if (fread(integ.prgm_name, 1, 7, gfile) != 7) return false; |
| 247 |
if (!read_int(&integ.prgm_length)) return false; |
| 248 |
if (fread(integ.active_prgm_name, 1, 7, gfile) != 7) return false; |
| 249 |
if (!read_int(&integ.active_prgm_length)) return false; |
| 250 |
if (fread(integ.var_name, 1, 7, gfile) != 7) return false; |
| 251 |
if (!read_int(&integ.var_length)) return false; |
| 252 |
if (!read_int(&integ.keep_running)) return false; |
| 253 |
if (!read_int(&integ.prev_prgm)) return false; |
| 254 |
if (!read_int4(&integ.prev_pc)) return false; |
| 255 |
if (integ_active()) |
| 256 |
integ.prev_pc = global_line2pc(integ.prev_prgm, integ.prev_pc); |
| 257 |
if (!read_int(&integ.state)) return false; |
| 258 |
if (!read_phloat(&integ.llim)) return false; |
| 259 |
if (!read_phloat(&integ.ulim)) return false; |
| 260 |
if (!read_phloat(&integ.acc)) return false; |
| 261 |
if (!read_phloat(&integ.a)) return false; |
| 262 |
if (!read_phloat(&integ.b)) return false; |
| 263 |
if (!read_phloat(&integ.eps)) return false; |
| 264 |
if (!read_int(&integ.n)) return false; |
| 265 |
if (!read_int(&integ.m)) return false; |
| 266 |
if (!read_int(&integ.i)) return false; |
| 267 |
if (!read_int(&integ.k)) return false; |
| 268 |
if (!read_phloat(&integ.h)) return false; |
| 269 |
if (!read_phloat(&integ.sum)) return false; |
| 270 |
for (int i = 0; i < ROMB_K; i++) |
| 271 |
if (!read_phloat(&integ.c[i])) return false; |
| 272 |
for (int i = 0; i <= ROMB_K; i++) |
| 273 |
if (!read_phloat(&integ.s[i])) return false; |
| 274 |
if (!read_int(&integ.nsteps)) return false; |
| 275 |
if (!read_phloat(&integ.p)) return false; |
| 276 |
if (!read_phloat(&integ.t)) return false; |
| 277 |
if (!read_phloat(&integ.u)) return false; |
| 278 |
if (!read_phloat(&integ.prev_int)) return false; |
| 279 |
if (!read_phloat(&integ.prev_res)) return false; |
| 280 |
if (ver >= 33) { |
| 281 |
if (!read_int(&integ.prev_sp)) return false; |
| 282 |
} else { |
| 283 |
integ.prev_sp = -2; |
| 284 |
} |
| 285 |
solve.f_gap = NAN_PHLOAT; |
| 286 |
|
| 287 |
return true; |
| 288 |
} |
| 289 |
|
| 290 |
void reset_math() { |
| 291 |
reset_solve(); |
| 292 |
reset_integ(); |
| 293 |
} |
| 294 |
|
| 295 |
static void clean_stack(int prev_sp) { |
| 296 |
if (flags.f.big_stack && prev_sp != -2 && sp > prev_sp) { |
| 297 |
int excess = sp - prev_sp; |
| 298 |
for (int i = 0; i < excess; i++) |
| 299 |
free_vartype(stack[sp - i]); |
| 300 |
sp -= excess; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
static void reset_solve() { |
| 305 |
int i; |
| 306 |
for (i = 0; i < NUM_SHADOWS; i++) |
| 307 |
solve.shadow_length[i] = 0; |
| 308 |
solve.prgm_length = 0; |
| 309 |
solve.active_prgm_length = 0; |
| 310 |
solve.state = 0; |
| 311 |
if (mode_appmenu == MENU_SOLVE) |
| 312 |
set_menu_return_err(MENULEVEL_APP, MENU_NONE, true); |
| 313 |
} |
| 314 |
|
| 315 |
static int find_shadow(const char *name, int length) { |
| 316 |
int i; |
| 317 |
for (i = 0; i < NUM_SHADOWS; i++) |
| 318 |
if (string_equals(solve.shadow_name[i], solve.shadow_length[i], |
| 319 |
name, length)) |
| 320 |
return i; |
| 321 |
return -1; |
| 322 |
} |
| 323 |
|
| 324 |
void put_shadow(const char *name, int length, phloat value) { |
| 325 |
remove_shadow(name, length); |
| 326 |
int i; |
| 327 |
for (i = 0; i < NUM_SHADOWS; i++) |
| 328 |
if (solve.shadow_length[i] == 0) |
| 329 |
goto do_insert; |
| 330 |
/* No empty slots available. Remove slot 0 (the oldest) and |
| 331 |
* move all subsequent ones down, freeing up slot NUM_SHADOWS - 1 |
| 332 |
*/ |
| 333 |
for (i = 0; i < NUM_SHADOWS - 1; i++) { |
| 334 |
string_copy(solve.shadow_name[i], &solve.shadow_length[i], |
| 335 |
solve.shadow_name[i + 1], solve.shadow_length[i + 1]); |
| 336 |
solve.shadow_value[i] = solve.shadow_value[i + 1]; |
| 337 |
} |
| 338 |
do_insert: |
| 339 |
string_copy(solve.shadow_name[i], &solve.shadow_length[i], name, length); |
| 340 |
solve.shadow_value[i] = value; |
| 341 |
} |
| 342 |
|
| 343 |
bool get_shadow(const char *name, int length, phloat *value) { |
| 344 |
int i = find_shadow(name, length); |
| 345 |
if (i == -1) |
| 346 |
return false; |
| 347 |
*value = solve.shadow_value[i]; |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
void remove_shadow(const char *name, int length) { |
| 352 |
int i = find_shadow(name, length); |
| 353 |
if (i == -1) |
| 354 |
return; |
| 355 |
while (i < NUM_SHADOWS - 1) { |
| 356 |
string_copy(solve.shadow_name[i], &solve.shadow_length[i], |
| 357 |
solve.shadow_name[i + 1], solve.shadow_length[i + 1]); |
| 358 |
solve.shadow_value[i] = solve.shadow_value[i + 1]; |
| 359 |
i++; |
| 360 |
} |
| 361 |
solve.shadow_length[NUM_SHADOWS - 1] = 0; |
| 362 |
} |
| 363 |
|
| 364 |
void set_solve_prgm(const char *name, int length) { |
| 365 |
string_copy(solve.prgm_name, &solve.prgm_length, name, length); |
| 366 |
} |
| 367 |
|
| 368 |
static int call_solve_fn(int which, int state) { |
| 369 |
if (solve.active_prgm_length == 0) |
| 370 |
return ERR_NONEXISTENT; |
| 371 |
int err, i; |
| 372 |
arg_struct arg; |
| 373 |
vartype *v = recall_var(solve.var_name, solve.var_length); |
| 374 |
phloat x = which == 1 ? solve.x1 : which == 2 ? solve.x2 : solve.x3; |
| 375 |
solve.prev_x = solve.curr_x; |
| 376 |
solve.curr_x = x; |
| 377 |
if (v == NULL || v->type != TYPE_REAL) { |
| 378 |
v = new_real(x); |
| 379 |
if (v == NULL) |
| 380 |
return ERR_INSUFFICIENT_MEMORY; |
| 381 |
err = store_var(solve.var_name, solve.var_length, v); |
| 382 |
if (err != ERR_NONE) { |
| 383 |
free_vartype(v); |
| 384 |
return err; |
| 385 |
} |
| 386 |
} else |
| 387 |
((vartype_real *) v)->x = x; |
| 388 |
solve.which = which; |
| 389 |
solve.state = state; |
| 390 |
arg.type = ARGTYPE_STR; |
| 391 |
arg.length = solve.active_prgm_length; |
| 392 |
for (i = 0; i < arg.length; i++) |
| 393 |
arg.val.text[i] = solve.active_prgm_name[i]; |
| 394 |
clean_stack(solve.prev_sp); |
| 395 |
err = docmd_gto(&arg); |
| 396 |
if (err != ERR_NONE) { |
| 397 |
free_vartype(v); |
| 398 |
return err; |
| 399 |
} |
| 400 |
err = push_rtn_addr(-2, 0); |
| 401 |
if (err != ERR_NONE) { |
| 402 |
current_prgm = solve.prev_prgm; |
| 403 |
pc = solve.prev_pc; |
| 404 |
return err; |
| 405 |
} else |
| 406 |
return ERR_RUN; |
| 407 |
} |
| 408 |
|
| 409 |
int start_solve(const char *name, int length, phloat x1, phloat x2) { |
| 410 |
if (solve_active()) |
| 411 |
return ERR_SOLVE_SOLVE; |
| 412 |
string_copy(solve.var_name, &solve.var_length, name, length); |
| 413 |
string_copy(solve.active_prgm_name, &solve.active_prgm_length, |
| 414 |
solve.prgm_name, solve.prgm_length); |
| 415 |
solve.prev_prgm = current_prgm; |
| 416 |
solve.prev_pc = pc; |
| 417 |
solve.prev_sp = flags.f.big_stack ? sp : -2; |
| 418 |
if (x1 == x2) { |
| 419 |
if (x1 == 0) { |
| 420 |
x2 = 1; |
| 421 |
solve.retry_counter = 0; |
| 422 |
} else { |
| 423 |
x2 = x1 * 1.000001; |
| 424 |
if (p_isinf(x2)) |
| 425 |
x2 = x1 * 0.999999; |
| 426 |
solve.retry_counter = -10; |
| 427 |
} |
| 428 |
} else { |
| 429 |
solve.retry_counter = 10; |
| 430 |
solve.retry_value = fabs(x1) < fabs(x2) ? x1 : x2; |
| 431 |
} |
| 432 |
if (x1 < x2) { |
| 433 |
solve.x1 = x1; |
| 434 |
solve.x2 = x2; |
| 435 |
} else { |
| 436 |
solve.x1 = x2; |
| 437 |
solve.x2 = x1; |
| 438 |
} |
| 439 |
solve.best_x = 0; |
| 440 |
solve.best_f = POS_HUGE_PHLOAT; |
| 441 |
solve.second_x = 0; |
| 442 |
solve.second_f = POS_HUGE_PHLOAT; |
| 443 |
solve.last_disp_time = 0; |
| 444 |
solve.toggle = 1; |
| 445 |
solve.secant_impatience = 0; |
| 446 |
solve.f_gap = NAN_PHLOAT; |
| 447 |
solve.keep_running = !should_i_stop_at_this_level() && program_running(); |
| 448 |
return call_solve_fn(1, 1); |
| 449 |
} |
| 450 |
|
| 451 |
struct message_spec { |
| 452 |
const char *text; |
| 453 |
int length; |
| 454 |
}; |
| 455 |
|
| 456 |
#define SOLVE_ROOT 0 |
| 457 |
#define SOLVE_SIGN_REVERSAL 1 |
| 458 |
#define SOLVE_EXTREMUM 2 |
| 459 |
#define SOLVE_BAD_GUESSES 3 |
| 460 |
#define SOLVE_CONSTANT 4 |
| 461 |
#define SOLVE_NOT_SURE -1 |
| 462 |
|
| 463 |
static const message_spec solve_message[] = { |
| 464 |
{ NULL, 0 }, |
| 465 |
{ "Sign Reversal", 13 }, |
| 466 |
{ "Extremum", 8 }, |
| 467 |
{ "Bad Guess(es)", 13 }, |
| 468 |
{ "Constant?", 9 } |
| 469 |
}; |
| 470 |
|
| 471 |
static int finish_solve(int message) { |
| 472 |
vartype *v, *new_x, *new_y, *new_z, *new_t; |
| 473 |
arg_struct arg; |
| 474 |
int dummy, print; |
| 475 |
|
| 476 |
phloat final_f = solve.curr_f; |
| 477 |
|
| 478 |
if (message == SOLVE_NOT_SURE) |
| 479 |
if (!p_isnan(solve.f_gap) && solve.f_gap_worsening_counter >= 3) |
| 480 |
message = SOLVE_SIGN_REVERSAL; |
| 481 |
else |
| 482 |
message = SOLVE_ROOT; |
| 483 |
|
| 484 |
if (solve.which == -1) { |
| 485 |
/* Ridders was terminated because it wasn't making progress; this does |
| 486 |
* not necessarily mean that x3 is the best guess so far. So, to be |
| 487 |
* sure, select the value with the lowest absolute function value. |
| 488 |
*/ |
| 489 |
phloat t1 = fabs(solve.fx1); |
| 490 |
phloat t2 = fabs(solve.fx2); |
| 491 |
phloat t3 = fabs(solve.curr_f); |
| 492 |
phloat t; |
| 493 |
if (t1 < t2) { |
| 494 |
solve.which = 1; |
| 495 |
t = t1; |
| 496 |
final_f = solve.fx1; |
| 497 |
} else { |
| 498 |
solve.which = 2; |
| 499 |
t = t2; |
| 500 |
final_f = solve.fx2; |
| 501 |
} |
| 502 |
if (t3 < t) { |
| 503 |
solve.which = 3; |
| 504 |
final_f = solve.curr_f; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
phloat b = solve.which == 1 ? solve.x1 : |
| 509 |
solve.which == 2 ? solve.x2 : solve.x3; |
| 510 |
phloat s; |
| 511 |
if (p_isinf(solve.best_f)) |
| 512 |
s = b; |
| 513 |
else if (solve.best_f > fabs(final_f)) |
| 514 |
s = solve.best_x; |
| 515 |
else if (p_isinf(solve.second_f)) |
| 516 |
s = solve.best_x; |
| 517 |
else |
| 518 |
s = solve.second_x; |
| 519 |
|
| 520 |
solve.state = 0; |
| 521 |
|
| 522 |
clean_stack(solve.prev_sp); |
| 523 |
v = recall_var(solve.var_name, solve.var_length); |
| 524 |
((vartype_real *) v)->x = b; |
| 525 |
if (flags.f.big_stack && !ensure_stack_capacity(4)) |
| 526 |
return ERR_INSUFFICIENT_MEMORY; |
| 527 |
new_x = dup_vartype(v); |
| 528 |
new_y = new_real(s); |
| 529 |
new_z = new_real(final_f); |
| 530 |
new_t = new_real(message); |
| 531 |
if (new_x == NULL || new_y == NULL || new_z == NULL || new_t == NULL) { |
| 532 |
free_vartype(new_x); |
| 533 |
free_vartype(new_y); |
| 534 |
free_vartype(new_z); |
| 535 |
free_vartype(new_t); |
| 536 |
return ERR_INSUFFICIENT_MEMORY; |
| 537 |
} |
| 538 |
if (flags.f.big_stack) |
| 539 |
sp += 4; |
| 540 |
else |
| 541 |
for (int i = 0; i < 4; i++) |
| 542 |
free_vartype(stack[i]); |
| 543 |
stack[sp] = new_x; |
| 544 |
stack[sp - 1] = new_y; |
| 545 |
stack[sp - 2] = new_z; |
| 546 |
stack[sp - 3] = new_t; |
| 547 |
|
| 548 |
current_prgm = solve.prev_prgm; |
| 549 |
pc = solve.prev_pc; |
| 550 |
|
| 551 |
arg.type = ARGTYPE_STR; |
| 552 |
string_copy(arg.val.text, &dummy, solve.var_name, solve.var_length); |
| 553 |
arg.length = solve.var_length; |
| 554 |
|
| 555 |
print = (flags.f.trace_print || flags.f.normal_print) && flags.f.printer_exists; |
| 556 |
|
| 557 |
if (!solve.keep_running) { |
| 558 |
view_helper(&arg, print); |
| 559 |
if (message != SOLVE_ROOT) { |
| 560 |
clear_row(1); |
| 561 |
draw_string(0, 1, solve_message[message].text, |
| 562 |
solve_message[message].length); |
| 563 |
flags.f.message = 1; |
| 564 |
flags.f.two_line_message = 1; |
| 565 |
flush_display(); |
| 566 |
} |
| 567 |
} else { |
| 568 |
if (print) { |
| 569 |
char namebuf[8], valbuf[22]; |
| 570 |
int namelen = 0, vallen; |
| 571 |
string2buf(namebuf, 8, &namelen, solve.var_name, solve.var_length); |
| 572 |
char2buf(namebuf, 8, &namelen, '='); |
| 573 |
vallen = vartype2string(v, valbuf, 22); |
| 574 |
print_wide(namebuf, namelen, valbuf, vallen); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (print && message != SOLVE_ROOT) |
| 579 |
print_lines(solve_message[message].text, |
| 580 |
solve_message[message].length, true); |
| 581 |
|
| 582 |
return solve.keep_running ? ERR_NONE : ERR_STOP; |
| 583 |
} |
| 584 |
|
| 585 |
static void track_f_gap() { |
| 586 |
phloat gap = solve.fx2 - solve.fx1; |
| 587 |
if (gap == 0 || p_isnan(gap)) { |
| 588 |
solve.f_gap = NAN_PHLOAT; |
| 589 |
return; |
| 590 |
} |
| 591 |
if (p_isnan(solve.f_gap) |
| 592 |
|| (gap > 0) != (solve.f_gap > 0) |
| 593 |
|| fabs(gap) < fabs(solve.f_gap)) |
| 594 |
solve.f_gap_worsening_counter = 0; |
| 595 |
else |
| 596 |
solve.f_gap_worsening_counter++; |
| 597 |
solve.f_gap = gap; |
| 598 |
} |
| 599 |
|
| 600 |
int return_to_solve(int failure, bool stop) { |
| 601 |
phloat f, slope, s, xnew, prev_f = solve.curr_f; |
| 602 |
uint4 now_time; |
| 603 |
|
| 604 |
if (stop) |
| 605 |
solve.keep_running = 0; |
| 606 |
|
| 607 |
if (solve.state == 0) |
| 608 |
return ERR_INTERNAL_ERROR; |
| 609 |
if (!failure) { |
| 610 |
if (sp == -1) |
| 611 |
return ERR_TOO_FEW_ARGUMENTS; |
| 612 |
if (stack[sp]->type == TYPE_REAL) { |
| 613 |
f = ((vartype_real *) stack[sp])->x; |
| 614 |
solve.curr_f = f; |
| 615 |
if (f == 0) |
| 616 |
return finish_solve(SOLVE_ROOT); |
| 617 |
if (fabs(f) < fabs(solve.best_f)) { |
| 618 |
solve.second_f = solve.best_f; |
| 619 |
solve.second_x = solve.best_x; |
| 620 |
solve.best_f = fabs(f); |
| 621 |
solve.best_x = solve.curr_x; |
| 622 |
} |
| 623 |
} else { |
| 624 |
solve.curr_f = POS_HUGE_PHLOAT; |
| 625 |
failure = 1; |
| 626 |
} |
| 627 |
} else |
| 628 |
solve.curr_f = POS_HUGE_PHLOAT; |
| 629 |
|
| 630 |
if (!failure && solve.retry_counter != 0) { |
| 631 |
if (solve.retry_counter > 0) |
| 632 |
solve.retry_counter--; |
| 633 |
else |
| 634 |
solve.retry_counter++; |
| 635 |
} |
| 636 |
|
| 637 |
now_time = shell_milliseconds(); |
| 638 |
if (now_time < solve.last_disp_time) |
| 639 |
// shell_milliseconds() wrapped around |
| 640 |
solve.last_disp_time = 0; |
| 641 |
if (!solve.keep_running && solve.state > 1 |
| 642 |
&& now_time >= solve.last_disp_time + 250) { |
| 643 |
/* Put on a show so the user won't think we're just drinking beer |
| 644 |
* while they're waiting anxiously for the solver to converge... |
| 645 |
*/ |
| 646 |
char buf[22]; |
| 647 |
int bufptr = 0, i; |
| 648 |
solve.last_disp_time = now_time; |
| 649 |
clear_display(); |
| 650 |
bufptr = phloat2string(solve.curr_x, buf, 22, 0, 0, 3, |
| 651 |
flags.f.thousands_separators); |
| 652 |
for (i = bufptr; i < 21; i++) |
| 653 |
buf[i] = ' '; |
| 654 |
buf[21] = failure ? '?' : solve.curr_f > 0 ? '+' : '-'; |
| 655 |
draw_string(0, 0, buf, 22); |
| 656 |
bufptr = phloat2string(solve.prev_x, buf, 22, 0, 0, 3, |
| 657 |
flags.f.thousands_separators); |
| 658 |
for (i = bufptr; i < 21; i++) |
| 659 |
buf[i] = ' '; |
| 660 |
buf[21] = prev_f == POS_HUGE_PHLOAT ? '?' : prev_f > 0 ? '+' : '-'; |
| 661 |
draw_string(0, 1, buf, 22); |
| 662 |
flush_display(); |
| 663 |
flags.f.message = 1; |
| 664 |
flags.f.two_line_message = 1; |
| 665 |
} |
| 666 |
|
| 667 |
switch (solve.state) { |
| 668 |
|
| 669 |
case 1: |
| 670 |
/* first evaluation of x1 */ |
| 671 |
if (failure) { |
| 672 |
if (solve.retry_counter > 0) |
| 673 |
solve.retry_counter = -solve.retry_counter; |
| 674 |
return call_solve_fn(2, 2); |
| 675 |
} else { |
| 676 |
solve.fx1 = f; |
| 677 |
return call_solve_fn(2, 3); |
| 678 |
} |
| 679 |
|
| 680 |
case 2: |
| 681 |
/* first evaluation of x2 after x1 was unsuccessful */ |
| 682 |
if (failure) |
| 683 |
return finish_solve(SOLVE_BAD_GUESSES); |
| 684 |
solve.fx2 = f; |
| 685 |
solve.x1 = (solve.x1 + solve.x2) / 2; |
| 686 |
if (solve.x1 == solve.x2) |
| 687 |
return finish_solve(SOLVE_BAD_GUESSES); |
| 688 |
return call_solve_fn(1, 3); |
| 689 |
|
| 690 |
case 3: |
| 691 |
/* make sure f(x1) != f(x2) */ |
| 692 |
if (failure) { |
| 693 |
if (solve.which == 1) |
| 694 |
solve.x1 = (solve.x1 + solve.x2) / 2; |
| 695 |
else |
| 696 |
solve.x2 = (solve.x1 + solve.x2) / 2; |
| 697 |
if (solve.x1 == solve.x2) |
| 698 |
return finish_solve(SOLVE_BAD_GUESSES); |
| 699 |
return call_solve_fn(solve.which, 3); |
| 700 |
} |
| 701 |
if (solve.which == 1) |
| 702 |
solve.fx1 = f; |
| 703 |
else |
| 704 |
solve.fx2 = f; |
| 705 |
if (solve.fx1 == solve.fx2) { |
| 706 |
/* If f(x1) == f(x2), we assume we're in a local flat spot. |
| 707 |
* We extend the interval exponentially until we have two |
| 708 |
* values of x, both of which are evaluated successfully, |
| 709 |
* and yielding different values; from that moment on, we can |
| 710 |
* apply the secant method. |
| 711 |
*/ |
| 712 |
int which; |
| 713 |
phloat x; |
| 714 |
if (solve.toggle) { |
| 715 |
x = solve.x2 + 100 * (solve.x2 - solve.x1); |
| 716 |
if (p_isinf(x)) { |
| 717 |
if (solve.retry_counter != 0) |
| 718 |
goto retry_solve; |
| 719 |
return finish_solve(SOLVE_CONSTANT); |
| 720 |
} |
| 721 |
which = 2; |
| 722 |
solve.x2 = x; |
| 723 |
} else { |
| 724 |
x = solve.x1 - 100 * (solve.x2 - solve.x1); |
| 725 |
if (p_isinf(x)) { |
| 726 |
if (solve.retry_counter != 0) |
| 727 |
goto retry_solve; |
| 728 |
return finish_solve(SOLVE_CONSTANT); |
| 729 |
} |
| 730 |
which = 1; |
| 731 |
solve.x1 = x; |
| 732 |
} |
| 733 |
solve.toggle = !solve.toggle; |
| 734 |
return call_solve_fn(which, 3); |
| 735 |
} |
| 736 |
/* When we get here, f(x1) != f(x2), and we can start |
| 737 |
* applying the secant method. |
| 738 |
*/ |
| 739 |
goto do_secant; |
| 740 |
|
| 741 |
case 4: |
| 742 |
/* secant method, evaluated x3 */ |
| 743 |
case 5: |
| 744 |
/* just performed bisection */ |
| 745 |
if (failure) { |
| 746 |
if (solve.x3 > solve.x2) { |
| 747 |
/* Failure outside [x1, x2]; approach x2 */ |
| 748 |
solve.x3 = (solve.x2 + solve.x3) / 2; |
| 749 |
if (solve.x3 == solve.x2) |
| 750 |
return finish_solve(SOLVE_EXTREMUM); |
| 751 |
} else if (solve.x3 < solve.x1) { |
| 752 |
/* Failure outside [x1, x2]; approach x1 */ |
| 753 |
solve.x3 = (solve.x1 + solve.x3) / 2; |
| 754 |
if (solve.x3 == solve.x1) |
| 755 |
return finish_solve(SOLVE_EXTREMUM); |
| 756 |
} else { |
| 757 |
/* Failure inside [x1, x2]; |
| 758 |
* alternately approach x1 and x2 */ |
| 759 |
if (solve.toggle) { |
| 760 |
phloat old_x3 = solve.x3; |
| 761 |
if (solve.x3 <= (solve.x1 + solve.x2) / 2) |
| 762 |
solve.x3 = (solve.x1 + solve.x3) / 2; |
| 763 |
else |
| 764 |
solve.x3 = (solve.x2 + solve.x3) / 2; |
| 765 |
if (solve.x3 == old_x3) |
| 766 |
return finish_solve(SOLVE_SIGN_REVERSAL); |
| 767 |
} else |
| 768 |
solve.x3 = solve.x1 + solve.x2 - solve.x3; |
| 769 |
solve.toggle = !solve.toggle; |
| 770 |
if (solve.x3 == solve.x1 || solve.x3 == solve.x2) |
| 771 |
return finish_solve(SOLVE_SIGN_REVERSAL); |
| 772 |
} |
| 773 |
return call_solve_fn(3, 4); |
| 774 |
} else if (solve.fx1 > 0 && solve.fx2 > 0) { |
| 775 |
if (f > 0) { |
| 776 |
if (f > solve.best_f) { |
| 777 |
if (++solve.secant_impatience > 30) { |
| 778 |
solve.which = -1; |
| 779 |
return finish_solve(SOLVE_EXTREMUM); |
| 780 |
} |
| 781 |
} else { |
| 782 |
solve.secant_impatience = 0; |
| 783 |
} |
| 784 |
} |
| 785 |
if (solve.fx1 > solve.fx2) { |
| 786 |
if (f >= solve.fx1 && solve.state != 5) |
| 787 |
goto do_bisection; |
| 788 |
solve.x1 = solve.x3; |
| 789 |
solve.fx1 = f; |
| 790 |
} else { |
| 791 |
if (f >= solve.fx2 && solve.state != 5) |
| 792 |
goto do_bisection; |
| 793 |
solve.x2 = solve.x3; |
| 794 |
solve.fx2 = f; |
| 795 |
} |
| 796 |
} else if (solve.fx1 < 0 && solve.fx2 < 0) { |
| 797 |
if (f < 0) { |
| 798 |
if (-f > solve.best_f) { |
| 799 |
if (++solve.secant_impatience > 30) { |
| 800 |
solve.which = -1; |
| 801 |
return finish_solve(SOLVE_EXTREMUM); |
| 802 |
} |
| 803 |
} else { |
| 804 |
solve.secant_impatience = 0; |
| 805 |
} |
| 806 |
} |
| 807 |
if (solve.fx1 < solve.fx2) { |
| 808 |
if (f <= solve.fx1 && solve.state != 5) |
| 809 |
goto do_bisection; |
| 810 |
solve.x1 = solve.x3; |
| 811 |
solve.fx1 = f; |
| 812 |
} else { |
| 813 |
if (f <= solve.fx2 && solve.state != 5) |
| 814 |
goto do_bisection; |
| 815 |
solve.x2 = solve.x3; |
| 816 |
solve.fx2 = f; |
| 817 |
} |
| 818 |
} else { |
| 819 |
/* f(x1) and f(x2) have opposite signs; assuming f is |
| 820 |
* continuous on the interval [x1, x2], there is at least |
| 821 |
* one root. We use x3 to replace x1 or x2 and narrow the |
| 822 |
* interval, even if f(x3) is actually worse than f(x1) and |
| 823 |
* f(x2). This way we're guaranteed to home in on the root |
| 824 |
* (but of course we'll get stuck if we encounter a |
| 825 |
* discontinuous sign reversal instead, e.g. 1/x at x = 0). |
| 826 |
* Such is life. |
| 827 |
*/ |
| 828 |
if ((solve.fx1 > 0 && f > 0) || (solve.fx1 < 0 && f < 0)) { |
| 829 |
solve.x1 = solve.x3; |
| 830 |
solve.fx1 = f; |
| 831 |
} else { |
| 832 |
solve.x2 = solve.x3; |
| 833 |
solve.fx2 = f; |
| 834 |
} |
| 835 |
} |
| 836 |
if (solve.x2 < solve.x1) { |
| 837 |
/* Make sure x1 is always less than x2 */ |
| 838 |
phloat tmp = solve.x1; |
| 839 |
solve.x1 = solve.x2; |
| 840 |
solve.x2 = tmp; |
| 841 |
tmp = solve.fx1; |
| 842 |
solve.fx1 = solve.fx2; |
| 843 |
solve.fx2 = tmp; |
| 844 |
} |
| 845 |
track_f_gap(); |
| 846 |
do_secant: |
| 847 |
if (solve.fx1 == solve.fx2) |
| 848 |
return finish_solve(SOLVE_EXTREMUM); |
| 849 |
if ((solve.fx1 > 0 && solve.fx2 < 0) |
| 850 |
|| (solve.fx1 < 0 && solve.fx2 > 0)) |
| 851 |
goto do_ridders; |
| 852 |
slope = (solve.fx2 - solve.fx1) / (solve.x2 - solve.x1); |
| 853 |
if (p_isinf(slope)) { |
| 854 |
solve.x3 = (solve.x1 + solve.x2) / 2; |
| 855 |
if (solve.x3 == solve.x1 || solve.x3 == solve.x2) |
| 856 |
return finish_solve(SOLVE_NOT_SURE); |
| 857 |
else |
| 858 |
return call_solve_fn(3, 4); |
| 859 |
} else if (slope == 0) { |
| 860 |
/* Underflow caused by x2 - x1 being too big. |
| 861 |
* We're changing the calculation sequence to steer |
| 862 |
* clear of trouble. |
| 863 |
*/ |
| 864 |
solve.x3 = solve.x1 - solve.fx1 * (solve.x2 - solve.x1) |
| 865 |
/ (solve.fx2 - solve.fx1); |
| 866 |
|
| 867 |
goto finish_secant; |
| 868 |
} else { |
| 869 |
int inf; |
| 870 |
solve.x3 = solve.x1 - solve.fx1 / slope; |
| 871 |
finish_secant: |
| 872 |
if ((inf = p_isinf(solve.x3)) != 0) { |
| 873 |
if (solve.retry_counter != 0) |
| 874 |
goto retry_solve; |
| 875 |
return finish_solve(SOLVE_EXTREMUM); |
| 876 |
} |
| 877 |
/* The next two 'if' statements deal with the case that the |
| 878 |
* secant extrapolation returns one of the points we already |
| 879 |
* had. We assume this means no improvement is possible. |
| 880 |
* We fudge the 'solve' struct a bit to make sure we don't |
| 881 |
* return the 'bad' value as the root. |
| 882 |
*/ |
| 883 |
if (solve.x3 == solve.x1) { |
| 884 |
if (fabs(slope) > 1e50) { |
| 885 |
// Not improving because slope too steep |
| 886 |
solve.x3 = solve.x1 - (solve.x2 - solve.x1) / 100; |
| 887 |
return call_solve_fn(3, 4); |
| 888 |
} |
| 889 |
solve.which = 1; |
| 890 |
solve.curr_f = solve.fx1; |
| 891 |
solve.prev_x = solve.x2; |
| 892 |
return finish_solve(SOLVE_NOT_SURE); |
| 893 |
} |
| 894 |
if (solve.x3 == solve.x2) { |
| 895 |
if (fabs(slope) > 1e50) { |
| 896 |
// Not improving because slope too steep |
| 897 |
solve.x3 = solve.x2 + (solve.x2 - solve.x1) / 100; |
| 898 |
return call_solve_fn(3, 4); |
| 899 |
} |
| 900 |
solve.which = 2; |
| 901 |
solve.curr_f = solve.fx2; |
| 902 |
solve.prev_x = solve.x1; |
| 903 |
return finish_solve(SOLVE_NOT_SURE); |
| 904 |
} |
| 905 |
/* If we're extrapolating, make sure we don't race away from |
| 906 |
* the current interval too quickly */ |
| 907 |
if (solve.x3 < solve.x1) { |
| 908 |
phloat min = solve.x1 - 100 * (solve.x2 - solve.x1); |
| 909 |
if (solve.x3 < min) |
| 910 |
solve.x3 = min; |
| 911 |
} else if (solve.x3 > solve.x2) { |
| 912 |
phloat max = solve.x2 + 100 * (solve.x2 - solve.x1); |
| 913 |
if (solve.x3 > max) |
| 914 |
solve.x3 = max; |
| 915 |
} else { |
| 916 |
/* If we're interpolating, make sure we actually make |
| 917 |
* some progress. Enforce a minimum distance between x3 |
| 918 |
* and the edges of the interval. |
| 919 |
*/ |
| 920 |
phloat eps = (solve.x2 - solve.x1) / 10; |
| 921 |
if (solve.x3 < solve.x1 + eps) |
| 922 |
solve.x3 = solve.x1 + eps; |
| 923 |
else if (solve.x3 > solve.x2 - eps) |
| 924 |
solve.x3 = solve.x2 - eps; |
| 925 |
} |
| 926 |
return call_solve_fn(3, 4); |
| 927 |
} |
| 928 |
|
| 929 |
retry_solve: |
| 930 |
/* We hit infinity without finding two values of x where f(x) has |
| 931 |
* opposite signs, but we got to infinity suspiciously quickly. |
| 932 |
* If we started with two guesses, we now retry with only the lower |
| 933 |
* of the two; if we started with one guess, we now retry with |
| 934 |
* starting guesses of 0 and 1. |
| 935 |
*/ |
| 936 |
if (solve.retry_counter > 0) { |
| 937 |
solve.x1 = solve.retry_value; |
| 938 |
solve.x2 = solve.x1 * 1.000001; |
| 939 |
if (p_isinf(solve.x2)) |
| 940 |
solve.x2 = solve.x1 * 0.999999; |
| 941 |
if (solve.x1 > solve.x2) { |
| 942 |
phloat tmp = solve.x1; |
| 943 |
solve.x1 = solve.x2; |
| 944 |
solve.x2 = tmp; |
| 945 |
} |
| 946 |
solve.retry_counter = -10; |
| 947 |
} else { |
| 948 |
solve.x1 = 0; |
| 949 |
solve.x2 = 1; |
| 950 |
solve.retry_counter = 0; |
| 951 |
} |
| 952 |
return call_solve_fn(1, 1); |
| 953 |
|
| 954 |
do_bisection: |
| 955 |
solve.x3 = (solve.x1 + solve.x2) / 2; |
| 956 |
return call_solve_fn(3, 5); |
| 957 |
|
| 958 |
case 6: |
| 959 |
/* Ridders' method, evaluated midpoint */ |
| 960 |
if (failure) |
| 961 |
goto do_bisection; |
| 962 |
s = sqrt(f * f - solve.fx1 * solve.fx2); |
| 963 |
if (s == 0) { |
| 964 |
/* Mathematically impossible, but numerically possible if |
| 965 |
* the function is so close to zero that f^2 underflows. |
| 966 |
* We could handle this better but this seems adequate. |
| 967 |
*/ |
| 968 |
solve.which = -1; |
| 969 |
return finish_solve(SOLVE_NOT_SURE); |
| 970 |
} |
| 971 |
solve.xm = solve.x3; |
| 972 |
solve.fxm = f; |
| 973 |
if (solve.fx1 < solve.fx2) |
| 974 |
s = -s; |
| 975 |
xnew = solve.xm + (solve.xm - solve.x1) * (solve.fxm / s); |
| 976 |
if (xnew == solve.x1 || xnew == solve.x2) { |
| 977 |
solve.which = -1; |
| 978 |
return finish_solve(SOLVE_NOT_SURE); |
| 979 |
} |
| 980 |
solve.x3 = xnew; |
| 981 |
return call_solve_fn(3, 7); |
| 982 |
|
| 983 |
case 7: |
| 984 |
/* Ridders' method, evaluated xnew */ |
| 985 |
if (failure) |
| 986 |
goto do_bisection; |
| 987 |
if ((f > 0 && solve.fxm < 0) || (f < 0 && solve.fxm > 0)) { |
| 988 |
if (solve.xm < solve.x3) { |
| 989 |
solve.x1 = solve.xm; |
| 990 |
solve.fx1 = solve.fxm; |
| 991 |
solve.x2 = solve.x3; |
| 992 |
solve.fx2 = f; |
| 993 |
} else { |
| 994 |
solve.x1 = solve.x3; |
| 995 |
solve.fx1 = f; |
| 996 |
solve.x2 = solve.xm; |
| 997 |
solve.fx2 = solve.fxm; |
| 998 |
} |
| 999 |
} else if ((f > 0 && solve.fx1 < 0) || (f < 0 && solve.fx1 > 0)) { |
| 1000 |
solve.x2 = solve.x3; |
| 1001 |
solve.fx2 = f; |
| 1002 |
} else /* f > 0 && solve.fx2 < 0 || f < 0 && solve.fx2 > 0 */ { |
| 1003 |
solve.x1 = solve.x3; |
| 1004 |
solve.fx1 = f; |
| 1005 |
} |
| 1006 |
track_f_gap(); |
| 1007 |
do_ridders: |
| 1008 |
solve.x3 = (solve.x1 + solve.x2) / 2; |
| 1009 |
// TODO: The following termination condition should really be |
| 1010 |
// |
| 1011 |
// if (solve.x3 == solve.x1 || solve.x3 == solve.x2) |
| 1012 |
// |
| 1013 |
// since it is mathematically impossible for x3 to lie outside the |
| 1014 |
// interval [x1, x2], but due to decimal round-off, it is, in fact, |
| 1015 |
// possible, e.g. consider x1 = 7...2016 and x2 = 7...2018; this |
| 1016 |
// results in x3 = 7...2015. Fixing this will require either |
| 1017 |
// extended-precision math for solver internals, or at least a |
| 1018 |
// slightly smarter implementation of the midpoint calculation, |
| 1019 |
// e.g. fall back on x3 = x1 + (x2 - x1) / 2 if x3 = (x1 + x2) / 2 |
| 1020 |
// returns an incorrect result. |
| 1021 |
if (solve.x3 <= solve.x1 || solve.x3 >= solve.x2) { |
| 1022 |
solve.which = -1; |
| 1023 |
return finish_solve(SOLVE_NOT_SURE); |
| 1024 |
} else |
| 1025 |
return call_solve_fn(3, 6); |
| 1026 |
|
| 1027 |
default: |
| 1028 |
return ERR_INTERNAL_ERROR; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
static void reset_integ() { |
| 1033 |
integ.prgm_length = 0; |
| 1034 |
integ.active_prgm_length = 0; |
| 1035 |
integ.state = 0; |
| 1036 |
if (mode_appmenu == MENU_INTEG || mode_appmenu == MENU_INTEG_PARAMS) |
| 1037 |
set_menu_return_err(MENULEVEL_APP, MENU_NONE, true); |
| 1038 |
} |
| 1039 |
|
| 1040 |
void set_integ_prgm(const char *name, int length) { |
| 1041 |
string_copy(integ.prgm_name, &integ.prgm_length, name, length); |
| 1042 |
} |
| 1043 |
|
| 1044 |
void get_integ_prgm(char *name, int *length) { |
| 1045 |
string_copy(name, length, integ.prgm_name, integ.prgm_length); |
| 1046 |
} |
| 1047 |
|
| 1048 |
void set_integ_var(const char *name, int length) { |
| 1049 |
string_copy(integ.var_name, &integ.var_length, name, length); |
| 1050 |
} |
| 1051 |
|
| 1052 |
void get_integ_var(char *name, int *length) { |
| 1053 |
string_copy(name, length, integ.var_name, integ.var_length); |
| 1054 |
} |
| 1055 |
|
| 1056 |
static int call_integ_fn() { |
| 1057 |
if (integ.active_prgm_length == 0) |
| 1058 |
return ERR_NONEXISTENT; |
| 1059 |
int err, i; |
| 1060 |
arg_struct arg; |
| 1061 |
phloat x = integ.u; |
| 1062 |
vartype *v = recall_var(integ.var_name, integ.var_length); |
| 1063 |
if (v == NULL || v->type != TYPE_REAL) { |
| 1064 |
v = new_real(x); |
| 1065 |
if (v == NULL) |
| 1066 |
return ERR_INSUFFICIENT_MEMORY; |
| 1067 |
err = store_var(integ.var_name, integ.var_length, v); |
| 1068 |
if (err != ERR_NONE) { |
| 1069 |
free_vartype(v); |
| 1070 |
return err; |
| 1071 |
} |
| 1072 |
} else |
| 1073 |
((vartype_real *) v)->x = x; |
| 1074 |
arg.type = ARGTYPE_STR; |
| 1075 |
arg.length = integ.active_prgm_length; |
| 1076 |
for (i = 0; i < arg.length; i++) |
| 1077 |
arg.val.text[i] = integ.active_prgm_name[i]; |
| 1078 |
clean_stack(integ.prev_sp); |
| 1079 |
err = docmd_gto(&arg); |
| 1080 |
if (err != ERR_NONE) { |
| 1081 |
free_vartype(v); |
| 1082 |
return err; |
| 1083 |
} |
| 1084 |
err = push_rtn_addr(-3, 0); |
| 1085 |
if (err != ERR_NONE) { |
| 1086 |
current_prgm = integ.prev_prgm; |
| 1087 |
pc = integ.prev_pc; |
| 1088 |
return err; |
| 1089 |
} else |
| 1090 |
return ERR_RUN; |
| 1091 |
} |
| 1092 |
|
| 1093 |
int start_integ(const char *name, int length) { |
| 1094 |
vartype *v; |
| 1095 |
if (integ_active()) |
| 1096 |
return ERR_INTEG_INTEG; |
| 1097 |
v = recall_var("LLIM", 4); |
| 1098 |
if (v == NULL) |
| 1099 |
return ERR_NONEXISTENT; |
| 1100 |
else if (v->type == TYPE_STRING) |
| 1101 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1102 |
else if (v->type != TYPE_REAL) |
| 1103 |
return ERR_INVALID_TYPE; |
| 1104 |
integ.llim = ((vartype_real *) v)->x; |
| 1105 |
v = recall_var("ULIM", 4); |
| 1106 |
if (v == NULL) |
| 1107 |
return ERR_NONEXISTENT; |
| 1108 |
else if (v->type == TYPE_STRING) |
| 1109 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1110 |
else if (v->type != TYPE_REAL) |
| 1111 |
return ERR_INVALID_TYPE; |
| 1112 |
integ.ulim = ((vartype_real *) v)->x; |
| 1113 |
v = recall_var("ACC", 3); |
| 1114 |
if (v == NULL) |
| 1115 |
integ.acc = 0; |
| 1116 |
else if (v->type == TYPE_STRING) |
| 1117 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1118 |
else if (v->type != TYPE_REAL) |
| 1119 |
return ERR_INVALID_TYPE; |
| 1120 |
else |
| 1121 |
integ.acc = ((vartype_real *) v)->x; |
| 1122 |
if (integ.acc > 1) |
| 1123 |
integ.acc = 1; |
| 1124 |
else { |
| 1125 |
phloat eps = phloat(1) - nextafter(phloat(1), phloat(0)); |
| 1126 |
#ifdef BCD_MATH |
| 1127 |
eps *= 10; |
| 1128 |
#else |
| 1129 |
eps *= 8; |
| 1130 |
#endif |
| 1131 |
if (integ.acc < eps) |
| 1132 |
integ.acc = eps; |
| 1133 |
} |
| 1134 |
string_copy(integ.var_name, &integ.var_length, name, length); |
| 1135 |
string_copy(integ.active_prgm_name, &integ.active_prgm_length, |
| 1136 |
integ.prgm_name, integ.prgm_length); |
| 1137 |
integ.prev_prgm = current_prgm; |
| 1138 |
integ.prev_pc = pc; |
| 1139 |
integ.prev_sp = flags.f.big_stack ? sp : -2; |
| 1140 |
|
| 1141 |
integ.a = integ.llim; |
| 1142 |
integ.b = integ.ulim - integ.llim; |
| 1143 |
integ.h = 2; |
| 1144 |
integ.prev_int = 0; |
| 1145 |
integ.nsteps = 1; |
| 1146 |
integ.n = 1; |
| 1147 |
integ.state = 1; |
| 1148 |
integ.s[0] = 0; |
| 1149 |
integ.k = 1; |
| 1150 |
integ.prev_res = 0; |
| 1151 |
|
| 1152 |
integ.keep_running = !should_i_stop_at_this_level() && program_running(); |
| 1153 |
if (!integ.keep_running) { |
| 1154 |
clear_row(0); |
| 1155 |
draw_string(0, 0, "Integrating", 11); |
| 1156 |
flush_display(); |
| 1157 |
flags.f.message = 1; |
| 1158 |
flags.f.two_line_message = 0; |
| 1159 |
} |
| 1160 |
return return_to_integ(false); |
| 1161 |
} |
| 1162 |
|
| 1163 |
static int finish_integ() { |
| 1164 |
vartype *x, *y; |
| 1165 |
int saved_trace = flags.f.trace_print; |
| 1166 |
integ.state = 0; |
| 1167 |
|
| 1168 |
clean_stack(integ.prev_sp); |
| 1169 |
x = new_real(integ.sum * integ.b * 0.75); |
| 1170 |
y = new_real(integ.eps); |
| 1171 |
if (x == NULL || y == NULL) { |
| 1172 |
free_vartype(x); |
| 1173 |
free_vartype(y); |
| 1174 |
return ERR_INSUFFICIENT_MEMORY; |
| 1175 |
} |
| 1176 |
flags.f.trace_print = 0; |
| 1177 |
if (recall_two_results(x, y) != ERR_NONE) |
| 1178 |
return ERR_INSUFFICIENT_MEMORY; |
| 1179 |
flags.f.trace_print = saved_trace; |
| 1180 |
|
| 1181 |
current_prgm = integ.prev_prgm; |
| 1182 |
pc = integ.prev_pc; |
| 1183 |
|
| 1184 |
if (!integ.keep_running) { |
| 1185 |
char buf[22]; |
| 1186 |
int bufptr = 0; |
| 1187 |
string2buf(buf, 22, &bufptr, "\3=", 2); |
| 1188 |
bufptr += vartype2string(x, buf + bufptr, 22 - bufptr); |
| 1189 |
clear_row(0); |
| 1190 |
draw_string(0, 0, buf, bufptr); |
| 1191 |
flush_display(); |
| 1192 |
flags.f.message = 1; |
| 1193 |
flags.f.two_line_message = 0; |
| 1194 |
if ((flags.f.trace_print || flags.f.normal_print) && flags.f.printer_exists) |
| 1195 |
print_wide(buf, 2, buf + 2, bufptr - 2); |
| 1196 |
return ERR_STOP; |
| 1197 |
} else |
| 1198 |
return ERR_NONE; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
/* approximate integral of `f' between `a' and `b' subject to a given |
| 1203 |
* error. Use Romberg method with refinement substitution, x = (3u-u^3)/2 |
| 1204 |
* which prevents endpoint evaluation and causes non-uniform sampling. |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
int return_to_integ(bool stop) { |
| 1208 |
if (stop) |
| 1209 |
integ.keep_running = 0; |
| 1210 |
|
| 1211 |
switch (integ.state) { |
| 1212 |
case 0: |
| 1213 |
return ERR_INTERNAL_ERROR; |
| 1214 |
|
| 1215 |
case 1: |
| 1216 |
integ.state = 2; |
| 1217 |
|
| 1218 |
loop1: |
| 1219 |
|
| 1220 |
integ.p = integ.h / 2 - 1; |
| 1221 |
integ.sum = 0.0; |
| 1222 |
integ.i = 0; |
| 1223 |
|
| 1224 |
loop2: |
| 1225 |
|
| 1226 |
integ.t = 1 - integ.p * integ.p; |
| 1227 |
integ.u = integ.p + integ.t * integ.p / 2; |
| 1228 |
integ.u = (integ.u * integ.b + integ.b) / 2 + integ.a; |
| 1229 |
return call_integ_fn(); |
| 1230 |
|
| 1231 |
case 2: |
| 1232 |
if (sp == -1) |
| 1233 |
return ERR_TOO_FEW_ARGUMENTS; |
| 1234 |
if (stack[sp]->type == TYPE_STRING) |
| 1235 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1236 |
else if (stack[sp]->type != TYPE_REAL) |
| 1237 |
return ERR_INVALID_TYPE; |
| 1238 |
integ.sum += integ.t * ((vartype_real *) stack[sp])->x; |
| 1239 |
integ.p += integ.h; |
| 1240 |
if (++integ.i < integ.nsteps) |
| 1241 |
goto loop2; |
| 1242 |
|
| 1243 |
// update integral moving resuslt |
| 1244 |
integ.prev_int = (integ.prev_int + integ.sum*integ.h)/2; |
| 1245 |
integ.s[integ.k++] = integ.prev_int; |
| 1246 |
|
| 1247 |
if (integ.n >= ROMB_K-1) { |
| 1248 |
int i, m; |
| 1249 |
int ns = ROMB_K-1; |
| 1250 |
phloat dm = 1; |
| 1251 |
for (i = 0; i < ROMB_K; ++i) integ.c[i] = integ.s[i]; |
| 1252 |
integ.sum = integ.s[ns]; |
| 1253 |
for (m = 1; m < ROMB_K; ++m) { |
| 1254 |
dm /= 4; |
| 1255 |
for (i = 0; i < ROMB_K-m; ++i) |
| 1256 |
integ.c[i] = (integ.c[i+1]-integ.c[i]*dm*4)/(1-dm); |
| 1257 |
integ.sum += integ.c[--ns]*dm; |
| 1258 |
} |
| 1259 |
|
| 1260 |
phloat res = integ.sum * integ.b * 0.75; |
| 1261 |
integ.eps = fabs(integ.prev_res - res); |
| 1262 |
integ.prev_res = res; |
| 1263 |
if (integ.eps <= integ.acc * fabs(res)) |
| 1264 |
// done! |
| 1265 |
return finish_integ(); |
| 1266 |
|
| 1267 |
for (i = 0; i < ROMB_K-1; ++i) integ.s[i] = integ.s[i+1]; |
| 1268 |
integ.k = ROMB_K-1; |
| 1269 |
} |
| 1270 |
|
| 1271 |
integ.nsteps <<= 1; |
| 1272 |
integ.h /= 2.0; |
| 1273 |
|
| 1274 |
if (++integ.n >= ROMB_MAX) |
| 1275 |
return finish_integ(); // too many |
| 1276 |
|
| 1277 |
goto loop1; |
| 1278 |
default: |
| 1279 |
return ERR_INTERNAL_ERROR; |
| 1280 |
} |
| 1281 |
} |