| 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_globals.h" |
| 21 |
#include "core_linalg1.h" |
| 22 |
#include "core_linalg2.h" |
| 23 |
#include "core_main.h" |
| 24 |
#include "core_math2.h" |
| 25 |
#include "core_sto_rcl.h" |
| 26 |
#include "core_variables.h" |
| 27 |
|
| 28 |
|
| 29 |
/**********************************/ |
| 30 |
/***** Matrix-matrix division *****/ |
| 31 |
/**********************************/ |
| 32 |
|
| 33 |
static int (*linalg_div_completion)(int, vartype *); |
| 34 |
static const vartype *linalg_div_left; |
| 35 |
static vartype *linalg_div_result; |
| 36 |
|
| 37 |
static int div_rr_completion1(int error, vartype_realmatrix *a, int4 *perm, |
| 38 |
phloat det); |
| 39 |
static int div_rr_completion2(int error, vartype_realmatrix *a, int4 *perm, |
| 40 |
vartype_realmatrix *b); |
| 41 |
static int div_rc_completion1(int error, vartype_complexmatrix *a, int4 *perm, |
| 42 |
phloat det_re, phloat det_im); |
| 43 |
static int div_rc_completion2(int error, vartype_complexmatrix *a, int4 *perm, |
| 44 |
vartype_complexmatrix *b); |
| 45 |
static int div_cr_completion1(int error, vartype_realmatrix *a, int4 *perm, |
| 46 |
phloat det); |
| 47 |
static int div_cr_completion2(int error, vartype_realmatrix *a, int4 *perm, |
| 48 |
vartype_complexmatrix *b); |
| 49 |
static int div_cc_completion1(int error, vartype_complexmatrix *a, int4 *perm, |
| 50 |
phloat det_re, phloat det_im); |
| 51 |
static int div_cc_completion2(int error, vartype_complexmatrix *a, int4 *perm, |
| 52 |
vartype_complexmatrix *b); |
| 53 |
static int small_div(const vartype *left, const vartype *right, |
| 54 |
int (*completion)(int, vartype *)); |
| 55 |
|
| 56 |
int linalg_div(const vartype *left, const vartype *right, |
| 57 |
int (*completion)(int, vartype *)) { |
| 58 |
if (left->type == TYPE_REALMATRIX) { |
| 59 |
if (right->type == TYPE_REALMATRIX) { |
| 60 |
vartype_realmatrix *num = (vartype_realmatrix *) left; |
| 61 |
vartype_realmatrix *denom = (vartype_realmatrix *) right; |
| 62 |
vartype *lu, *res; |
| 63 |
int4 rows = num->rows; |
| 64 |
int4 columns = num->columns; |
| 65 |
int4 *perm; |
| 66 |
if (denom->rows != rows || denom->columns != rows) |
| 67 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 68 |
if (denom->rows <= 2) |
| 69 |
return small_div(left, right, completion); |
| 70 |
perm = (int4 *) malloc(rows * sizeof(int4)); |
| 71 |
if (perm == NULL) |
| 72 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 73 |
lu = new_realmatrix(rows, rows); |
| 74 |
if (lu == NULL) { |
| 75 |
free(perm); |
| 76 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 77 |
} |
| 78 |
res = new_realmatrix(rows, columns); |
| 79 |
if (res == NULL) { |
| 80 |
free(perm); |
| 81 |
free_vartype(lu); |
| 82 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 83 |
} |
| 84 |
matrix_copy(lu, right); |
| 85 |
linalg_div_completion = completion; |
| 86 |
linalg_div_left = left; |
| 87 |
linalg_div_result = res; |
| 88 |
return lu_decomp_r((vartype_realmatrix *) lu, perm, |
| 89 |
div_rr_completion1); |
| 90 |
} else { |
| 91 |
vartype_realmatrix *num = (vartype_realmatrix *) left; |
| 92 |
vartype_complexmatrix *denom = (vartype_complexmatrix *) right; |
| 93 |
vartype *lu, *res; |
| 94 |
int4 rows = num->rows; |
| 95 |
int4 columns = num->columns; |
| 96 |
int4 *perm; |
| 97 |
if (denom->rows != rows || denom->columns != rows) |
| 98 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 99 |
if (denom->rows <= 2) |
| 100 |
return small_div(left, right, completion); |
| 101 |
perm = (int4 *) malloc(rows * sizeof(int4)); |
| 102 |
if (perm == NULL) |
| 103 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 104 |
lu = new_complexmatrix(rows, rows); |
| 105 |
if (lu == NULL) { |
| 106 |
free(perm); |
| 107 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 108 |
} |
| 109 |
res = new_complexmatrix(rows, columns); |
| 110 |
if (res == NULL) { |
| 111 |
free(perm); |
| 112 |
free_vartype(lu); |
| 113 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 114 |
} |
| 115 |
matrix_copy(lu, right); |
| 116 |
linalg_div_completion = completion; |
| 117 |
linalg_div_left = left; |
| 118 |
linalg_div_result = res; |
| 119 |
return lu_decomp_c((vartype_complexmatrix *) lu, perm, |
| 120 |
div_rc_completion1); |
| 121 |
} |
| 122 |
} else { |
| 123 |
if (right->type == TYPE_REALMATRIX) { |
| 124 |
vartype_complexmatrix *num = (vartype_complexmatrix *) left; |
| 125 |
vartype_realmatrix *denom = (vartype_realmatrix *) right; |
| 126 |
vartype *lu, *res; |
| 127 |
int4 rows = num->rows; |
| 128 |
int4 columns = num->columns; |
| 129 |
int4 *perm; |
| 130 |
if (denom->rows != rows || denom->columns != rows) |
| 131 |
return completion(ERR_DIMENSION_ERROR, 0); |
| 132 |
if (denom->rows <= 2) |
| 133 |
return small_div(left, right, completion); |
| 134 |
perm = (int4 *) malloc(rows * sizeof(int4)); |
| 135 |
if (perm == NULL) |
| 136 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 137 |
lu = new_realmatrix(rows, rows); |
| 138 |
if (lu == NULL) { |
| 139 |
free(perm); |
| 140 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 141 |
} |
| 142 |
res = new_complexmatrix(rows, columns); |
| 143 |
if (res == NULL) { |
| 144 |
free(perm); |
| 145 |
free_vartype(lu); |
| 146 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 147 |
} |
| 148 |
matrix_copy(lu, right); |
| 149 |
linalg_div_completion = completion; |
| 150 |
linalg_div_left = left; |
| 151 |
linalg_div_result = res; |
| 152 |
return lu_decomp_r((vartype_realmatrix *) lu, perm, |
| 153 |
div_cr_completion1); |
| 154 |
} else { |
| 155 |
vartype_complexmatrix *num = (vartype_complexmatrix *) left; |
| 156 |
vartype_complexmatrix *denom = (vartype_complexmatrix *) right; |
| 157 |
vartype *lu, *res; |
| 158 |
int4 rows = num->rows; |
| 159 |
int4 columns = num->columns; |
| 160 |
int4 *perm; |
| 161 |
if (denom->rows != rows || denom->columns != rows) |
| 162 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 163 |
if (denom->rows <= 2) |
| 164 |
return small_div(left, right, completion); |
| 165 |
perm = (int4 *) malloc(rows * sizeof(int4)); |
| 166 |
if (perm == NULL) |
| 167 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 168 |
lu = new_complexmatrix(rows, rows); |
| 169 |
if (lu == NULL) { |
| 170 |
free(perm); |
| 171 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 172 |
} |
| 173 |
res = new_complexmatrix(rows, columns); |
| 174 |
if (res == NULL) { |
| 175 |
free(perm); |
| 176 |
free_vartype(lu); |
| 177 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 178 |
} |
| 179 |
matrix_copy(lu, right); |
| 180 |
linalg_div_completion = completion; |
| 181 |
linalg_div_left = left; |
| 182 |
linalg_div_result = res; |
| 183 |
return lu_decomp_c((vartype_complexmatrix *) lu, perm, |
| 184 |
div_cc_completion1); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static int div_rr_completion1(int error, vartype_realmatrix *a, int4 *perm, |
| 190 |
phloat det) { |
| 191 |
if (error != ERR_NONE) { |
| 192 |
free_vartype((vartype *) a); |
| 193 |
free(perm); |
| 194 |
free_vartype(linalg_div_result); |
| 195 |
return error; |
| 196 |
} else { |
| 197 |
matrix_copy(linalg_div_result, linalg_div_left); |
| 198 |
return lu_backsubst_rr(a, perm, |
| 199 |
(vartype_realmatrix *) linalg_div_result, |
| 200 |
div_rr_completion2); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
static int div_rr_completion2(int error, vartype_realmatrix *a, int4 *perm, |
| 205 |
vartype_realmatrix *b) { |
| 206 |
if (error != ERR_NONE) |
| 207 |
free_vartype(linalg_div_result); /* Note: linalg_div_result == b */ |
| 208 |
free_vartype((vartype *) a); |
| 209 |
free(perm); |
| 210 |
return linalg_div_completion(error, linalg_div_result); |
| 211 |
} |
| 212 |
|
| 213 |
static int div_rc_completion1(int error, vartype_complexmatrix *a, int4 *perm, |
| 214 |
phloat det_re, phloat det_im) { |
| 215 |
if (error != ERR_NONE) { |
| 216 |
free_vartype((vartype *) a); |
| 217 |
free(perm); |
| 218 |
free_vartype(linalg_div_result); |
| 219 |
return error; |
| 220 |
} else { |
| 221 |
matrix_copy(linalg_div_result, linalg_div_left); |
| 222 |
return lu_backsubst_cc(a, perm, |
| 223 |
(vartype_complexmatrix *) linalg_div_result, |
| 224 |
div_rc_completion2); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static int div_rc_completion2(int error, vartype_complexmatrix *a, int4 *perm, |
| 229 |
vartype_complexmatrix *b) { |
| 230 |
if (error != ERR_NONE) |
| 231 |
free_vartype(linalg_div_result); /* Note: linalg_div_result == b */ |
| 232 |
free_vartype((vartype *) a); |
| 233 |
free(perm); |
| 234 |
return linalg_div_completion(error, linalg_div_result); |
| 235 |
} |
| 236 |
|
| 237 |
static int div_cr_completion1(int error, vartype_realmatrix *a, int4 *perm, |
| 238 |
phloat det) { |
| 239 |
if (error != ERR_NONE) { |
| 240 |
free_vartype((vartype *) a); |
| 241 |
free(perm); |
| 242 |
free_vartype(linalg_div_result); |
| 243 |
return error; |
| 244 |
} else { |
| 245 |
matrix_copy(linalg_div_result, linalg_div_left); |
| 246 |
return lu_backsubst_rc(a, perm, |
| 247 |
(vartype_complexmatrix *) linalg_div_result, |
| 248 |
div_cr_completion2); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
static int div_cr_completion2(int error, vartype_realmatrix *a, int4 *perm, |
| 253 |
vartype_complexmatrix *b) { |
| 254 |
if (error != ERR_NONE) |
| 255 |
free_vartype(linalg_div_result); /* Note: linalg_div_result == b */ |
| 256 |
free_vartype((vartype *) a); |
| 257 |
free(perm); |
| 258 |
return linalg_div_completion(error, linalg_div_result); |
| 259 |
} |
| 260 |
|
| 261 |
static int div_cc_completion1(int error, vartype_complexmatrix *a, int4 *perm, |
| 262 |
phloat det_re, phloat det_im) { |
| 263 |
if (error != ERR_NONE) { |
| 264 |
free_vartype((vartype *) a); |
| 265 |
free(perm); |
| 266 |
free_vartype(linalg_div_result); |
| 267 |
return error; |
| 268 |
} else { |
| 269 |
matrix_copy(linalg_div_result, linalg_div_left); |
| 270 |
return lu_backsubst_cc(a, perm, |
| 271 |
(vartype_complexmatrix *) linalg_div_result, |
| 272 |
div_cc_completion2); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static int div_cc_completion2(int error, vartype_complexmatrix *a, int4 *perm, |
| 277 |
vartype_complexmatrix *b) { |
| 278 |
if (error != ERR_NONE) |
| 279 |
free_vartype(linalg_div_result); /* Note: linalg_div_result == b */ |
| 280 |
free_vartype((vartype *) a); |
| 281 |
free(perm); |
| 282 |
return linalg_div_completion(error, linalg_div_result); |
| 283 |
} |
| 284 |
|
| 285 |
static int small_inv_r(vartype_realmatrix *ma, int (*completion)(int, vartype *)); |
| 286 |
static int small_inv_c(vartype_complexmatrix *ma, int (*completion)(int, vartype *)); |
| 287 |
static int matrix_mul_rr(vartype_realmatrix *left, vartype_realmatrix *right, int (*completion)(int, vartype *)); |
| 288 |
static int matrix_mul_cr(vartype_complexmatrix *left, vartype_realmatrix *right, int (*completion)(int, vartype *)); |
| 289 |
static int matrix_mul_rc(vartype_realmatrix *left, vartype_complexmatrix *right, int (*completion)(int, vartype *)); |
| 290 |
static int matrix_mul_cc(vartype_complexmatrix *left, vartype_complexmatrix *right, int (*completion)(int, vartype *)); |
| 291 |
|
| 292 |
static vartype *small_div_res; |
| 293 |
static int (*small_div_completion)(int, vartype *); |
| 294 |
|
| 295 |
static int small_div_completion_1(int error, vartype *v) { |
| 296 |
small_div_res = v; |
| 297 |
return error; |
| 298 |
} |
| 299 |
|
| 300 |
static int small_div_completion_2(int err, vartype *v) { |
| 301 |
free_vartype(small_div_res); |
| 302 |
return small_div_completion(err, v); |
| 303 |
} |
| 304 |
|
| 305 |
static int small_div(const vartype *left, const vartype *right, int (*completion)(int, vartype *)) { |
| 306 |
int err; |
| 307 |
if (right->type == TYPE_REALMATRIX) { |
| 308 |
err = small_inv_r((vartype_realmatrix *) right, small_div_completion_1); |
| 309 |
if (err != ERR_NONE) |
| 310 |
return completion(err, NULL); |
| 311 |
small_div_completion = completion; |
| 312 |
if (left->type == TYPE_REALMATRIX) |
| 313 |
return matrix_mul_rr((vartype_realmatrix *) small_div_res, (vartype_realmatrix *) left, small_div_completion_2); |
| 314 |
else |
| 315 |
return matrix_mul_rc((vartype_realmatrix *) small_div_res, (vartype_complexmatrix *) left, small_div_completion_2); |
| 316 |
} else { |
| 317 |
err = small_inv_c((vartype_complexmatrix *) right, small_div_completion_1); |
| 318 |
if (err != ERR_NONE) |
| 319 |
return completion(err, NULL); |
| 320 |
small_div_completion = completion; |
| 321 |
if (left->type == TYPE_REALMATRIX) |
| 322 |
return matrix_mul_cr((vartype_complexmatrix *) small_div_res, (vartype_realmatrix *) left, small_div_completion_2); |
| 323 |
else |
| 324 |
return matrix_mul_cc((vartype_complexmatrix *) small_div_res, (vartype_complexmatrix *) left, small_div_completion_2); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/****************************************/ |
| 330 |
/***** Matrix-matrix multiplication *****/ |
| 331 |
/****************************************/ |
| 332 |
|
| 333 |
struct mul_rr_data_struct { |
| 334 |
vartype_realmatrix *left; |
| 335 |
vartype_realmatrix *right; |
| 336 |
vartype *result; |
| 337 |
int4 i, j, k; |
| 338 |
phloat sum; |
| 339 |
int (*completion)(int error, vartype *result); |
| 340 |
}; |
| 341 |
|
| 342 |
static mul_rr_data_struct *mul_rr_data; |
| 343 |
|
| 344 |
static int matrix_mul_rr_worker(bool interrupted); |
| 345 |
|
| 346 |
static int matrix_mul_rr(vartype_realmatrix *left, vartype_realmatrix *right, |
| 347 |
int (*completion)(int, vartype *)) { |
| 348 |
|
| 349 |
mul_rr_data_struct *dat; |
| 350 |
int error; |
| 351 |
|
| 352 |
if (left->columns != right->rows) { |
| 353 |
error = ERR_DIMENSION_ERROR; |
| 354 |
goto finished; |
| 355 |
} |
| 356 |
|
| 357 |
if (contains_strings(left) || contains_strings(right)) { |
| 358 |
error = ERR_ALPHA_DATA_IS_INVALID; |
| 359 |
goto finished; |
| 360 |
} |
| 361 |
|
| 362 |
dat = (mul_rr_data_struct *) malloc(sizeof(mul_rr_data_struct)); |
| 363 |
if (dat == NULL) { |
| 364 |
error = ERR_INSUFFICIENT_MEMORY; |
| 365 |
goto finished; |
| 366 |
} |
| 367 |
|
| 368 |
dat->result = new_realmatrix(left->rows, right->columns); |
| 369 |
if (dat->result == NULL) { |
| 370 |
free(dat); |
| 371 |
error = ERR_INSUFFICIENT_MEMORY; |
| 372 |
goto finished; |
| 373 |
} |
| 374 |
|
| 375 |
dat->left = left; |
| 376 |
dat->right = right; |
| 377 |
dat->i = 0; |
| 378 |
dat->j = 0; |
| 379 |
dat->k = 0; |
| 380 |
dat->sum = 0; |
| 381 |
dat->completion = completion; |
| 382 |
|
| 383 |
mul_rr_data = dat; |
| 384 |
mode_interruptible = matrix_mul_rr_worker; |
| 385 |
mode_stoppable = false; |
| 386 |
return ERR_INTERRUPTIBLE; |
| 387 |
|
| 388 |
finished: |
| 389 |
return completion(error, NULL); |
| 390 |
} |
| 391 |
|
| 392 |
static int matrix_mul_rr_worker(bool interrupted) { |
| 393 |
mul_rr_data_struct *dat = mul_rr_data; |
| 394 |
int count = 0; |
| 395 |
int inf; |
| 396 |
phloat *l = dat->left->array->data; |
| 397 |
phloat *r = dat->right->array->data; |
| 398 |
phloat *p = ((vartype_realmatrix *) dat->result)->array->data; |
| 399 |
int4 i = dat->i; |
| 400 |
int4 j = dat->j; |
| 401 |
int4 k = dat->k; |
| 402 |
int4 m = dat->left->rows; |
| 403 |
int4 n = dat->right->columns; |
| 404 |
int4 q = dat->left->columns; |
| 405 |
phloat sum = dat->sum; |
| 406 |
|
| 407 |
if (interrupted) { |
| 408 |
int err = dat->completion(ERR_INTERRUPTED, NULL); |
| 409 |
free_vartype(dat->result); |
| 410 |
free(dat); |
| 411 |
return err; |
| 412 |
} |
| 413 |
|
| 414 |
while (count++ < 1000) { |
| 415 |
sum += l[i * q + k] * r[k * n + j]; |
| 416 |
if (++k < q) |
| 417 |
continue; |
| 418 |
k = 0; |
| 419 |
if ((inf = p_isinf(sum)) != 0) { |
| 420 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 421 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 422 |
free_vartype(dat->result); |
| 423 |
free(dat); |
| 424 |
return err; |
| 425 |
} else |
| 426 |
sum = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 427 |
} |
| 428 |
p[i * n + j] = sum; |
| 429 |
sum = 0; |
| 430 |
if (++j < n) |
| 431 |
continue; |
| 432 |
j = 0; |
| 433 |
if (++i < m) |
| 434 |
continue; |
| 435 |
else { |
| 436 |
int err = dat->completion(ERR_NONE, dat->result); |
| 437 |
free(dat); |
| 438 |
return err; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
dat->i = i; |
| 443 |
dat->j = j; |
| 444 |
dat->k = k; |
| 445 |
dat->sum = sum; |
| 446 |
return ERR_INTERRUPTIBLE; |
| 447 |
} |
| 448 |
|
| 449 |
#if 0 |
| 450 |
/* TODO: Blocked matrix multiplication |
| 451 |
* This commented-out function implements a working blocked matrix |
| 452 |
* multiplication algorithm. It works by breaking up the multiplication into |
| 453 |
* multiplications of submatrices of the multiplicands. If the submatrices are |
| 454 |
* sufficiently small, they can be made to fit entirely in the CPU's L1 cache. |
| 455 |
* With block sizes of around 45, I have observed a speed-up factor of circa |
| 456 |
* 1.7 on a Pentium MMX, and with block sizes of about 90, I have observed a |
| 457 |
* speed-up factor of circa 3 on a Duron. (Those were the optimum block sizes |
| 458 |
* observed for each processor.) These numbers suggest the Pentium MMX has a 32 |
| 459 |
* kilobyte L1 cache, and the Duron has a 128 kilobyte L1 cache. |
| 460 |
* For production use, the block size should be a user-configurable parameter, |
| 461 |
* with a built-in tool to automatically determine the optimum value. Since the |
| 462 |
* UI for this kind of advanced configuration does not exist yet, and sice the |
| 463 |
* determination of optimum block size is a very slow process, and since I |
| 464 |
* expect most HP-42S users will not be very interested in using a calculator |
| 465 |
* to work with order-100+ matrices, and since even in the optimum case the |
| 466 |
* speed-up is not staggering, I'm putting full implementation of this feature |
| 467 |
* off until some later date. |
| 468 |
*/ |
| 469 |
static int matrix_mul_rr(vartype_realmatrix *left, vartype_realmatrix *right, |
| 470 |
vartype **result) { |
| 471 |
phloat *cache = NULL; |
| 472 |
phloat *leftcache, *rightcache; |
| 473 |
|
| 474 |
phloat *l = left->array->data; |
| 475 |
phloat *r = right->array->data; |
| 476 |
phloat *p; |
| 477 |
int inf; |
| 478 |
int4 m, n, q; |
| 479 |
int4 i, j, k; |
| 480 |
|
| 481 |
q = left->columns; |
| 482 |
if (q != right->rows) |
| 483 |
return ERR_DIMENSION_ERROR; |
| 484 |
m = left->rows; |
| 485 |
n = right->columns; |
| 486 |
|
| 487 |
if (contains_strings(left) || contains_strings(right)) |
| 488 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 489 |
|
| 490 |
*result = new_realmatrix(m, n); |
| 491 |
if (*result == NULL) |
| 492 |
return ERR_INSUFFICIENT_MEMORY; |
| 493 |
p = ((vartype_realmatrix *) *result)->array->data; |
| 494 |
|
| 495 |
if (BLOCK_SIZE > 0) { |
| 496 |
int4 cachesize = 2 * BLOCK_SIZE * BLOCK_SIZE; |
| 497 |
int4 alignment = CACHE_ALIGNMENT; |
| 498 |
int4 misalgn; |
| 499 |
if (alignment == 0) |
| 500 |
alignment = 1; |
| 501 |
misalgn = alignment % sizeof(phloat); |
| 502 |
if (misalgn != 0) |
| 503 |
alignment += sizeof(phloat) - misalgn; |
| 504 |
|
| 505 |
cache = (phloat *) malloc(cachesize * sizeof(phloat) + alignment); |
| 506 |
if (cache != NULL) { |
| 507 |
misalgn = ((unsigned long) cache) % alignment; |
| 508 |
if (misalgn == 0) |
| 509 |
leftcache = cache; |
| 510 |
else |
| 511 |
leftcache = cache + (alignment - misalgn) / sizeof(phloat); |
| 512 |
rightcache = leftcache + BLOCK_SIZE * BLOCK_SIZE; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if (cache == NULL) { |
| 517 |
|
| 518 |
/* Falling back on basic i,j,k algorithm */ |
| 519 |
for (i = 0; i < m; i++) |
| 520 |
for (j = 0; j < n; j++) { |
| 521 |
phloat sum = 0; |
| 522 |
for (k = 0; k < q; k++) |
| 523 |
sum += l[i * q + k] * r[k * n + j]; |
| 524 |
if ((inf = p_isinf(sum)) != 0) { |
| 525 |
if (core_settings.matrix_outofrange |
| 526 |
&& !flags.f.range_error_ignore) |
| 527 |
return ERR_OUT_OF_RANGE; |
| 528 |
else |
| 529 |
sum = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 530 |
} |
| 531 |
p[i * n + j] = sum; |
| 532 |
} |
| 533 |
return ERR_NONE; |
| 534 |
|
| 535 |
} else { |
| 536 |
|
| 537 |
/* Blocked i,j,k algorithm for optimal cache utilization */ |
| 538 |
int4 i, j, k, ii, jj, kk; |
| 539 |
for (i = 0; i < m; i += BLOCK_SIZE) { |
| 540 |
int4 iimax = m - i; |
| 541 |
if (iimax > BLOCK_SIZE) |
| 542 |
iimax = BLOCK_SIZE; |
| 543 |
for (j = 0; j < n; j += BLOCK_SIZE) { |
| 544 |
int4 jjmax = n - j; |
| 545 |
if (jjmax > BLOCK_SIZE) |
| 546 |
jjmax = BLOCK_SIZE; |
| 547 |
for (k = 0; k < q; k += BLOCK_SIZE) { |
| 548 |
int4 kkmax = q - k; |
| 549 |
if (kkmax > BLOCK_SIZE) |
| 550 |
kkmax = BLOCK_SIZE; |
| 551 |
for (ii = 0; ii < iimax; ii++) |
| 552 |
for (kk = 0; kk < kkmax; kk++) |
| 553 |
leftcache[ii * BLOCK_SIZE + kk] |
| 554 |
= l[(ii + i) * q + (kk + k)]; |
| 555 |
for (kk = 0; kk < kkmax; kk++) |
| 556 |
for (jj = 0; jj < jjmax; jj++) |
| 557 |
rightcache[kk * BLOCK_SIZE + jj] |
| 558 |
= r[(kk + k) * n + (jj + j)]; |
| 559 |
for (ii = 0; ii < iimax; ii++) |
| 560 |
for (jj = 0; jj < jjmax; jj++) { |
| 561 |
phloat sum = p[(ii + i) * n + (jj + j)]; |
| 562 |
for (kk = 0; kk < kkmax; kk++) |
| 563 |
sum += leftcache[ii * BLOCK_SIZE + kk] |
| 564 |
* rightcache[kk * BLOCK_SIZE + jj]; |
| 565 |
if ((inf = p_isinf(sum)) != 0) { |
| 566 |
if (core_settings.matrix_outofrange |
| 567 |
&& !flags.f.range_error_ignore) { |
| 568 |
free(cache); |
| 569 |
return ERR_OUT_OF_RANGE; |
| 570 |
} else |
| 571 |
sum = inf < 0 ? NEG_HUGE_PHLOAT |
| 572 |
: POS_HUGE_PHLOAT; |
| 573 |
} |
| 574 |
p[(ii + i) * n + (jj + j)] = sum; |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
free(cache); |
| 580 |
return ERR_NONE; |
| 581 |
|
| 582 |
} |
| 583 |
} |
| 584 |
#endif |
| 585 |
|
| 586 |
struct mul_rc_data_struct { |
| 587 |
vartype_realmatrix *left; |
| 588 |
vartype_complexmatrix *right; |
| 589 |
vartype *result; |
| 590 |
int4 i, j, k; |
| 591 |
phloat sum_re, sum_im; |
| 592 |
int (*completion)(int error, vartype *result); |
| 593 |
}; |
| 594 |
|
| 595 |
static mul_rc_data_struct *mul_rc_data; |
| 596 |
|
| 597 |
static int matrix_mul_rc_worker(bool interrupted); |
| 598 |
|
| 599 |
static int matrix_mul_rc(vartype_realmatrix *left, vartype_complexmatrix *right, |
| 600 |
int (*completion)(int, vartype *)) { |
| 601 |
|
| 602 |
mul_rc_data_struct *dat; |
| 603 |
int error; |
| 604 |
|
| 605 |
if (left->columns != right->rows) { |
| 606 |
error = ERR_DIMENSION_ERROR; |
| 607 |
goto finished; |
| 608 |
} |
| 609 |
|
| 610 |
if (contains_strings(left)) { |
| 611 |
error = ERR_ALPHA_DATA_IS_INVALID; |
| 612 |
goto finished; |
| 613 |
} |
| 614 |
|
| 615 |
dat = (mul_rc_data_struct *) malloc(sizeof(mul_rc_data_struct)); |
| 616 |
if (dat == NULL) { |
| 617 |
error = ERR_INSUFFICIENT_MEMORY; |
| 618 |
goto finished; |
| 619 |
} |
| 620 |
|
| 621 |
dat->result = new_complexmatrix(left->rows, right->columns); |
| 622 |
if (dat->result == NULL) { |
| 623 |
free(dat); |
| 624 |
error = ERR_INSUFFICIENT_MEMORY; |
| 625 |
goto finished; |
| 626 |
} |
| 627 |
|
| 628 |
dat->left = left; |
| 629 |
dat->right = right; |
| 630 |
dat->i = 0; |
| 631 |
dat->j = 0; |
| 632 |
dat->k = 0; |
| 633 |
dat->sum_re = 0; |
| 634 |
dat->sum_im = 0; |
| 635 |
dat->completion = completion; |
| 636 |
|
| 637 |
mul_rc_data = dat; |
| 638 |
mode_interruptible = matrix_mul_rc_worker; |
| 639 |
mode_stoppable = false; |
| 640 |
return ERR_INTERRUPTIBLE; |
| 641 |
|
| 642 |
finished: |
| 643 |
return completion(error, NULL); |
| 644 |
} |
| 645 |
|
| 646 |
static int matrix_mul_rc_worker(bool interrupted) { |
| 647 |
mul_rc_data_struct *dat = mul_rc_data; |
| 648 |
int count = 0; |
| 649 |
int inf; |
| 650 |
phloat *l = dat->left->array->data; |
| 651 |
phloat *r = dat->right->array->data; |
| 652 |
phloat *p = ((vartype_complexmatrix *) dat->result)->array->data; |
| 653 |
int4 i = dat->i; |
| 654 |
int4 j = dat->j; |
| 655 |
int4 k = dat->k; |
| 656 |
int4 m = dat->left->rows; |
| 657 |
int4 n = dat->right->columns; |
| 658 |
int4 q = dat->left->columns; |
| 659 |
phloat sum_re = dat->sum_re; |
| 660 |
phloat sum_im = dat->sum_im; |
| 661 |
|
| 662 |
if (interrupted) { |
| 663 |
int err = dat->completion(ERR_INTERRUPTED, NULL); |
| 664 |
free_vartype(dat->result); |
| 665 |
free(dat); |
| 666 |
return err; |
| 667 |
} |
| 668 |
|
| 669 |
while (count++ < 1000) { |
| 670 |
phloat tmp = l[i * q + k]; |
| 671 |
sum_re += tmp * r[2 * (k * n + j)]; |
| 672 |
sum_im += tmp * r[2 * (k * n + j) + 1]; |
| 673 |
if (++k < q) |
| 674 |
continue; |
| 675 |
k = 0; |
| 676 |
if ((inf = p_isinf(sum_re)) != 0) { |
| 677 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 678 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 679 |
free_vartype(dat->result); |
| 680 |
free(dat); |
| 681 |
return err; |
| 682 |
} else |
| 683 |
sum_re = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 684 |
} |
| 685 |
if ((inf = p_isinf(sum_im)) != 0) { |
| 686 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 687 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 688 |
free_vartype(dat->result); |
| 689 |
free(dat); |
| 690 |
return err; |
| 691 |
} else |
| 692 |
sum_im = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 693 |
} |
| 694 |
p[2 * (i * n + j)] = sum_re; |
| 695 |
p[2 * (i * n + j) + 1] = sum_im; |
| 696 |
sum_re = 0; |
| 697 |
sum_im = 0; |
| 698 |
if (++j < n) |
| 699 |
continue; |
| 700 |
j = 0; |
| 701 |
if (++i < m) |
| 702 |
continue; |
| 703 |
else { |
| 704 |
int err = dat->completion(ERR_NONE, dat->result); |
| 705 |
free(dat); |
| 706 |
return err; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
dat->i = i; |
| 711 |
dat->j = j; |
| 712 |
dat->k = k; |
| 713 |
dat->sum_re = sum_re; |
| 714 |
dat->sum_im = sum_im; |
| 715 |
return ERR_INTERRUPTIBLE; |
| 716 |
} |
| 717 |
|
| 718 |
struct mul_cr_data_struct { |
| 719 |
vartype_complexmatrix *left; |
| 720 |
vartype_realmatrix *right; |
| 721 |
vartype *result; |
| 722 |
int4 i, j, k; |
| 723 |
phloat sum_re, sum_im; |
| 724 |
int (*completion)(int error, vartype *result); |
| 725 |
}; |
| 726 |
|
| 727 |
static mul_cr_data_struct *mul_cr_data; |
| 728 |
|
| 729 |
static int matrix_mul_cr_worker(bool interrupted); |
| 730 |
|
| 731 |
static int matrix_mul_cr(vartype_complexmatrix *left, vartype_realmatrix *right, |
| 732 |
int (*completion)(int, vartype *)) { |
| 733 |
|
| 734 |
mul_cr_data_struct *dat; |
| 735 |
int error; |
| 736 |
|
| 737 |
if (left->columns != right->rows) { |
| 738 |
error = ERR_DIMENSION_ERROR; |
| 739 |
goto finished; |
| 740 |
} |
| 741 |
|
| 742 |
if (contains_strings(right)) { |
| 743 |
error = ERR_ALPHA_DATA_IS_INVALID; |
| 744 |
goto finished; |
| 745 |
} |
| 746 |
|
| 747 |
dat = (mul_cr_data_struct *) malloc(sizeof(mul_cr_data_struct)); |
| 748 |
if (dat == NULL) { |
| 749 |
error = ERR_INSUFFICIENT_MEMORY; |
| 750 |
goto finished; |
| 751 |
} |
| 752 |
|
| 753 |
dat->result = new_complexmatrix(left->rows, right->columns); |
| 754 |
if (dat->result == NULL) { |
| 755 |
free(dat); |
| 756 |
error = ERR_INSUFFICIENT_MEMORY; |
| 757 |
goto finished; |
| 758 |
} |
| 759 |
|
| 760 |
dat->left = left; |
| 761 |
dat->right = right; |
| 762 |
dat->i = 0; |
| 763 |
dat->j = 0; |
| 764 |
dat->k = 0; |
| 765 |
dat->sum_re = 0; |
| 766 |
dat->sum_im = 0; |
| 767 |
dat->completion = completion; |
| 768 |
|
| 769 |
mul_cr_data = dat; |
| 770 |
mode_interruptible = matrix_mul_cr_worker; |
| 771 |
mode_stoppable = false; |
| 772 |
return ERR_INTERRUPTIBLE; |
| 773 |
|
| 774 |
finished: |
| 775 |
return completion(error, NULL); |
| 776 |
} |
| 777 |
|
| 778 |
static int matrix_mul_cr_worker(bool interrupted) { |
| 779 |
mul_cr_data_struct *dat = mul_cr_data; |
| 780 |
int count = 0; |
| 781 |
int inf; |
| 782 |
phloat *l = dat->left->array->data; |
| 783 |
phloat *r = dat->right->array->data; |
| 784 |
phloat *p = ((vartype_complexmatrix *) dat->result)->array->data; |
| 785 |
int4 i = dat->i; |
| 786 |
int4 j = dat->j; |
| 787 |
int4 k = dat->k; |
| 788 |
int4 m = dat->left->rows; |
| 789 |
int4 n = dat->right->columns; |
| 790 |
int4 q = dat->left->columns; |
| 791 |
phloat sum_re = dat->sum_re; |
| 792 |
phloat sum_im = dat->sum_im; |
| 793 |
|
| 794 |
if (interrupted) { |
| 795 |
int err = dat->completion(ERR_INTERRUPTED, NULL); |
| 796 |
free_vartype(dat->result); |
| 797 |
free(dat); |
| 798 |
return err; |
| 799 |
} |
| 800 |
|
| 801 |
while (count++ < 1000) { |
| 802 |
phloat tmp = r[k * n + j]; |
| 803 |
sum_re += tmp * l[2 * (i * q + k)]; |
| 804 |
sum_im += tmp * l[2 * (i * q + k) + 1]; |
| 805 |
if (++k < q) |
| 806 |
continue; |
| 807 |
k = 0; |
| 808 |
if ((inf = p_isinf(sum_re)) != 0) { |
| 809 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 810 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 811 |
free_vartype(dat->result); |
| 812 |
free(dat); |
| 813 |
return err; |
| 814 |
} else |
| 815 |
sum_re = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 816 |
} |
| 817 |
if ((inf = p_isinf(sum_im)) != 0) { |
| 818 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 819 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 820 |
free_vartype(dat->result); |
| 821 |
free(dat); |
| 822 |
return err; |
| 823 |
} else |
| 824 |
sum_im = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 825 |
} |
| 826 |
p[2 * (i * n + j)] = sum_re; |
| 827 |
p[2 * (i * n + j) + 1] = sum_im; |
| 828 |
sum_re = 0; |
| 829 |
sum_im = 0; |
| 830 |
if (++j < n) |
| 831 |
continue; |
| 832 |
j = 0; |
| 833 |
if (++i < m) |
| 834 |
continue; |
| 835 |
else { |
| 836 |
int err = dat->completion(ERR_NONE, dat->result); |
| 837 |
free(dat); |
| 838 |
return err; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
dat->i = i; |
| 843 |
dat->j = j; |
| 844 |
dat->k = k; |
| 845 |
dat->sum_re = sum_re; |
| 846 |
dat->sum_im = sum_im; |
| 847 |
return ERR_INTERRUPTIBLE; |
| 848 |
} |
| 849 |
|
| 850 |
struct mul_cc_data_struct { |
| 851 |
vartype_complexmatrix *left; |
| 852 |
vartype_complexmatrix *right; |
| 853 |
vartype *result; |
| 854 |
int4 i, j, k; |
| 855 |
phloat sum_re, sum_im; |
| 856 |
int (*completion)(int error, vartype *result); |
| 857 |
}; |
| 858 |
|
| 859 |
static mul_cc_data_struct *mul_cc_data; |
| 860 |
|
| 861 |
static int matrix_mul_cc_worker(bool interrupted); |
| 862 |
|
| 863 |
static int matrix_mul_cc(vartype_complexmatrix *left, vartype_complexmatrix *right, |
| 864 |
int (*completion)(int, vartype *)) { |
| 865 |
|
| 866 |
mul_cc_data_struct *dat; |
| 867 |
int error; |
| 868 |
|
| 869 |
if (left->columns != right->rows) { |
| 870 |
error = ERR_DIMENSION_ERROR; |
| 871 |
goto finished; |
| 872 |
} |
| 873 |
|
| 874 |
dat = (mul_cc_data_struct *) malloc(sizeof(mul_cc_data_struct)); |
| 875 |
if (dat == NULL) { |
| 876 |
error = ERR_INSUFFICIENT_MEMORY; |
| 877 |
goto finished; |
| 878 |
} |
| 879 |
|
| 880 |
dat->result = new_complexmatrix(left->rows, right->columns); |
| 881 |
if (dat->result == NULL) { |
| 882 |
free(dat); |
| 883 |
error = ERR_INSUFFICIENT_MEMORY; |
| 884 |
goto finished; |
| 885 |
} |
| 886 |
|
| 887 |
dat->left = left; |
| 888 |
dat->right = right; |
| 889 |
dat->i = 0; |
| 890 |
dat->j = 0; |
| 891 |
dat->k = 0; |
| 892 |
dat->sum_re = 0; |
| 893 |
dat->sum_im = 0; |
| 894 |
dat->completion = completion; |
| 895 |
|
| 896 |
mul_cc_data = dat; |
| 897 |
mode_interruptible = matrix_mul_cc_worker; |
| 898 |
mode_stoppable = false; |
| 899 |
return ERR_INTERRUPTIBLE; |
| 900 |
|
| 901 |
finished: |
| 902 |
return completion(error, NULL); |
| 903 |
} |
| 904 |
|
| 905 |
static int matrix_mul_cc_worker(bool interrupted) { |
| 906 |
mul_cc_data_struct *dat = mul_cc_data; |
| 907 |
int count = 0; |
| 908 |
int inf; |
| 909 |
phloat *l = dat->left->array->data; |
| 910 |
phloat *r = dat->right->array->data; |
| 911 |
phloat *p = ((vartype_complexmatrix *) dat->result)->array->data; |
| 912 |
int4 i = dat->i; |
| 913 |
int4 j = dat->j; |
| 914 |
int4 k = dat->k; |
| 915 |
int4 m = dat->left->rows; |
| 916 |
int4 n = dat->right->columns; |
| 917 |
int4 q = dat->left->columns; |
| 918 |
phloat sum_re = dat->sum_re; |
| 919 |
phloat sum_im = dat->sum_im; |
| 920 |
|
| 921 |
if (interrupted) { |
| 922 |
int err = dat->completion(ERR_INTERRUPTED, NULL); |
| 923 |
free_vartype(dat->result); |
| 924 |
free(dat); |
| 925 |
return err; |
| 926 |
} |
| 927 |
|
| 928 |
while (count++ < 1000) { |
| 929 |
phloat l_re = l[2 * (i * q + k)]; |
| 930 |
phloat l_im = l[2 * (i * q + k) + 1]; |
| 931 |
phloat r_re = r[2 * (k * n + j)]; |
| 932 |
phloat r_im = r[2 * (k * n + j) + 1]; |
| 933 |
sum_re += l_re * r_re - l_im * r_im; |
| 934 |
sum_im += l_im * r_re + l_re * r_im; |
| 935 |
if (++k < q) |
| 936 |
continue; |
| 937 |
k = 0; |
| 938 |
if ((inf = p_isinf(sum_re)) != 0) { |
| 939 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 940 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 941 |
free_vartype(dat->result); |
| 942 |
free(dat); |
| 943 |
return err; |
| 944 |
} else |
| 945 |
sum_re = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 946 |
} |
| 947 |
if ((inf = p_isinf(sum_im)) != 0) { |
| 948 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore){ |
| 949 |
int err = dat->completion(ERR_OUT_OF_RANGE, NULL); |
| 950 |
free_vartype(dat->result); |
| 951 |
free(dat); |
| 952 |
return err; |
| 953 |
} else |
| 954 |
sum_im = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 955 |
} |
| 956 |
p[2 * (i * n + j)] = sum_re; |
| 957 |
p[2 * (i * n + j) + 1] = sum_im; |
| 958 |
sum_re = 0; |
| 959 |
sum_im = 0; |
| 960 |
if (++j < n) |
| 961 |
continue; |
| 962 |
j = 0; |
| 963 |
if (++i < m) |
| 964 |
continue; |
| 965 |
else { |
| 966 |
int err = dat->completion(ERR_NONE, dat->result); |
| 967 |
free(dat); |
| 968 |
return err; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
dat->i = i; |
| 973 |
dat->j = j; |
| 974 |
dat->k = k; |
| 975 |
dat->sum_re = sum_re; |
| 976 |
dat->sum_im = sum_im; |
| 977 |
return ERR_INTERRUPTIBLE; |
| 978 |
} |
| 979 |
|
| 980 |
int linalg_mul(const vartype *left, const vartype *right, |
| 981 |
int (*completion)(int, vartype *)) { |
| 982 |
if (left->type == TYPE_REALMATRIX) { |
| 983 |
if (right->type == TYPE_REALMATRIX) |
| 984 |
return matrix_mul_rr((vartype_realmatrix *) left, |
| 985 |
(vartype_realmatrix *) right, |
| 986 |
completion); |
| 987 |
else |
| 988 |
return matrix_mul_rc((vartype_realmatrix *) left, |
| 989 |
(vartype_complexmatrix *) right, |
| 990 |
completion); |
| 991 |
} else { |
| 992 |
if (right->type == TYPE_REALMATRIX) |
| 993 |
return matrix_mul_cr((vartype_complexmatrix *) left, |
| 994 |
(vartype_realmatrix *) right, |
| 995 |
completion); |
| 996 |
else |
| 997 |
return matrix_mul_cc((vartype_complexmatrix *) left, |
| 998 |
(vartype_complexmatrix *) right, |
| 999 |
completion); |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
/**************************/ |
| 1005 |
/***** Matrix inverse *****/ |
| 1006 |
/**************************/ |
| 1007 |
|
| 1008 |
static int (*linalg_inv_completion)(int error, vartype *det); |
| 1009 |
static vartype *linalg_inv_result; |
| 1010 |
|
| 1011 |
static int inv_r_completion1(int error, vartype_realmatrix *a, int4 *perm, |
| 1012 |
phloat det); |
| 1013 |
static int inv_r_completion2(int error, vartype_realmatrix *a, int4 *perm, |
| 1014 |
vartype_realmatrix *b); |
| 1015 |
static int inv_c_completion1(int error, vartype_complexmatrix *a, int4 *perm, |
| 1016 |
phloat det_re, phloat det_im); |
| 1017 |
static int inv_c_completion2(int error, vartype_complexmatrix *a, int4 *perm, |
| 1018 |
vartype_complexmatrix *b); |
| 1019 |
|
| 1020 |
int linalg_inv(const vartype *src, int (*completion)(int, vartype *)) { |
| 1021 |
int4 n; |
| 1022 |
int4 *perm; |
| 1023 |
if (src->type == TYPE_REALMATRIX) { |
| 1024 |
vartype_realmatrix *ma = (vartype_realmatrix *) src; |
| 1025 |
vartype *lu, *inv; |
| 1026 |
n = ma->rows; |
| 1027 |
if (n != ma->columns) |
| 1028 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 1029 |
if (contains_strings(ma)) |
| 1030 |
return completion(ERR_ALPHA_DATA_IS_INVALID, NULL); |
| 1031 |
if (n <= 2) |
| 1032 |
return small_inv_r(ma, completion); |
| 1033 |
lu = new_realmatrix(n, n); |
| 1034 |
if (lu == NULL) |
| 1035 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1036 |
inv = new_realmatrix(n, n); |
| 1037 |
if (inv == NULL) { |
| 1038 |
free_vartype(lu); |
| 1039 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1040 |
} |
| 1041 |
perm = (int4 *) malloc(n * sizeof(int4)); |
| 1042 |
if (perm == NULL) { |
| 1043 |
free_vartype(lu); |
| 1044 |
free_vartype(inv); |
| 1045 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1046 |
} |
| 1047 |
matrix_copy(lu, src); |
| 1048 |
linalg_inv_completion = completion; |
| 1049 |
linalg_inv_result = inv; |
| 1050 |
return lu_decomp_r((vartype_realmatrix *) lu, perm, inv_r_completion1); |
| 1051 |
} else { |
| 1052 |
vartype_complexmatrix *ma = (vartype_complexmatrix *) src; |
| 1053 |
vartype *lu, *inv; |
| 1054 |
n = ma->rows; |
| 1055 |
if (n != ma->columns) |
| 1056 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 1057 |
if (n <= 2) |
| 1058 |
return small_inv_c(ma, completion); |
| 1059 |
lu = new_complexmatrix(n, n); |
| 1060 |
if (lu == NULL) |
| 1061 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1062 |
inv = new_complexmatrix(n, n); |
| 1063 |
if (inv == NULL) { |
| 1064 |
free_vartype(lu); |
| 1065 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1066 |
} |
| 1067 |
perm = (int4 *) malloc(n * sizeof(int4)); |
| 1068 |
if (perm == NULL) { |
| 1069 |
free_vartype(lu); |
| 1070 |
free_vartype(inv); |
| 1071 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1072 |
} |
| 1073 |
matrix_copy(lu, src); |
| 1074 |
linalg_inv_completion = completion; |
| 1075 |
linalg_inv_result = inv; |
| 1076 |
return lu_decomp_c((vartype_complexmatrix *) lu, perm, |
| 1077 |
inv_c_completion1); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
static int inv_r_completion1(int error, vartype_realmatrix *a, int4 *perm, |
| 1082 |
phloat det) { |
| 1083 |
if (error != ERR_NONE) { |
| 1084 |
free_vartype(linalg_inv_result); |
| 1085 |
free_vartype((vartype *) a); |
| 1086 |
free(perm); |
| 1087 |
return linalg_inv_completion(error, NULL); |
| 1088 |
} else { |
| 1089 |
int4 i, n = a->rows; |
| 1090 |
vartype_realmatrix *inv = (vartype_realmatrix *) linalg_inv_result; |
| 1091 |
for (i = 0; i < n; i++) |
| 1092 |
inv->array->data[i * (n + 1)] = 1; |
| 1093 |
return lu_backsubst_rr(a, perm, inv, inv_r_completion2); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
static int inv_r_completion2(int error, vartype_realmatrix *a, int4 *perm, |
| 1098 |
vartype_realmatrix *b) { |
| 1099 |
if (error != ERR_NONE) |
| 1100 |
free_vartype(linalg_inv_result); /* Note: linalg_inv_result == b */ |
| 1101 |
free_vartype((vartype *) a); |
| 1102 |
free(perm); |
| 1103 |
return linalg_inv_completion(error, linalg_inv_result); |
| 1104 |
} |
| 1105 |
|
| 1106 |
static int inv_c_completion1(int error, vartype_complexmatrix *a, int4 *perm, |
| 1107 |
phloat det_re, phloat det_im) { |
| 1108 |
if (error != ERR_NONE) { |
| 1109 |
free_vartype(linalg_inv_result); |
| 1110 |
free_vartype((vartype *) a); |
| 1111 |
free(perm); |
| 1112 |
return linalg_inv_completion(error, NULL); |
| 1113 |
} else { |
| 1114 |
int4 i, n = a->rows; |
| 1115 |
vartype_complexmatrix *inv = |
| 1116 |
(vartype_complexmatrix *) linalg_inv_result; |
| 1117 |
for (i = 0; i < n; i++) |
| 1118 |
inv->array->data[2 * (i * (n + 1))] = 1; |
| 1119 |
return lu_backsubst_cc(a, perm, inv, inv_c_completion2); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
static int inv_c_completion2(int error, vartype_complexmatrix *a, int4 *perm, |
| 1124 |
vartype_complexmatrix *b) { |
| 1125 |
if (error != ERR_NONE) |
| 1126 |
free_vartype(linalg_inv_result); /* Note: linalg_inv_result == b */ |
| 1127 |
free_vartype((vartype *) a); |
| 1128 |
free(perm); |
| 1129 |
return linalg_inv_completion(error, linalg_inv_result); |
| 1130 |
} |
| 1131 |
|
| 1132 |
static int small_det_r(vartype_realmatrix *m, phloat *r); |
| 1133 |
static int small_det_c(vartype_complexmatrix *m, phloat *dre, phloat *dim); |
| 1134 |
|
| 1135 |
static vartype *small_inv_res; |
| 1136 |
static int small_inv_completion(int err, vartype *res) { |
| 1137 |
small_inv_res = res; |
| 1138 |
return err; |
| 1139 |
} |
| 1140 |
|
| 1141 |
static int small_inv_r(vartype_realmatrix *ma, int (*completion)(int, vartype *)) { |
| 1142 |
if (ma->rows == 1) { |
| 1143 |
phloat x = ma->array->data[0]; |
| 1144 |
if (x == 0) { |
| 1145 |
if (core_settings.matrix_singularmatrix) |
| 1146 |
return completion(ERR_SINGULAR_MATRIX, NULL); |
| 1147 |
x = POS_HUGE_PHLOAT; |
| 1148 |
} else { |
| 1149 |
x = phloat(1) / x; |
| 1150 |
int inf = p_isinf(x); |
| 1151 |
if (inf != 0) { |
| 1152 |
if (core_settings.matrix_outofrange && !flags.f.range_error_ignore) |
| 1153 |
return completion(ERR_OUT_OF_RANGE, NULL); |
| 1154 |
x = inf > 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
vartype *r = new_realmatrix(1, 1); |
| 1158 |
if (r == NULL) |
| 1159 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1160 |
((vartype_realmatrix *) r)->array->data[0] = x; |
| 1161 |
return completion(ERR_NONE, r); |
| 1162 |
} |
| 1163 |
phloat det; |
| 1164 |
int err = small_det_r(ma, &det); |
| 1165 |
if (det == 0) { |
| 1166 |
if (core_settings.matrix_singularmatrix) |
| 1167 |
return completion(ERR_SINGULAR_MATRIX, NULL); |
| 1168 |
vartype_realmatrix *sm = (vartype_realmatrix *) new_realmatrix(2, 2); |
| 1169 |
if (sm == NULL) |
| 1170 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1171 |
sm->array->data[0] = POS_HUGE_PHLOAT; |
| 1172 |
sm->array->data[3] = POS_HUGE_PHLOAT; |
| 1173 |
return completion(ERR_NONE, (vartype *) sm); |
| 1174 |
} |
| 1175 |
vartype *d = new_real(det); |
| 1176 |
if (d == NULL) |
| 1177 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1178 |
bool saved_range_error_ignore = flags.f.range_error_ignore; |
| 1179 |
flags.f.range_error_ignore = !core_settings.matrix_outofrange || flags.f.range_error_ignore; |
| 1180 |
err = generic_div(d, (vartype *) ma, small_inv_completion); |
| 1181 |
flags.f.range_error_ignore = saved_range_error_ignore; |
| 1182 |
free_vartype(d); |
| 1183 |
if (err != ERR_NONE) |
| 1184 |
return completion(err, NULL); |
| 1185 |
phloat *data = ((vartype_realmatrix *) small_inv_res)->array->data; |
| 1186 |
phloat t = data[0]; |
| 1187 |
data[0] = data[3]; |
| 1188 |
data[3] = t; |
| 1189 |
data[1] = -data[1]; |
| 1190 |
data[2] = -data[2]; |
| 1191 |
return completion(ERR_NONE, small_inv_res); |
| 1192 |
} |
| 1193 |
|
| 1194 |
static int small_inv_c(vartype_complexmatrix *ma, int (*completion)(int, vartype *)) { |
| 1195 |
if (ma->rows == 1) { |
| 1196 |
phloat xre = ma->array->data[0]; |
| 1197 |
phloat xim = ma->array->data[1]; |
| 1198 |
if (xre == 0 && xim == 0) { |
| 1199 |
if (core_settings.matrix_singularmatrix) |
| 1200 |
return completion(ERR_SINGULAR_MATRIX, NULL); |
| 1201 |
xre = POS_HUGE_PHLOAT; |
| 1202 |
xim = 0; |
| 1203 |
} else { |
| 1204 |
bool saved_range_error_ignore = flags.f.range_error_ignore; |
| 1205 |
flags.f.range_error_ignore = !core_settings.matrix_outofrange || flags.f.range_error_ignore; |
| 1206 |
int err = math_inv(xre, xim, &xre, &xim); |
| 1207 |
flags.f.range_error_ignore = saved_range_error_ignore; |
| 1208 |
if (err != ERR_NONE) |
| 1209 |
return completion(err, NULL); |
| 1210 |
} |
| 1211 |
vartype *r = new_complexmatrix(1, 1); |
| 1212 |
if (r == NULL) |
| 1213 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1214 |
((vartype_complexmatrix *) r)->array->data[0] = xre; |
| 1215 |
((vartype_complexmatrix *) r)->array->data[1] = xim; |
| 1216 |
return completion(ERR_NONE, r); |
| 1217 |
} |
| 1218 |
phloat dre, dim; |
| 1219 |
int err = small_det_c(ma, &dre, &dim); |
| 1220 |
if (dre == 0 && dim == 0) { |
| 1221 |
if (core_settings.matrix_singularmatrix) |
| 1222 |
return completion(ERR_SINGULAR_MATRIX, NULL); |
| 1223 |
vartype_complexmatrix *sm = (vartype_complexmatrix *) new_complexmatrix(2, 2); |
| 1224 |
if (sm == NULL) |
| 1225 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1226 |
sm->array->data[0] = POS_HUGE_PHLOAT; |
| 1227 |
sm->array->data[6] = POS_HUGE_PHLOAT; |
| 1228 |
return completion(ERR_NONE, (vartype *) sm); |
| 1229 |
} |
| 1230 |
vartype *d = new_complex(dre, dim); |
| 1231 |
if (d == NULL) |
| 1232 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1233 |
bool saved_range_error_ignore = flags.f.range_error_ignore; |
| 1234 |
flags.f.range_error_ignore = !core_settings.matrix_outofrange || flags.f.range_error_ignore; |
| 1235 |
err = generic_div(d, (vartype *) ma, small_inv_completion); |
| 1236 |
flags.f.range_error_ignore = saved_range_error_ignore; |
| 1237 |
free_vartype(d); |
| 1238 |
if (err != ERR_NONE) |
| 1239 |
return completion(err, NULL); |
| 1240 |
phloat *data = ((vartype_complexmatrix *) small_inv_res)->array->data; |
| 1241 |
phloat t = data[0]; |
| 1242 |
data[0] = data[6]; |
| 1243 |
data[6] = t; |
| 1244 |
t = data[1]; |
| 1245 |
data[1] = data[7]; |
| 1246 |
data[7] = t; |
| 1247 |
data[2] = -data[2]; |
| 1248 |
data[3] = -data[3]; |
| 1249 |
data[4] = -data[4]; |
| 1250 |
data[5] = -data[5]; |
| 1251 |
return completion(ERR_NONE, small_inv_res); |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
/******************************/ |
| 1256 |
/***** Matrix determinant *****/ |
| 1257 |
/******************************/ |
| 1258 |
|
| 1259 |
static int (*linalg_det_completion)(int, vartype *det); |
| 1260 |
static bool linalg_det_prev_sm_err; |
| 1261 |
|
| 1262 |
static int det_r_completion(int error, vartype_realmatrix *a, int4 *perm, |
| 1263 |
phloat det); |
| 1264 |
static int det_c_completion(int error, vartype_complexmatrix *a, int4 *perm, |
| 1265 |
phloat det_re, phloat det_im); |
| 1266 |
|
| 1267 |
int linalg_det(const vartype *src, int (*completion)(int, vartype *)) { |
| 1268 |
int4 n; |
| 1269 |
int4 *perm; |
| 1270 |
if (src->type == TYPE_REALMATRIX) { |
| 1271 |
vartype_realmatrix *ma = (vartype_realmatrix *) src; |
| 1272 |
n = ma->rows; |
| 1273 |
if (n != ma->columns) |
| 1274 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 1275 |
if (contains_strings(ma)) |
| 1276 |
return completion(ERR_ALPHA_DATA_IS_INVALID, NULL); |
| 1277 |
if (n <= 2) { |
| 1278 |
phloat d; |
| 1279 |
int err = small_det_r(ma, &d); |
| 1280 |
if (err != ERR_NONE) |
| 1281 |
return completion(err, NULL); |
| 1282 |
vartype *v = new_real(d); |
| 1283 |
if (v == NULL) |
| 1284 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1285 |
return completion(ERR_NONE, v); |
| 1286 |
} |
| 1287 |
ma = (vartype_realmatrix *) dup_vartype(src); |
| 1288 |
if (ma == NULL) |
| 1289 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1290 |
if (!disentangle((vartype *) ma)) { |
| 1291 |
free_vartype((vartype *) ma); |
| 1292 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1293 |
} |
| 1294 |
perm = (int4 *) malloc(n * sizeof(int4)); |
| 1295 |
if (perm == NULL) { |
| 1296 |
free_vartype((vartype *) ma); |
| 1297 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1298 |
} |
| 1299 |
|
| 1300 |
/* Before calling lu_decomp_r, make sure the 'singular matrix' |
| 1301 |
* error reporting mode is on; we don't want the HP-42S compatible |
| 1302 |
* zero-pivot-fudging to take place when all we're doing is computing |
| 1303 |
* the determinant. |
| 1304 |
* The completion routine will restore the 'singular matrix' error |
| 1305 |
* mode to its original value. |
| 1306 |
*/ |
| 1307 |
linalg_det_prev_sm_err = core_settings.matrix_singularmatrix; |
| 1308 |
core_settings.matrix_singularmatrix = true; |
| 1309 |
|
| 1310 |
linalg_det_completion = completion; |
| 1311 |
return lu_decomp_r(ma, perm, det_r_completion); |
| 1312 |
} else /* src->type == TYPE_COMPLEXMATRIX */ { |
| 1313 |
vartype_complexmatrix *ma = (vartype_complexmatrix *) src; |
| 1314 |
n = ma->rows; |
| 1315 |
if (n != ma->columns) |
| 1316 |
return completion(ERR_DIMENSION_ERROR, NULL); |
| 1317 |
if (n <= 2) { |
| 1318 |
phloat dre, dim; |
| 1319 |
int err = small_det_c(ma, &dre, &dim); |
| 1320 |
if (err != ERR_NONE) |
| 1321 |
return completion(err, NULL); |
| 1322 |
vartype *v = new_complex(dre, dim); |
| 1323 |
if (v == NULL) |
| 1324 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1325 |
return completion(ERR_NONE, v); |
| 1326 |
} |
| 1327 |
ma = (vartype_complexmatrix *) dup_vartype(src); |
| 1328 |
if (ma == NULL) |
| 1329 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1330 |
if (!disentangle((vartype *) ma)) { |
| 1331 |
free_vartype((vartype *) ma); |
| 1332 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1333 |
} |
| 1334 |
n = ma->rows; |
| 1335 |
perm = (int4 *) malloc(n * sizeof(int4)); |
| 1336 |
if (perm == NULL) { |
| 1337 |
free_vartype((vartype *) ma); |
| 1338 |
return completion(ERR_INSUFFICIENT_MEMORY, NULL); |
| 1339 |
} |
| 1340 |
|
| 1341 |
/* Before calling lu_decomp_c, make sure the 'singular matrix' |
| 1342 |
* error reporting mode is on; we don't want the HP-42S compatible |
| 1343 |
* zero-pivot-fudging to take place when all we're doing is computing |
| 1344 |
* the determinant. |
| 1345 |
* The completion routine will restore the 'singular matrix' error |
| 1346 |
* mode to its original value. |
| 1347 |
*/ |
| 1348 |
linalg_det_prev_sm_err = core_settings.matrix_singularmatrix; |
| 1349 |
core_settings.matrix_singularmatrix = true; |
| 1350 |
|
| 1351 |
linalg_det_completion = completion; |
| 1352 |
return lu_decomp_c(ma, perm, det_c_completion); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
static int det_r_completion(int error, vartype_realmatrix *a, int4 *perm, |
| 1357 |
phloat det) { |
| 1358 |
vartype *det_v = NULL; |
| 1359 |
|
| 1360 |
core_settings.matrix_singularmatrix = linalg_det_prev_sm_err; |
| 1361 |
|
| 1362 |
free_vartype((vartype *) a); |
| 1363 |
free(perm); |
| 1364 |
if (error == ERR_SINGULAR_MATRIX) { |
| 1365 |
det = 0; |
| 1366 |
error = ERR_NONE; |
| 1367 |
} |
| 1368 |
if (error == ERR_NONE) { |
| 1369 |
int inf = p_isinf(det); |
| 1370 |
if (inf != 0) { |
| 1371 |
if (flags.f.range_error_ignore) |
| 1372 |
det = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1373 |
else |
| 1374 |
error = ERR_OUT_OF_RANGE; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
if (error == ERR_NONE) { |
| 1378 |
det_v = new_real(det); |
| 1379 |
if (det_v == NULL) |
| 1380 |
error = ERR_INSUFFICIENT_MEMORY; |
| 1381 |
} |
| 1382 |
|
| 1383 |
return linalg_det_completion(error, det_v); |
| 1384 |
} |
| 1385 |
|
| 1386 |
static int det_c_completion(int error, vartype_complexmatrix *a, int4 *perm, |
| 1387 |
phloat det_re, phloat det_im) { |
| 1388 |
vartype *det_v = NULL; |
| 1389 |
|
| 1390 |
core_settings.matrix_singularmatrix = linalg_det_prev_sm_err; |
| 1391 |
|
| 1392 |
free_vartype((vartype *) a); |
| 1393 |
free(perm); |
| 1394 |
if (error == ERR_SINGULAR_MATRIX) { |
| 1395 |
det_re = 0; |
| 1396 |
det_im = 0; |
| 1397 |
error = ERR_NONE; |
| 1398 |
} |
| 1399 |
if (error == ERR_NONE) { |
| 1400 |
int inf; |
| 1401 |
if ((inf = p_isinf(det_re)) != 0) { |
| 1402 |
if (flags.f.range_error_ignore) |
| 1403 |
det_re = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1404 |
else |
| 1405 |
error = ERR_OUT_OF_RANGE; |
| 1406 |
} |
| 1407 |
if ((inf = p_isinf(det_im)) != 0) { |
| 1408 |
if (flags.f.range_error_ignore) |
| 1409 |
det_im = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1410 |
else |
| 1411 |
error = ERR_OUT_OF_RANGE; |
| 1412 |
} |
| 1413 |
} |
| 1414 |
if (error == ERR_NONE) { |
| 1415 |
det_v = new_complex(det_re, det_im); |
| 1416 |
if (det_v == NULL) |
| 1417 |
error = ERR_INSUFFICIENT_MEMORY; |
| 1418 |
} |
| 1419 |
|
| 1420 |
return linalg_det_completion(error, det_v); |
| 1421 |
} |
| 1422 |
|
| 1423 |
static phloat dot_2d(phloat a1, phloat a2, phloat a3, phloat a4, bool add, int *scale = NULL) { |
| 1424 |
int s1 = a1 == 0 || p_isinf(a1) != 0 ? 0 : ilogb(a1); |
| 1425 |
int s2 = a2 == 0 || p_isinf(a2) != 0 ? 0 : ilogb(a2); |
| 1426 |
int s3 = a3 == 0 || p_isinf(a3) != 0 ? 0 : ilogb(a3); |
| 1427 |
int s4 = a4 == 0 || p_isinf(a4) != 0 ? 0 : ilogb(a4); |
| 1428 |
phloat p1 = scalbn(a1, -s1) * scalbn(a3, -s3); |
| 1429 |
int z1 = s1 + s3; |
| 1430 |
phloat p2 = scalbn(a2, -s2) * scalbn(a4, -s4); |
| 1431 |
int z2 = s2 + s4; |
| 1432 |
if (z1 > z2) { |
| 1433 |
p2 = scalbn(p2, z2 - z1); |
| 1434 |
z2 = z1; |
| 1435 |
} else if (z2 > z1) { |
| 1436 |
p1 = scalbn(p1, z1 - z2); |
| 1437 |
z1 = z2; |
| 1438 |
} |
| 1439 |
if (scale == NULL) |
| 1440 |
return scalbn(add ? p1 + p2 : p1 - p2, z1); |
| 1441 |
*scale = z1; |
| 1442 |
return add ? p1 + p2 : p1 - p2; |
| 1443 |
} |
| 1444 |
|
| 1445 |
static phloat ssub(phloat a1, int s1, phloat a2, int s2) { |
| 1446 |
if (s1 > s2) { |
| 1447 |
a2 = scalbn(a2, s2 - s1); |
| 1448 |
s2 = s1; |
| 1449 |
} else if (s2 > s1) { |
| 1450 |
a1 = scalbn(a1, s1 - s2); |
| 1451 |
s1 = s2; |
| 1452 |
} |
| 1453 |
return scalbn(a1 - a2, s1); |
| 1454 |
} |
| 1455 |
|
| 1456 |
static int small_det_r(vartype_realmatrix *m, phloat *r) { |
| 1457 |
if (m->rows == 1) { |
| 1458 |
*r = m->array->data[0]; |
| 1459 |
return ERR_NONE; |
| 1460 |
} |
| 1461 |
phloat *a = m->array->data; |
| 1462 |
*r = dot_2d(a[0], a[1], a[3], a[2], false); |
| 1463 |
int inf = p_isinf(*r); |
| 1464 |
if (inf != 0) |
| 1465 |
if (flags.f.range_error_ignore) |
| 1466 |
*r = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1467 |
else |
| 1468 |
return ERR_OUT_OF_RANGE; |
| 1469 |
return ERR_NONE; |
| 1470 |
} |
| 1471 |
|
| 1472 |
static int small_det_c(vartype_complexmatrix *m, phloat *dre, phloat *dim) { |
| 1473 |
if (m->rows == 1) { |
| 1474 |
*dre = m->array->data[0]; |
| 1475 |
*dim = m->array->data[1]; |
| 1476 |
return ERR_NONE; |
| 1477 |
} |
| 1478 |
phloat *a = m->array->data; |
| 1479 |
int s1, s2, s3, s4; |
| 1480 |
phloat r1re = dot_2d(a[0], a[1], a[6], a[7], false, &s1); |
| 1481 |
phloat r1im = dot_2d(a[0], a[1], a[7], a[6], true, &s2); |
| 1482 |
phloat r2re = dot_2d(a[2], a[3], a[4], a[5], false, &s3); |
| 1483 |
phloat r2im = dot_2d(a[2], a[3], a[5], a[4], true, &s4); |
| 1484 |
*dre = ssub(r1re, s1, r2re, s3); |
| 1485 |
*dim = ssub(r1im, s2, r2im, s4); |
| 1486 |
int inf = p_isinf(*dre); |
| 1487 |
if (inf != 0) |
| 1488 |
if (flags.f.range_error_ignore) |
| 1489 |
*dre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1490 |
else |
| 1491 |
return ERR_OUT_OF_RANGE; |
| 1492 |
inf = p_isinf(*dim); |
| 1493 |
if (inf != 0) |
| 1494 |
if (flags.f.range_error_ignore) |
| 1495 |
*dim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 1496 |
else |
| 1497 |
return ERR_OUT_OF_RANGE; |
| 1498 |
return ERR_NONE; |
| 1499 |
} |