eigenmath/laplace.cpp

111 lines
1.3 KiB
C++
Raw Permalink Normal View History

2008-06-28 01:09:18 +02:00
// Laplace transform
#include "stdafx.h"
#include "defs.h"
void
eval_laplace(void)
{
push(cadr(p1));
eval();
push(symbol(SYMBOL_T));
laplace();
}
#define F p3
#define T p4
#define A p5
void
laplace(void)
{
int h;
save();
T = pop();
F = pop();
// L[f + g] = L[f] + L[g]
if (car(F) == symbol(ADD)) {
p1 = cdr(F);
h = tos;
while (iscons(p1)) {
push(car(p1));
push(T);
laplace();
p1 = cdr(p1);
}
add_all(tos - h);
restore();
return;
}
// L[Af] = A L[f]
if (car(F) == symbol(MULTIPLY)) {
push(F);
push(T);
partition();
F = pop();
A = pop();
2008-06-28 01:55:01 +02:00
laplace_main();
push(A);
multiply();
} else
laplace_main();
restore();
}
2008-06-28 01:09:18 +02:00
2008-06-28 01:55:01 +02:00
void
laplace_main(void)
{
int n;
2008-06-28 01:09:18 +02:00
2008-06-28 01:55:01 +02:00
// L[t] = 1 / s^2
2008-06-28 01:09:18 +02:00
2008-06-28 01:55:01 +02:00
if (F == symbol(SYMBOL_T)) {
push_symbol(SYMBOL_S);
push_integer(-2);
power();
return;
}
// L[t^n] = n! / s^(n+1)
if (car(F) == symbol(POWER) && cadr(F) == T) {
push(caddr(F));
n = pop_integer();
if (n > 0) {
push_integer(n);
factorial();
push_symbol(SYMBOL_S);
push_integer(n + 1);
power();
divide();
return;
}
}
stop("laplace: cannot solve");
2008-06-28 01:09:18 +02:00
}
#if SELFTEST
static char *s[] = {
2008-06-28 01:55:01 +02:00
// float ok?
"laplace(3t^2.0)",
"6/(s^3)",
2008-06-28 01:09:18 +02:00
};
void
test_laplace(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
#endif