Symbolic Mathematics: Polynomial Differentiation and Integration
Symbolic mathematics has been a central pillar of Lisp and functional programming since the earliest days of artificial intelligence. In 1958, John McCarthy invented Lisp at MIT in part to support symbolic differentiation and algebraic manipulation. Unlike numerical algorithms that approximate derivatives using finite differences or approximate integrals using quadrature sums, symbolic algebra manipulates mathematical expressions directly as structured data.
A symbolic algebra system computes exact closed-form expressions. When you differentiate
, the system yields the polynomial
. When you integrate that same polynomial over the interval
, it applies the Fundamental Theorem of Calculus to compute the exact rational value
, without any floating-point rounding error.
In this chapter, we construct a complete symbolic calculus engine for single-variable polynomials in pure Gerbil Scheme. The system relies entirely on the Gerbil standard library and the Gambit numeric tower, using defstruct records to model mathematical primitives with exact rational arithmetic.
Theoretical Foundations
Before building the data representations, we establish the underlying calculus principles.
Polynomial Representation and Monomials
A single-variable polynomial
of degree
in the variable
is an expression of the form:

where each
is a coefficient and each
is a non-negative integer exponent.
Each building block
is a monomial (or term). To maintain a canonical form, polynomials must combine like terms having identical exponents, discard terms with zero coefficients, and sort terms in descending order by exponent.
Symbolic Differentiation: The Power Rule
Differentiation is a linear operator on polynomials. Because
, we differentiate a full polynomial by differentiating each monomial term independently and summing the results.
For any single monomial term, the power rule states:

For a constant term where
, the derivative is zero:

Higher-order derivatives
are obtained by iteratively differentiating
times. A critical point of
occurs at any value
where the first derivative vanishes:
.
Symbolic Integration: The Reverse Power Rule
Integration is the inverse operation of differentiation. The indefinite integral (or antiderivative) of a polynomial is determined by the reverse power rule:

Here Scheme’s exact rational arithmetic provides a distinct advantage. If
and
, the coefficient becomes the exact rational number
rather than the inexact floating-point value
.
By the Fundamental Theorem of Calculus, the definite integral of
from lower limit
to upper limit
is computed from its antiderivative
:

When
and
are exact numbers, the resulting definite integral is also exact.
The Data Layer (data.ss)
The data.ss module establishes the core mathematical data structures using defstruct. It defines representations for variables, named constants, monomial terms, polynomials, and integrals, along with normalization and arithmetic operations.
1 ;; File: data.ss
2 ;; Core data structures for symbolic mathematics.
3 ;;
4 ;; sym-variable -- a symbolic variable (e.g. x, y, t)
5 ;; sym-constant -- a named constant (e.g. pi, e, 42)
6 ;; sym-term -- a single monomial: coefficient * variable ^ exponent
7 ;; sym-polynomial -- a polynomial in one variable (ordered list of terms)
8 ;; sym-integral -- a definite or indefinite integral
9 ;;
10 ;; Coefficients may be any Scheme real, including exact rationals such as 1/2,
11 ;; so integration results stay exact (just as in Common Lisp).
12
13 (import :std/sort :std/format)
14 (export make-variable variable? variable-name variable-domain variable=?
15 make-constant constant? constant-name constant-value constant-numeric-value
16 make-term term? term-coefficient term-variable term-exponent
17 term=? term-negate term-scale term->string
18 make-polynomial polynomial? polynomial-variable polynomial-terms
19 polynomial-domain polynomial-degree polynomial-leading-term
20 polynomial-add polynomial-subtract polynomial-negate polynomial-scale
21 polynomial-evaluate polynomial->string polynomial-normalize
22 zero-polynomial constant-polynomial identity-polynomial
23 make-integral integral? integral-integrand integral-variable
24 integral-lower integral-upper integral-definite? integral->string)
25
26 ;;; ------------------------------------------------------------------------
27 ;;; 1. sym-variable
28 ;;; ------------------------------------------------------------------------
29
30 (defstruct sym-variable (name domain) transparent: #t)
31
32 ;; Create a symbolic variable with NAME (a symbol) and optional DOMAIN,
33 ;; one of 'real (default), 'complex, or 'integer.
34 (def (make-variable name domain: (domain 'real))
35 (unless (symbol? name) (error "Variable name must be a symbol:" name))
36 (unless (memq domain '(real complex integer))
37 (error "DOMAIN must be real, complex, or integer:" domain))
38 (make-sym-variable name domain))
39
40 (def variable? sym-variable?)
41 (def variable-name sym-variable-name)
42 (def variable-domain sym-variable-domain)
43
44 ;; Two variables are equal when their names and domains match.
45 (def (variable=? a b)
46 (and (variable? a) (variable? b)
47 (eq? (variable-name a) (variable-name b))
48 (eq? (variable-domain a) (variable-domain b))))
49
50 ;;; ------------------------------------------------------------------------
51 ;;; 2. sym-constant
52 ;;; ------------------------------------------------------------------------
53
54 (defstruct sym-constant (name value) transparent: #t)
55
56 ;; Create a named constant. VALUE may be a real number, 'pi, or 'e.
57 (def (make-constant name value)
58 (unless (or (real? value) (memq value '(pi e)))
59 (error "VALUE must be a number, 'pi, or 'e:" value))
60 (make-sym-constant name value))
61
62 (def constant? sym-constant?)
63 (def constant-name sym-constant-name)
64 (def constant-value sym-constant-value)
65
66 ;; Return the numeric (floating-point) value of constant C.
67 (def (constant-numeric-value c)
68 (case (constant-value c)
69 ((pi) (* 4.0 (atan 1.0)))
70 ((e) (exp 1.0))
71 (else (constant-value c))))
72
73 ;;; ------------------------------------------------------------------------
74 ;;; 3. sym-term (monomial: coefficient * variable ^ exponent)
75 ;;; ------------------------------------------------------------------------
76
77 (defstruct sym-term (coefficient variable exponent) transparent: #t)
78
79 ;; Create a monomial COEFFICIENT * VARIABLE ^ EXPONENT (EXPONENT >= 0).
80 ;; (make-term 3 x 2) ; 3x^2
81 ;; (make-term 5 x 0) ; constant 5
82 (def (make-term coefficient variable exponent)
83 (unless (real? coefficient) (error "Coefficient must be real:" coefficient))
84 (unless (variable? variable) (error "Not a sym-variable:" variable))
85 (unless (and (exact-integer? exponent) (>= exponent 0))
86 (error "Exponent must be a non-negative integer:" exponent))
87 (make-sym-term coefficient variable exponent))
88
89 (def term? sym-term?)
90 (def term-coefficient sym-term-coefficient)
91 (def term-variable sym-term-variable)
92 (def term-exponent sym-term-exponent)
93
94 (def (term=? a b)
95 (and (term? a) (term? b)
96 (= (term-coefficient a) (term-coefficient b))
97 (variable=? (term-variable a) (term-variable b))
98 (= (term-exponent a) (term-exponent b))))
99
100 (def (term-negate term)
101 (term-scale term -1))
102
103 (def (term-scale term scalar)
104 (make-sym-term (* scalar (term-coefficient term))
105 (term-variable term)
106 (term-exponent term)))
107
108 ;; Human-readable form, e.g. "3x^2", "-1x", "5".
109 (def (term->string term)
110 (let ((c (term-coefficient term))
111 (v (symbol->string (variable-name (term-variable term))))
112 (e (term-exponent term)))
113 (cond
114 ((zero? e) (format "~a" c))
115 ((= e 1) (format "~ax" c))
116 (else (format "~ax^~a" c e)))))
117
118 ;;; ------------------------------------------------------------------------
119 ;;; 4. sym-polynomial
120 ;;; ------------------------------------------------------------------------
121
122 (defstruct sym-polynomial (variable terms domain) transparent: #t)
123
124 (def polynomial? sym-polynomial?)
125 (def polynomial-variable sym-polynomial-variable)
126 (def polynomial-terms sym-polynomial-terms)
127 (def polynomial-domain sym-polynomial-domain)
128
129 ;; Internal helper: group like exponents, sum coefficients, drop zeros,
130 ;; and sort terms in descending order by exponent.
131 (def (sort-and-combine-terms terms)
132 (let ((ht (make-hash-table)))
133 (for-each (lambda (term)
134 (hash-update! ht (term-exponent term) (lambda (c) (+ c (term-coefficient term))) 0))
135 terms)
136 (let ((combined '()))
137 (hash-for-each (lambda (exp coeff)
138 (unless (zero? coeff)
139 (set! combined (cons (cons exp coeff) combined))))
140 ht)
141 (sort combined (lambda (a b) (> (car a) (car b)))))))
142
143 ;; Create a polynomial in VARIABLE from a list of TERMS.
144 (def (make-polynomial variable terms domain: (domain 'real))
145 (unless (variable? variable) (error "Not a sym-variable:" variable))
146 (for-each (lambda (term)
147 (unless (variable=? variable (term-variable term))
148 (error "Term variable does not match polynomial variable:"
149 (variable-name (term-variable term))
150 (variable-name variable))))
151 terms)
152 (unless (memq domain '(real complex))
153 (error "DOMAIN must be real or complex:" domain))
154 (make-sym-polynomial
155 variable
156 (map (lambda (pair) (make-sym-term (cdr pair) variable (car pair)))
157 (sort-and-combine-terms terms))
158 domain))
159
160 (def (polynomial-normalize poly)
161 (make-polynomial (polynomial-variable poly) (polynomial-terms poly)
162 domain: (polynomial-domain poly)))
163
164 ;; Degree (highest exponent) of POLY; -1 for the zero polynomial.
165 (def (polynomial-degree poly)
166 (if (null? (polynomial-terms poly))
167 -1
168 (term-exponent (car (polynomial-terms poly)))))
169
170 (def (polynomial-leading-term poly)
171 (and (pair? (polynomial-terms poly)) (car (polynomial-terms poly))))
172
173 ;; Arithmetic happens in the wider of two domains (real is a subset of complex).
174 (def (join-domains a b)
175 (if (or (eq? a 'complex) (eq? b 'complex)) 'complex 'real))
176
177 (def (polynomial-add p q)
178 (unless (variable=? (polynomial-variable p) (polynomial-variable q))
179 (error "Cannot add polynomials in different variables"))
180 (make-polynomial (polynomial-variable p)
181 (append (polynomial-terms p) (polynomial-terms q))
182 domain: (join-domains (polynomial-domain p) (polynomial-domain q))))
183
184 (def (polynomial-negate poly)
185 (polynomial-scale poly -1))
186
187 (def (polynomial-subtract p q)
188 (polynomial-add p (polynomial-negate q)))
189
190 (def (polynomial-scale poly scalar)
191 (make-polynomial (polynomial-variable poly)
192 (map (lambda (term) (term-scale term scalar))
193 (polynomial-terms poly))
194 domain: (polynomial-domain poly)))
195
196 ;; Evaluate POLY at VALUE by substituting the variable.
197 (def (polynomial-evaluate poly value)
198 (foldl (lambda (term acc)
199 (+ acc (* (term-coefficient term) (expt value (term-exponent term)))))
200 0
201 (polynomial-terms poly)))
202
203 ;; e.g. "3x^2 + -1x + 5"; the zero polynomial renders as "0".
204 (def (polynomial->string poly)
205 (if (null? (polynomial-terms poly))
206 "0"
207 (let loop ((terms (cdr (polynomial-terms poly)))
208 (acc (term->string (car (polynomial-terms poly)))))
209 (if (null? terms)
210 acc
211 (loop (cdr terms) (string-append acc " + " (term->string (car terms))))))))
212
213 ;;; Convenience constructors
214
215 (def (zero-polynomial variable domain: (domain 'real))
216 (make-sym-polynomial variable '() domain))
217
218 (def (constant-polynomial variable value)
219 (make-polynomial variable (list (make-term value variable 0))))
220
221 (def (identity-polynomial variable)
222 (make-polynomial variable (list (make-term 1 variable 1))))
223
224 ;;; ------------------------------------------------------------------------
225 ;;; 5. sym-integral
226 ;;; ------------------------------------------------------------------------
227
228 ;; INTEGRAND is usually a sym-polynomial. LOWER/UPPER are numbers,
229 ;; sym-constants, or #f for an indefinite integral.
230 (defstruct sym-integral (integrand variable lower upper) transparent: #t)
231
232 (def integral? sym-integral?)
233 (def integral-integrand sym-integral-integrand)
234 (def integral-variable sym-integral-variable)
235 (def integral-lower sym-integral-lower)
236 (def integral-upper sym-integral-upper)
237
238 (def (make-integral integrand variable lower: (lower #f) upper: (upper #f))
239 (unless (variable? variable) (error "Not a sym-variable:" variable))
240 (unless (eq? (not lower) (not upper))
241 (error "Either both LOWER and UPPER must be provided, or neither."))
242 (make-sym-integral integrand variable lower upper))
243
244 (def (integral-definite? integral)
245 (and (integral-lower integral) #t))
246
247 (def (bound->string bound)
248 (cond
249 ((not bound) "")
250 ((number? bound) (number->string bound))
251 ((constant? bound)
252 (case (constant-value bound)
253 ((pi) "π")
254 ((e) "e")
255 (else (symbol->string (constant-name bound)))))
256 (else (format "~a" bound))))
257
258 ;; Indefinite: "∫(expr) dx" Definite: "∫[a,b](expr) dx"
259 (def (integral->string integral)
260 (let ((var (symbol->string (variable-name (integral-variable integral))))
261 (body (if (polynomial? (integral-integrand integral))
262 (polynomial->string (integral-integrand integral))
263 (format "~a" (integral-integrand integral))))
264 (bounds (if (integral-definite? integral)
265 (format "[~a,~a]" (bound->string (integral-lower integral))
266 (bound->string (integral-upper integral)))
267 "")))
268 (format "∫~a(~a) d~a" bounds body var)))
Notice that sort-and-combine-terms normalizes any list of terms into a canonical representation: identical exponents are combined, terms with zero coefficients vanish, and the remaining monomials are sorted from highest degree to lowest.
Symbolic Differentiation (differentiation.ss)
With polynomial representation in place, differentiation is implemented in differentiation.ss. It provides term differentiation, whole-polynomial differentiation, iterative n-th order derivatives, numerical gradient evaluation, and critical point testing.
1 ;; File: differentiation.ss
2 ;; Symbolic differentiation of polynomials.
3 ;;
4 ;; Power rule: d/dx(c * x^n) = n*c * x^(n-1) (n >= 1)
5 ;; d/dx(constant) = 0
6 ;; Sum rule: d/dx(p + q) = p' + q'
7
8 (import "data")
9 (export differentiate-term differentiate differentiate-n
10 gradient-at critical-point?)
11
12 ;; Apply the power rule to a single monomial TERM.
13 ;; Returns a new term, or #f when the term differentiates to zero.
14 (def (differentiate-term term)
15 (let ((c (term-coefficient term))
16 (v (term-variable term))
17 (n (term-exponent term)))
18 (if (zero? n)
19 #f ; constant term -> zero, drop it
20 (make-term (* n c) v (- n 1)))))
21
22 ;; Differentiate polynomial POLY with respect to its variable.
23 ;; p = 3x^2 - x + 5 => p' = 6x - 1
24 (def (differentiate poly)
25 (let ((var (polynomial-variable poly))
26 (diff-terms (filter-map differentiate-term (polynomial-terms poly))))
27 (if (null? diff-terms)
28 (zero-polynomial var domain: (polynomial-domain poly))
29 (make-polynomial var diff-terms domain: (polynomial-domain poly)))))
30
31 ;; Return the N-th derivative of POLY (N = 0 returns POLY unchanged).
32 (def (differentiate-n poly n)
33 (if (zero? n)
34 poly
35 (differentiate-n (differentiate poly) (- n 1))))
36
37 ;; Numerical value of the derivative of POLY at VALUE: differentiate
38 ;; symbolically, then evaluate.
39 (def (gradient-at poly value)
40 (polynomial-evaluate (differentiate poly) value))
41
42 ;; #t if VALUE is (numerically) a critical point of POLY: |p'(VALUE)| < TOLERANCE.
43 (def (critical-point? poly value tolerance: (tolerance 1e-9))
44 (< (abs (gradient-at poly value)) tolerance))
The function filter-map applies differentiate-term across all terms and automatically drops any that return #f (the constant terms whose derivatives evaluate to zero).
Symbolic Integration (integration.ss)
The module integration.ss implements antidifferentiation and definite integration.
1 ;; File: integration.ss
2 ;; Symbolic integration of polynomials.
3 ;;
4 ;; Reverse power rule: integral of c * x^n dx = (c / (n+1)) * x^(n+1) + C
5 ;; Sum rule: integral of (p + q) = integral p + integral q
6 ;; Fundamental theorem: integral over [a,b] of f(x) dx = F(b) - F(a)
7
8 (import "data")
9 (export integrate-term integrate integrate-n
10 evaluate-definite evaluate-integral
11 make-indefinite-integral make-definite-integral)
12
13 ;; Apply the reverse power rule to a single monomial TERM.
14 ;; Scheme keeps c/(n+1) as an exact rational, e.g. -1/2.
15 (def (integrate-term term)
16 (let ((new-exp (+ (term-exponent term) 1)))
17 (make-term (/ (term-coefficient term) new-exp)
18 (term-variable term)
19 new-exp)))
20
21 ;; Antiderivative of POLY (without the constant of integration +C).
22 ;; p = 3x^2 - x + 5 => F = x^3 - (1/2)x^2 + 5x
23 (def (integrate poly)
24 (let ((var (polynomial-variable poly))
25 (new-terms (map integrate-term (polynomial-terms poly))))
26 (if (null? new-terms)
27 (zero-polynomial var domain: (polynomial-domain poly))
28 (make-polynomial var new-terms domain: (polynomial-domain poly)))))
29
30 ;; N-th iterated antiderivative of POLY.
31 (def (integrate-n poly n)
32 (if (zero? n)
33 poly
34 (integrate-n (integrate poly) (- n 1))))
35
36 ;; Internal: convert a bound (number or sym-constant) to a number.
37 (def (resolve-bound bound)
38 (cond
39 ((not bound) (error "Bound is #f -- use evaluate-definite only on definite integrals."))
40 ((number? bound) bound)
41 ((constant? bound) (constant-numeric-value bound))
42 (else (error "Unrecognised bound type:" bound))))
43
44 ;; Evaluate the definite integral of POLY over [LOWER, UPPER] using the
45 ;; Fundamental Theorem of Calculus. Bounds may be numbers or sym-constants.
46 (def (evaluate-definite poly lower upper)
47 (let ((F (integrate poly)))
48 (- (polynomial-evaluate F (resolve-bound upper))
49 (polynomial-evaluate F (resolve-bound lower)))))
50
51 ;; Numeric value of a definite sym-integral.
52 (def (evaluate-integral integral)
53 (unless (integral-definite? integral)
54 (error "INTEGRAL is indefinite; only definite integrals have a numeric value."))
55 (unless (polynomial? (integral-integrand integral))
56 (error "INTEGRAL's integrand must be a sym-polynomial"))
57 (evaluate-definite (integral-integrand integral)
58 (integral-lower integral)
59 (integral-upper integral)))
60
61 ;; Wrap POLY in an indefinite integral shell: ∫(expr) dx
62 (def (make-indefinite-integral poly)
63 (make-integral poly (polynomial-variable poly)))
64
65 ;; Wrap POLY with explicit bounds: ∫[a,b](expr) dx
66 (def (make-definite-integral poly lower upper)
67 (make-integral poly (polynomial-variable poly) lower: lower upper: upper))
In integrate-term, the division (/ (term-coefficient term) new-exp) generates exact Scheme rationals. For instance, integrating -1x^1 produces a term with coefficient
, preserving infinite precision throughout algebraic manipulation.
Complete Test Suite (test.ss)
The verification driver in test.ss exercises polynomial arithmetic, derivatives, critical point checks, antiderivatives, and definite integrals with exact and transcendental bounds:
1 ;; File: test.ss
2 ;; Smoke tests for the data layer, differentiation, and integration.
3 (import :std/format "data" "differentiation" "integration")
4
5 (def (show label value) (printf "~a: ~a\n" label value))
6
7 (def x (make-variable 'x))
8 (def pi-c (make-constant 'pi 'pi))
9
10 ;; p = 3x^2 - x + 5
11 (def p (make-polynomial x (list (make-term 3 x 2) (make-term -1 x 1) (make-term 5 x 0))))
12 ;; q = x + 2
13 (def q (make-polynomial x (list (make-term 1 x 1) (make-term 2 x 0))))
14 ;; r = x^4 - 2x^3 + x
15 (def r (make-polynomial x (list (make-term 1 x 4) (make-term -2 x 3) (make-term 1 x 1))))
16 ;; s = 6x + 2
17 (def s (make-polynomial x (list (make-term 6 x 1) (make-term 2 x 0))))
18
19 (displayln "\n=== Symbolic Math Data Layer ===\n")
20 (show "p " (polynomial->string p))
21 (show "q " (polynomial->string q))
22 (show "p + q " (polynomial->string (polynomial-add p q)))
23 (show "p - q " (polynomial->string (polynomial-subtract p q)))
24 (show "2 * p " (polynomial->string (polynomial-scale p 2)))
25 (show "degree(p) " (polynomial-degree p))
26 (show "p(0) " (polynomial-evaluate p 0))
27 (show "p(1) " (polynomial-evaluate p 1))
28 (show "p(2) " (polynomial-evaluate p 2))
29 (show "indef " (integral->string (make-integral p x)))
30 (show "def " (integral->string (make-integral p x lower: 0 upper: 1)))
31 (show "pi-int " (integral->string (make-integral p x lower: 0 upper: pi-c)))
32
33 (displayln "\n=== Differentiation ===\n")
34 (show "p " (polynomial->string p))
35 (show "p' " (polynomial->string (differentiate p)))
36 (show "p'' " (polynomial->string (differentiate-n p 2)))
37 (show "p''' " (polynomial->string (differentiate-n p 3)))
38 (show "r " (polynomial->string r))
39 (show "r' " (polynomial->string (differentiate r)))
40 (show "r'' (via n=2) " (polynomial->string (differentiate-n r 2)))
41 (show "gradient-at(p, 0) (expected -1)" (gradient-at p 0))
42 (show "gradient-at(p, 1) (expected 5)" (gradient-at p 1))
43 (show "critical-point?(p, 1/6) (expected #t)" (critical-point? p 1/6))
44 (show "critical-point?(p, 0) (expected #f)" (critical-point? p 0))
45
46 (displayln "\n=== Integration ===\n")
47 (show "p " (polynomial->string p))
48 (show "∫p dx " (polynomial->string (integrate p)))
49 (show "∫∫p dx dx " (polynomial->string (integrate-n p 2)))
50 (show "s " (polynomial->string s))
51 (show "∫s dx " (polynomial->string (integrate s)))
52 (show "∫[0,1] p dx (expected 11/2)" (evaluate-definite p 0 1))
53 (show "∫[0,1] s dx (expected 5) " (evaluate-definite s 0 1))
54 (show "∫[0,π] p dx " (evaluate-definite p 0 pi-c))
55 (show "indef shell " (integral->string (make-indefinite-integral p)))
56 (show "def [0,1] " (integral->string (make-definite-integral p 0 1)))
57 (show "def [0,π] " (integral->string (make-definite-integral p 0 pi-c)))
58 (show "evaluate shell (expected 11/2)" (evaluate-integral (make-definite-integral p 0 1)))
59 (show "d/dx ∫p dx == p ?" (equal? (polynomial->string (differentiate (integrate p)))
60 (polynomial->string p)))
Running the Code and Expected Output
Execute the test suite using make test:
1 $ make test
2 gxi test.ss
3
4 === Symbolic Math Data Layer ===
5
6 p : 3x^2 + -1x + 5
7 q : 1x + 2
8 p + q : 3x^2 + 7
9 p - q : 3x^2 + -2x + 3
10 2 * p : 6x^2 + -2x + 10
11 degree(p) : 2
12 p(0) : 5
13 p(1) : 7
14 p(2) : 15
15 indef : ∫(3x^2 + -1x + 5) dx
16 def : ∫[0,1](3x^2 + -1x + 5) dx
17 pi-int : ∫[0,π](3x^2 + -1x + 5) dx
18
19 === Differentiation ===
20
21 p : 3x^2 + -1x + 5
22 p' : 6x + -1
23 p'' : 6
24 p''' : 0
25 r : 1x^4 + -2x^3 + 1x
26 r' : 4x^3 + -6x^2 + 1
27 r'' (via n=2) : 12x^2 + -12x
28 gradient-at(p, 0) (expected -1): -1
29 gradient-at(p, 1) (expected 5): 5
30 critical-point?(p, 1/6) (expected #t): #t
31 critical-point?(p, 0) (expected #f): #f
32
33 === Integration ===
34
35 p : 3x^2 + -1x + 5
36 ∫p dx : 1x^3 + -1/2x^2 + 5x
37 ∫∫p dx dx : 1/4x^4 + -1/6x^3 + 5/2x^2
38 s : 6x + 2
39 ∫s dx : 3x^2 + 2x
40 ∫[0,1] p dx (expected 11/2): 11/2
41 ∫[0,1] s dx (expected 5) : 5
42 ∫[0,π] p dx : 41.77943774770411
43 indef shell : ∫(3x^2 + -1x + 5) dx
44 def [0,1] : ∫[0,1](3x^2 + -1x + 5) dx
45 def [0,π] : ∫[0,π](3x^2 + -1x + 5) dx
46 evaluate shell (expected 11/2): 11/2
47 d/dx ∫p dx == p ?: #t
Interpreting the Results
The output confirms several crucial algebraic and calculus behaviors:
- Exact Rational Retention: Notice that
∫[0,1] p dxevaluates to the exact rational fraction11/2rather than5.5. Similarly,∫p dxproduces-1/2x^2, and double integration produces1/4x^4 + -1/6x^3 + 5/2x^2. Exact fractions prevent the accumulation of numerical drift during compound algebraic workflows. - Analytical Gradient Precision: Evaluating
p'(1/6)identifies the critical point where6(1/6) - 1 = 0. The predicate(critical-point? p 1/6)returns#tbecause the derivative is calculated symbolically before substitution. - Calculus Inversion: The identity check
(d/dx ∫p dx == p)confirms that differentiating the antiderivative of a polynomial restores the original polynomial verbatim.
Wrap Up
In this chapter we implemented a symbolic calculus library for single-variable polynomials in Gerbil Scheme. By leveraging Gerbil’s defstruct system and Gambit’s exact numeric tower, we achieved closed-form differentiation and integration without external symbolic algebra packages. The power rule and reverse power rule translate directly into high-order operations on term lists, proving that classic Lisp symbolic computing remains expressive and practical today.
Optional Practice Problems
- Polynomial Multiplication: Implement
(polynomial-multiply p q)indata.ssby computing the Cauchy product of the term lists, multiplying each pair of monomials and combining like terms. - Root Finding with Newton-Raphson: Write a function
(polynomial-find-root poly initial-guess (iterations 20))that uses symbolic differentiation to implement Newton’s method:
.
- Multi-variable Extension: Extend
make-termandmake-polynomialto support multi-variable monomials (e.g.3 x^2 y^3) and implement partial differentiation(partial-differentiate poly var).