1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
* Mathomatic constant factorizing routines.
*
* Copyright (C) 1987-2006 George Gesslein II.
*/
#include "includes.h"
static void try_factor();
static int fc_recurse();
/* The following data is used to factor integers: */
static double nn, vv;
static double sq[] = { /* Additive array that skips over multiples of 2, 3, 5, and 7. */
10, 2, 4, 2, 4, 6, 2, 6,
4, 2, 4, 6, 6, 2, 6, 4,
2, 6, 4, 6, 8, 4, 2, 4,
2, 4, 8, 6, 4, 6, 2, 4,
6, 2, 6, 6, 4, 2, 4, 6,
2, 6, 4, 2, 4, 2,10, 2
}; /* sum of all numbers = 210 = (2*3*5*7) */
/*
* Factor the integer in "start".
* Store the prime factors in the unique[] array.
*
* Return true if successful.
*/
int
factor_one(start)
double start;
{
int i;
double d;
uno = 0;
nn = start;
if (nn == 0.0) {
return false;
}
if (fabs(nn) >= MAX_K_INTEGER) {
/* too large to factor */
return false;
}
if (fmod(nn, 1.0) != 0.0) {
/* not an integer */
return false;
}
vv = 1.0 + sqrt(fabs(nn));
try_factor(2.0);
try_factor(3.0);
try_factor(5.0);
try_factor(7.0);
d = 1.0;
while (d <= vv) {
for (i = 0; i < ARR_CNT(sq); i++) {
d += sq[i];
try_factor(d);
}
}
if (nn != 1.0) {
try_factor(nn);
}
if (start != multiply_out_unique()) {
error("Error factoring integers.");
return false;
}
return true;
}
/*
* See if "arg" is one or more factors of "nn".
* If so, save it and remove it from "nn".
*/
static void
try_factor(arg)
double arg;
{
while (fmod(nn, arg) == 0.0) {
if (uno > 0 && unique[uno-1] == arg) {
ucnt[uno-1]++;
} else {
unique[uno] = arg;
ucnt[uno] = 1;
uno++;
}
nn /= arg;
vv = 1.0 + sqrt(fabs(nn));
if (nn <= 1.0 && nn >= -1.0)
break;
}
}
/*
* Convert unique[] back into an integer.
* Return the double integer.
*/
double
multiply_out_unique()
{
int i, j;
double d;
d = 1.0;
for (i = 0; i < uno; i++) {
for (j = 0; j < ucnt[i]; j++) {
d *= unique[i];
}
}
return d;
}
/*
* Display the prime factors in the unique[] array.
*/
display_unique()
{
int i;
fprintf(gfp, "%.0f = ", multiply_out_unique());
for (i = 0; i < uno;) {
fprintf(gfp, "%.0f", unique[i]);
if (ucnt[i] > 1) {
fprintf(gfp, "^%d", ucnt[i]);
}
i++;
if (i < uno) {
fprintf(gfp, " * ");
}
}
fprintf(gfp, "\n");
}
/*
* Factor integers in an expression.
*
* Return true if expression was modified.
*/
int
factor_int(equation, np)
token_type *equation;
int *np;
{
int i, j;
int xsize;
int level;
int modified = false;
for (i = 0; i < *np; i += 2) {
if (equation[i].kind == CONSTANT && factor_one(equation[i].token.constant) && uno > 0) {
if (uno == 1 && ucnt[0] <= 1)
continue; /* prime number */
level = equation[i].level;
if (uno > 1 && *np > 1)
level++;
xsize = -2;
for (j = 0; j < uno; j++) {
if (ucnt[j] > 1)
xsize += 4;
else
xsize += 2;
}
if (*np + xsize > n_tokens) {
error_huge();
}
for (j = 0; j < uno; j++) {
if (ucnt[j] > 1)
xsize = 4;
else
xsize = 2;
if (j == 0)
xsize -= 2;
if (xsize > 0) {
blt(&equation[i+xsize], &equation[i], (*np - i) * sizeof(token_type));
*np += xsize;
if (j > 0) {
i++;
equation[i].kind = OPERATOR;
equation[i].level = level;
equation[i].token.operatr = TIMES;
i++;
}
}
equation[i].kind = CONSTANT;
equation[i].level = level;
equation[i].token.constant = unique[j];
if (ucnt[j] > 1) {
equation[i].level = level + 1;
i++;
equation[i].kind = OPERATOR;
equation[i].level = level + 1;
equation[i].token.operatr = POWER;
i++;
equation[i].level = level + 1;
equation[i].kind = CONSTANT;
equation[i].token.constant = ucnt[j];
}
}
modified = true;
}
}
return modified;
}
/*
* Factor integers in an equation space.
*/
factor_int_sub(n)
int n; /* equation space number */
{
factor_int(lhs[n], &n_lhs[n]);
factor_int(rhs[n], &n_rhs[n]);
}
/*
* Factor constants in equation side.
*
* This routine is often necessary because the expression compare (se_compare())
* does not return a multiplier (except for +/-1.0).
* This routine is not used during polynomial operations.
* It is required for simplification of algebraic fractions, etc.
*
* If "level_code" is 0, all additive expressions are normalized
* by making at least one coefficient unity by factoring out
* the smallest constant.
*
* If "level_code" is 1, any level 1 additive expression is factored
* nicely for readability, while all deeper levels are normalized.
*
* If "level_code" is 2, nothing is normalized unless it increases
* readability.
*
* If "level_code" is 3, nothing is done.
*
* Return true if equation side was modified.
*/
int
factor_constants(equation, np, level_code)
token_type *equation;
int *np;
int level_code;
{
if (level_code > 2)
return false;
return fc_recurse(equation, np, 0, 1, level_code);
}
static int
fc_recurse(equation, np, loc, level, level_code)
token_type *equation;
int *np, loc, level;
int level_code;
{
int modified = false;
int i, j, k;
int op;
int neg_flag = true;
double d, minimum = 1.0;
int first = true;
int count = 0;
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
modified |= fc_recurse(equation, np, i, level + 1, level_code);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
continue;
}
i++;
}
if (modified)
return true;
for (i = loc;;) {
break_cont:
if (i >= *np || equation[i].level < level)
break;
if (equation[i].level == level) {
switch (equation[i].kind) {
case CONSTANT:
if (i == loc && equation[i].token.constant >= 0.0)
neg_flag = false;
d = fabs(equation[i].token.constant);
if (first) {
minimum = d;
first = false;
} else if (minimum > d) {
minimum = d;
}
break;
case OPERATOR:
count++;
switch (equation[i].token.operatr) {
case PLUS:
neg_flag = false;
case MINUS:
break;
default:
return modified;
}
break;
default:
if (i == loc)
neg_flag = false;
if (first) {
minimum = 1.0;
first = false;
} else if (minimum > 1.0) {
minimum = 1.0;
}
break;
}
} else {
op = 0;
for (j = i + 1; j < *np && equation[j].level > level; j += 2) {
if (equation[j].level == level + 1) {
op = equation[j].token.operatr;
}
}
if (op == TIMES || op == DIVIDE) {
for (k = i; k < j; k++) {
if (equation[k].level == (level + 1) && equation[k].kind == CONSTANT) {
if (i == loc && equation[k].token.constant >= 0.0)
neg_flag = false;
d = fabs(equation[k].token.constant);
if (first) {
minimum = d;
first = false;
} else if (d < minimum) {
minimum = d;
}
i = j;
goto break_cont;
}
}
}
if (i == loc)
neg_flag = false;
if (first) {
minimum = 1.0;
first = false;
} else if (1.0 < minimum)
minimum = 1.0;
i = j;
continue;
}
i++;
}
if (first || count == 0 || (!neg_flag && minimum == 1.0))
return modified;
if (!isfinite(minimum))
return modified;
if (level_code > 1 || (level_code && (level == 1))) {
for (i = loc;;) {
d = 1.0;
if (equation[i].kind == CONSTANT) {
if (equation[i].level == level || ((i + 1) < *np
&& equation[i].level == (level + 1)
&& equation[i+1].level == (level + 1)
&& (equation[i+1].token.operatr == TIMES
|| equation[i+1].token.operatr == DIVIDE))) {
d = equation[i].token.constant;
}
}
if ((minimum < 1.0 && fmod(d, 1.0) == 0.0) || (fmod(d / minimum, 1.0) != 0.0)) {
if (neg_flag) {
minimum = 1.0;
break;
}
return modified;
}
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
if (i >= *np || equation[i].level < level)
break;
i++;
}
}
if (neg_flag)
minimum = -minimum;
if (*np + ((count + 2) * 2) > n_tokens) {
error_huge();
}
for (i = loc; i < *np && equation[i].level >= level; i++) {
if (equation[i].kind != OPERATOR) {
for (j = i;;) {
equation[j].level++;
j++;
if (j >= *np || equation[j].level <= level)
break;
}
blt(&equation[j+2], &equation[j], (*np - j) * sizeof(token_type));
*np += 2;
equation[j].level = level + 1;
equation[j].kind = OPERATOR;
equation[j].token.operatr = DIVIDE;
j++;
equation[j].level = level + 1;
equation[j].kind = CONSTANT;
equation[j].token.constant = minimum;
i = j;
}
}
for (i = loc; i < *np && equation[i].level >= level; i++) {
equation[i].level++;
}
blt(&equation[i+2], &equation[i], (*np - i) * sizeof(token_type));
*np += 2;
equation[i].level = level;
equation[i].kind = OPERATOR;
equation[i].token.operatr = TIMES;
i++;
equation[i].level = level;
equation[i].kind = CONSTANT;
equation[i].token.constant = minimum;
return true;
}
|