Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / coeff.cpp
bloba104efb9da6a294306ae617470b8fcd4217fcc7b
1 // get the coefficient of x^n in polynomial p(x)
3 #include "stdafx.h"
4 #include "defs.h"
6 #define P p1
7 #define X p2
8 #define N p3
10 void
11 eval_coeff(void)
13 push(cadr(p1)); // 1st arg, p
14 eval();
16 push(caddr(p1)); // 2nd arg, x
17 eval();
19 push(cadddr(p1)); // 3rd arg, n
20 eval();
22 N = pop();
23 X = pop();
24 P = pop();
26 if (N == symbol(NIL)) { // only 2 args?
27 N = X;
28 X = symbol(SYMBOL_X);
31 push(P); // divide p by x^n
32 push(X);
33 push(N);
34 power();
35 divide();
37 push(X); // keep the constant part
38 filter();
41 //-----------------------------------------------------------------------------
43 // Put polynomial coefficients on the stack
45 // Input: tos-2 p(x)
47 // tos-1 x
49 // Output: Returns number of coefficients on stack
51 // tos-n Coefficient of x^0
53 // tos-1 Coefficient of x^(n-1)
55 //-----------------------------------------------------------------------------
57 int
58 coeff(void)
60 int h, n;
62 save();
64 p2 = pop();
65 p1 = pop();
67 h = tos;
69 for (;;) {
71 push(p1);
72 push(p2);
73 push(zero);
74 subst();
75 eval();
77 p3 = pop();
78 push(p3);
80 push(p1);
81 push(p3);
82 subtract();
84 p1 = pop();
86 if (equal(p1, zero)) {
87 n = tos - h;
88 restore();
89 return n;
92 push(p1);
93 push(p2);
94 divide();
95 p1 = pop();