# Rational

#####

declaration
Rational

MathLib.Rational is the MathLib implementation of rational numbers.

#### Simple use case:

    // Create the rational number 2/3
    var r = new MathLib.Rational(2, 3);

    var Rational = (function () {
    		function Rational (numerator, denominator) {
    			if (typeof denominator === 'undefined') { denominator = 1; }
    			this.type = 'rational';
    			if (MathLib.isZero(denominator)) {
    				MathLib.error({message: 'The denominator cannot be zero.', method: 'Rational.constructor'});
    				throw 'The denominator cannot be zero.';
    			}

    			if ((typeof denominator === 'number' && denominator < 0) || (denominator.type === 'integer' && denominator.sign === '-')) {
    				numerator = MathLib.negative(numerator);
    				denominator = MathLib.negative(denominator);
    			}

    			this.numerator = numerator;
    			this.denominator = denominator;
    		}

# toContentMathML

#####

method
Rational.toContentMathML() → string

A content MathML string representation

    Rational.toContentMathML = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			if (options.strict) {
    				return '<csymbol cd="setname1">Q</csymbol>';
    			}
    			return '<rationals/>';
    		};

# toLaTeX

#####

method
Rational.toLaTeX() → string

A LaTeX string representation

    Rational.toLaTeX = function () {
    			return 'Rational Field $\\mathbb{Q}$';
    		};

# toMathML

#####

method
Rational.toMathML() → string

A presentation MathML string representation

    Rational.toMathML = function () {
    			return '<mrow><mtext>Rational Field</mtext><mi mathvariant="double-struck">Q</mi></mrow>';
    		};

# toString

#####

method
Rational.toString() → string

Custom toString function

    Rational.toString = function () {
    			return 'Rational Field ℚ';
    		};

# coerceTo

#####

method
Rational.prototype.coerceTo() → Integer Rational number Complex

Coerces the rational to some other data type

    Rational.prototype.coerceTo = function (type) {
    			if (type === 'rational') {
    				if (this.denominator === 1) {
    					return new MathLib.Integer(this.numerator);
    				}
    				// TODO: coercion error
    			}

    			if (type === 'rational') {
    				return this.copy();
    			}

    			if (type === 'number') {
    				return this.numerator / this.denominator;
    			}

    			if (type === 'complex') {
    				//		return new MathLib.Complex(this, new MathLib.Rational(0));
    				return new MathLib.Complex(this, 0);
    			}
    		};

# compare

#####

method
Rational.prototype.compare() → number

Option name | Type | Description
--- | --- | ---
rational | Rational | The number to compare

Compares two rational numbers

    Rational.prototype.compare = function (rational) {
    			return MathLib.sign(this.numerator * rational.denominator - this.denominator * rational.numerator);
    		};

# copy

#####

method
Rational.prototype.copy() → Rational

Copy the rational number

    Rational.prototype.copy = function () {
    			return new MathLib.Rational(MathLib.copy(this.numerator), MathLib.copy(this.denominator));
    		};

# divide

#####

method
Rational.prototype.divide() → Rational

Option name | Type | Description
--- | --- | ---
divisor | Rational number | The divisor

Divides rational numbers

    Rational.prototype.divide = function (divisor) {
    			if (divisor.type === 'rational') {
    				return new MathLib.Rational(MathLib.times(this.numerator, divisor.denominator), MathLib.times(this.denominator, divisor.numerator));
    			}
    			else if (typeof divisor === 'number') {
    				return new MathLib.Rational(this.numerator, MathLib.times(this.denominator, divisor));
    			}
    			else {
    				return divisor.inverse().times(this);
    			}
    		};

# inverse

#####

method
Rational.prototype.inverse() → Rational

Calculates the inverse of a rational number

    Rational.prototype.inverse = function () {
    			if (!MathLib.isZero(this.numerator)) {
    				return new MathLib.Rational(this.denominator, this.numerator);
    			}
    		};

# isEqual

#####

method
Rational.prototype.isEqual() → boolean

Option name | Type | Description
--- | --- | ---
n | Integer Rational number Complex | The number to compare

Checks if the rational number is equal to an other number

    Rational.prototype.isEqual = function (n) {
    			if (n.type !== 'rational') {
    				return MathLib.isEqual.apply(null, MathLib.coerce(this, n));
    			}
    			else {
    				return MathLib.isEqual(MathLib.times(this.numerator, n.denominator), MathLib.times(this.denominator, n.numerator));
    			}
    		};

# isZero

#####

method
Rational.prototype.isZero() → boolean

Checks if the rational number is zero

    Rational.prototype.isZero = function () {
    			return MathLib.isZero(this.numerator);
    		};

# minus

#####

method
Rational.prototype.minus() → Rational

Option name | Type | Description
--- | --- | ---
subtrahend | Rational number | The number to be subtracted

Subtracts rational numbers

    Rational.prototype.minus = function (subtrahend) {
    			if (subtrahend.type !== 'rational') {
    				return MathLib.minus.apply(null, MathLib.coerce(this, subtrahend));
    			}
    			else {
    				return new MathLib.Rational(MathLib.minus(MathLib.times(this.numerator, subtrahend.denominator), MathLib.times(this.denominator, subtrahend.numerator)), MathLib.times(this.denominator, subtrahend.denominator));
    			}
    		};

# negative

#####

method
Rational.prototype.negative() → Rational

Calculates the negative of a rational number

    Rational.prototype.negative = function () {
    			return new MathLib.Rational(-this.numerator, this.denominator);
    		};

# plus

#####

method
Rational.prototype.plus() → Rational number Complex

Option name | Type | Description
--- | --- | ---
summand | Integer Rational number Complex | The number to be added

Adds rational numbers

    Rational.prototype.plus = function (summand) {
    			if (summand.type !== 'rational') {
    				return MathLib.plus.apply(null, MathLib.coerce(this, summand));
    			}
    			else {
    				return new MathLib.Rational(MathLib.plus(MathLib.times(this.denominator, summand.numerator), MathLib.times(this.numerator, summand.denominator)), MathLib.times(this.denominator, summand.denominator));
    			}
    		};

# reduce

#####

method
Rational.prototype.reduce() → Rational

Reduces the rational number

    Rational.prototype.reduce = function () {
    			var gcd = MathLib.sign(this.denominator) * MathLib.gcd([this.numerator, this.denominator]);
    			return new MathLib.Rational(this.numerator / gcd, this.denominator / gcd);
    		};

# times

#####

method
Rational.prototype.times() → Rational

Option name | Type | Description
--- | --- | ---
factor | Rational number | The number to be multiplied

Multiplies rational numbers

    Rational.prototype.times = function (factor) {
    			if (factor.type === 'rational') {
    				return new MathLib.Rational(MathLib.times(this.numerator, factor.numerator), MathLib.times(this.denominator, factor.denominator));
    			}
    			else if (typeof factor === 'number') {
    				return new MathLib.Rational(MathLib.times(this.numerator, factor), this.denominator);
    			}
    			else {
    				return factor.times(this);
    			}
    		};

# toContentMathML

#####

method
Rational.prototype.toContentMathML() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns the Content MathML representation of the rational number

    Rational.prototype.toContentMathML = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			var base;

    			if (options.strict) {
    				return '<apply><csymbol cd="nums1">rational</csymbol>' + MathLib.toContentMathML(this.numerator, options) + MathLib.toContentMathML(this.denominator, options) + '</apply>';
    			}
    			else {
    				base = (options.base || 10);
    				return '<cn type="rational"' + ((base !== 10) ? ' base="' + base + '"' : '') + '>' + MathLib.toString(this.numerator, {base: base}) + '<sep/>' + MathLib.toString(this.denominator, {base: base}) + '</cn>';
    			}
    		};

