This chapter aims to walk through the steps of creating a reasonably complex module for polynomials. This builds on the parametric data type first introduced using polynomials in Section 12.3, the modules and test suite in Chapter 23 and the ability to parse strings found in Chapter 26. The goal is to use all of these pieces to build up to a module that has quite a bit of functionality of polynomials.
module Poly
export Polynomial
struct Polynomial{T <: Number}
coeffs::Vector{T}
end
function Base.show(io::IO, p::Polynomial)
print(io, strip(reduce((str, n) -> "$str $(n==1 ? "" : "+") $(p.coeffs[n]) x^$(n-1)", 1:length(p.coeffs), init="")))
end
end # of the module Poly
Note we have called the module Poly to not get ourselves confused over the struct Polynomial. Place this code into a file called Poly.jl. Recall that we also developed code for addition/subtraction and scalar multiplication. We will add this later. Also, we have adapted the Base.show command to trim the ends of the string of any whitespace. We will load the Revise package and then load this file as
where it is important that we use the includet function from the Revise package. Recall that this allows us to update the package and then reuse structs and functions in the package without restarting the kernel. Test that this is working with
We are going to start testing the module immediately. Here’s a fair straightforward test suite that just creates the polynomial and tests each are the correct type:
where this time we use the include command. We don’t need the includet since this is not a module that needs to be tracked. Upon running this, you should see:
This test code creates 6 polynomials of different types and then tests that each is the correct type. Note that for poly6, the array of coefficients is a mix of integers, floats and rationals, but Julia automatically changes it to an array of Floats.
The other part of the code that we have included in the module is that of the Base.show command. Recall that this is usually called when producing output, however it is also called when a polynomial is wrapped with the string method. For example,
and running the test code results in a all passing. The stringificaiton of poly4 is a bit of a problem because the complex numbers should be wrapped in parentheses.
The standard for developing code today falls into what is called Test Driven Development or TDD. In short, this means that before any code is written, the test suites are created. For developers that have never done this before, it is very difficult way to develop because developers typically just want to write code--why test? We’ll show how this is done for the rest of the chapter and show how it can be beneficial.
Section47.5Tests for Addition, Subtraction and Multiplication
We first start with tests for Addition, subtraction, scalar multiplication and polynomial multiplication. We have already written this code earlier, but we’ll write the tests before including the code. We’ll start with handling polynomials with integer coefficients. If we start with the following polynomials
Although we have a constructor for creating a polynomial from the coefficients, it’s also nice to have a constructor that parses a string. For example, if we have
After defining these, each of these should then be tested for constructions and other operations (stringification, addition, subtraction and multiplication). All of these can be found here.