eigenmath/circexp.cpp

178 lines
2.4 KiB
C++
Raw Permalink Normal View History

2006-09-20 18:09:25 +02:00
// Change circular functions to exponentials
#include "stdafx.h"
#include "defs.h"
void
eval_circexp(void)
{
push(cadr(p1));
eval();
2008-07-04 17:25:29 +02:00
2006-09-22 02:34:28 +02:00
circexp();
2006-09-20 18:09:25 +02:00
2008-07-04 17:25:29 +02:00
// normalize
eval();
2006-09-22 02:34:28 +02:00
}
2006-09-20 18:09:25 +02:00
void
2008-07-04 17:25:29 +02:00
circexp(void)
2006-09-20 18:09:25 +02:00
{
int i, h;
2008-07-04 17:25:29 +02:00
save();
2006-09-22 02:34:28 +02:00
p1 = pop();
2006-09-20 18:09:25 +02:00
if (car(p1) == symbol(COS)) {
push(cadr(p1));
expcos();
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (car(p1) == symbol(SIN)) {
push(cadr(p1));
expsin();
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (car(p1) == symbol(TAN)) {
p1 = cadr(p1);
push(imaginaryunit);
push(p1);
multiply();
exponential();
p2 = pop();
push(imaginaryunit);
push(p1);
multiply();
negate();
exponential();
p3 = pop();
push(p3);
push(p2);
subtract();
push(imaginaryunit);
multiply();
push(p2);
push(p3);
add();
divide();
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (car(p1) == symbol(COSH)) {
p1 = cadr(p1);
push(p1);
exponential();
push(p1);
negate();
exponential();
add();
push_rational(1, 2);
multiply();
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (car(p1) == symbol(SINH)) {
p1 = cadr(p1);
push(p1);
exponential();
push(p1);
negate();
exponential();
subtract();
push_rational(1, 2);
multiply();
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (car(p1) == symbol(TANH)) {
p1 = cadr(p1);
push(p1);
push_integer(2);
multiply();
exponential();
p1 = pop();
push(p1);
push_integer(1);
subtract();
push(p1);
push_integer(1);
add();
divide();
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (iscons(p1)) {
h = tos;
while (iscons(p1)) {
push(car(p1));
2006-09-22 02:34:28 +02:00
circexp();
2006-09-20 18:09:25 +02:00
p1 = cdr(p1);
}
list(tos - h);
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
if (p1->k == TENSOR) {
push(p1);
copy_tensor();
p1 = pop();
for (i = 0; i < p1->u.tensor->nelem; i++) {
push(p1->u.tensor->elem[i]);
2006-09-22 02:34:28 +02:00
circexp();
2006-09-20 18:09:25 +02:00
p1->u.tensor->elem[i] = pop();
}
push(p1);
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
return;
}
push(p1);
2008-07-04 17:25:29 +02:00
restore();
2006-09-20 18:09:25 +02:00
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2006-09-20 18:09:25 +02:00
static char *s[] = {
"circexp(cos(x))",
"1/2*exp(-i*x)+1/2*exp(i*x)",
"circexp(sin(x))",
"1/2*i*exp(-i*x)-1/2*i*exp(i*x)",
"circexp(tan(x))",
"i*exp(-i*x)/(exp(-i*x)+exp(i*x))-i*exp(i*x)/(exp(-i*x)+exp(i*x))",
"circexp(cosh(x))",
"1/2*exp(x)+1/2*exp(-x)",
"circexp(sinh(x))",
"1/2*exp(x)-1/2*exp(-x)",
"circexp(tanh(x))",
"-1/(1+exp(2*x))+exp(2*x)/(1+exp(2*x))",
2006-09-22 02:34:28 +02:00
"circexp((cos(x),sin(x)))",
"(1/2*exp(-i*x)+1/2*exp(i*x),1/2*i*exp(-i*x)-1/2*i*exp(i*x))",
"circexp(cos(x)*sin(x))-expcos(x)*expsin(x)",
"0",
2006-09-20 18:09:25 +02:00
};
void
test_circexp(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif