| 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 |
#include <string.h> |
| 20 |
#include <limits.h> |
| 21 |
|
| 22 |
#include "core_commands1.h" |
| 23 |
#include "core_commands2.h" |
| 24 |
#include "core_commands3.h" |
| 25 |
#include "core_commands4.h" |
| 26 |
#include "core_display.h" |
| 27 |
#include "core_helpers.h" |
| 28 |
#include "core_linalg1.h" |
| 29 |
#include "core_main.h" |
| 30 |
#include "core_math2.h" |
| 31 |
#include "core_sto_rcl.h" |
| 32 |
#include "core_variables.h" |
| 33 |
|
| 34 |
/********************************************************/ |
| 35 |
/* Implementations of HP-42S built-in functions, part 3 */ |
| 36 |
/********************************************************/ |
| 37 |
|
| 38 |
static int mappable_acosh_r(phloat x, phloat *y) { |
| 39 |
if (x >= 1) { |
| 40 |
*y = acosh(x); |
| 41 |
return ERR_NONE; |
| 42 |
} else |
| 43 |
return ERR_INVALID_DATA; |
| 44 |
} |
| 45 |
|
| 46 |
static int mappable_acosh_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 47 |
return math_acosh(xre, xim, yre, yim); |
| 48 |
} |
| 49 |
|
| 50 |
int docmd_acosh(arg_struct *arg) { |
| 51 |
vartype *v; |
| 52 |
if (stack[sp]->type == TYPE_REAL) { |
| 53 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 54 |
if (x < 1) { |
| 55 |
if (flags.f.real_result_only) |
| 56 |
return ERR_INVALID_DATA; |
| 57 |
else { |
| 58 |
phloat re, im; |
| 59 |
int err = math_acosh(x, 0, &re, &im); |
| 60 |
if (err != ERR_NONE) |
| 61 |
return err; |
| 62 |
v = new_complex(re, im); |
| 63 |
} |
| 64 |
} else |
| 65 |
v = new_real(acosh(x)); |
| 66 |
if (v == NULL) |
| 67 |
return ERR_INSUFFICIENT_MEMORY; |
| 68 |
} else { |
| 69 |
int err = map_unary(stack[sp], &v, mappable_acosh_r, mappable_acosh_c); |
| 70 |
if (err != ERR_NONE) |
| 71 |
return err; |
| 72 |
} |
| 73 |
unary_result(v); |
| 74 |
return ERR_NONE; |
| 75 |
} |
| 76 |
|
| 77 |
int docmd_aleng(arg_struct *arg) { |
| 78 |
vartype *v = new_real(reg_alpha_length); |
| 79 |
if (v == NULL) |
| 80 |
return ERR_INSUFFICIENT_MEMORY; |
| 81 |
return recall_result(v); |
| 82 |
} |
| 83 |
|
| 84 |
int docmd_aoff(arg_struct *arg) { |
| 85 |
set_menu(MENULEVEL_ALPHA, MENU_NONE); |
| 86 |
return ERR_NONE; |
| 87 |
} |
| 88 |
|
| 89 |
int docmd_aon(arg_struct *arg) { |
| 90 |
mode_alpha_entry = false; |
| 91 |
set_menu(MENULEVEL_ALPHA, MENU_ALPHA1); |
| 92 |
return ERR_NONE; |
| 93 |
} |
| 94 |
|
| 95 |
int docmd_arot(arg_struct *arg) { |
| 96 |
phloat x; |
| 97 |
char buf[44]; |
| 98 |
int i, j; |
| 99 |
if (reg_alpha_length == 0) |
| 100 |
goto done; |
| 101 |
x = ((vartype_real *) stack[sp])->x; |
| 102 |
if (x < 0) |
| 103 |
x = -floor(-x); |
| 104 |
else |
| 105 |
x = floor(x); |
| 106 |
j = to_int(fmod(x, reg_alpha_length)); |
| 107 |
if (j == 0) |
| 108 |
goto done; |
| 109 |
if (j < 0) |
| 110 |
j += reg_alpha_length; |
| 111 |
for (i = 0; i < reg_alpha_length; i++) |
| 112 |
buf[i] = reg_alpha[i]; |
| 113 |
for (i = 0; i < reg_alpha_length; i++) |
| 114 |
reg_alpha[i] = buf[(i + j) % reg_alpha_length]; |
| 115 |
done: |
| 116 |
if (flags.f.trace_print && flags.f.printer_exists) |
| 117 |
docmd_pra(NULL); |
| 118 |
return ERR_NONE; |
| 119 |
} |
| 120 |
|
| 121 |
int docmd_ashf(arg_struct *arg) { |
| 122 |
int i; |
| 123 |
reg_alpha_length -= 6; |
| 124 |
if (reg_alpha_length < 0) |
| 125 |
reg_alpha_length = 0; |
| 126 |
for (i = 0; i < reg_alpha_length; i++) |
| 127 |
reg_alpha[i] = reg_alpha[i + 6]; |
| 128 |
if (flags.f.trace_print && flags.f.printer_exists) |
| 129 |
docmd_pra(NULL); |
| 130 |
return ERR_NONE; |
| 131 |
} |
| 132 |
|
| 133 |
static int mappable_asinh_r(phloat x, phloat *y) { |
| 134 |
*y = asinh(x); |
| 135 |
return ERR_NONE; |
| 136 |
} |
| 137 |
|
| 138 |
static int mappable_asinh_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 139 |
return math_asinh(xre, xim, yre, yim); |
| 140 |
} |
| 141 |
|
| 142 |
int docmd_asinh(arg_struct *arg) { |
| 143 |
vartype *v; |
| 144 |
int err = map_unary(stack[sp], &v, mappable_asinh_r, mappable_asinh_c); |
| 145 |
if (err == ERR_NONE) |
| 146 |
unary_result(v); |
| 147 |
return err; |
| 148 |
} |
| 149 |
|
| 150 |
static int mappable_atanh_r(phloat x, phloat *y) { |
| 151 |
if (x == 1 || x == -1) |
| 152 |
return ERR_INVALID_DATA; |
| 153 |
*y = atanh(x); |
| 154 |
|
| 155 |
/* Theoretically, you could go out of range, but in practice, |
| 156 |
* you can't get close enough to the critical values to cause |
| 157 |
* trouble. |
| 158 |
*/ |
| 159 |
return ERR_NONE; |
| 160 |
} |
| 161 |
|
| 162 |
int docmd_atanh(arg_struct *arg) { |
| 163 |
vartype *v; |
| 164 |
int err; |
| 165 |
if (stack[sp]->type == TYPE_REAL) { |
| 166 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 167 |
if (x == 1 || x == -1) |
| 168 |
return ERR_INVALID_DATA; |
| 169 |
if (x < -1 || x > 1) { |
| 170 |
if (flags.f.real_result_only) |
| 171 |
return ERR_INVALID_DATA; |
| 172 |
else { |
| 173 |
phloat tre, tim; |
| 174 |
err = math_atanh(x, 0, &tre, &tim); |
| 175 |
if (err != ERR_NONE) |
| 176 |
return err; |
| 177 |
v = new_complex(tre, tim); |
| 178 |
} |
| 179 |
} else |
| 180 |
v = new_real(atanh(x)); |
| 181 |
if (v == NULL) |
| 182 |
return ERR_INSUFFICIENT_MEMORY; |
| 183 |
unary_result(v); |
| 184 |
return ERR_NONE; |
| 185 |
} else { |
| 186 |
err = map_unary(stack[sp], &v, mappable_atanh_r, math_atanh); |
| 187 |
if (err == ERR_NONE) |
| 188 |
unary_result(v); |
| 189 |
return err; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
int docmd_atox(arg_struct *arg) { |
| 194 |
vartype *v = new_real(reg_alpha_length == 0 ? 0 : |
| 195 |
(unsigned char) reg_alpha[0]); |
| 196 |
if (v == NULL) |
| 197 |
return ERR_INSUFFICIENT_MEMORY; |
| 198 |
if (reg_alpha_length > 0) { |
| 199 |
int i; |
| 200 |
reg_alpha_length--; |
| 201 |
for (i = 0; i < reg_alpha_length; i++) |
| 202 |
reg_alpha[i] = reg_alpha[i + 1]; |
| 203 |
} |
| 204 |
if (flags.f.trace_print && flags.f.printer_exists) |
| 205 |
docmd_pra(NULL); |
| 206 |
return recall_result(v); |
| 207 |
} |
| 208 |
|
| 209 |
static int mappable_cosh_r(phloat x, phloat *y) { |
| 210 |
int inf; |
| 211 |
*y = cosh(x); |
| 212 |
if ((inf = p_isinf(*y)) != 0) { |
| 213 |
if (flags.f.range_error_ignore) |
| 214 |
*y = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 215 |
else |
| 216 |
return ERR_OUT_OF_RANGE; |
| 217 |
} |
| 218 |
return ERR_NONE; |
| 219 |
} |
| 220 |
|
| 221 |
static int mappable_cosh_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 222 |
if (xim == 0) { |
| 223 |
*yim = 0; |
| 224 |
return mappable_cosh_r(xre, yre); |
| 225 |
} else if (xre == 0) { |
| 226 |
*yre = cos(xim); |
| 227 |
*yim = 0; |
| 228 |
return ERR_NONE; |
| 229 |
} |
| 230 |
|
| 231 |
phloat sinhxre, coshxre; |
| 232 |
phloat sinxim, cosxim; |
| 233 |
int inf; |
| 234 |
sinhxre = sinh(xre); |
| 235 |
coshxre = cosh(xre); |
| 236 |
p_sincos(xim, &sinxim, &cosxim); |
| 237 |
*yre = coshxre * cosxim; |
| 238 |
if ((inf = p_isinf(*yre)) != 0) { |
| 239 |
if (flags.f.range_error_ignore) |
| 240 |
*yre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 241 |
else |
| 242 |
return ERR_OUT_OF_RANGE; |
| 243 |
} |
| 244 |
*yim = sinhxre * sinxim; |
| 245 |
if ((inf = p_isinf(*yim)) != 0) { |
| 246 |
if (flags.f.range_error_ignore) |
| 247 |
*yim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 248 |
else |
| 249 |
return ERR_OUT_OF_RANGE; |
| 250 |
} |
| 251 |
return ERR_NONE; |
| 252 |
} |
| 253 |
|
| 254 |
int docmd_cosh(arg_struct *arg) { |
| 255 |
vartype *v; |
| 256 |
int err = map_unary(stack[sp], &v, mappable_cosh_r, mappable_cosh_c); |
| 257 |
if (err == ERR_NONE) |
| 258 |
unary_result(v); |
| 259 |
return err; |
| 260 |
} |
| 261 |
|
| 262 |
int docmd_cross(arg_struct *arg) { |
| 263 |
if (stack[sp]->type == TYPE_STRING || stack[sp - 1]->type == TYPE_STRING) |
| 264 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 265 |
else if (stack[sp]->type == TYPE_COMPLEX && stack[sp - 1]->type == TYPE_COMPLEX) { |
| 266 |
vartype_complex *left = (vartype_complex *) stack[sp - 1]; |
| 267 |
vartype_complex *right = (vartype_complex *) stack[sp]; |
| 268 |
vartype *v; |
| 269 |
phloat d = left->re * right->im - left->im * right->re; |
| 270 |
int inf; |
| 271 |
if ((inf = p_isinf(d)) != 0) { |
| 272 |
if (flags.f.range_error_ignore) |
| 273 |
d = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 274 |
else |
| 275 |
return ERR_OUT_OF_RANGE; |
| 276 |
} |
| 277 |
v = new_real(d); |
| 278 |
if (v == NULL) |
| 279 |
return ERR_INSUFFICIENT_MEMORY; |
| 280 |
return binary_result(v); |
| 281 |
} else if (stack[sp]->type == TYPE_REALMATRIX |
| 282 |
&& stack[sp - 1]->type == TYPE_REALMATRIX) { |
| 283 |
vartype_realmatrix *left = (vartype_realmatrix *) stack[sp - 1]; |
| 284 |
vartype_realmatrix *right = (vartype_realmatrix *) stack[sp]; |
| 285 |
int4 ls = left->rows * left->columns; |
| 286 |
int4 rs = right->rows * right->columns; |
| 287 |
int4 i; |
| 288 |
int inf; |
| 289 |
phloat xl, yl = 0, zl = 0, xr, yr = 0, zr = 0; |
| 290 |
phloat xres, yres, zres; |
| 291 |
vartype_realmatrix *res; |
| 292 |
if (ls > 3 || rs > 3) |
| 293 |
return ERR_DIMENSION_ERROR; |
| 294 |
for (i = 0; i < ls; i++) |
| 295 |
if (left->array->is_string[i] != 0) |
| 296 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 297 |
for (i = 0; i < rs; i++) |
| 298 |
if (right->array->is_string[i] != 0) |
| 299 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 300 |
switch (ls) { |
| 301 |
case 3: zl = left->array->data[2]; |
| 302 |
case 2: yl = left->array->data[1]; |
| 303 |
case 1: xl = left->array->data[0]; |
| 304 |
} |
| 305 |
switch (rs) { |
| 306 |
case 3: zr = right->array->data[2]; |
| 307 |
case 2: yr = right->array->data[1]; |
| 308 |
case 1: xr = right->array->data[0]; |
| 309 |
} |
| 310 |
xres = yl * zr - zl * yr; |
| 311 |
if ((inf = p_isinf(xres)) != 0) { |
| 312 |
if (flags.f.range_error_ignore) |
| 313 |
xres = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 314 |
else |
| 315 |
return ERR_OUT_OF_RANGE; |
| 316 |
} |
| 317 |
yres = zl * xr - xl * zr; |
| 318 |
if ((inf = p_isinf(yres)) != 0) { |
| 319 |
if (flags.f.range_error_ignore) |
| 320 |
yres = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 321 |
else |
| 322 |
return ERR_OUT_OF_RANGE; |
| 323 |
} |
| 324 |
zres = xl * yr - yl * xr; |
| 325 |
if ((inf = p_isinf(zres)) != 0) { |
| 326 |
if (flags.f.range_error_ignore) |
| 327 |
zres = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 328 |
else |
| 329 |
return ERR_OUT_OF_RANGE; |
| 330 |
} |
| 331 |
res = (vartype_realmatrix *) new_realmatrix(1, 3); |
| 332 |
if (res == NULL) |
| 333 |
return ERR_INSUFFICIENT_MEMORY; |
| 334 |
res->array->data[0] = xres; |
| 335 |
res->array->data[1] = yres; |
| 336 |
res->array->data[2] = zres; |
| 337 |
return binary_result((vartype *) res); |
| 338 |
} else |
| 339 |
return ERR_INVALID_TYPE; |
| 340 |
} |
| 341 |
|
| 342 |
int docmd_custom(arg_struct *arg) { |
| 343 |
if (mode_plainmenu != MENU_CUSTOM1 |
| 344 |
&& mode_plainmenu != MENU_CUSTOM2 |
| 345 |
&& mode_plainmenu != MENU_CUSTOM3) |
| 346 |
set_plainmenu(MENU_CUSTOM1, NULL, 0); |
| 347 |
return ERR_NONE; |
| 348 |
} |
| 349 |
|
| 350 |
int docmd_delr(arg_struct *arg) { |
| 351 |
vartype *m, *newx; |
| 352 |
vartype_realmatrix *rm; |
| 353 |
vartype_complexmatrix *cm; |
| 354 |
vartype_list *list; |
| 355 |
int4 rows, columns, i, j, n, newi; |
| 356 |
int err, refcount; |
| 357 |
int interactive; |
| 358 |
|
| 359 |
err = matedit_get(&m); |
| 360 |
if (err != ERR_NONE) |
| 361 |
return err; |
| 362 |
|
| 363 |
if (m->type == TYPE_REALMATRIX) { |
| 364 |
rm = (vartype_realmatrix *) m; |
| 365 |
rows = rm->rows; |
| 366 |
columns = rm->columns; |
| 367 |
refcount = rm->array->refcount; |
| 368 |
} else if (m->type == TYPE_COMPLEXMATRIX) { |
| 369 |
cm = (vartype_complexmatrix *) m; |
| 370 |
rows = cm->rows; |
| 371 |
columns = cm->columns; |
| 372 |
refcount = cm->array->refcount; |
| 373 |
} else { |
| 374 |
list = (vartype_list *) m; |
| 375 |
rows = list->size; |
| 376 |
columns = 1; |
| 377 |
refcount = list->array->refcount; |
| 378 |
} |
| 379 |
|
| 380 |
if (rows == 1 && m->type != TYPE_LIST) |
| 381 |
return ERR_DIMENSION_ERROR; |
| 382 |
|
| 383 |
if (matedit_i >= rows) |
| 384 |
matedit_i = rows - 1; |
| 385 |
if (matedit_j >= columns) |
| 386 |
matedit_j = columns - 1; |
| 387 |
n = (matedit_i + 1) * columns + matedit_j; |
| 388 |
|
| 389 |
if (matedit_i == rows - 1) { |
| 390 |
/* Deleting the bottom row; we need to move up one row, and |
| 391 |
* the next IJ value we show in X is going to be from *two* rows |
| 392 |
* higher: if you delete row 4 and it's not the bottom row, then |
| 393 |
* we'll show the value that's now in row 5; if it is the bottom |
| 394 |
* row, we move up one, and show the value that's now in row 3. |
| 395 |
*/ |
| 396 |
newi = matedit_i - 1; |
| 397 |
n -= 2 * columns; |
| 398 |
} else |
| 399 |
newi = matedit_i; |
| 400 |
|
| 401 |
interactive = matedit_mode == 2 || matedit_mode == 3; |
| 402 |
if (interactive) { |
| 403 |
if (m->type == TYPE_REALMATRIX) { |
| 404 |
if (rm->array->is_string[n] != 0) { |
| 405 |
char *text; |
| 406 |
int4 len; |
| 407 |
get_matrix_string(rm, n, &text, &len); |
| 408 |
newx = new_string(text, len); |
| 409 |
} else |
| 410 |
newx = new_real(rm->array->data[n]); |
| 411 |
} else if (m->type == TYPE_COMPLEXMATRIX) { |
| 412 |
newx = new_complex(cm->array->data[2 * n], |
| 413 |
cm->array->data[2 * n + 1]); |
| 414 |
} else { |
| 415 |
if (n < 0) |
| 416 |
newx = new_real(0); |
| 417 |
else |
| 418 |
newx = dup_vartype(list->array->data[n]); |
| 419 |
} |
| 420 |
|
| 421 |
if (newx == NULL) |
| 422 |
return ERR_INSUFFICIENT_MEMORY; |
| 423 |
} |
| 424 |
|
| 425 |
if (refcount == 1) { |
| 426 |
/* We have this array to ourselves so we can modify it in place |
| 427 |
* We shuffle the array elements around so that if the reallocation |
| 428 |
* succeeds, everything is in the right place, but if it fails, we |
| 429 |
* still have all the data so we can roll everything back. And best |
| 430 |
* of all, no temporary memory allocations needed! |
| 431 |
*/ |
| 432 |
if (m->type == TYPE_REALMATRIX) { |
| 433 |
for (j = 0; j < columns; j++) { |
| 434 |
phloat tempd = rm->array->data[matedit_i * columns + j]; |
| 435 |
char tempc = rm->array->is_string[matedit_i * columns + j]; |
| 436 |
for (i = matedit_i; i < rows - 1; i++) { |
| 437 |
rm->array->data[i * columns + j] = |
| 438 |
rm->array->data[(i + 1) * columns + j]; |
| 439 |
rm->array->is_string[i * columns + j] = |
| 440 |
rm->array->is_string[(i + 1) * columns + j]; |
| 441 |
} |
| 442 |
rm->array->data[(rows - 1) * columns + j] = tempd; |
| 443 |
rm->array->is_string[(rows - 1) * columns + j] = tempc; |
| 444 |
} |
| 445 |
err = dimension_array_ref(m, rows - 1, columns); |
| 446 |
if (err != ERR_NONE) { |
| 447 |
/* Dang! Now we have to rotate everything back to where |
| 448 |
* it was before. */ |
| 449 |
for (j = 0; j < columns; j++) { |
| 450 |
phloat tempd = rm->array->data[(rows - 1) * columns + j]; |
| 451 |
char tempc = rm->array->is_string[(rows - 1) * columns + j]; |
| 452 |
for (i = rows - 1; i > matedit_i; i--) { |
| 453 |
rm->array->data[i * columns + j] = |
| 454 |
rm->array->data[(i - 1) * columns + j]; |
| 455 |
rm->array->is_string[i * columns + j] = |
| 456 |
rm->array->is_string[(i - 1) * columns + j]; |
| 457 |
} |
| 458 |
rm->array->data[matedit_i * columns + j] = tempd; |
| 459 |
rm->array->is_string[matedit_i * columns + j] = tempc; |
| 460 |
} |
| 461 |
if (interactive) |
| 462 |
free_vartype(newx); |
| 463 |
return err; |
| 464 |
} |
| 465 |
} else if (m->type == TYPE_COMPLEXMATRIX) { |
| 466 |
for (j = 0; j < 2 * columns; j++) { |
| 467 |
phloat tempd = cm->array->data[matedit_i * 2 * columns + j]; |
| 468 |
for (i = matedit_i; i < rows - 1; i++) |
| 469 |
cm->array->data[i * 2 * columns + j] = |
| 470 |
cm->array->data[(i + 1) * 2 * columns + j]; |
| 471 |
cm->array->data[(rows - 1) * 2 * columns + j] = tempd; |
| 472 |
} |
| 473 |
err = dimension_array_ref(m, rows - 1, columns); |
| 474 |
if (err != ERR_NONE) { |
| 475 |
/* Dang! Now we have to rotate everything back to where |
| 476 |
* it was before. */ |
| 477 |
for (j = 0; j < 2 * columns; j++) { |
| 478 |
phloat tempd = cm->array->data[(rows - 1) * 2 * columns + j]; |
| 479 |
for (i = rows - 1; i > matedit_i; i--) |
| 480 |
cm->array->data[i * 2 * columns + j] = |
| 481 |
cm->array->data[(i - 1) * 2 * columns + j]; |
| 482 |
cm->array->data[matedit_i * 2 * columns + j] = tempd; |
| 483 |
} |
| 484 |
if (interactive) |
| 485 |
free_vartype(newx); |
| 486 |
return err; |
| 487 |
} |
| 488 |
} else /* m->type == TYPE_LIST */ { |
| 489 |
/* This one is easy because shrinking an unshared list always succeeds. */ |
| 490 |
vartype *tmp = list->array->data[matedit_i]; |
| 491 |
memmove(list->array->data + matedit_i, list->array->data + matedit_i + 1, |
| 492 |
(rows - matedit_i - 1) * sizeof(vartype *)); |
| 493 |
list->array->data[rows - 1] = tmp; |
| 494 |
dimension_array_ref(m, rows - 1, 1); |
| 495 |
} |
| 496 |
} else { |
| 497 |
/* We're sharing this array. I don't use disentangle() because it |
| 498 |
* does not deal with resizing. */ |
| 499 |
int4 newsize = (rows - 1) * columns; |
| 500 |
if (m->type == TYPE_REALMATRIX) { |
| 501 |
realmatrix_data *array = (realmatrix_data *) |
| 502 |
malloc(sizeof(realmatrix_data)); |
| 503 |
if (array == NULL) { |
| 504 |
if (interactive) |
| 505 |
free_vartype(newx); |
| 506 |
return ERR_INSUFFICIENT_MEMORY; |
| 507 |
} |
| 508 |
array->data = (phloat *) malloc(newsize * sizeof(phloat)); |
| 509 |
if (array->data == NULL) { |
| 510 |
if (interactive) |
| 511 |
free_vartype(newx); |
| 512 |
free(array); |
| 513 |
return ERR_INSUFFICIENT_MEMORY; |
| 514 |
} |
| 515 |
array->is_string = (char *) malloc(newsize); |
| 516 |
if (array->is_string == NULL) { |
| 517 |
if (interactive) |
| 518 |
free_vartype(newx); |
| 519 |
free(array->data); |
| 520 |
free(array); |
| 521 |
return ERR_INSUFFICIENT_MEMORY; |
| 522 |
} |
| 523 |
for (i = 0; i < matedit_i * columns; i++) { |
| 524 |
array->is_string[i] = rm->array->is_string[i]; |
| 525 |
array->data[i] = rm->array->data[i]; |
| 526 |
} |
| 527 |
for (i = matedit_i * columns; i < (matedit_i + 1) * columns; i++) |
| 528 |
if (array->is_string[i] == 2) |
| 529 |
free(*(void **) &array->data[i]); |
| 530 |
for (i = matedit_i * columns; i < newsize; i++) { |
| 531 |
array->is_string[i] = rm->array->is_string[i + columns]; |
| 532 |
array->data[i] = rm->array->data[i + columns]; |
| 533 |
} |
| 534 |
array->refcount = 1; |
| 535 |
rm->array->refcount--; |
| 536 |
rm->array = array; |
| 537 |
rm->rows--; |
| 538 |
} else if (m->type == TYPE_COMPLEXMATRIX) { |
| 539 |
complexmatrix_data *array = (complexmatrix_data *) |
| 540 |
malloc(sizeof(complexmatrix_data)); |
| 541 |
if (array == NULL) { |
| 542 |
if (interactive) |
| 543 |
free_vartype(newx); |
| 544 |
return ERR_INSUFFICIENT_MEMORY; |
| 545 |
} |
| 546 |
array->data = (phloat *) malloc(2 * newsize * sizeof(phloat)); |
| 547 |
if (array->data == NULL) { |
| 548 |
if (interactive) |
| 549 |
free_vartype(newx); |
| 550 |
free(array); |
| 551 |
return ERR_INSUFFICIENT_MEMORY; |
| 552 |
} |
| 553 |
for (i = 0; i < 2 * matedit_i * columns; i++) |
| 554 |
array->data[i] = cm->array->data[i]; |
| 555 |
for (i = 2 * matedit_i * columns; i < 2 * newsize; i++) |
| 556 |
array->data[i] = cm->array->data[i + 2 * columns]; |
| 557 |
array->refcount = 1; |
| 558 |
cm->array->refcount--; |
| 559 |
cm->array = array; |
| 560 |
cm->rows--; |
| 561 |
} else /* m->type == TYPE_LIST */ { |
| 562 |
list_data *array = (list_data *) malloc(sizeof(list_data)); |
| 563 |
if (array == NULL) { |
| 564 |
if (interactive) |
| 565 |
free_vartype(newx); |
| 566 |
return ERR_INSUFFICIENT_MEMORY; |
| 567 |
} |
| 568 |
array->data = (vartype **) malloc(newsize * sizeof(vartype *)); |
| 569 |
if (array->data == NULL) { |
| 570 |
if (interactive) |
| 571 |
free_vartype(newx); |
| 572 |
free(array); |
| 573 |
return ERR_INSUFFICIENT_MEMORY; |
| 574 |
} |
| 575 |
for (int4 i = 0; i < newsize; i++) { |
| 576 |
array->data[i] = dup_vartype(list->array->data[i < matedit_i ? i : i + 1]); |
| 577 |
if (array->data[i] == NULL) { |
| 578 |
for (int4 j = 0; j < i; j++) |
| 579 |
free_vartype(array->data[j]); |
| 580 |
if (interactive) |
| 581 |
free_vartype(newx); |
| 582 |
free(array->data); |
| 583 |
free(array); |
| 584 |
return ERR_INSUFFICIENT_MEMORY; |
| 585 |
} |
| 586 |
} |
| 587 |
array->refcount = 1; |
| 588 |
list->array->refcount--; |
| 589 |
list->array = array; |
| 590 |
list->size--; |
| 591 |
} |
| 592 |
} |
| 593 |
if (interactive) { |
| 594 |
if (sp == -1) |
| 595 |
sp = 0; |
| 596 |
else |
| 597 |
free_vartype(stack[sp]); |
| 598 |
stack[sp] = newx; |
| 599 |
mode_disable_stack_lift = true; |
| 600 |
} |
| 601 |
matedit_i = newi; |
| 602 |
return ERR_NONE; |
| 603 |
} |
| 604 |
|
| 605 |
static int det_completion(int error, vartype *det) { |
| 606 |
if (error == ERR_NONE) |
| 607 |
unary_result(det); |
| 608 |
return error; |
| 609 |
} |
| 610 |
|
| 611 |
int docmd_det(arg_struct *arg) { |
| 612 |
return linalg_det(stack[sp], det_completion); |
| 613 |
} |
| 614 |
|
| 615 |
int docmd_dim(arg_struct *arg) { |
| 616 |
phloat x, y; |
| 617 |
int err; |
| 618 |
if (arg->type == ARGTYPE_IND_NUM |
| 619 |
|| arg->type == ARGTYPE_IND_STK |
| 620 |
|| arg->type == ARGTYPE_IND_STR) { |
| 621 |
err = resolve_ind_arg(arg); |
| 622 |
if (err != ERR_NONE) |
| 623 |
return err; |
| 624 |
} |
| 625 |
if (arg->type != ARGTYPE_STR) |
| 626 |
return ERR_INVALID_TYPE; |
| 627 |
|
| 628 |
x = ((vartype_real *) stack[sp])->x; |
| 629 |
if (x == 0) |
| 630 |
return ERR_DIMENSION_ERROR; |
| 631 |
if (x < 0) |
| 632 |
x = -x; |
| 633 |
if (x >= 2147483648.0) |
| 634 |
return ERR_INSUFFICIENT_MEMORY; |
| 635 |
y = ((vartype_real *) stack[sp - 1])->x; |
| 636 |
if (y == 0) |
| 637 |
return ERR_DIMENSION_ERROR; |
| 638 |
if (y < 0) |
| 639 |
y = -y; |
| 640 |
if (y >= 2147483648.0) |
| 641 |
return ERR_INSUFFICIENT_MEMORY; |
| 642 |
return dimension_array(arg->val.text, arg->length, to_int(y), to_int(x), true); |
| 643 |
} |
| 644 |
|
| 645 |
int docmd_dot(arg_struct *arg) { |
| 646 |
/* TODO: look for range errors in intermediate results. |
| 647 |
* Right now, 1e6000+1e6000i DOT 1e6000-1e6000i returns NaN |
| 648 |
* in the Decimal build, because two infinities of opposite |
| 649 |
* signs are added. 1e300+1e300i DOT 1e300-1e300i probably |
| 650 |
* does the same in the Binary build. |
| 651 |
*/ |
| 652 |
vartype *v; |
| 653 |
if (stack[sp]->type == TYPE_STRING || stack[sp - 1]->type == TYPE_STRING) |
| 654 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 655 |
else if (stack[sp]->type == TYPE_REALMATRIX && stack[sp - 1]->type == TYPE_REALMATRIX) { |
| 656 |
vartype_realmatrix *rm1 = (vartype_realmatrix *) stack[sp]; |
| 657 |
vartype_realmatrix *rm2 = (vartype_realmatrix *) stack[sp - 1]; |
| 658 |
int4 size = rm1->rows * rm1->columns; |
| 659 |
int4 i; |
| 660 |
phloat dot = 0; |
| 661 |
int inf; |
| 662 |
if (size != rm2->rows * rm2->columns) |
| 663 |
return ERR_DIMENSION_ERROR; |
| 664 |
if (contains_strings(rm1) || contains_strings(rm2)) |
| 665 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 666 |
for (i = 0; i < size; i++) |
| 667 |
dot += rm1->array->data[i] * rm2->array->data[i]; |
| 668 |
if ((inf = p_isinf(dot)) != 0) { |
| 669 |
if (flags.f.range_error_ignore) |
| 670 |
dot = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 671 |
else |
| 672 |
return ERR_OUT_OF_RANGE; |
| 673 |
} |
| 674 |
v = new_real(dot); |
| 675 |
} else if ((stack[sp]->type == TYPE_REALMATRIX |
| 676 |
&& stack[sp - 1]->type == TYPE_COMPLEXMATRIX) |
| 677 |
|| |
| 678 |
(stack[sp]->type == TYPE_COMPLEXMATRIX |
| 679 |
&& stack[sp - 1]->type == TYPE_REALMATRIX)) { |
| 680 |
vartype_realmatrix *rm; |
| 681 |
vartype_complexmatrix *cm; |
| 682 |
int4 size, i; |
| 683 |
phloat dot_re = 0, dot_im = 0; |
| 684 |
int inf; |
| 685 |
if (stack[sp]->type == TYPE_REALMATRIX) { |
| 686 |
rm = (vartype_realmatrix *) stack[sp]; |
| 687 |
cm = (vartype_complexmatrix *) stack[sp - 1]; |
| 688 |
} else { |
| 689 |
rm = (vartype_realmatrix *) stack[sp - 1]; |
| 690 |
cm = (vartype_complexmatrix *) stack[sp]; |
| 691 |
} |
| 692 |
size = rm->rows * rm->columns; |
| 693 |
if (size != cm->rows * cm->columns) |
| 694 |
return ERR_DIMENSION_ERROR; |
| 695 |
if (contains_strings(rm)) |
| 696 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 697 |
for (i = 0; i < size; i++) { |
| 698 |
dot_re += rm->array->data[i] * cm->array->data[2 * i]; |
| 699 |
dot_im += rm->array->data[i] * cm->array->data[2 * i + 1]; |
| 700 |
} |
| 701 |
if ((inf = p_isinf(dot_re)) != 0) { |
| 702 |
if (flags.f.range_error_ignore) |
| 703 |
dot_re = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 704 |
else |
| 705 |
return ERR_OUT_OF_RANGE; |
| 706 |
} |
| 707 |
if ((inf = p_isinf(dot_im)) != 0) { |
| 708 |
if (flags.f.range_error_ignore) |
| 709 |
dot_im = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 710 |
else |
| 711 |
return ERR_OUT_OF_RANGE; |
| 712 |
} |
| 713 |
v = new_complex(dot_re, dot_im); |
| 714 |
} else if (stack[sp]->type == TYPE_COMPLEXMATRIX |
| 715 |
&& stack[sp - 1]->type == TYPE_COMPLEXMATRIX) { |
| 716 |
vartype_complexmatrix *cm1 = (vartype_complexmatrix *) stack[sp]; |
| 717 |
vartype_complexmatrix *cm2 = (vartype_complexmatrix *) stack[sp - 1]; |
| 718 |
int4 size, i; |
| 719 |
phloat dot_re = 0, dot_im = 0; |
| 720 |
int inf; |
| 721 |
size = cm1->rows * cm1->columns; |
| 722 |
if (size != cm2->rows * cm2->columns) |
| 723 |
return ERR_DIMENSION_ERROR; |
| 724 |
size *= 2; |
| 725 |
for (i = 0; i < size; i += 2) { |
| 726 |
phloat re1 = cm1->array->data[i]; |
| 727 |
phloat im1 = cm1->array->data[i + 1]; |
| 728 |
phloat re2 = cm2->array->data[i]; |
| 729 |
phloat im2 = cm2->array->data[i + 1]; |
| 730 |
dot_re += re1 * re2 - im1 * im2; |
| 731 |
dot_im += re1 * im2 + re2 * im1; |
| 732 |
} |
| 733 |
if ((inf = p_isinf(dot_re)) != 0) { |
| 734 |
if (flags.f.range_error_ignore) |
| 735 |
dot_re = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 736 |
else |
| 737 |
return ERR_OUT_OF_RANGE; |
| 738 |
} |
| 739 |
if ((inf = p_isinf(dot_im)) != 0) { |
| 740 |
if (flags.f.range_error_ignore) |
| 741 |
dot_im = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 742 |
else |
| 743 |
return ERR_OUT_OF_RANGE; |
| 744 |
} |
| 745 |
v = new_complex(dot_re, dot_im); |
| 746 |
} else if (stack[sp]->type == TYPE_COMPLEX && stack[sp - 1]->type == TYPE_COMPLEX) { |
| 747 |
vartype_complex *x = (vartype_complex *) stack[sp]; |
| 748 |
vartype_complex *y = (vartype_complex *) stack[sp - 1]; |
| 749 |
phloat d = x->re * y->re + x->im * y->im; |
| 750 |
int inf; |
| 751 |
if ((inf = p_isinf(d)) != 0) { |
| 752 |
if (flags.f.range_error_ignore) |
| 753 |
d = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 754 |
else |
| 755 |
return ERR_OUT_OF_RANGE; |
| 756 |
} |
| 757 |
v = new_real(d); |
| 758 |
} else |
| 759 |
return ERR_INVALID_TYPE; |
| 760 |
if (v == NULL) |
| 761 |
return ERR_INSUFFICIENT_MEMORY; |
| 762 |
return binary_result(v); |
| 763 |
} |
| 764 |
|
| 765 |
int matedit_get_dim(int4 *rows, int4 *columns, vartype **res) { |
| 766 |
vartype *m; |
| 767 |
|
| 768 |
int err = matedit_get(&m); |
| 769 |
if (err != ERR_NONE) |
| 770 |
return err; |
| 771 |
|
| 772 |
if (m->type == TYPE_REALMATRIX) { |
| 773 |
vartype_realmatrix *rm = (vartype_realmatrix *) m; |
| 774 |
*rows = rm->rows; |
| 775 |
*columns = rm->columns; |
| 776 |
} else if (m->type == TYPE_COMPLEXMATRIX) { |
| 777 |
vartype_complexmatrix *cm = (vartype_complexmatrix *) m; |
| 778 |
*rows = cm->rows; |
| 779 |
*columns = cm->columns; |
| 780 |
} else { // TYPE_LIST |
| 781 |
vartype_list *list = (vartype_list *) m; |
| 782 |
*rows = list->size; |
| 783 |
*columns = 1; |
| 784 |
} |
| 785 |
if (res != NULL) |
| 786 |
*res = m; |
| 787 |
return ERR_NONE; |
| 788 |
} |
| 789 |
|
| 790 |
/* NOTE: this is a callback for set_menu(); it is declared in |
| 791 |
* core_display.h but defined here. |
| 792 |
*/ |
| 793 |
int appmenu_exitcallback_1(int menuid, bool exitall) { |
| 794 |
if (menuid == MENU_MATRIX_EDIT1 || menuid == MENU_MATRIX_EDIT2) { |
| 795 |
/* Just switching menus within the editor */ |
| 796 |
mode_appmenu = menuid; |
| 797 |
set_appmenu_exitcallback(1); |
| 798 |
return ERR_NONE; |
| 799 |
} else { |
| 800 |
/* The user is trying to leave the editor; we only allow that |
| 801 |
* if storing X is successful. But if the stack is empty, there |
| 802 |
* is nothing to store. */ |
| 803 |
int err; |
| 804 |
if (sp == -1) |
| 805 |
err = ERR_NONE; |
| 806 |
else |
| 807 |
err = docmd_stoel(NULL); |
| 808 |
if (err == ERR_INSUFFICIENT_MEMORY) |
| 809 |
/* There's no graceful way to handle this, at least not without |
| 810 |
* some code restructuring (TODO); this is the one error that |
| 811 |
* should *not* prevent the user from getting out of the matrix |
| 812 |
* editor! Unfortunately, setting the error to ERR_NONE means we |
| 813 |
* aren't reporting the fact that changing the matrix element |
| 814 |
* was unsuccessful. |
| 815 |
*/ |
| 816 |
err = ERR_NONE; |
| 817 |
if (err == ERR_NONEXISTENT) |
| 818 |
/* This can happen when the user overwrites the indexed |
| 819 |
* matrix with a scalar. The fact that that is even possible |
| 820 |
* is a bug; attempting to do that, or to CLV it, should |
| 821 |
* cause a Restricted Operation message. |
| 822 |
* This code is a workaround to allow users to escape from |
| 823 |
* the Matrix Editor once its state has become hosed in the |
| 824 |
* above manner. We could remove this hack eventually. (TODO) |
| 825 |
*/ |
| 826 |
err = ERR_NONE; |
| 827 |
if (err != ERR_NONE) { |
| 828 |
/* Reinstate the callback so we'll get called again when |
| 829 |
* the user tries to leave again. */ |
| 830 |
set_appmenu_exitcallback(1); |
| 831 |
return err; |
| 832 |
} |
| 833 |
if (matedit_mode == 2) { |
| 834 |
if (sp == -1) |
| 835 |
sp = 0; |
| 836 |
else |
| 837 |
free_vartype(stack[sp]); |
| 838 |
stack[sp] = matedit_x; |
| 839 |
matedit_x = NULL; |
| 840 |
} |
| 841 |
matedit_mode = 0; |
| 842 |
free(matedit_stack); |
| 843 |
matedit_stack = NULL; |
| 844 |
matedit_stack_depth = 0; |
| 845 |
flags.f.grow = 0; |
| 846 |
flags.f.stack_lift_disable = 0; |
| 847 |
/* Note: no need to check the value returned by set_menu() here: |
| 848 |
* since we're in a set_menu() callback, we know that there is no |
| 849 |
* callback registered at the moment (set_menu() unregisters |
| 850 |
* callbacks just before invoking them), so these set_menu() calls |
| 851 |
* won't fail. |
| 852 |
*/ |
| 853 |
if (menuid == MENU_NONE && !exitall) |
| 854 |
set_menu(MENULEVEL_APP, matedit_prev_appmenu); |
| 855 |
else |
| 856 |
set_menu(MENULEVEL_APP, menuid); |
| 857 |
return ERR_NONE; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
static int finish_edit() { |
| 862 |
if (matedit_mode == 2 || matedit_mode == 3) |
| 863 |
/* Try to finish current interactive editing session using |
| 864 |
* set_menu(). The callback (appmenu_exitcallback) will |
| 865 |
* return its error status to set_menu(), which passes it |
| 866 |
* on to us. |
| 867 |
*/ |
| 868 |
return set_menu_return_err(MENULEVEL_APP, MENU_NONE, false); |
| 869 |
else |
| 870 |
return ERR_NONE; |
| 871 |
} |
| 872 |
|
| 873 |
int docmd_edit(arg_struct *arg) { |
| 874 |
int err = finish_edit(); |
| 875 |
if (err != ERR_NONE) |
| 876 |
return err; |
| 877 |
if (stack[sp]->type != TYPE_REALMATRIX |
| 878 |
&& stack[sp]->type != TYPE_COMPLEXMATRIX |
| 879 |
&& stack[sp]->type != TYPE_LIST) |
| 880 |
return ERR_INVALID_TYPE; |
| 881 |
|
| 882 |
vartype *v; |
| 883 |
if (stack[sp]->type == TYPE_REALMATRIX) { |
| 884 |
vartype_realmatrix *rm = (vartype_realmatrix *) stack[sp]; |
| 885 |
if (rm->array->is_string[0] != 0) { |
| 886 |
char *text; |
| 887 |
int4 len; |
| 888 |
get_matrix_string(rm, 0, &text, &len); |
| 889 |
v = new_string(text, len); |
| 890 |
} else |
| 891 |
v = new_real(rm->array->data[0]); |
| 892 |
} else if (stack[sp]->type == TYPE_COMPLEXMATRIX) { |
| 893 |
vartype_complexmatrix *cm = (vartype_complexmatrix *) stack[sp]; |
| 894 |
v = new_complex(cm->array->data[0], cm->array->data[1]); |
| 895 |
} else { |
| 896 |
vartype_list *list = (vartype_list *) stack[sp]; |
| 897 |
if (list->size == 0) |
| 898 |
v = new_real(0); |
| 899 |
else |
| 900 |
v = dup_vartype(list->array->data[0]); |
| 901 |
} |
| 902 |
if (v == NULL) |
| 903 |
return ERR_INSUFFICIENT_MEMORY; |
| 904 |
matedit_mode = 2; |
| 905 |
flags.f.grow = 0; |
| 906 |
matedit_x = stack[sp]; |
| 907 |
stack[sp] = v; |
| 908 |
matedit_i = 0; |
| 909 |
matedit_j = 0; |
| 910 |
matedit_is_list = matedit_x->type == TYPE_LIST; |
| 911 |
flags.f.matrix_edge_wrap = 0; |
| 912 |
flags.f.matrix_end_wrap = 0; |
| 913 |
if (mode_appmenu >= MENU_MATRIX1 && mode_appmenu <= MENU_MATRIX_SIMQ) |
| 914 |
matedit_prev_appmenu = mode_appmenu; |
| 915 |
else |
| 916 |
matedit_prev_appmenu = MENU_NONE; |
| 917 |
set_menu(MENULEVEL_APP, MENU_MATRIX_EDIT1); |
| 918 |
set_appmenu_exitcallback(1); |
| 919 |
print_trace(); |
| 920 |
if (flags.f.big_stack) |
| 921 |
mode_disable_stack_lift = true; |
| 922 |
else |
| 923 |
/* HP-42S bug compatibility */ |
| 924 |
mode_disable_stack_lift = flags.f.stack_lift_disable; |
| 925 |
return ERR_NONE; |
| 926 |
} |
| 927 |
|
| 928 |
int docmd_editn(arg_struct *arg) { |
| 929 |
vartype *m; |
| 930 |
int err; |
| 931 |
|
| 932 |
err = finish_edit(); |
| 933 |
if (err != ERR_NONE) |
| 934 |
return err; |
| 935 |
|
| 936 |
if (arg->type == ARGTYPE_IND_NUM |
| 937 |
|| arg->type == ARGTYPE_IND_STK |
| 938 |
|| arg->type == ARGTYPE_IND_STR) { |
| 939 |
err = resolve_ind_arg(arg); |
| 940 |
if (err != ERR_NONE) |
| 941 |
return err; |
| 942 |
} |
| 943 |
if (arg->type != ARGTYPE_STR) |
| 944 |
return ERR_INVALID_TYPE; |
| 945 |
|
| 946 |
int mi = lookup_var(arg->val.text, arg->length); |
| 947 |
if (mi == -1) |
| 948 |
return ERR_NONEXISTENT; |
| 949 |
m = vars[mi].value; |
| 950 |
if (m->type != TYPE_REALMATRIX |
| 951 |
&& m->type != TYPE_COMPLEXMATRIX |
| 952 |
&& m->type != TYPE_LIST) |
| 953 |
return ERR_INVALID_TYPE; |
| 954 |
|
| 955 |
vartype *v; |
| 956 |
if (m->type == TYPE_REALMATRIX) { |
| 957 |
vartype_realmatrix *rm = (vartype_realmatrix *) m; |
| 958 |
if (rm->array->is_string[0] != 0) { |
| 959 |
char *text; |
| 960 |
int4 len; |
| 961 |
get_matrix_string(rm , 0, &text, &len); |
| 962 |
v = new_string(text, len); |
| 963 |
} else |
| 964 |
v = new_real(rm->array->data[0]); |
| 965 |
} else if (m->type == TYPE_COMPLEXMATRIX) { |
| 966 |
vartype_complexmatrix *cm = (vartype_complexmatrix *) m; |
| 967 |
v = new_complex(cm->array->data[0], cm->array->data[1]); |
| 968 |
} else { |
| 969 |
vartype_list *list = (vartype_list *) m; |
| 970 |
if (list->size == 0) |
| 971 |
v = new_real(0); |
| 972 |
else |
| 973 |
v = dup_vartype(list->array->data[0]); |
| 974 |
} |
| 975 |
|
| 976 |
if (v == NULL) |
| 977 |
return ERR_INSUFFICIENT_MEMORY; |
| 978 |
|
| 979 |
/* If the matrix we're about to edit is local, and another matrix |
| 980 |
* is already indexed, and that other matrix is not a local at the same |
| 981 |
* level, then we push the IJ pointers, so the previous indexing is |
| 982 |
* restored when this function returns. |
| 983 |
*/ |
| 984 |
if (matedit_mode == 1 && matedit_level < vars[mi].level) |
| 985 |
push_indexed_matrix(); |
| 986 |
|
| 987 |
/* TODO: implement a mechanism that locks a matrix while it is |
| 988 |
* under edit. While locked, operations such as CLV, DIM, or |
| 989 |
* assignment should fail with ERR_RESTRICTED_OPERATION. |
| 990 |
*/ |
| 991 |
matedit_mode = 3; |
| 992 |
flags.f.grow = 0; |
| 993 |
string_copy(matedit_name, &matedit_length, arg->val.text, arg->length); |
| 994 |
matedit_level = vars[mi].level; |
| 995 |
if (sp == -1) |
| 996 |
sp = 0; |
| 997 |
else |
| 998 |
free_vartype(stack[sp]); |
| 999 |
stack[sp] = v; |
| 1000 |
matedit_i = 0; |
| 1001 |
matedit_j = 0; |
| 1002 |
matedit_is_list = m->type == TYPE_LIST; |
| 1003 |
flags.f.matrix_edge_wrap = 0; |
| 1004 |
flags.f.matrix_end_wrap = 0; |
| 1005 |
if (mode_appmenu >= MENU_MATRIX1 && mode_appmenu <= MENU_MATRIX_SIMQ) |
| 1006 |
matedit_prev_appmenu = mode_appmenu; |
| 1007 |
else |
| 1008 |
matedit_prev_appmenu = MENU_NONE; |
| 1009 |
set_menu(MENULEVEL_APP, MENU_MATRIX_EDIT1); |
| 1010 |
set_appmenu_exitcallback(1); |
| 1011 |
print_trace(); |
| 1012 |
if (flags.f.big_stack) |
| 1013 |
mode_disable_stack_lift = true; |
| 1014 |
else |
| 1015 |
/* HP-42S bug compatibility */ |
| 1016 |
mode_disable_stack_lift = flags.f.stack_lift_disable; |
| 1017 |
return ERR_NONE; |
| 1018 |
} |
| 1019 |
|
| 1020 |
int docmd_exitall(arg_struct *arg) { |
| 1021 |
return set_menu_return_err(MENULEVEL_APP, MENU_NONE, true); |
| 1022 |
} |
| 1023 |
|
| 1024 |
static int mappable_e_pow_x_1_r(phloat x, phloat *y) { |
| 1025 |
*y = expm1(x); |
| 1026 |
if (p_isinf(*y) != 0) { |
| 1027 |
if (flags.f.range_error_ignore) |
| 1028 |
*y = POS_HUGE_PHLOAT; |
| 1029 |
else |
| 1030 |
return ERR_OUT_OF_RANGE; |
| 1031 |
} |
| 1032 |
return ERR_NONE; |
| 1033 |
} |
| 1034 |
|
| 1035 |
static int mappable_e_pow_x_1_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 1036 |
phloat x = xre; |
| 1037 |
phloat y = exp(x); |
| 1038 |
phloat s = sin(xim / 2); |
| 1039 |
*yre = y - 1 - y * (log(y) - x + 2 * s * s); |
| 1040 |
*yim = y * sin(xim); |
| 1041 |
if (p_isinf(*yre)) { |
| 1042 |
if (flags.f.range_error_ignore) |
| 1043 |
*yre = *yre < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1044 |
else |
| 1045 |
return ERR_OUT_OF_RANGE; |
| 1046 |
} |
| 1047 |
if (p_isinf(*yim)) { |
| 1048 |
if (flags.f.range_error_ignore) |
| 1049 |
*yim = *yim < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1050 |
else |
| 1051 |
return ERR_OUT_OF_RANGE; |
| 1052 |
} |
| 1053 |
return ERR_NONE; |
| 1054 |
} |
| 1055 |
|
| 1056 |
int docmd_e_pow_x_1(arg_struct *arg) { |
| 1057 |
vartype *v; |
| 1058 |
int err = map_unary(stack[sp], &v, mappable_e_pow_x_1_r, NULL); |
| 1059 |
if (err == ERR_NONE) |
| 1060 |
unary_result(v); |
| 1061 |
return err; |
| 1062 |
} |
| 1063 |
|
| 1064 |
int docmd_c_e_pow_x_1(arg_struct *arg) { |
| 1065 |
vartype *v; |
| 1066 |
int err = map_unary(stack[sp], &v, mappable_e_pow_x_1_r, mappable_e_pow_x_1_c); |
| 1067 |
if (err == ERR_NONE) |
| 1068 |
unary_result(v); |
| 1069 |
return err; |
| 1070 |
} |
| 1071 |
|
| 1072 |
static int fnrm(vartype *m, phloat *norm) { |
| 1073 |
int4 size; |
| 1074 |
phloat *data; |
| 1075 |
if (m->type == TYPE_REALMATRIX) { |
| 1076 |
vartype_realmatrix *rm = (vartype_realmatrix *) m; |
| 1077 |
if (contains_strings(rm)) |
| 1078 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1079 |
size = rm->rows * rm->columns; |
| 1080 |
data = rm->array->data; |
| 1081 |
} else { |
| 1082 |
vartype_complexmatrix *cm = (vartype_complexmatrix *) m; |
| 1083 |
size = 2 * cm->rows * cm->columns; |
| 1084 |
data = cm->array->data; |
| 1085 |
} |
| 1086 |
int max_exp = INT_MIN; |
| 1087 |
for (int4 i = 0; i < size; i++) { |
| 1088 |
int s = ilogb(data[i]); |
| 1089 |
if (s > max_exp) |
| 1090 |
max_exp = s; |
| 1091 |
} |
| 1092 |
phloat nrm = 0; |
| 1093 |
for (int4 i = 0; i < size; i++) { |
| 1094 |
phloat x = scalbn(data[i], -max_exp); |
| 1095 |
nrm += x * x; |
| 1096 |
} |
| 1097 |
nrm = scalbn(sqrt(nrm), max_exp); |
| 1098 |
if (p_isinf(nrm)) { |
| 1099 |
if (flags.f.range_error_ignore) |
| 1100 |
nrm = POS_HUGE_PHLOAT; |
| 1101 |
else |
| 1102 |
return ERR_OUT_OF_RANGE; |
| 1103 |
} |
| 1104 |
*norm = nrm; |
| 1105 |
return ERR_NONE; |
| 1106 |
} |
| 1107 |
|
| 1108 |
int docmd_fnrm(arg_struct *arg) { |
| 1109 |
phloat norm; |
| 1110 |
vartype *v; |
| 1111 |
int err = fnrm(stack[sp], &norm); |
| 1112 |
if (err != ERR_NONE) |
| 1113 |
return err; |
| 1114 |
v = new_real(norm); |
| 1115 |
if (v == NULL) |
| 1116 |
return ERR_INSUFFICIENT_MEMORY; |
| 1117 |
unary_result(v); |
| 1118 |
return ERR_NONE; |
| 1119 |
} |
| 1120 |
|
| 1121 |
int docmd_getm(arg_struct *arg) { |
| 1122 |
vartype *m; |
| 1123 |
int4 x, y; |
| 1124 |
|
| 1125 |
int err = matedit_get(&m); |
| 1126 |
if (err != ERR_NONE) |
| 1127 |
return err; |
| 1128 |
if (m->type == TYPE_LIST) |
| 1129 |
return ERR_INVALID_TYPE; |
| 1130 |
|
| 1131 |
if (stack[sp]->type == TYPE_STRING) |
| 1132 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1133 |
if (stack[sp]->type != TYPE_REAL) |
| 1134 |
return ERR_INVALID_TYPE; |
| 1135 |
if (stack[sp - 1]->type == TYPE_STRING) |
| 1136 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 1137 |
if (stack[sp - 1]->type != TYPE_REAL) |
| 1138 |
return ERR_INVALID_TYPE; |
| 1139 |
|
| 1140 |
if (!dim_to_int4(stack[sp], &x)) |
| 1141 |
return ERR_DIMENSION_ERROR; |
| 1142 |
x++; |
| 1143 |
if (!dim_to_int4(stack[sp - 1], &y)) |
| 1144 |
return ERR_DIMENSION_ERROR; |
| 1145 |
y++; |
| 1146 |
|
| 1147 |
if (m->type == TYPE_REALMATRIX) { |
| 1148 |
vartype_realmatrix *src, *dst; |
| 1149 |
int4 i, j; |
| 1150 |
src = (vartype_realmatrix *) m; |
| 1151 |
if (src->rows < matedit_i + y || src->columns < matedit_j + x) |
| 1152 |
return ERR_DIMENSION_ERROR; |
| 1153 |
dst = (vartype_realmatrix *) new_realmatrix(y, x); |
| 1154 |
if (dst == NULL) |
| 1155 |
return ERR_INSUFFICIENT_MEMORY; |
| 1156 |
for (i = 0; i < y; i++) |
| 1157 |
for (j = 0; j < x; j++) { |
| 1158 |
int4 n1 = (i + matedit_i) * src->columns + j + matedit_j; |
| 1159 |
int4 n2 = i * dst->columns + j; |
| 1160 |
if (src->array->is_string[n1] == 2) { |
| 1161 |
int4 *sp = *(int4 **) &src->array->data[n1]; |
| 1162 |
int4 *dp = (int4 *) malloc(*sp + 4); |
| 1163 |
if (dp == NULL) { |
| 1164 |
free_vartype((vartype *) dst); |
| 1165 |
return ERR_INSUFFICIENT_MEMORY; |
| 1166 |
} |
| 1167 |
memcpy(dp, sp, *sp + 4); |
| 1168 |
*(int4 **) &dst->array->data[n2] = dp; |
| 1169 |
} else { |
| 1170 |
dst->array->data[n2] = src->array->data[n1]; |
| 1171 |
} |
| 1172 |
dst->array->is_string[n2] = src->array->is_string[n1]; |
| 1173 |
} |
| 1174 |
return binary_result((vartype *) dst); |
| 1175 |
} else /* m->type == TYPE_COMPLEXMATRIX */ { |
| 1176 |
vartype_complexmatrix *src, *dst; |
| 1177 |
int4 i, j; |
| 1178 |
src = (vartype_complexmatrix *) m; |
| 1179 |
if (src->rows < matedit_i + y || src->columns < matedit_j + x) |
| 1180 |
return ERR_DIMENSION_ERROR; |
| 1181 |
dst = (vartype_complexmatrix *) new_complexmatrix(y, x); |
| 1182 |
if (dst == NULL) |
| 1183 |
return ERR_INSUFFICIENT_MEMORY; |
| 1184 |
for (i = 0; i < y; i++) |
| 1185 |
for (j = 0; j < x; j++) { |
| 1186 |
int4 n1 = (i + matedit_i) * src->columns + j + matedit_j; |
| 1187 |
int4 n2 = i * dst->columns + j; |
| 1188 |
dst->array->data[n2 * 2] = src->array->data[n1 * 2]; |
| 1189 |
dst->array->data[n2 * 2 + 1] = src->array->data[n1 * 2 + 1]; |
| 1190 |
} |
| 1191 |
return binary_result((vartype *) dst); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
int docmd_grow(arg_struct *arg) { |
| 1196 |
flags.f.grow = 1; |
| 1197 |
return ERR_NONE; |
| 1198 |
} |
| 1199 |
|
| 1200 |
static int hms_add_or_sub(bool add) { |
| 1201 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 1202 |
phloat y = ((vartype_real *) stack[sp - 1])->x; |
| 1203 |
phloat r; |
| 1204 |
bool neg; |
| 1205 |
|
| 1206 |
if (x < 0) { |
| 1207 |
x = -x; |
| 1208 |
add = !add; |
| 1209 |
} |
| 1210 |
if (y < 0) { |
| 1211 |
y = -y; |
| 1212 |
add = !add; |
| 1213 |
neg = 1; |
| 1214 |
} else |
| 1215 |
neg = 0; |
| 1216 |
|
| 1217 |
#ifdef BCD_MATH |
| 1218 |
phloat xh, xm, xs, yh, ym, ys, t; |
| 1219 |
xh = floor(x); |
| 1220 |
t = (x - xh) * 100; |
| 1221 |
xm = floor(t); |
| 1222 |
xs = (t - xm) * 100; |
| 1223 |
yh = floor(y); |
| 1224 |
t = (y - yh) * 100; |
| 1225 |
ym = floor(t); |
| 1226 |
ys = (t - ym) * 100; |
| 1227 |
|
| 1228 |
if (add) { |
| 1229 |
ys += xs; |
| 1230 |
while (ys >= 60) { |
| 1231 |
ys -= 60; |
| 1232 |
ym++; |
| 1233 |
} |
| 1234 |
ym += xm; |
| 1235 |
while (ym >= 60) { |
| 1236 |
ym -= 60; |
| 1237 |
yh++; |
| 1238 |
} |
| 1239 |
yh += xh; |
| 1240 |
} else { |
| 1241 |
ys -= xs; |
| 1242 |
if (ys >= 60) { |
| 1243 |
ys -= 60; |
| 1244 |
ym++; |
| 1245 |
} else { |
| 1246 |
while (ys < 0) { |
| 1247 |
ys += 60; |
| 1248 |
ym--; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
ym -= xm; |
| 1252 |
if (ym >= 60) { |
| 1253 |
ym -= 60; |
| 1254 |
yh++; |
| 1255 |
} else { |
| 1256 |
while (ym < 0) { |
| 1257 |
ym += 60; |
| 1258 |
yh--; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
yh -= xh; |
| 1262 |
if (yh < 0) { |
| 1263 |
if (ys != 0) { |
| 1264 |
ys -= 60; |
| 1265 |
ym++; |
| 1266 |
} |
| 1267 |
if (ym != 0) { |
| 1268 |
ym -= 60; |
| 1269 |
yh++; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
r = yh + ym / 100 + ys / 10000; |
| 1275 |
|
| 1276 |
#else |
| 1277 |
|
| 1278 |
/* I'm doing the computation in integer math. The nastiness is necessary, |
| 1279 |
* because this is one of those cases (along with ->HR, ISG, and DSE) |
| 1280 |
* where binary representation errors can mess things up pretty badly... |
| 1281 |
* For example, you enter 0.45, thinking 45 minutes; the closest binary |
| 1282 |
* representation is 0.449999999..., which could easily be mistaken for 44 |
| 1283 |
* minutes 99.99999... seconds -- 40 seconds off! |
| 1284 |
*/ |
| 1285 |
double rx, ry, res; |
| 1286 |
int8 ix, iy, ixhr, iyhr, ires, ireshr; |
| 1287 |
|
| 1288 |
rx = floor(x); |
| 1289 |
ry = floor(y); |
| 1290 |
res = add ? ry + rx : ry - rx; |
| 1291 |
ix = (int8) (((x - rx) * 1000000000000.0) + 0.5); |
| 1292 |
iy = (int8) (((y - ry) * 1000000000000.0) + 0.5); |
| 1293 |
ixhr = ix % 10000000000LL; |
| 1294 |
iyhr = iy % 10000000000LL; |
| 1295 |
ix /= 10000000000LL; |
| 1296 |
iy /= 10000000000LL; |
| 1297 |
ixhr += (ix % 100) * 6000000000LL; |
| 1298 |
iyhr += (iy % 100) * 6000000000LL; |
| 1299 |
ixhr += (ix / 100) * 360000000000LL; |
| 1300 |
iyhr += (iy / 100) * 360000000000LL; |
| 1301 |
ireshr = add ? iyhr + ixhr : iyhr - ixhr; |
| 1302 |
while (ireshr < 0 && res > 0) { |
| 1303 |
ireshr += 360000000000LL; |
| 1304 |
res -= 1; |
| 1305 |
} |
| 1306 |
while (ireshr > 0 && res < 0) { |
| 1307 |
ireshr -= 360000000000LL; |
| 1308 |
res += 1; |
| 1309 |
} |
| 1310 |
ires = ireshr % 6000000000LL; |
| 1311 |
ireshr /= 6000000000LL; |
| 1312 |
ires += (ireshr % 60) * 10000000000LL; |
| 1313 |
ires += (ireshr / 60) * 1000000000000LL; |
| 1314 |
res += ires / 1000000000000.0; |
| 1315 |
r = res; |
| 1316 |
#endif |
| 1317 |
|
| 1318 |
/* Round-off may have caused the minutes or seconds to reach 60; |
| 1319 |
* detect this and fix... |
| 1320 |
*/ |
| 1321 |
r = fix_hms(r); |
| 1322 |
|
| 1323 |
if (neg) |
| 1324 |
r = -r; |
| 1325 |
|
| 1326 |
int inf; |
| 1327 |
if ((inf = p_isinf(r)) != 0) { |
| 1328 |
if (flags.f.range_error_ignore) |
| 1329 |
r = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1330 |
else |
| 1331 |
return ERR_OUT_OF_RANGE; |
| 1332 |
} |
| 1333 |
|
| 1334 |
vartype *v = new_real(r); |
| 1335 |
if (v == NULL) |
| 1336 |
return ERR_INSUFFICIENT_MEMORY; |
| 1337 |
return binary_result(v); |
| 1338 |
} |
| 1339 |
|
| 1340 |
int docmd_hmsadd(arg_struct *arg) { |
| 1341 |
return hms_add_or_sub(true); |
| 1342 |
} |
| 1343 |
|
| 1344 |
int docmd_hmssub(arg_struct *arg) { |
| 1345 |
return hms_add_or_sub(false); |
| 1346 |
} |
| 1347 |
|
| 1348 |
int docmd_i_add(arg_struct *arg) { |
| 1349 |
int4 rows, columns; |
| 1350 |
int err = matedit_get_dim(&rows, &columns); |
| 1351 |
if (err != ERR_NONE) |
| 1352 |
return err; |
| 1353 |
if (rows == 0) |
| 1354 |
// Empty list. We allow matedit_i == 0, because we have to allow *something*. |
| 1355 |
rows = 1; |
| 1356 |
if (++matedit_i >= rows) { |
| 1357 |
flags.f.matrix_edge_wrap = 1; |
| 1358 |
matedit_i = 0; |
| 1359 |
if (++matedit_j >= columns) { |
| 1360 |
flags.f.matrix_end_wrap = 1; |
| 1361 |
matedit_j = 0; |
| 1362 |
} else |
| 1363 |
flags.f.matrix_end_wrap = 0; |
| 1364 |
} else { |
| 1365 |
flags.f.matrix_edge_wrap = 0; |
| 1366 |
flags.f.matrix_end_wrap = 0; |
| 1367 |
} |
| 1368 |
return ERR_NONE; |
| 1369 |
} |
| 1370 |
|
| 1371 |
int docmd_i_sub(arg_struct *arg) { |
| 1372 |
int4 rows, columns; |
| 1373 |
int err = matedit_get_dim(&rows, &columns); |
| 1374 |
if (err != ERR_NONE) |
| 1375 |
return err; |
| 1376 |
if (rows == 0) |
| 1377 |
// Empty list. We allow matedit_i == 0, because we have to allow *something*. |
| 1378 |
rows = 1; |
| 1379 |
if (--matedit_i < 0) { |
| 1380 |
flags.f.matrix_edge_wrap = 1; |
| 1381 |
matedit_i = rows - 1; |
| 1382 |
if (--matedit_j < 0) { |
| 1383 |
flags.f.matrix_end_wrap = 1; |
| 1384 |
matedit_j = columns - 1; |
| 1385 |
} else |
| 1386 |
flags.f.matrix_end_wrap = 0; |
| 1387 |
} else { |
| 1388 |
flags.f.matrix_edge_wrap = 0; |
| 1389 |
flags.f.matrix_end_wrap = 0; |
| 1390 |
} |
| 1391 |
return ERR_NONE; |
| 1392 |
} |
| 1393 |
|
| 1394 |
void matedit_goto(int4 row, int4 column) { |
| 1395 |
int4 rows, columns; |
| 1396 |
int err = matedit_get_dim(&rows, &columns); |
| 1397 |
if (err == ERR_NONE) { |
| 1398 |
if (rows == 0) |
| 1399 |
// Empty list. We allow matedit_i == 0, because we have to allow *something*. |
| 1400 |
rows = 1; |
| 1401 |
if (row == 0 || row > rows || column == 0 || column > columns) |
| 1402 |
err = ERR_DIMENSION_ERROR; |
| 1403 |
else { |
| 1404 |
matedit_i = row - 1; |
| 1405 |
matedit_j = column - 1; |
| 1406 |
if (sp != -1) |
| 1407 |
flags.f.stack_lift_disable = 1; |
| 1408 |
err = docmd_rclel(NULL); |
| 1409 |
mode_disable_stack_lift = true; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
if (err != ERR_NONE) { |
| 1413 |
display_error(err); |
| 1414 |
flush_display(); |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
int docmd_index(arg_struct *arg) { |
| 1419 |
int err; |
| 1420 |
vartype *m; |
| 1421 |
|
| 1422 |
if (matedit_mode == 2 || matedit_mode == 3) |
| 1423 |
/* TODO: on the real HP-42S, you get this error message the |
| 1424 |
* moment you say INDEX, that is, before even having to |
| 1425 |
* complete the command. Doing that in free42 would require |
| 1426 |
* a check in start_incomplete_command() or do_interactive(). |
| 1427 |
* I'm putting that off until I have a better idea of whether |
| 1428 |
* this is an isolated special case (in which case I'll go |
| 1429 |
* for the quick-and-dirty hack) or something more general |
| 1430 |
* (in which case I'll redesign stuff if necessary). |
| 1431 |
*/ |
| 1432 |
return ERR_RESTRICTED_OPERATION; |
| 1433 |
|
| 1434 |
if (arg->type == ARGTYPE_IND_NUM |
| 1435 |
|| arg->type == ARGTYPE_IND_STK |
| 1436 |
|| arg->type == ARGTYPE_IND_STR) { |
| 1437 |
err = resolve_ind_arg(arg); |
| 1438 |
if (err != ERR_NONE) |
| 1439 |
return err; |
| 1440 |
} |
| 1441 |
if (arg->type != ARGTYPE_STR) |
| 1442 |
return ERR_INVALID_TYPE; |
| 1443 |
|
| 1444 |
int mi = lookup_var(arg->val.text, arg->length); |
| 1445 |
if (mi == -1) |
| 1446 |
return ERR_NONEXISTENT; |
| 1447 |
m = vars[mi].value; |
| 1448 |
if (m->type != TYPE_REALMATRIX |
| 1449 |
&& m->type != TYPE_COMPLEXMATRIX |
| 1450 |
&& m->type != TYPE_LIST) |
| 1451 |
return ERR_INVALID_TYPE; |
| 1452 |
|
| 1453 |
/* If the matrix we're about to index is local, and another matrix |
| 1454 |
* is already indexed, and that other matrix is not a local at the same |
| 1455 |
* level, then we push the IJ pointers, so the previous indexing is |
| 1456 |
* restored when this function returns. |
| 1457 |
*/ |
| 1458 |
if (matedit_mode == 1 && matedit_level < vars[mi].level) |
| 1459 |
push_indexed_matrix(); |
| 1460 |
|
| 1461 |
/* TODO: keep a 'weak' lock on the matrix while it is indexed. |
| 1462 |
* If it is deleted or redimensioned, I and J should be reset to 1. |
| 1463 |
* Note that the current code uses a lazy, keep-it-safe approach |
| 1464 |
* (it does not assume I and J are necessarily in bounds when I+, J-, |
| 1465 |
* LEFT, RIGHT, etc., are called, but simply forces I and J within |
| 1466 |
* bounds just before using them), but this does not always yield the |
| 1467 |
* same result (try deleting the indexed matrix and then recreating it |
| 1468 |
* immediately; on the real HP-42S, I and J are now both 1; on Free42, |
| 1469 |
* I and J are unchanged). |
| 1470 |
*/ |
| 1471 |
matedit_mode = 1; |
| 1472 |
matedit_i = 0; |
| 1473 |
matedit_j = 0; |
| 1474 |
matedit_is_list = m->type == TYPE_LIST; |
| 1475 |
flags.f.matrix_edge_wrap = 0; |
| 1476 |
flags.f.matrix_end_wrap = 0; |
| 1477 |
string_copy(matedit_name, &matedit_length, arg->val.text, arg->length); |
| 1478 |
matedit_level = vars[mi].level; |
| 1479 |
return ERR_NONE; |
| 1480 |
} |
| 1481 |
|
| 1482 |
int docmd_uvec(arg_struct *arg) { |
| 1483 |
phloat norm; |
| 1484 |
int err; |
| 1485 |
vartype *v; |
| 1486 |
if (stack[sp]->type == TYPE_COMPLEX) { |
| 1487 |
vartype_complex *z = (vartype_complex *) stack[sp]; |
| 1488 |
if (z->re == 0 && z->im == 0) |
| 1489 |
return ERR_INVALID_DATA; |
| 1490 |
else |
| 1491 |
return docmd_sign(arg); |
| 1492 |
} |
| 1493 |
err = fnrm(stack[sp], &norm); |
| 1494 |
if (err != ERR_NONE) |
| 1495 |
return err; |
| 1496 |
if (norm == 0) { |
| 1497 |
return ERR_INVALID_DATA; |
| 1498 |
} else { |
| 1499 |
vartype_realmatrix *src = (vartype_realmatrix *) stack[sp]; |
| 1500 |
vartype_realmatrix *dst; |
| 1501 |
int4 rows = src->rows; |
| 1502 |
int4 columns = src->columns; |
| 1503 |
int4 size = rows * columns; |
| 1504 |
int4 i; |
| 1505 |
dst = (vartype_realmatrix *) new_realmatrix(rows, columns); |
| 1506 |
if (dst == NULL) |
| 1507 |
return ERR_INSUFFICIENT_MEMORY; |
| 1508 |
for (i = 0; i < size; i++) |
| 1509 |
dst->array->data[i] = src->array->data[i] / norm; |
| 1510 |
v = (vartype *) dst; |
| 1511 |
} |
| 1512 |
unary_result(v); |
| 1513 |
return ERR_NONE; |
| 1514 |
} |