2008-05-25 17:38:07 +02:00
|
|
|
/* Taylor expansion of a function
|
2006-10-06 20:28:26 +02:00
|
|
|
|
2008-05-25 17:29:42 +02:00
|
|
|
push(F)
|
|
|
|
push(X)
|
|
|
|
push(N)
|
|
|
|
push(A)
|
|
|
|
taylor()
|
2006-10-06 20:28:26 +02:00
|
|
|
*/
|
2004-03-03 21:24:06 +01:00
|
|
|
|
2005-08-21 03:19:56 +02:00
|
|
|
#include "stdafx.h"
|
2004-03-03 21:24:06 +01:00
|
|
|
#include "defs.h"
|
|
|
|
|
2005-08-21 03:19:56 +02:00
|
|
|
void
|
|
|
|
eval_taylor(void)
|
|
|
|
{
|
|
|
|
// 1st arg
|
|
|
|
|
|
|
|
p1 = cdr(p1);
|
|
|
|
push(car(p1));
|
|
|
|
eval();
|
|
|
|
|
|
|
|
// 2nd arg
|
|
|
|
|
|
|
|
p1 = cdr(p1);
|
|
|
|
push(car(p1));
|
|
|
|
eval();
|
|
|
|
p2 = pop();
|
2006-01-16 20:37:31 +01:00
|
|
|
if (p2 == symbol(NIL))
|
2008-05-25 17:31:13 +02:00
|
|
|
guess();
|
2005-08-21 03:19:56 +02:00
|
|
|
else
|
|
|
|
push(p2);
|
|
|
|
|
|
|
|
// 3rd arg
|
|
|
|
|
|
|
|
p1 = cdr(p1);
|
|
|
|
push(car(p1));
|
|
|
|
eval();
|
|
|
|
p2 = pop();
|
2006-01-16 20:37:31 +01:00
|
|
|
if (p2 == symbol(NIL))
|
2005-08-21 03:19:56 +02:00
|
|
|
push_integer(24); // default number of terms
|
|
|
|
else
|
|
|
|
push(p2);
|
|
|
|
|
|
|
|
// 4th arg
|
|
|
|
|
|
|
|
p1 = cdr(p1);
|
|
|
|
push(car(p1));
|
|
|
|
eval();
|
|
|
|
p2 = pop();
|
2006-01-16 20:37:31 +01:00
|
|
|
if (p2 == symbol(NIL))
|
2005-08-21 03:19:56 +02:00
|
|
|
push_integer(0); // default expansion point
|
|
|
|
else
|
|
|
|
push(p2);
|
|
|
|
|
|
|
|
taylor();
|
|
|
|
}
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
#define F p1
|
|
|
|
#define X p2
|
|
|
|
#define N p3
|
|
|
|
#define A p4
|
|
|
|
#define C p5
|
|
|
|
|
2006-10-06 20:28:26 +02:00
|
|
|
void
|
2008-05-25 17:29:42 +02:00
|
|
|
taylor(void)
|
2004-03-03 21:24:06 +01:00
|
|
|
{
|
|
|
|
int i, k;
|
|
|
|
|
2008-05-25 17:29:42 +02:00
|
|
|
save();
|
|
|
|
|
2004-03-03 21:24:06 +01:00
|
|
|
A = pop();
|
|
|
|
N = pop();
|
|
|
|
X = pop();
|
|
|
|
F = pop();
|
|
|
|
|
|
|
|
push(N);
|
|
|
|
k = pop_integer();
|
|
|
|
if (k == (int) 0x80000000) {
|
|
|
|
push_symbol(TAYLOR);
|
|
|
|
push(F);
|
|
|
|
push(X);
|
|
|
|
push(N);
|
|
|
|
push(A);
|
|
|
|
list(5);
|
2008-05-25 17:29:42 +02:00
|
|
|
restore();
|
2004-03-03 21:24:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
push(F); // f(a)
|
|
|
|
push(X);
|
|
|
|
push(A);
|
2006-10-06 20:28:26 +02:00
|
|
|
subst();
|
|
|
|
eval();
|
2004-03-03 21:24:06 +01:00
|
|
|
|
2008-05-25 17:29:42 +02:00
|
|
|
push_integer(1);
|
|
|
|
C = pop();
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
for (i = 1; i <= k; i++) {
|
|
|
|
|
|
|
|
push(F); // f = f'
|
|
|
|
push(X);
|
|
|
|
derivative();
|
|
|
|
F = pop();
|
|
|
|
|
|
|
|
if (iszero(F))
|
|
|
|
break;
|
|
|
|
|
|
|
|
push(C); // c = c * (x - a)
|
|
|
|
push(X);
|
|
|
|
push(A);
|
|
|
|
subtract();
|
|
|
|
multiply();
|
|
|
|
C = pop();
|
|
|
|
|
|
|
|
push(F); // f(a)
|
|
|
|
push(X);
|
|
|
|
push(A);
|
2006-10-06 20:28:26 +02:00
|
|
|
subst();
|
|
|
|
eval();
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
push(C);
|
|
|
|
multiply();
|
|
|
|
push_integer(i);
|
|
|
|
factorial();
|
|
|
|
divide();
|
|
|
|
|
|
|
|
add();
|
|
|
|
}
|
2008-05-25 17:29:42 +02:00
|
|
|
|
|
|
|
restore();
|
2004-03-03 21:24:06 +01:00
|
|
|
}
|
|
|
|
|
2007-05-08 16:57:30 +02:00
|
|
|
#if SELFTEST
|
|
|
|
|
2004-03-03 21:24:06 +01:00
|
|
|
static char *s[] = {
|
|
|
|
|
2006-02-10 17:21:52 +01:00
|
|
|
"taylor(1/(5+4*cos(x)),x,6,0)-(1/9+2/81*x^2+5/1458*x^4+49/131220*x^6)",
|
|
|
|
"0",
|
2004-03-03 21:24:06 +01:00
|
|
|
|
2006-02-10 17:21:52 +01:00
|
|
|
"taylor(1/(5+4*cos(x)),x,6)-(1/9+2/81*x^2+5/1458*x^4+49/131220*x^6)",
|
|
|
|
"0",
|
2004-03-03 21:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
test_taylor(void)
|
|
|
|
{
|
|
|
|
test(__FILE__, s, sizeof s / sizeof (char *));
|
|
|
|
}
|
2007-05-08 16:57:30 +02:00
|
|
|
|
|
|
|
#endif
|