RatPoly

public class RatPoly : CustomStringConvertible, Equatable

RatPoly represents an immutable single-variate polynomial expression, as in the examples below.

AbstractState: RatPolys are sums of RatTerms with non-negative exponents.

Example RatPolys include 0, x-10, and x^3-2*x^2+5/3*x+3, and NaN.

Note

See RatNum’s documentation for a definition of immutable.
  • nan

    A constant holding a Not-a-Number (NaN) value of type RatPoly.

    Declaration

    Swift

    public static let nan: RatPoly
  • A constant holding a zero value of type RatPoly.

    Declaration

    Swift

    public static let zero: RatPoly
  • Effects**: Constructs a new Poly, 0.

    Declaration

    Swift

    public init()
  • Requires: term.expt >= 0

    Effects: Constructs a new Poly equal to term. If term.coeff is zero, constructs a 0 polynomial.

    Declaration

    Swift

    public init(term: RatTerm)

    Parameters

    term

    The single term which the new RatPoly equals.

  • Builds a new RatPoly, given a descriptive String. Valid inputs include 0, x-10, and x^3-2*x^2+5/3*x+3, and NaN.

    Requires: ‘polyStr’ is an instance of a string with no spaces that expresses a poly in the form defined in the description computer property.

    Declaration

    Swift

    public convenience init?(_ description: String)

    Parameters

    description

    A string of the format described in the Requires clause.

    Return Value

    a RatPoly p such that p.description = description if description is an instance of a string with no spaces that expresses a poly in the form defined in the description computed property. if description is not valid in that regard, this initializer returns nil.

  • The degree of this RatPoly, which must not be NaN. In other words, the largest exponent with a non-zero coefficient, or 0 if self is 0.

    Requires: !self.isNan

    Note

    It’s a little unusual for a computed property to have a precondition, but you can just assert it as usual at the beginning of the code below.

    Declaration

    Swift

    public var degree: Int { get }
  • true if and only if self has some coefficient = NaN.

    Declaration

    Swift

    public var isNaN: Bool { get }
  • Return the additive inverse of this RatPoly.

    Declaration

    Swift

    public prefix static func - (rhs: RatPoly) -> RatPoly

    Return Value

    a RatPoly equal to 0 - rhs; if rhs.isNaN, returns some r such that r.isNaN

  • Addition operation.

    Declaration

    Swift

    public static func + (lhs: RatPoly, rhs: RatPoly) -> RatPoly

    Return Value

    a RatPoly, r, such that r = lhs + rhs; if lhs.isNaN or rhs.isNaN, returns some r such that r.isNaN

  • Subtraction operation.

    Declaration

    Swift

    public static func - (lhs: RatPoly, rhs: RatPoly) -> RatPoly

    Return Value

    a RatPoly, r, such that r = lhs - rhs; if lhs.isNaN or rhs.isNaN, returns some r such that r.isNaN

  • Multiplication operation.

    Declaration

    Swift

    public static func * (lhs: RatPoly, rhs: RatPoly) -> RatPoly

    Return Value

    a RatPoly, r, such that r = lhs * rhs; if lhs.isNaN or rhs.isNaN, returns some r such that r.isNaN

  • Division operation (truncating).

    Division of polynomials is defined as follows: Given two polynomials u and v, with v != 0, we can divide u by v to obtain a quotient polynomial q and a remainder polynomial r satisfying the condition u = q * v + r, where the degree of r is strictly less than the degree of v, the degree of q is no greater than the degree of u, and r and q have no negative exponents.

    For the purposes of this class, the operation u / v returns q as defined above.

    The following are examples of div’s behavior:

    • x^3-2*x+3 / 3*x^2 = 1/3*x (with r = -2*x+3)
    • x^2+2*x+15 / 2*x^3 = 0 (with r = x^2+2*x+15)
    • x^3+x-1 / x+1 = x^2-x+2 (with r =-3")

    Note that this truncating behavior is similar to the behavior of integer division on computers.

    Declaration

    Swift

    public static func / (lhs: RatPoly, rhs: RatPoly) -> RatPoly

    Return Value

    a RatPoly, r, such that r = lhs / rhs; if lhs.isNaN or rhs.isNaN or rhs == zero, returns some r such that r.isNaN

  • Return the derivative of this RatPoly.

    The derivative of a polynomial is the sum of the derivative of each term.

    Declaration

    Swift

    public func differentiate() -> RatPoly

    Return Value

    a RatPoly, q, such that q = dy/dx, where self == y. In other words, q is the derivative of self. If self.isNaN, then return some q such that q.isNaN

  • Returns the antiderivative of this RatPoly.

    The antiderivative of a polynomial is the sum of the antiderivative of each term plus some constant.

    Declaration

    Swift

    public func antiDifferentiate(withConstant integrationConstant: RatNum) -> RatPoly

    Parameters

    integrationConstant

    The constant of integration to use when computating the antiderivative.

    Return Value

    a RatPoly, q, such that dq/dx = self and the constant of integration is integrationConstant In other words, q is the antiderivative of self. If self.isNaN or integrationConstant.isNaN, then return some q such that q.isNaN.

  • Returns the integral of this RatPoly, integrated from lowerBound to upperBound.

    The Fundamental Theorem of Calculus states that the definite integral of f(x) with bounds a to b is F(b) - F(a) where dF/dx = f(x) NOTE: Remember that the lowerBound can be higher than the upperBound.

    Declaration

    Swift

    public func integrate(from lowerBound: Double, to upperBound: Double) -> Double

    Parameters

    lowerBound

    The lower bound of integration.

    upperBound

    The upper bound of integration.

    Return Value

    a double that is the definite integral of self with bounds of integration between lowerBound and upperBound. If self.isNaN, or either lowerBound or upperBound is Double.NaN, return Double.NaN.

  • Returns the value of this RatPoly, evaluated at d.

    Declaration

    Swift

    public func eval(at d: Double) -> Double

    Parameters

    d

    The value at which to evaluate this polynomial.

    Return Value

    the value of this polynomial when evaluated at ‘d’. For example, x+2 evaluated at 3 is 5, and x^2-x evaluated at 3 is 6. if (self.isNaN), return Double.NaN

  • A String representation of the expression represented by this, with the terms sorted in order of degree from highest to lowest.

    There is no whitespace in the returned string.

    If the polynomial is itself zero, the returned string will just be 0.

    If self.isNaN, then the returned string will be just NaN

    The string for a non-zero, non-NaN poly is in the form (-)T(+|-)T(+|-)…, where (-) refers to a possible minus sign, if needed, and (+|-) refer to either a plus or minus sign, as needed. For each term, T takes the form C*x^E or C*x where C > 0, UNLESS: (1) the exponent E is zero, in which case T takes the form C, or (2) the coefficient C is one, in which case T takes the form x^E or x. In cases were both (1) and (2) apply, (1) is used.

    Valid example outputs include x^17-3/2*x^2+1, -x+1, , and 0.

    Declaration

    Swift

    public var description: String { get }
  • Standard equality operation.

    Declaration

    Swift

    public static func == (lhs: RatPoly, rhs: RatPoly) -> Bool

    Return Value

    true if and only if ‘lhs’ and ‘rhs’ represent the same rational polynomial. Note that all NaN RatPolys are equal.