eigenmath/quotient.cpp

127 lines
1.7 KiB
C++
Raw Permalink Normal View History

2006-02-07 21:35:04 +01:00
// Divide polynomials
2004-03-03 21:24:06 +01:00
#include "stdafx.h"
2006-02-07 21:35:04 +01:00
#include "defs.h"
void
2006-02-10 01:55:47 +01:00
eval_quotient(void)
2006-02-07 21:35:04 +01:00
{
push(cadr(p1)); // 1st arg, p(x)
eval();
push(caddr(p1)); // 2nd arg, q(x)
eval();
push(cadddr(p1)); // 3rd arg, x
eval();
2006-02-08 02:22:55 +01:00
p1 = pop(); // default x
2006-02-07 21:35:04 +01:00
if (p1 == symbol(NIL))
2006-02-08 02:22:55 +01:00
p1 = symbol(SYMBOL_X);
push(p1);
2006-02-07 21:35:04 +01:00
divpoly();
}
2004-03-03 21:24:06 +01:00
//-----------------------------------------------------------------------------
//
// Divide polynomials
//
// Input: tos-3 Dividend
//
// tos-2 Divisor
//
// tos-1 x
//
2006-02-07 21:35:04 +01:00
// Output: tos-1 Quotient
2004-03-03 21:24:06 +01:00
//
//-----------------------------------------------------------------------------
#define DIVIDEND p1
#define DIVISOR p2
#define X p3
#define Q p4
2006-02-07 21:35:04 +01:00
#define QUOTIENT p5
2004-03-03 21:24:06 +01:00
void
divpoly(void)
{
int h, i, m, n, x;
U **dividend, **divisor;
save();
X = pop();
DIVISOR = pop();
DIVIDEND = pop();
h = tos;
dividend = stack + tos;
push(DIVIDEND);
push(X);
m = coeff() - 1; // m is dividend's power
divisor = stack + tos;
push(DIVISOR);
push(X);
n = coeff() - 1; // n is divisor's power
x = m - n;
2006-02-07 21:35:04 +01:00
push_integer(0);
QUOTIENT = pop();
2004-03-03 21:24:06 +01:00
while (x >= 0) {
push(dividend[m]);
push(divisor[n]);
divide();
Q = pop();
for (i = 0; i <= n; i++) {
push(dividend[x + i]);
push(divisor[i]);
push(Q);
multiply();
subtract();
dividend[x + i] = pop();
}
2006-02-07 21:35:04 +01:00
push(QUOTIENT);
2004-03-03 21:24:06 +01:00
push(Q);
push(X);
push_integer(x);
power();
multiply();
add();
2006-02-07 21:35:04 +01:00
QUOTIENT = pop();
2004-03-03 21:24:06 +01:00
m--;
x--;
}
2006-02-07 21:35:04 +01:00
tos = h;
push(QUOTIENT);
2004-03-03 21:24:06 +01:00
restore();
}
2006-02-07 21:35:04 +01:00
static char *s[] = {
2006-02-10 17:21:52 +01:00
"quotient(x^2+1,x+1)-x+1",
"0",
2006-02-08 02:22:55 +01:00
2006-02-10 17:21:52 +01:00
"quotient(a*x^2+b*x+c,d*x+e)-(-a*e/(d^2)+a*x/d+b/d)",
"0",
2006-02-07 21:35:04 +01:00
};
void
2006-02-10 01:55:47 +01:00
test_quotient(void)
2006-02-07 21:35:04 +01:00
{
test(__FILE__, s, sizeof s / sizeof (char *));
}