State of Mathlify Expression class
Mar 4 2024
Project status
The Expression class in Mathlify is now capable of handling most algebraic manipulations seen at the lower secondary level so I thought it will be a good time to try to clean up the code base and API and jot down some thoughts I have about it.
The base ExpressionTypes
We have the current ExpressionTypes implemented
ProductSumQuotientExponentFnBracket
NumeralFraction
Variable
In the future, I foresee extending the Fn and Numeral types to include
functions like square roots and logarithms for the former, and floats for the
latter.
An Expression is a wrapper around these base ExpressionTypes, with methods
on it to manipulate it. We build up a tree from there: the Numeral and
Variable types are the leaf nodes, and all other types contain sub-expressions
as children.
- The
Productnode is made up of acoeff(of theNumeraltype) andfactors(an array ofExpression) - The
Sumnode is made up ofterms(an array ofExpression) - The
Quotientnode is made up of anumandden(both anExpression) - The
Exponentnode is made up of abaseandpower(both anExpression) - The
Fnnode contains aBracketnode with contains a singleExpressionnode
Naming convention
Mutation
Expression and ExpressionType are equipped with a simplify method that
will mutate the current instance.
All other public methods should not mutate the instance but instead return new instances if necessary.
Internal or experimental methods that mutate the instance will have a trailing underscore.
Internal vs external vs experimental
Methods and properties that are considered "public" will be camelCased. We aspire to maintain semantic versioning on these when we hit 1.0.
Methods and properties that are considered as only for internal use have a leading underscore and will be snake_cased. These methods should be used with care as they may be subject to change any time. Methods that mutate the current instance will end with an underscore as well.
"Experimental" methods and properties subject to redesign in the future will have a leading underscore and be camelCased.
Common methods
Constructors
Constructors will accept only numbers, strings or Expression. Type coercions
will be pushed to the macros module. In fact, we probably should only ship
these macros and only ship types for the Expression and ExpressionType
classes.
toString
All classes have a toString method that return markup strings.
valueOf
The Numeral and Fraction class also has the valueOf method that return the
JavaScript number type.
simplify
All Expression and ExpressionType classes implement the simplify method
that mutates the current instance.
clone
All classes also implement the clone method.
Finalized Expression class APIs
Properties
node:ExpressionType
Methods
expand()combineFraction()works onExpressionwith aSumnodefactorize.commonFactor()works onExpressionwith aSumnodenegative()subIn(scope)
Static methods to ponder over
We currently have static methods gcd() and lcm() on the Expression class.
However, if we no longer ship the class, we should ship these functions
separately under a different namespace.