eigenmath/rect.cpp

81 lines
946 B
C++
Raw Permalink Normal View History

2007-08-25 19:04:21 +02:00
/* Convert complex z to rectangular form
Input: push z
Output: Result on stack
*/
2005-10-12 01:25:25 +02:00
#include "stdafx.h"
#include "defs.h"
void
eval_rect(void)
{
push(cadr(p1));
eval();
rect();
}
void
rect(void)
{
save();
p1 = pop();
if (car(p1) == symbol(ADD)) {
push_integer(0);
p1 = cdr(p1);
while (iscons(p1)) {
push(car(p1));
rect();
add();
p1 = cdr(p1);
}
} else {
2005-10-15 22:41:33 +02:00
push(p1); // mag(z) * (cos(arg(z)) + i sin(arg(z)))
2005-10-12 01:25:25 +02:00
mag();
push(p1);
arg();
p1 = pop();
push(p1);
cosine();
push(imaginaryunit);
push(p1);
sine();
multiply();
add();
multiply();
}
restore();
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2005-10-09 23:24:06 +02:00
static char *s[] = {
"rect(a+i*b)",
"a+i*b",
"rect(exp(a+i*b))",
"i*exp(a)*sin(b)+exp(a)*cos(b)",
"rect(1+exp(i*pi/3))",
"3/2+1/2*i*3^(1/2)",
2006-08-09 20:42:01 +02:00
"z=(a+b*i)/(c+d*i)",
"",
"rect(z)-real(z)-i*imag(z)",
"0",
"z=quote(z)",
"",
2005-10-09 23:24:06 +02:00
};
void
test_rect(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif