2012-05-30

SICP Exercise 2.90: Supporting Dense and Sparse Polynomials - Part 1

Suppose we want to have a polynomial system that is efficient for both sparse and dense polynomials. One way to do this is to allow both kinds of term-list representations in our system. The situation is analogous to the complex-number example of section 2.4, where we allowed both rectangular and polar representations. To do this we must distinguish different types of term lists and make the operations on term lists generic. Redesign the polynomial system to implement this generalization. This is a major effort, not a local change.
They're not kidding - it's not a local change. I've split the solution into two posts. In this post I'll discuss the changes required, and only give a few snippets of code. In the second post I'll present the code itself and talk about various interesting bits of it.

Using the Analogy

We're told that the situation is analogous to the complex-number example, so let's first examine the complex-number example to see how it handles its different representations. Then we can think about how we can apply that approach to this exercise.

In the complex-number example the rectangular and polar representations are extracted to their own packages, named rectangular and polar respectively. These two packages expose identical sets of primitive operations: constructors and accessors for the components of the complex number. They do not expose, or implement in any way, any complex operations such as arithmetic operations. Nor are data objects of these types directly constructed. Instead an overarching complex type is added to support these.

This complex package exposes two different construction operations that allow for the creation of complex number representations appropriate to the semantics of the passed parameters. I.e. rectangular representations are used for {real, complex} tuples and polar representations for {magnitude, angle} tuples. These delegate much of the construction of the representation to the rectangular and polar packages, simply tagging the results as 'complex types. It also provides representation-agnostic implementations of arithmetic operations that utilize the set of primitive operations exposed by the two representations and that select the best representation for the result based upon the operation.

The authors show how this fits into the type system in figure 2.23 at the start of section 2.5. Here it is again:
Programs that use numbers
add sub mul div
Generic arithmetic package
add-rat sub-rat
mul-rat div-rat
add-complex sub-complex
mul-complex div-complex
+ - * /
Rational arithmetic Complex arithmetic Ordinary arithmetic
Rectangular Polar

By analogy this suggest that we should identify a set of primitive operations that are common to both the sparse and dense polynomial representations that will allow for the implementation of the higher-level operations to be defined in terms of these operations only. Doing so will allow us to provide an over-arching polynomial package that can perform operations on either (or both!) of the two representations without bypassing this abstraction layer.

We can show how this will fit into the system by extending the figure above:
Programs that use numbers
add sub mul div
Generic arithmetic package
add-rat sub-rat
mul-rat div-rat
add-complex sub-complex
mul-complex div-complex
add-poly sub-poly
mul-poly div-poly
+ - * /
Rational arithmetic Complex arithmetic Polynomial arithmetic Ordinary arithmetic
Rectangular Polar Dense Sparse

Identifying the Interface

If we look at the original polynomial package implementation (which you can find in its entirety at the top of my solution to exercise 2.87), then you can see that the authors have carefully split out the term-list-specific portions of the operations in the package. add-terms is split out from add-poly, mul-terms is split out from mul-poly and so on.

We've retained this split throughout the subsequent exercises. As a result, if we compare the sparse and dense polynomial packages we've produced then we can see that they differ only from the level of these operations down. This suggests that we should treat these operations as the primitive operations that we should extract. In other words, we'll use different representations for the term-lists, but the surrounding polynomial representation (i.e. a tuple consisting of a variable and a term-list) will remain unchanged.

If we do choose these as our primitive operations then we'll quickly hit a stumbling block: we can't add two polynomials together which have different term-list representations. The implementation of add-terms for sparse term-lists will only work with, and will only be installed for, pairs of sparse term-lists. Similarly the implementation of add-terms for dense term-lists will only work with, and will only be installed for, pairs of dense term-lists. So we'll get an error if we try to add a sparse and a dense term-list together.

