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
|
/*
* Floating point complex number routines specifically for Mathomatic.
*
* Copyright (C) 1987-2006 George Gesslein II.
*/
#include "includes.h"
/*
* Convert doubles x and y from rectangular coordinates to polar coordinates.
*
* The amplitude is stored in *radiusp and the angle in radians is stored in *thetap.
*/
rect_to_polar(x, y, radiusp, thetap)
double x, y, *radiusp, *thetap;
{
*radiusp = sqrt(x * x + y * y);
*thetap = atan2(y, x);
}
#if !LIBRARY
/*
* The roots command.
*/
int
roots_cmd(cp)
char *cp;
{
#define MAX_ROOT 1000.0
complexs c, c2, check;
double d, k;
double root;
double radius, theta;
double radius_root = 0.0;
char buf[MAX_CMD_LEN];
if (*cp == '\0') {
my_strlcpy(prompt_str, _("Enter root (positive integer): "), sizeof(prompt_str));
if ((cp = get_string(buf, sizeof(buf))) == NULL)
return false;
}
root = strtod(cp, &cp);
if ((*cp && !isspace(*cp)) || root < 0.0 || root > MAX_ROOT || fmod(root, 1.0) != 0.0) {
printf(_("Root must be a positive integer less than or equal to %.0f.\n"), MAX_ROOT);
return false;
}
cp = skip_space(cp);
if (*cp == '\0') {
my_strlcpy(prompt_str, _("Enter real part (X): "), sizeof(prompt_str));
if ((cp = get_string(buf, sizeof(buf))) == NULL)
return false;
}
c.re = strtod(cp, &cp);
if (*cp && !isspace(*cp)) {
printf(_("Invalid number.\n"));
return false;
}
cp = skip_space(cp);
if (*cp == '\0') {
my_strlcpy(prompt_str, _("Enter imaginary part (Y): "), sizeof(prompt_str));
if ((cp = get_string(buf, sizeof(buf))) == NULL)
return false;
}
c.im = strtod(cp, &cp);
if (*cp) {
printf(_("Invalid number.\n"));
return false;
}
if (c.re == 0.0 && c.im == 0.0) {
return false;
}
/* convert to polar coordinates */
errno = 0;
rect_to_polar(c.re, c.im, &radius, &theta);
if (root) {
radius_root = pow(radius, 1.0 / root);
}
check_err();
fprintf(gfp, _("\nThe polar coordinates are:\n%.12g amplitude and %.12g radians (%.12g degrees).\n\n"),
radius, theta, theta * 180.0 / M_PI);
if (root) {
if (c.im == 0.0) {
fprintf(gfp, _("The %.12g roots of %.12g^(1/%.12g) are:\n\n"), root, c.re, root);
} else {
fprintf(gfp, _("The %.12g roots of (%.12g%+.12g*i#)^(1/%.12g) are:\n\n"), root, c.re, c.im, root);
}
for (k = 0.0; k < root; k += 1.0) {
/* add constants to theta and convert back to rectangular coordinates */
c2.re = radius_root * cos((theta + 2.0 * k * M_PI) / root);
c2.im = radius_root * sin((theta + 2.0 * k * M_PI) / root);
complex_fixup(&c2);
if (c2.im == 0.0) {
fprintf(gfp, "%.12g\n", c2.re);
} else {
fprintf(gfp, "%.12g %+.12g*i#\n", c2.re, c2.im);
}
check = c2;
for (d = 1.0; d < root; d += 1.0) {
check = complex_mult(check, c2);
}
complex_fixup(&check);
if (check.im == 0.0) {
printf(_("Inverse Check: %.12g\n\n"), check.re);
} else {
printf(_("Inverse Check: %.12g %+.12g*i#\n\n"), check.re, check.im);
}
}
}
return true;
}
#endif
/*
* Approximate roots of complex numbers:
* (complex^real) and (real^complex) and (complex^complex).
*
* Returns true if expression was modified.
*/
int
complex_root_simp(equation, np)
token_type *equation; /* equation side pointer */
int *np; /* pointer to length of equation side */
{
int i, j;
int level;
int len;
complexs c, p;
int modified = false;
start_over:
for (i = 1; i < *np; i += 2) {
if (equation[i].token.operatr != POWER)
continue;
level = equation[i].level;
for (j = i + 2; j < *np && equation[j].level >= level; j += 2)
;
len = j - (i + 1);
if (!parse_complex(&equation[i+1], len, &p))
continue;
for (j = i - 1; j >= 0 && equation[j].level >= level; j--)
;
j++;
if (!parse_complex(&equation[j], i - j, &c))
continue;
if (c.im == 0.0 && p.im == 0.0)
continue;
i += len + 1;
c = complex_pow(c, p);
if (*np + 5 - (i - j) > n_tokens) {
error_huge();
}
blt(&equation[j+5], &equation[i], (*np - i) * sizeof(token_type));
*np += 5 - (i - j);
equation[j].level = level;
equation[j].kind = CONSTANT;
equation[j].token.constant = c.re;
j++;
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = PLUS;
j++;
level++;
equation[j].level = level;
equation[j].kind = CONSTANT;
equation[j].token.constant = c.im;
j++;
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = TIMES;
j++;
equation[j].level = level;
equation[j].kind = VARIABLE;
equation[j].token.variable = IMAGINARY;
modified = true;
goto start_over;
}
return modified;
}
/*
* Get a constant, if the passed expression is a constant.
*
* Return true if successful, with number in "*dp".
*/
int
get_constant(equation, n, dp)
token_type *equation; /* expression pointer */
int n; /* length of expression */
double *dp; /* pointer to returned double */
{
if (n != 1)
return false;
switch (equation[0].kind) {
case CONSTANT:
*dp = equation[0].token.constant;
return true;
case VARIABLE:
if (var_is_const(equation[0].token.variable, dp)) {
return true;
}
}
return false;
}
/*
* Parse a complex number expression.
*
* If successful return true with complex number in "*cp".
*/
int
parse_complex(equation, n, cp)
token_type *equation; /* expression pointer */
int n; /* length of expression */
complexs *cp; /* pointer to returned complex number */
{
int j;
int imag_cnt = 0, plus_cnt = 0, times_cnt = 0;
complexs c;
int level2;
double junk;
if (get_constant(equation, n, &c.re)) {
c.im = 0.0;
*cp = c;
return true;
}
c.re = 0.0;
c.im = 1.0;
for (j = n - 1; j >= 0; j--) {
switch (equation[j].kind) {
case CONSTANT:
break;
case VARIABLE:
if (var_is_const(equation[j].token.variable, &junk))
break;
if (equation[j].token.variable != IMAGINARY)
return false;
imag_cnt++;
break;
case OPERATOR:
level2 = equation[j].level;
switch (equation[j].token.operatr) {
case TIMES:
if (++times_cnt > 1)
return false;
if (equation[j-1].level != level2 || equation[j+1].level != level2)
return false;
if (equation[j-1].kind == VARIABLE && equation[j-1].token.variable == IMAGINARY) {
if (!get_constant(&equation[j+1], 1, &c.im))
return false;
continue;
}
if (equation[j+1].kind == VARIABLE && equation[j+1].token.variable == IMAGINARY) {
if (!get_constant(&equation[j-1], 1, &c.im))
return false;
continue;
}
return false;
case DIVIDE:
if (++times_cnt > 1)
return false;
if (equation[j-1].level != level2 || equation[j+1].level != level2)
return false;
if (equation[j-1].kind == VARIABLE && equation[j-1].token.variable == IMAGINARY) {
if (!get_constant(&equation[j+1], 1, &c.im))
return false;
c.im = 1.0 / c.im;
continue;
}
return false;
case MINUS:
if (imag_cnt) {
c.im = -c.im;
}
case PLUS:
if (++plus_cnt > 1)
return false;
if (equation[j-1].level == level2 && get_constant(&equation[j-1], 1, &c.re)) {
continue;
}
if (equation[j+1].level == level2 && get_constant(&equation[j+1], 1, &c.re)) {
if (equation[j].token.operatr == MINUS)
c.re = -c.re;
continue;
}
}
default:
return false;
}
}
if (imag_cnt != 1)
return false;
*cp = c;
return true;
}
|