eigenmath/log.cpp

152 lines
1.6 KiB
C++
Raw Permalink Normal View History

2005-06-30 03:25:26 +02:00
// natural logarithm
2004-07-16 02:36:18 +02:00
#include "stdafx.h"
#include "defs.h"
void
eval_log(void)
{
push(cadr(p1));
eval();
logarithm();
}
void
logarithm(void)
{
save();
2006-01-04 02:47:50 +01:00
yylog();
2004-07-16 02:36:18 +02:00
restore();
}
2006-01-04 02:47:50 +01:00
void
yylog(void)
2004-07-16 02:36:18 +02:00
{
double d;
p1 = pop();
2006-04-23 15:19:39 +02:00
if (p1 == symbol(E)) {
push_integer(1);
2004-07-16 02:36:18 +02:00
return;
}
2006-04-23 15:19:39 +02:00
if (equaln(p1, 1)) {
2004-07-16 02:36:18 +02:00
push_integer(0);
return;
}
2006-04-23 15:19:39 +02:00
if (isnegativenumber(p1)) {
push(p1);
negate();
logarithm();
push(imaginaryunit);
push_symbol(PI);
multiply();
add();
return;
}
if (isdouble(p1)) {
d = log(p1->u.d);
push_double(d);
2004-07-16 02:36:18 +02:00
return;
}
2006-04-21 00:21:07 +02:00
// rational number and not an integer?
if (isfraction(p1)) {
push(p1);
numerator();
logarithm();
push(p1);
denominator();
logarithm();
subtract();
return;
}
// log(a ^ b) --> b log(a)
2004-07-16 02:36:18 +02:00
if (car(p1) == symbol(POWER)) {
push(caddr(p1));
push(cadr(p1));
logarithm();
multiply();
return;
}
2006-04-21 00:21:07 +02:00
// log(a * b) --> log(a) + log(b)
if (car(p1) == symbol(MULTIPLY)) {
push_integer(0);
p1 = cdr(p1);
while (iscons(p1)) {
push(car(p1));
logarithm();
add();
p1 = cdr(p1);
}
return;
}
2004-07-16 02:36:18 +02:00
push_symbol(LOG);
push(p1);
list(2);
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2004-07-16 02:36:18 +02:00
static char *s[] = {
"log(1)",
"0",
"log(exp(1))",
"1",
"log(exp(x))",
"x",
"exp(log(x))",
"x",
"log(x^2)",
"2*log(x)",
"log(1/x)",
"-log(x)",
"log(a^b)",
"b*log(a)",
"log(2)",
"log(2)",
"log(2.0)",
"0.693147",
"float(log(2))",
"0.693147",
2006-04-21 00:21:07 +02:00
"log(a*b)",
"log(a)+log(b)",
"log(1/3)+log(3)",
"0",
2006-04-23 15:19:39 +02:00
"log(-1)",
"i*pi",
"log(-1.0)",
"i*pi",
2004-07-16 02:36:18 +02:00
};
void
test_log(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif