Computer Algebra Kit (c) 1993,00 by Comp.Alg.Objects.  All Rights Reserved.

Fraction

Inherits from: CAObject

Maturity Index: Relatively immature

Class Description

Fraction objects consist of a numerator object divided by a denominator object.   The numerators and denominators can be arbitrary Computer Algebra Kit objects, but the current implementation has been tested only in the case of total fractions i.e., numerators and denominators are taken as elements of the same set.  For example, a fraction of two integers is a rational number; a fraction of two polynomials is a rational function.

Canonical Form

The arithmetical operations on fractions assume that the fractions are in a simplified (canonical) form, and produce, as result, a fraction in canonical form.  In the case of the integers, or any ordered, integral domain (see inIntegralDomain and inOrderedSet), the numerator and denominator are kept coprime (common gcd is divided out) and the denominator is kept positive.  For example, the fraction minus one, is represented as (-1)/1, and never as 1/(-1).  In the case of an unordered, integral domain, such as polynomials over the integers or over a field, the denominator and numerator are also coprime, and the leading unit of the denominator is equal to one (over fields, this means that the denominator is kept monic, see leadingUnit). For example, the fraction minus x is represented over Z mod 3 as (2 x)/1 and not as x/2.  Finally, for rational functions with floating-point coefficients, no gcd is computed, but the denominator of the fraction is kept monic.

Method types

Creation	- totalFraction:
	- numerator:denominator:
	- copy
	- deepCopy

Identity	- numerator
	- denominator
	- hash
	- isEqual:
	- compare:

Coercion	- asNumerical
	- asModp:
	- asIntegral

Addition	- zero
	- isZero
	- isOpposite:
	- negate
	- double
	- add:
	- subtract:

Multiplication	- one
	- isOne
	- isMinusOne
	- square
	- inverse
	- multiply:
	- divide:
	- power:

Multiplication by Int Value	- intValue:
	- intValue
	- multiplyIntValue:
	- divideIntValue:

Printing	- printsLeadingSign
	- printsSum
	- printsProduct
	- printOn:

Methods

totalFraction:
+totalFraction:aNumerator

Returns a new fraction with numerator set to aNumerator and denominator set to the element one for aNumerator.

numerator:denominator:
+numerator:aNumeratordenominator:aDenominator

Returns a new fraction with numerator set to aNumerator and denominator set to aDenominator. 

copy
-copy

Returns a new copy of the fraction.

deepCopy
-deepCopy

Returns a new copy of the fraction; sends deepCopy messages to denominator and numerator.

numerator
-numerator

Returns a for a fraction a/b. 

denominator
-denominator

Returns b for a fraction a/b. 

hash
- (unsigned)hash

Returns a small integer that is the same for fractions that are equal in the sense of isEqual:.

isEqual:
- (BOOL)isEqual:b

Two fractions are equal if they are pointer equal, or if their numerators and denominators are equal.  If the numerators and denominators are coprime, but not ordered, the method returns YES if numerators and denominators are opposite to each other.  If the fraction is not in canonical form, it is verified that the difference of the two fractions is zero.

compare:
- (int)compare:b

Compares the fractions by comparing the products of numerators and denominators.

asNumerical
-asNumerical

Returns the numerical value of the fraction.  Attempts to divide the numerical value of the numerator and denominator.

asModp:
-asModp:(unsigned short)p

Returns the value of the fraction modulo p.  Attempts to divide the values of the numerator and denominator.  If the division fails, the method generates an error message.

asIntegral
-asIntegral

Returns, if the denominator of the fraction is equal to one, a new reference to the numerator.  Otherwise, the method generates an error message.

zero
-zero

Returns a new fraction with numerator equal to zero.

isZero
- (BOOL)isZero

Whether the numerator of the fraction is equal to zero.

isOpposite:
- (BOOL)isOpposite:b

Two fractions are opposite if their numerators are opposite and the denominators are equal.  However, if the numerators and denominators are coprime, but not ordered, the method returns YES if the numerators are equal and the denominators are opposite to each other.  If the fraction is not in canonical form, it is verified that the sum of the two fractions is zero.

negate
-negate

Returns a new fraction, the opposite of self by negating the numerator.

double
-double

Returns a new fraction, the double of self by doubling the numerator.

add:
-add:b

Returns a new fraction, the sum of self and b.  The method first handles some special cases and then distinguishes between the case that both fractions have a common denominator or not.

subtract:
-subtract:b

Returns a new fraction, the difference of self and b.  The method first handles some special cases and then distinguishes between the case that both fractions have a common denominator or not.

one
-one

Returns a new fraction with numerator equal to one.

isOne
- (BOOL)isOne

Whether the numerator is equal to the denominator.

isMinusOne
- (BOOL)isMinusOne

Whether the numerator is the opposite of the denominator.

square
-square

Returns a new fraction; squares numerator and denominator.

inverse
-inverse

Returns the inverse of the fraction; interchanges, in the case of a total fraction, denominator and numerator.

multiply:
-multiply:b

Returns a new fraction; multiplies the numerators and the denominators together.

divide:
-divide:b

Returns a new fraction by multiplying the fraction by the inverse of b.

power:
-power:(int)n

Returns a new fraction; raises numerator and denominator to the n-th power.

intValue:
-intValue:(int)intValue

Creates a fraction with numerator equal to intValue and denominator one.

intValue
- (int)intValue

Returns intValue of numerator divided by intValue of denominator.

multiplyIntValue:
-multiplyIntValue:(int)intValue

Returns a new fraction by multiplying the numerator by intValue.

divideIntValue:
-divideIntValue:(int)intValue

Returns a new fraction by multiplying the denominator by intValue.

printsLeadingSign
- (BOOL)printsLeadingSign

Whether the fraction prints a leading minus sign.

printsSum
- (BOOL)printsSum

Whether the fraction prints as a sum.

printsProduct
- (BOOL)printsProduct

Whether the fraction prints as a product.

printOn:
-printOn:(IOD)aFile

Prints the fraction to aFile by sending printOn: messages to numerator and denominator.