eigenmath/heaviside.cpp

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

2005-07-30 21:37:29 +02:00
//-----------------------------------------------------------------------------
//
// Author : philippe.billet@noos.fr
//
// heaviside unit function
//
// A number of things are done to produce a canonical form:
// heaviside(-x)=1-heaviside(x)
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "defs.h"
2006-09-20 18:09:25 +02:00
static int exp_flag;
2005-07-30 21:37:29 +02:00
void
eval_heaviside(void)
{
push(cadr(p1));
eval();
heaviside();
}
void
heaviside(void)
{
save();
2005-10-27 19:39:15 +02:00
yyheaviside();
2005-07-30 21:37:29 +02:00
restore();
}
#define X p1
void
2005-10-27 19:39:15 +02:00
yyheaviside(void)
2005-07-30 21:37:29 +02:00
{
X = pop();
2005-08-06 22:57:37 +02:00
if (isdouble(p1)) {
2005-07-30 21:37:29 +02:00
if (p1->u.d > 0)
{push_integer(1);
return;}
else
if (p1->u.d == 0)
{push_rational(1, 2);
return;}
else
{push_integer(0);
return;}
}
2005-08-06 22:57:37 +02:00
if (isrational(p1)) {
2005-07-30 21:37:29 +02:00
if (MSIGN(mmul(p1->u.q.a,p1->u.q.b)) == -1)
{push_integer(0);
return;}
else
if (MZERO(mmul(p1->u.q.a,p1->u.q.b)))
{push_rational(1, 2);
return;}
else
{push_integer(1);
return;}
}
2006-05-06 01:27:26 +02:00
if (exp_flag) {
2005-07-30 21:37:29 +02:00
push_rational(1, 2);
push(p1);
sgn();
push_integer(1);
add();
multiply();
return;
}
if (car(p1) == symbol(POWER)) {
push_symbol(HEAVISIDE);
push(cadr(p1));
list(2);
return;
}
if (isnegativeterm(X)) {
push_symbol(HEAVISIDE);
push(X);
negate();
list(2);
push_integer(-1);
multiply();
push_integer(1);
add();
return;
}
push_symbol(HEAVISIDE);
push(X);
list(2);
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2005-07-30 21:37:29 +02:00
static char *s[] = {
"heaviside(-1)",
"0",
"heaviside(0)",
"1/2",
"heaviside(1)",
"1",
};
void
test_heaviside(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif