1 | ### deg =====================================================================
|
2 |
|
3 | Tags
|
4 | ----
|
5 | scripting, JS, internal, treenode, general concept
|
6 |
|
7 | Parameters
|
8 | ----------
|
9 | p,x
|
10 |
|
11 | General description
|
12 | -------------------
|
13 | Returns the degree of polynomial p(x).
|
14 |
|
15 | ###
|
16 |
|
17 |
|
18 | Eval_degree = ->
|
19 | push(cadr(p1))
|
20 | Eval()
|
21 | push(caddr(p1))
|
22 | Eval()
|
23 | p1 = pop()
|
24 | if (p1 == symbol(NIL))
|
25 | guess()
|
26 | else
|
27 | push(p1)
|
28 | degree()
|
29 |
|
30 | #-----------------------------------------------------------------------------
|
31 | #
|
32 | # Find the degree of a polynomial
|
33 | #
|
34 | # Input: tos-2 p(x)
|
35 | #
|
36 | # tos-1 x
|
37 | #
|
38 | # Output: Result on stack
|
39 | #
|
40 | # Note: Finds the largest numerical power of x. Does not check for
|
41 | # weirdness in p(x).
|
42 | #
|
43 | #-----------------------------------------------------------------------------
|
44 |
|
45 | #define POLY p1
|
46 | #define X p2
|
47 | #define DEGREE p3
|
48 |
|
49 | degree = ->
|
50 | save()
|
51 | p2 = pop()
|
52 | p1 = pop()
|
53 | p3 = zero
|
54 | yydegree(p1)
|
55 | push(p3)
|
56 | restore()
|
57 |
|
58 | yydegree = (p) ->
|
59 | if (equal(p, p2))
|
60 | if (iszero(p3))
|
61 | p3 = one
|
62 | else if (car(p) == symbol(POWER))
|
63 | if (equal(cadr(p), p2) && isnum(caddr(p)) && lessp(p3, caddr(p)))
|
64 | p3 = caddr(p)
|
65 | else if (iscons(p))
|
66 | p = cdr(p)
|
67 | while (iscons(p))
|
68 | yydegree(car(p))
|
69 | p = cdr(p)
|