Obviously we can't work around this by simply e.g. installing the dense term-list operations for all permutations of term-list representations. The internal representations are different and so errors will occur. Instead we'll need to coerce the types of parameters to some common type (probably sparse term-lists, as we'd not want to expand out a sparse term-list with, say, an order 1,000,000 term to a dense term-list with all the zero-padding that would involve!).

Coercing Dense and Sparse Term-Lists

In section 2.5.2 and the related exercises we developed a mechanism for automatic coercion of types based upon a tower-of-types. Surely all we need to do then is to add the sparse and dense term-list types into the tower-of-types and all will work, won't it?

Well, not quite.

In our solution to this exercise sparse and dense term-lists will not be "primary" types. I.e like the rectangular and polar representations of complex numbers, they are not directly constructable and do not expose the full set of arithmetic operations. Instead they will be "secondary" (or sub-) types of the polynomial type. As a result they do not fit into the tower-of-types we have been using so far. What we need is an entirely separate tower-of-types for term-list representations and have apply-generic support this.

Thankfully this isn't as difficult as it sounds. Our current implementation for raising and dropping types is dependant upon there being an unbroken chain of coercion operations that can raise a type from any point in the tower-of-types right the way up to the top of the tower. This requirement is made explicit in apply-raise, where it tries to obtain an appropriate coercion operation to raise the type one level in the tower. If no such operation can be found it raises an error:
…
(let ((raiser (get-coercion (type-tag x) (cadr types))))
  (if raiser
      (raiser (contents x))
      (error "No coercion procedure found for types"
             (list (type-tag x) (cadr types)))))
And as apply-raise is also used by project to lower types, (by passing in the reversed tower-of-types to apply-raise), the current implementation also depends upon their being a similar chain of coercion operations that can lower any type all the way to the bottom of the tower.

If we loosen this restriction and allow for breaks in the chain of coercion operations up and down the tower then this effectively allows multiple towers-of-types to be represented with a single tower. All we need is for the implementation to assume that a missing coercion operation indicates a point at which a type should not be lowered any further. To do this all we need to do is to return the value passed in instead of raising an error. Here's the updated apply-raise:
(define (apply-raise x types)
  (cond ((null? types)
         (error "Type not found in the tower-of-types"
                (list (type-tag x) tower-of-types)))
        ((eq? (type-tag x) (car types))
         (if (null? (cdr types))
             x
             (let ((raiser (get-coercion (type-tag x) (cadr types))))
               (if raiser
                   (raiser (contents x))
                   x))))
        (else (apply-raise x (cdr types)))))
Having made this change we can just add the sparse and dense term-list types to the tower, and add the appropriate coercion procedures, and it'll all work. We'll leave the coercion procedures for now, as they'll fall out of the work we do to create the term-lists.

Coercing Polynomials

While we're talking about coercions it's worth noting that we can also fit our overarching polynomial package into the tower-of-types too.

Let's start with raising a complex type to a polynomial. What we need to produce is a polynomial of order 0 whose coefficient of the order 0 term is the complex value we're raising. We need to consider, however, what variable we will create the polynomial with. If we choose e.g. 'x as our variable then we'll be unable to use it in any calculations with polynomials whose variable is 'y and vice versa - despite the fact that the polynomial has no actual terms in which the variable is important.

One way of addressing this is to introduce a special marker variable that is only used for polynomials of order 0 that indicates that it can be used in calculations with any other polynomial. Effectively it late-binds the variable at the point the calculation is performed. I chose to call this marker 'unbound. Supporting this first requires a simple extension to same-variable? so that it matches 'unbound with any other variable:
(define (same-variable? v1 v2)
  (and (variable? v1)
       (variable? v2)
       (or (eq? v1 v2)
           (eq? v1 'unbound)
           (eq? v2 'unbound))))
It also requires a modification to the arithmetic operations so that they select the appropriate variable for any polynomial resulting from a calculation. At the moment our arithmetic operations just use the variable of the first polynomial involved in any calculation. All we need to do is to modify these so that they identify whether or not one this variable is 'unbound and, if so, use the other polynomial's variable instead. This selection can be encapsulated in a separate operation as follows:
(define (select-variable p1 p2)
  (let ((v1 (variable p1)))
    (if (eq? v1 'unbound)
        (variable p2)
        v1)))
Once we can raise complex types to polynomial types in this manner, it's a trivial matter to drop them again. We know that the order 0 term of a polynomial is equivalent to a constant numerical value. As a result, we can successfully drop a polynomial of order 0 to a complex by simply taking the coefficient of the order 0 term. Now due to the manner in drop works (i.e. it projects down the tree, which is allowed to drop information, then raises the result and checks that this is the same), this means that our coercion procedure can follow this approach, regardless of the order of the polynomial. I.e. it takes the coefficient of the order 0 term, and discards the rest of the polynomial.

After we incorporate these changes we'll have a new tower-of-types that effectively looks like this:
    sparse-terms
         ↑
    dense-terms

     polynomial
         ↑
      complex
         ↑
      rational
         ↑
      integer

Creating Polynomials

I noted above that coercing between sparse and dense term-lists would fall out of the work on creating polynomials, so let's look at that next.

The implementations of the polynomial package that we've dealt with in the preceding exercises for sparse and dense polynomials have produced two ways in which we can construct polynomials. For sparse polynomials we create a polynomial using a list of terms, with each term being a tuple of the term's order and its coefficient. For dense polynomials we create a polynomial using a list of the term coefficients, with the order of the term corresponding to a particular coefficient being implicit in the position of the coefficient in that list.

Now let's suppose we implement our system so that we can create either term-list representation in either manner. I.e. we can create a term list with either a list of terms or a list of coefficients, regardless of whether we're using the sparse or dense representations. In such a system we would be able to easily convert (i.e. coerce) a sparse term-list into a dense term-list by simply creating a dense term-list using the sparse term-list's terms. Similarly, we would be able to easily convert a dense term-list into a sparse term-list by creating a sparse term-list using the dense term-list's coefficients.

So, as you can see, by implementing our system in this manner the coercion operations will fall out of the work on creating polynomials.

The creation operations themselves are pretty straightforward. We already have appropriate operations for creating sparse polynomials from term lists and dense polynomials from coefficient lists. In order to create a sparse polynomial from a coefficient list we simply run through the list and add a term of the appropriate order for each coefficient (i.e. the term order for a coefficient is dictated by its position in the list). To create a dense polynomial from a term list we perform a similar operation to the one of building a sparse polynomial from a term list, except that the list we build consists of coefficients only, and so we determine whether or not we're inserting at the right location based upon the length of the list.

Dense or Sparse?

One side-effect of supporting coercion between dense and sparse term-list representations is that, thanks to the work we did in exercise 2.85, apply-generic will automatically drop a sparse term-list to a dense term-list. Now we don't necessarily want to do this, as the result of an arithmetic operation on two sparse term-lists may itself be a term-list that's best represented sparsely. We'll want to block the coercion in this case. One way of doing this is to simply have the coercion procedure decide whether or not it should coerce and, if not, just return the term-list unchanged (well, apart from re-tagging it). Our implementation of drop already checks for coercion procedures doing this and uses it to indicate that the value could not be dropped any further.

To make things a bit more complex, I'd like to generalize my statement above... The result of an arithmetic operation on any pair of term-lists, regardless of their representations, may be a term-list that's best represented sparsely. We should also make sure that the result of dense term-list arithmetic operations is provided in the best representation. The raising won't happen automatically for us here, so we'll need to do it ourselves.

So when should we use a dense term-list and when a sparse term-list? The book defines the difference as follows: "A polynomial is said to be dense if it has nonzero coefficients in terms of most orders. If it has many zero terms it is said to be sparse." There's no hard and fast rule about what having "nonzero coefficients in terms of most orders" or "many zero terms" mean, so I opted for the following: we'll represent a term-list sparsely when more than 10% of its terms have a zero coefficient, with a special rule to deal with term-lists whose highest order term is order 10 or lower.

Right, I think that's enough discussion of it. Let's move on to the code!

2012-04-30

SICP Exercise 2.89 Redux: Stripping Out the Deadwood

Define procedures that implement the term-list representation described above as appropriate for dense polynomials.
Right at the start of exercise 2.89 I suggested that, in anticipation of exercise 2.90 it might be best to produce the dense polynomial package by making as little change to the general structure of the polynomial package as possible and that this would mean leaving in some procedures that could be stripped out.

Well, I've just started into the exercise (yes, I know, it's been over a month since I posted the last one, but I've been busy!) and come to the conclusion that this won't really help much due to the extent of the changes that'll be required. So here's exercise 2.89 with the "procedures that could be stripped out" actually stripped out:
(define (install-polynomial-package)
  ;; internal procedures
  ;; representation of poly
  (define (make-poly variable term-list)
    (cons variable (strip-leading-zeros term-list)))
  (define (variable p) (car p))
  (define (term-list p) (cdr p))
  
  ;; procedures same-variable? and variable? from section 2.3.2
  (define (variable? x) (symbol? x))
  (define (same-variable? v1 v2)
    (and (variable? v1) (variable? v2) (eq? v1 v2)))

  ;; representation of terms and term lists
  (define zero-term (make-integer 0))
  (define (adjoin-term term-order term-coeff term-list)
    (cond ((=zero? term-coeff) term-list)
          ((= term-order (+ 1 (order term-list)))
           (cons term-coeff term-list))
          ((> term-order (order term-list))
           (adjoin-term term-order
                        term-coeff
                        (cons zero-term term-list)))
          (else (error "Cannot adjoin term of lower order than term list -- ADJOIN-TERM"
                       (list term-order term-coeff term-list)))))
  (define (the-empty-termlist) '())
  (define (first-term term-list) (car term-list))
  (define (rest-terms term-list) (cdr term-list))
  (define (empty-termlist? term-list) (null? term-list))
  (define (order term-list)
    (- (length term-list) 1))

  (define (strip-leading-zeros term-list)
    (cond ((empty-termlist? term-list) (the-empty-termlist))
          ((not (=zero? (first-term term-list))) term-list)
          (else (strip-leading-zeros (rest-terms term-list)))))
  
  ;; procedures used by add-poly
  (define (add-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (add-terms (term-list p1)
                              (term-list p2)))
        (error "Polys not in same var -- ADD-POLY"
               (list p1 p2))))
  (define (add-terms L1 L2)
    (cond ((empty-termlist? L1) L2)
          ((empty-termlist? L2) L1)
          (else
           (let* ((t1 (first-term L1))
                  (t2 (first-term L2))
                  (o1 (order L1))
                  (o2 (order L2)))
             (cond ((> o1 o2)
                    (adjoin-term o1 t1 (add-terms (rest-terms L1) L2)))
                   ((< o1 o2)
                    (adjoin-term o2 t2 (add-terms L1 (rest-terms L2))))
                   (else
                    (adjoin-term
                     o1
                     (add t1 t2)
                     (add-terms (rest-terms L1)
                                (rest-terms L2)))))))))

  ;; procedures used by mul-poly
  (define (mul-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (mul-terms (term-list p1)
                              (term-list p2)))
        (error "Polys not in same var -- MUL-POLY"
               (list p1 p2))))
  (define (mul-terms L1 L2)
    (if (empty-termlist? L1)
        (the-empty-termlist)
        (add-terms (mul-term-by-all-terms (order L1) (first-term L1) L2)
                   (mul-terms (rest-terms L1) L2))))
  (define (mul-term-by-all-terms o1 c1 L)
    (if (empty-termlist? L)
        (the-empty-termlist)
        (let* ((t2 (first-term L))
               (new-order (+ o1 (order L))))
          (adjoin-term
           new-order
           (mul c1 t2)
           (mul-term-by-all-terms o1 c1 (rest-terms L))))))

  ;; Subtraction
  (define (sub-poly p1 p2)
    (add-poly p1 (negate-poly p2)))
  
  ;; zero test
  (define (=zero-all-terms? L)
    (cond ((empty-termlist? L) #t)
          ((not (=zero? (first-term L))) #f)
          (else (=zero-all-terms? (rest-terms L)))))
  (define (=zero-poly? p)
    (=zero-all-terms? (term-list p)))
        
  ;; Negation
  (define (negate-terms L)
    (if (empty-termlist? L)
        (the-empty-termlist)
        (let ((term (first-term L)))
          (adjoin-term (order L)
                       (negate term)
                       (negate-terms (rest-terms L))))))
  (define (negate-poly p)
    (make-poly (variable p)
               (negate-terms (term-list p))))
  
  ;; interface to rest of the system
  (define (tag p) (attach-tag 'polynomial p))
  (put 'add '(polynomial polynomial) 
       (lambda (p1 p2) (tag (add-poly p1 p2))))
  (put 'mul '(polynomial polynomial) 
       (lambda (p1 p2) (tag (mul-poly p1 p2))))
  (put '=zero? '(polynomial) =zero-poly?)
  (put 'negate '(polynomial)
       (lambda (p) (tag (negate-poly p))))
  (put 'sub '(polynomial polynomial)
       (lambda (p1 p2) (tag (sub-poly p1 p2))))
  (put 'make 'polynomial
       (lambda (var terms) (tag (make-poly var terms))))
  'done)
Right, now back to exercise 2.90...

2012-04-08

SICP Exercise 2.89: Representing Dense Polynomials

Define procedures that implement the term-list representation described above as appropriate for dense polynomials.
In anticipation of the next exercise, let's try to do this with as little change to the general structure of the polynomial package as possible. That way it should be less of a step to supporting two different representations of term lists when we get there. This means we're going to leave in some procedures that could be stripped out (I'm looking at you, make-term and coeff). We will, of course, have to make a few changes to the existing system, but we'll try to do these in such a way that we can support both dense and sparse polynomials easily after the changes.

Dense Polynomial Term Manipulation

First let's deal with the term operations. In our sparse polynomial representation each term consisted of both the order of the term and the coefficient. However, in the discussion on the representation of term lists for dense polynomials presented in the book the authors note that dense polynomial term lists "are most efficiently represented as lists of the coefficients", with coefficients of zero used to indicate zero terms.

As the terms for dense polynomials are just represented as the coefficients we could remove the operations make-poly and coeff. However, remembering that we're trying to minimise the set of changes we produce, let's retain these procedures with the same operands. For dense polynomials make-poly will just ignore the order operand and return the coeff operand unchanged, while coeff will just return the term operand unchanged:
(define (make-term order coeff) coeff)
(define (coeff term) term)
The remaining operation for manipulating terms, order, runs into a spot of bother with the dense polynomial representation. As the representation of a term is just its coefficient it's not possible to determine the order of a term from the term itself. However, the authors note that "the order of a term in this representation is the length of the sublist beginning with that term's coefficient, decremented by 1." So we can still implement an order procedure for dense polynomials, but it will need to work on the full term list, not just a single term. It can be implemented as follows:
(define (order termlist)
  (- (length term-list) 1))

Dense Polynomial Term List Manipulation

Now let's move on to manipulating term lists for dense polynomials. We've already noted that the authors suggest representing the term lists as list of the coefficients. So, just like sparse polynomial term lists, dense polynomial term lists can be represented as Scheme lists. Of course the actual representation of the terms differ, but the data structure used to hold the terms is unchanged.

Note that, in order that we can determine which coefficient applies to which order of the indeterminate this representation requires that the coefficients be ordered in a predictable manner, and the text implies that we use an identical ordering as is used for the sparse polynomial term lists. I.e. from highest order term to lowest order term.

Given all this, we can create empty term lists, test for empty term lists and obtain the first and rest of the terms in the same manner as we do for sparse polynomials and so the-empty-termlist, empty-termlist?, first-term and rest-terms remain unchanged.

When it comes to adding terms to term lists, however, a bit more work is required. We need to ensure that the new term being adjoined to a term list is one order higher than the current order of the term list, and zero-pad the head of the list if it isn't. As we can't determine the order of the term from the term alone we'll now have to pass the order to adjoin-term. We'll then have to check the order of the term list and, if it's not one less than the new term's order, add a zero term to the head of the list and try again. And, because I'm paranoid, I'm also going to add a check that the term we're trying to adjoin is definitely of a higher order than the current term list...

Note that, instead of passing the order of the term and the term itself, we'll pass the order and coefficient of the term as the two operands. We can then create the term within adjoin-term using make-term, rather than creating it externally and passing it in. That makes little difference to dense polynomials, as the term is the coefficient. For sparse polynomials it makes the interface feel a little cleaner, as we won't be passing in both the order and the term, which contains the order.

Anyway, here's adjoin-term for dense polynomials:
(define zero-term (make-integer 0))
(define (adjoin-term term-order term-coeff term-list)
  (cond ((=zero? term-coeff) term-list)
        ((= term-order (+ 1 (order term-list)))
         (cons (make-term term-order term-coeff) term-list))
        ((> term-order (order term-list))
         (adjoin-term term-order
                      term-coeff
                      (cons zero-term term-list)))
        (else (error "Cannot adjoin term of lower order than term list -- ADJOIN-TERM"
                     (list term-order term-coeff term-list)))))

Updating the Dense Polynomial Operations

We've done our best above to minimise the changes we make to the polynomial package. However, we still had to modify order so that it takes the term list instead of just a single term and we've split the term operand for adjoin-term into both the order and the coefficient. This of course means that we need to modify the usages of these operations appropriately.

For adding dense polynomials we can update add-terms so that it calculates the orders of the terms at the same time it's pulling out the terms at the heads of each list. We'll use let* to do this succinctly. Then we can update the calls to adjoin-term to pass through the order:
(define (add-terms L1 L2)
  (cond ((empty-termlist? L1) L2)
        ((empty-termlist? L2) L1)
        (else
         (let* ((t1 (first-term L1))
                (t2 (first-term L2))
                (o1 (order L1))
                (o2 (order L2)))
           (cond ((> o1 o2)
                  (adjoin-term
                   o1 (coeff t1) (add-terms (rest-terms L1) L2)))
                 ((< o1 o2)
                  (adjoin-term
                   o2 (coeff t2) (add-terms L1 (rest-terms L2))))
                 (else
                  (adjoin-term
                   o1
                   (add (coeff t1) (coeff t2))
                   (add-terms (rest-terms L1)
                              (rest-terms L2)))))))))
Multiplying polynomials requires a more complex process. We go through each term in the first polynomial's term list and, for each one, produce a new term list that's the result of multiplying each term in the second polynomial's term list by this term. We add up all these term lists to produce the term list for the polynomial that results from the multiplication. The outer loop for this is implemented by mul-terms, while the inner loop (i.e. the multiplication of each term in the second polynomial's term list by a term in the first polynomial is implemented by mul-term-by-all-terms.

Looking at the inner loop first, we can see that this needs to know the order of the term from the first polynomial in order to calculate the results of multiplying this term by the first term in the second polynomial's term list. Like adjoin-term this means that we will need to pass in the order of the term as it's no longer possible to calculate it from the term itself. In fact we can apply a change to mul-term-by-all-terms that's similar to the one we applied to adjoin-term. I.e. pass in the order and the coefficient of the term as separate operands.

This then has a knock-on effect on mul-terms, where we need to pass these in to the call to mul-term-by-all-terms.

Here's the updated operations:
(define (mul-terms L1 L2)
  (if (empty-termlist? L1)
      (the-empty-termlist)
      (add-terms (mul-term-by-all-terms (order L1) (coeff (first-term L1)) L2)
                 (mul-terms (rest-terms L1) L2))))

(define (mul-term-by-all-terms o1 c1 L)
  (if (empty-termlist? L)
      (the-empty-termlist)
      (let* ((t2 (first-term L))
             (new-order (+ o1 (order L))))
        (adjoin-term
         new-order
         (mul c1 (coeff t2))
         (mul-term-by-all-terms o1 c1 (rest-terms L))))))
There's one more operation to consider, sub-poly. However, this just calls through to add-poly with the first polynomial and the negation of the second polynomial that it was invoked with. So sub-poly remains unchanged. We need to make a minor change to negate-terms to account for the change in the order and adjoin-term operations though:
(define (negate-terms L)
  (if (empty-termlist? L)
      (the-empty-termlist)
      (let ((term (first-term L)))
        (adjoin-term (order L)
                     (negate (coeff term))
                     (negate-terms (rest-terms L))))))

Creating Dense Polynomials

The last thing we need to deal with for dense polynomials is constructing them.

We should note that the list-of-coefficients representation allows us to use a simpler interface for constructing dense polynomials than for sparse polynomials. Provided we stipulate that the list of coefficients must be ordered from highest order coefficient to lowest, has a coefficient for order 0 and has zero coefficients for zero terms then all we require are the indeterminate and the list of the coefficients; we don't need the orders of the terms any more as this is implicit. As a result, we won't need to expose an operation for making terms for dense polynomials such as was added in my addendum to exercise 2.87.

In that addendum I also modified make-poly from the implementation provided in the book so that it reordered the terms passed to it to ensure that they were ordered from high to low order. Given that we're making the ordering of coefficients into part of the creation operation's contract we no longer need to perform this reordering. However, we'll still want to pre-process the provided set of coefficients to remove any leading zero coefficients. Leading zero coefficients do not contribute to the evaluation of the polynomial, and so can be safely ignored.

We can do this quite simply by iterating through the term list, checking the head of the list at each iteration with =zero?, and returning the list as soon as we encounter a non-zero entry. Obviously we'll also need to check to see if we've got an empty term list - as could happen if we're passed either an entirely empty term list or one in which all coefficients are zero.

Here's make-poly and the accompanying helper operation:
(define (make-poly variable term-list)
  (cons variable (strip-leading-zeros term-list)))
(define (strip-leading-zeros term-list)
  (cond ((empty-termlist? term-list) (the-empty-termlist))
        ((not (=zero? (first-term term-list))) term-list)
        (else (strip-leading-zeros (rest-terms term-list)))))
Note that, although the term list representations differ, the make-poly implementations for both dense and sparse polynomials create the same "outer" structure for representing polynomials: a pair with the indeterminate as the first item and the term list as the second item. This means that the variable and term-list operations can remain unchanged. Also, as we're not changing the representation of the indeterminate, variable? and same-variable? can also remain as is.

Putting It All Together

We've now gone through the entirety of the polynomial package and produced a minimal set of changes to convert it from sparse polynomial to dense polynomial representation. You could pull the dense polynomial version together from above, but to save you the hassle, here it its:
(define (install-polynomial-package)
  ;; internal procedures
  ;; representation of poly
  (define (make-poly variable term-list)
    (cons variable (strip-leading-zeros term-list)))
  (define (variable p) (car p))
  (define (term-list p) (cdr p))
  
  ;; procedures same-variable? and variable? from section 2.3.2
  (define (variable? x) (symbol? x))
  (define (same-variable? v1 v2)
    (and (variable? v1) (variable? v2) (eq? v1 v2)))

  ;; representation of terms and term lists
  (define zero-term (make-integer 0))
  (define (adjoin-term term-order term-coeff term-list)
    (cond ((= term-order (+ 1 (order term-list)))
           (cons (make-term term-order term-coeff) term-list))
          ((> term-order (order term-list))
           (adjoin-term term-order
                        term-coeff
                        (cons zero-term term-list)))
          (else (error "Cannot adjoin term of lower order than term list -- ADJOIN-TERM"
                       (list term-order term-coeff term-list)))))
  (define (the-empty-termlist) '())
  (define (first-term term-list) (car term-list))
  (define (rest-terms term-list) (cdr term-list))
  (define (empty-termlist? term-list) (null? term-list))
  (define (make-term order coeff) coeff)
  (define (order term-list)
    (- (length term-list) 1))
  (define (coeff term) term)

  (define (strip-leading-zeros term-list)
    (cond ((empty-termlist? term-list) (the-empty-termlist))
          ((not (=zero? (first-term term-list))) term-list)
          (else (strip-leading-zeros (rest-terms term-list)))))
  
  ;; procedures used by add-poly
  (define (add-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (add-terms (term-list p1)
                              (term-list p2)))
        (error "Polys not in same var -- ADD-POLY"
               (list p1 p2))))
  (define (add-terms L1 L2)
    (cond ((empty-termlist? L1) L2)
          ((empty-termlist? L2) L1)
          (else
           (let* ((t1 (first-term L1))
                  (t2 (first-term L2))
                  (o1 (order L1))
                  (o2 (order L2)))
             (cond ((> o1 o2)
                    (adjoin-term
                     o1 (coeff t1) (add-terms (rest-terms L1) L2)))
                   ((< o1 o2)
                    (adjoin-term
                     o2 (coeff t2) (add-terms L1 (rest-terms L2))))
                   (else
                    (adjoin-term
                     o1
                     (add (coeff t1) (coeff t2))
                     (add-terms (rest-terms L1)
                                (rest-terms L2)))))))))

  ;; procedures used by mul-poly
  (define (mul-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (mul-terms (term-list p1)
                              (term-list p2)))
        (error "Polys not in same var -- MUL-POLY"
               (list p1 p2))))
  (define (mul-terms L1 L2)
    (if (empty-termlist? L1)
        (the-empty-termlist)
        (add-terms (mul-term-by-all-terms (order L1) (coeff (first-term L1)) L2)
                   (mul-terms (rest-terms L1) L2))))
  (define (mul-term-by-all-terms o1 c1 L)
    (if (empty-termlist? L)
        (the-empty-termlist)
        (let* ((t2 (first-term L))
               (new-order (+ o1 (order L))))
          (adjoin-term
           new-order
           (mul c1 (coeff t2))
           (mul-term-by-all-terms o1 c1 (rest-terms L))))))

  ;; Subtraction
  (define (sub-poly p1 p2)
    (add-poly p1 (negate-poly p2)))
  
  ;; zero test
  (define (=zero-all-terms? L)
    (cond ((empty-termlist? L) #t)
          ((not (=zero? (coeff (first-term L)))) #f)
          (else (=zero-all-terms? (rest-terms L)))))
  (define (=zero-poly? p)
    (=zero-all-terms? (term-list p)))
        
  ;; Negation
  (define (negate-terms L)
    (if (empty-termlist? L)
        (the-empty-termlist)
        (let ((term (first-term L)))
          (adjoin-term (order L)
                       (negate (coeff term))
                       (negate-terms (rest-terms L))))))
  (define (negate-poly p)
    (make-poly (variable p)
               (negate-terms (term-list p))))
  
  ;; interface to rest of the system
  (define (tag p) (attach-tag 'polynomial p))
  (put 'add '(polynomial polynomial) 
       (lambda (p1 p2) (tag (add-poly p1 p2))))
  (put 'mul '(polynomial polynomial) 
       (lambda (p1 p2) (tag (mul-poly p1 p2))))
  (put '=zero? '(polynomial) =zero-poly?)
  (put 'negate '(polynomial)
       (lambda (p) (tag (negate-poly p))))
  (put 'sub '(polynomial polynomial)
       (lambda (p1 p2) (tag (sub-poly p1 p2))))
  (put 'make 'polynomial
       (lambda (var terms) (tag (make-poly var terms))))
  'done)

(define (make-polynomial var terms)
  ((get 'make 'polynomial) var terms))
Now we've got our dense polynomial package, let's give it a spin:
> (make-polynomial 'x '())
(polynomial x)
> (make-polynomial 'x (list (make-integer 3)
                            (make-integer 1)
                            (make-real 2.3)))
(polynomial x (integer . 3) (integer . 1) (real . 2.3))
> (make-polynomial 'x (list (make-real 0)
                            (make-rational 0 4)
                            (make-integer 0)
                            (make-complex-from-real-imag
                             (make-integer 0)
                             (make-integer 0))))
(polynomial x)
> (define inner-poly (make-polynomial 
                      'y
                      (list (make-integer 1)
                            (make-rational -3 2)
                            (make-integer 42))))
> (define outer-poly (make-polynomial
                      'x
                      (list (make-rational 7 2)
                            (make-complex-from-real-imag
                             (make-integer 42)
                             (make-integer -13))
                            inner-poly
                            (make-integer 0)
                            (make-integer 5))))
> (negate outer-poly)
(polynomial x (rational -7 . 2)
              (complex rectangular (integer . -42) integer . 13)
              (polynomial y (integer . -1) (rational 3 . 2) (integer . -42))
              (integer . 0)
              (integer . -5))
> (add (make-polynomial 'x
                        (list (make-integer 4)
                              (make-integer 0)
                              (make-integer 2)
                              (make-integer 0)
                              (make-integer 1)))
       (make-polynomial 'x
                        (list (make-integer 5)
                              (make-integer 4)
                              (make-integer 0)
                              (make-integer 1)
                              (make-integer 1))))
(polynomial x (integer . 9)
              (integer . 4)
              (integer . 2)
              (integer . 1)
              (integer . 2))
> (add (make-polynomial 'x
                        (list (make-integer 5)
                              (make-integer 4)
                              (make-integer 3)
                              (make-integer 2)
                              (make-integer 1)))
       (make-polynomial 'x
                        (list (make-integer 3)
                              (make-integer 2)
                              (make-integer 1))))
(polynomial x (integer . 5)
              (integer . 4)
              (integer . 6)
              (integer . 4)
              (integer . 2))
> (add (make-polynomial 'x
                        (list (make-integer 3)
                              (make-integer 2)
                              (make-integer 1)))
       (make-polynomial 'x
                        (list (make-integer 5)
                              (make-integer 4)
                              (make-integer 3)
                              (make-integer 2)
                              (make-integer 1))))
(polynomial x (integer . 5)
              (integer . 4)
              (integer . 6)
              (integer . 4)
              (integer . 2))
> (sub (make-polynomial 'x
                        (list (make-integer 4)
                              (make-integer 0)
                              (make-integer 2)
                              (make-integer 0)
                              (make-integer 1)))
       (make-polynomial 'x
                        (list (make-integer 5)
                              (make-integer 4)
                              (make-integer 0)
                              (make-integer 1)
                              (make-integer 1))))
(polynomial x (integer . -1)
              (integer . -4)
              (integer . 2)
              (integer . -1)
              (integer . 0))
> (sub (make-polynomial 'x
                        (list (make-integer 3)
                              (make-integer 2)
                              (make-integer 1)))
       (make-polynomial 'x
                        (list (make-integer 5)
                              (make-integer 4)
                              (make-integer 3)
                              (make-integer 2)
                              (make-integer 1))))
(polynomial x (integer . -5)
              (integer . -4)
              (integer . 0)
              (integer . 0)
              (integer . 0))
> (mul (make-polynomial 'x
                        (list (make-integer 3)
                              (make-integer 2)
                              (make-integer 1)))
       (make-polynomial 'x
                        (list (make-integer 6)
                              (make-integer 5)
                              (make-integer 4)
                              (make-integer 3)
                              (make-integer 2)
                              (make-integer 1))))
(polynomial x (integer . 18)
              (integer . 27)
              (integer . 28)
              (integer . 22)
              (integer . 16)
              (integer . 10)
              (integer . 4)
              (integer . 1))