RatTerm
public struct RatTerm : Equatable, CustomStringConvertible
RatTerm is an immutable representation of a term in a single-variable polynomial expression. The term has the form C*x^E where C is a rational number and E is an integer.
AbstractState: A RatTerm, t, can be notated by the pair (C . E), where C is the coefficient of t, and E is the exponent of t.
The zero RatTerm, (0 . 0), is the only RatTerm that may have a zero coefficient. For example, (0 . 7) is an invalid RatTerm and an attempt to construct such a RatTerm (through the constructor or arithmetic operations on existing RatTerms) will return the semantically equivalent RatTerm (0 . 0). For example, (1 . 7) + (-1 . 7) = (0 . 0).
(0 . 0), (1 . 0), (1 . 1), (1 . 3), (¾ . 17), (7/2 . -1), and (NaN . 74)
are all valid RatTerms, corresponding to the polynomial terms 0
, 1
, x
,
x^3
, ¾*x^17
, 7/2*x^-1
and NaN*x^74
, respectively.
-
Coefficient of this term.
Declaration
Swift
public let coeff: RatNum
-
Exponent of this term.
Declaration
Swift
public let exponent: Int
-
A constant holding a Not-a-Number (NaN) value of type RatTerm
Declaration
Swift
public static let nan: RatTerm
-
A constant holding a zero value of type RatTerm
Declaration
Swift
public static let zero: RatTerm
-
true if and only if self has NaN as a coefficient.
Declaration
Swift
public var isNaN: Bool { get }
-
true if and only if self has zero as a coefficient.
Declaration
Swift
public var isZero: Bool { get }
-
A String representation of the expression represented by this RatTerm:
- There is no whitespace in the returned string.
- If the term 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 RatTerm is in the form
C*x^E
where C is a valid string representation of aRatNum
and E is an integer. UNLESS:- the exponent E is zero, in which case T takes the form
C
- the exponent E is one, in which case T takes the form
C*x
- the coefficient C is one, in which case T
takes the form
x^E
orx
(if E is one) or1
(if E is zero).
Valid example outputs include
3/2*x^2
,-½
,0
, andNaN
.Declaration
Swift
public var description: String { get }
-
Returns the value of this RatTerm, evaluated at d.
Declaration
Swift
public func eval(_ d: Double) -> Double
Parameters
d
The value at which to evaluate this term.
Return Value
the value of this polynomial when evaluated at ‘d’. For example,
3*x^2
evaluated at 2 is 12. if (self.isNaN == true), return Double.NaN -
Declaration
Swift
public prefix static func - (rhs: RatTerm) -> RatTerm
Return Value
a RatTerm equals to (-rhs). If rhs is NaN, then returns NaN.
-
Addition operation.
Declaration
Swift
public static func + (lhs: RatTerm, rhs: RatTerm) -> RatTerm?
Parameters
lhs
The first operand.
rhs
The second operand.
Return Value
A RatTerm equals to (lhs + rhs), or: if either argument is NaN, then returns NaN, or: if (lhs.expt != rhs.expt) and neither argument is zero or NaN, then returns nil.
-
Subtraction operation.
Declaration
Swift
public static func - (lhs: RatTerm, rhs: RatTerm) -> RatTerm?
Parameters
lhs
The first operand.
rhs
The second operand.
Return Value
A RatTerm equals to (lhs - rhs), or: if either argument is NaN, then returns NaN, or: if (lhs.expt != rhs.expt) and neither argument is zero or NaN, then returns nil.
-
Multiplication operation.
Declaration
Swift
public static func * (lhs: RatTerm, rhs: RatTerm) -> RatTerm
Parameters
lhs
The first operand.
rhs
The second operand.
Return Value
A RatTerm equals to (lhs * rhs). If either argument is NaN, then returns NaN.
-
Division operation.
Declaration
Swift
public static func / (lhs: RatTerm, rhs: RatTerm) -> RatTerm
Parameters
lhs
The first operand.
rhs
The second operand.
Return Value
A RatTerm equals to (lhs / rhs). If either argument is NaN, then returns NaN.
-
Return the derivative of this RatTerm.
Given a term, a*x^b, the derivative of the term is: (a*b)*x^(b-1) for b > 0 and 0 for b == 0 (Do not worry about the case when
b < 0
. The caller of this function, RatPoly, contains a rep. invariant stating that b is never less than 0.)Requires: If self is not NaN, then expt >= 0.
Declaration
Swift
public func differentiate() -> RatTerm
Return Value
a RatTerm that, 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 RatTerm.
Given a term, a*x^b, (where b >= 0) the antiderivative of the term is: a/(b+1)*x^(b+1) (Do not worry about the case when b < 0. The caller of this function, RatPoly, contains a rep. invariant stating that b is never less than 0.)
Requires: If self is not NaN, then expt >= 0.
Declaration
Swift
public func antiDerivative() -> RatTerm
Return Value
a RatTerm, q, such that dq/dx = self where the constant of intergration is assumed to be 0. In other words, q is the antiderivative of self. If self.isNaN, then return some q such that q.isNaN
-
Builds a new RatTerm, given a descriptive String.
Requires: ‘description’ is an instance of a string with no spaces that expresses a RatTerm in the form defined by the
description
property ofRatTerm
Valid inputs include
0
,x
, and-5/3*x^3
, andNaN
.Declaration
Swift
public init?(_ description: String)
Parameters
description
A string of the format described in the @requires clause.
Return Value
a RatTerm t such that t.toString() = description
-
Standard equality operation.
Declaration
Swift
public static func == (lhs: RatTerm, rhs: RatTerm) -> Bool
Return Value
true iff lhs and rhs represent the same RatTerm. Note that all NaN RatTerms are equal.