Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / quotient.cpp
blob7713f3c502733a0bd1ee954b0b4a46a3846e5266
1 // Divide polynomials
3 #include "stdafx.h"
4 #include "defs.h"
6 void
7 eval_quotient(void)
9 push(cadr(p1)); // 1st arg, p(x)
10 eval();
12 push(caddr(p1)); // 2nd arg, q(x)
13 eval();
15 push(cadddr(p1)); // 3rd arg, x
16 eval();
18 p1 = pop(); // default x
19 if (p1 == symbol(NIL))
20 p1 = symbol(SYMBOL_X);
21 push(p1);
23 divpoly();
26 //-----------------------------------------------------------------------------
28 // Divide polynomials
30 // Input: tos-3 Dividend
32 // tos-2 Divisor
34 // tos-1 x
36 // Output: tos-1 Quotient
38 //-----------------------------------------------------------------------------
40 #define DIVIDEND p1
41 #define DIVISOR p2
42 #define X p3
43 #define Q p4
44 #define QUOTIENT p5
46 void
47 divpoly(void)
49 int h, i, m, n, x;
50 U **dividend, **divisor;
52 save();
54 X = pop();
55 DIVISOR = pop();
56 DIVIDEND = pop();
58 h = tos;
60 dividend = stack + tos;
62 push(DIVIDEND);
63 push(X);
64 m = coeff() - 1; // m is dividend's power
66 divisor = stack + tos;
68 push(DIVISOR);
69 push(X);
70 n = coeff() - 1; // n is divisor's power
72 x = m - n;
74 push_integer(0);
75 QUOTIENT = pop();
77 while (x >= 0) {
79 push(dividend[m]);
80 push(divisor[n]);
81 divide();
82 Q = pop();
84 for (i = 0; i <= n; i++) {
85 push(dividend[x + i]);
86 push(divisor[i]);
87 push(Q);
88 multiply();
89 subtract();
90 dividend[x + i] = pop();
93 push(QUOTIENT);
94 push(Q);
95 push(X);
96 push_integer(x);
97 power();
98 multiply();
99 add();
100 QUOTIENT = pop();
102 m--;
103 x--;
106 tos = h;
108 push(QUOTIENT);
110 restore();
113 #if SELFTEST
115 static char *s[] = {
117 "quotient(x^2+1,x+1)-x+1",
118 "0",
120 "quotient(a*x^2+b*x+c,d*x+e)-(-a*e/(d^2)+a*x/d+b/d)",
121 "0",
124 void
125 test_quotient(void)
127 test(__FILE__, s, sizeof s / sizeof (char *));
130 #endif