# toLaTeX

#####

method
Rational.prototype.toLaTeX() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns the LaTeX representation of the rational number

    Rational.prototype.toLaTeX = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			var numerator, str = '', passOptions = {base: options.base, baseSubscript: options.baseSubscript};

    			if (options.sign) {
    				str = MathLib.toString(this.numerator, {sign: true}).slice(0, 1);
    				numerator = MathLib.toLaTeX(MathLib.abs(this.numerator), passOptions);
    			}
    			else {
    				numerator = MathLib.toLaTeX(this.numerator, passOptions);
    			}

    			return str + '\\frac{' + numerator + '}{' + MathLib.toLaTeX(this.denominator, passOptions) + '}';
    		};

# toMathML

#####

method
Rational.prototype.toMathML() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns the MathML representation of the rational number

    Rational.prototype.toMathML = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			var numerator, str = '', passOptions = {base: options.base, baseSubscript: options.baseSubscript};

    			if (options.sign) {
    				str = '<mo>' + MathLib.toString(this.numerator, {sign: true}).slice(0, 1) + '</mo>';
    				numerator = MathLib.toMathML(MathLib.abs(this.numerator), passOptions);
    			}
    			else {
    				numerator = MathLib.toMathML(this.numerator, passOptions);
    			}

    			return str + '<mfrac>' + numerator + MathLib.toMathML(this.denominator, passOptions) + '</mfrac>';
    		};

# toString

#####

method
Rational.prototype.toString() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Custom toString function

    Rational.prototype.toString = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			return MathLib.toString(this.numerator, options) + '/' + MathLib.toString(this.denominator, {base: options.base, baseSubscript: options.baseSubscript});
    		};
    		return Rational;
    	})();
    	MathLib.Rational = Rational;
    })(MathLib || (MathLib = {}));