cox.jmatt.java.MathTools
Class MRadical

java.lang.Object
  extended by cox.jmatt.java.MathTools.MRadical

public class MRadical
extends java.lang.Object

The MRadical class provides an easy way to create and present radicals without requiring double approximation or other theatrics. Once created, MRadicals are immutable. The sign, radicand, and index can be examined but not changed. MRadical also has methods for comparing two MRadicals. Apart from these methods MRadical is primarily a formatting class.

The true power of this class is in the format() method. It provides tokens for the 'raw' radical as well as the simplified radical. If a radicand can be simplified, i.e. it contains a perfect-power factor, the perfect power is factored out, represented as a coefficient to the radical, and the radicand reduced appropriately. This is an automatic part of the formatting process.


Field Summary
static java.lang.String FORMAT_DEBUG
          Format string for debugging purposes.
static java.lang.String FORMAT_LATEX
          LaTeX raw form.
static java.lang.String FORMAT_LATEX_SIMP
          LaTeX simplified.
static java.lang.String FORMAT_OO
          OpenOffice.org, raw form.
static java.lang.String FORMAT_OO_SIMP
          OpenOffice.org, simplified .
static java.lang.String FORMAT_STANDARD
          Standard raw form for radicals: radical(index, radicand).
static java.lang.String FORMAT_STANDARD_SIMP
          Standard simplified form for radicals: coef radical(index, simp.
 
Constructor Summary
MRadical()
          Standard script-exposure constructor.
MRadical(int pIndex, int pRadicand)
          Constructor to specify all critical parts of the radical: radicand, and index.
MRadical(MRadical pRadical)
          Copy constructor.
 
Method Summary
 int absRadicand()
          Get the absolute value of the radicand.
 int compareTo(MRadical pRadical)
          Compare this MRadical to another.
static MRadical createMRadical(int pRadicand)
          Static method to create a square root.
static MRadical createMRadical(int pIndex, int pRadicand)
          Static method to create a MRadical given its radicand and index.
static MRadical createMRadical(int pCoef, int pIndex, int pRadicand)
          Method to construct a MRadical with a 'coefficient,' e.g.
 boolean equals(MRadical pRadical)
          Check to see if the two MRadicals represent the same value.
 java.lang.String format()
          Format this MRadical per its currently-set format String.
 java.lang.String format(java.lang.String pTemplate)
          Format this MRadical per the template supplied.
 boolean identicalTo(MRadical pRadical)
          This method tests to see if the two MRadicals are exactly identical, not just equal in value.
 int index()
          Get the value of the index.
 MRadical newMRadical(int pRadicand)
          Instance method to create a square root.
 MRadical newMRadical(int pIndex, int pRadicand)
          Instance method to create a new MRadical.
 MRadical newMRadical(int pCoef, int pIndex, int pRadicand)
          Instance version of the three-int factory method.
 int radicand()
          Get the value of the radicand.
 java.lang.String sCompareTo(MRadical pRadical)
          Compare this MRadical to another but return a single-character String: '<', '=' or '>'.
static void setDefaultFormat(java.lang.String pFormat)
          Set the default format for all MRadicals created after this method is called.
 void setFormat(java.lang.String pFormat)
          Set the format String for this MRadical.
 void setNewDefaultFormat(java.lang.String pFormat)
          Instance version of setDefaultFormat().
 int signum()
          Return the sign of the MRadical: -1 if negative, 0 if zero, +1 if positive.
 double toDouble()
          Evaluate the radical as a double.
 java.lang.String toMathML()
          Express the radical using MathML.
 java.lang.String toString()
          This method returns a constructor-like representation of the MRadical.
 java.lang.String toXML(java.lang.String pID)
          Express the radical in simplified XML notation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FORMAT_DEBUG

public static final java.lang.String FORMAT_DEBUG
Format string for debugging purposes. NOT an official format!

See Also:
Constant Field Values

FORMAT_STANDARD

public static final java.lang.String FORMAT_STANDARD
Standard raw form for radicals: radical(index, radicand).

See Also:
Constant Field Values

FORMAT_STANDARD_SIMP

public static final java.lang.String FORMAT_STANDARD_SIMP
Standard simplified form for radicals: coef radical(index, simp. radicand).

See Also:
Constant Field Values

FORMAT_OO

public static final java.lang.String FORMAT_OO
OpenOffice.org, raw form.

See Also:
Constant Field Values

FORMAT_OO_SIMP

public static final java.lang.String FORMAT_OO_SIMP
OpenOffice.org, simplified .

See Also:
Constant Field Values

FORMAT_LATEX

public static final java.lang.String FORMAT_LATEX
LaTeX raw form.

See Also:
Constant Field Values

FORMAT_LATEX_SIMP

public static final java.lang.String FORMAT_LATEX_SIMP
LaTeX simplified.

See Also:
Constant Field Values
Constructor Detail

MRadical

public MRadical()
Standard script-exposure constructor. The default MRadical is zero, or, the first root of zero.


MRadical

public MRadical(int pIndex,
                int pRadicand)
Constructor to specify all critical parts of the radical: radicand, and index. Negative values for the index are made positive. If the radicand is negative and the index is even, the default radical is created. If the index is zero, the radical becomes 1.

Parameters:
pRadicand - The radicand.
pIndex - The index.

MRadical

public MRadical(MRadical pRadical)
Copy constructor.

Method Detail

createMRadical

public static MRadical createMRadical(int pIndex,
                                      int pRadicand)
Static method to create a MRadical given its radicand and index.


newMRadical

public MRadical newMRadical(int pIndex,
                            int pRadicand)
Instance method to create a new MRadical.


createMRadical

public static MRadical createMRadical(int pRadicand)
Static method to create a square root. Index is automatically 2!


newMRadical

public MRadical newMRadical(int pRadicand)
Instance method to create a square root.


createMRadical

public static MRadical createMRadical(int pCoef,
                                      int pIndex,
                                      int pRadicand)
Method to construct a MRadical with a 'coefficient,' e.g. 2 sqrt(5) which is equivalent to sqrt(20).

Parameters:
pCoef - The number multiplied onto the radical.
pIndex - The index (root) of the radical.
pRadicand - The radicand.

newMRadical

public MRadical newMRadical(int pCoef,
                            int pIndex,
                            int pRadicand)
Instance version of the three-int factory method.


toString

public java.lang.String toString()
This method returns a constructor-like representation of the MRadical.

Overrides:
toString in class java.lang.Object

toDouble

public double toDouble()
Evaluate the radical as a double.


radicand

public int radicand()
Get the value of the radicand.


absRadicand

public int absRadicand()
Get the absolute value of the radicand.


index

public int index()
Get the value of the index.


signum

public int signum()
Return the sign of the MRadical: -1 if negative, 0 if zero, +1 if positive.


equals

public boolean equals(MRadical pRadical)
Check to see if the two MRadicals represent the same value. It checks actual value and NOT identity, so the square root of four (2) WILL test as equal to the cube root of 8 (also 2).


identicalTo

public boolean identicalTo(MRadical pRadical)
This method tests to see if the two MRadicals are exactly identical, not just equal in value.


compareTo

public int compareTo(MRadical pRadical)
Compare this MRadical to another. Returns -1 if this is less than pRadical, 0 if both are equal, and 1 if this is greater than pRadical.

Throws:
java.lang.NullPointerException - If pRadical is null.

sCompareTo

public java.lang.String sCompareTo(MRadical pRadical)
Compare this MRadical to another but return a single-character String: '<', '=' or '>'. This method will return null if the argument is null.


format

public java.lang.String format(java.lang.String pTemplate)

Format this MRadical per the template supplied. This is a standard Question.fillTemplate() with the following tokens supplied:

NOTE: Tokens 7 to 11 result from the radical being simplified. The greatest 'perfect power' of the index is factored out of the radicand and interpreted as a coefficient for the radical expression. If there is no perfect power factor inside the radicand, the simplified tokens equal their unsimplified equivalents.

MRadical maintains the sign separate from the radicand itself. Tokens 10 and 11 reflect this through the simplification process. The negative sign, if present, appears on the coefficient. When using the simplified tokens, if there is a chance the radicand might be negative token 2 should be used for the actual radicand, otherwise the negative sign may show up twice!

Parameters:
pTemplate - The template to use when formatting the MRadical. If null or empty, FORMAT_STANDARD is used.
Returns:
The freshly-formatted MRadical.

toMathML

public java.lang.String toMathML()
Express the radical using MathML. No formatting is done, so it's just the radicand and the index. If the index is 2, <msqrt> tags are used, otherwise the tags are <mroot>.


toXML

public java.lang.String toXML(java.lang.String pID)
Express the radical in simplified XML notation. It is an empty <MRadical> tag with attributes 'radicand=' and 'index='.


setFormat

public void setFormat(java.lang.String pFormat)
Set the format String for this MRadical. A blank or null value resets to the default format.


setDefaultFormat

public static void setDefaultFormat(java.lang.String pFormat)
Set the default format for all MRadicals created after this method is called. An empty or null value resets to FORMAT_STANDARD.


setNewDefaultFormat

public void setNewDefaultFormat(java.lang.String pFormat)
Instance version of setDefaultFormat().


format

public java.lang.String format()
Format this MRadical per its currently-set format String.