btm-expressions
Version:
BTM (bowtie-math) is a math object model to enable parsing of mathematical expressions into a tree structure that can be manipulated, evaluated, and compared.
150 lines (125 loc) • 3.71 kB
JavaScript
/*!
* BTM JavaScript Library v@VERSION
* https://github.com/dbrianwalton/BTM
*
* Copyright D. Brian Walton
* Released under the MIT license (https://opensource.org/licenses/MIT)
*
* Date: @DATE
*/
/* *******************************
** Define a class to work with sets using intervals and point sets
******************************* */
import { MathObject } from "./expression.js"
import { exprType, BTM } from "./BTM_root.js"
import { scalar_expr, create_scalar } from "./scalar_expr";
export class math_set extends MathObject {
}
export class real_interval extends math_set {
constructor(a, b, interval_type) {
super();
a = create_scalar(a);
}
if (!(a instanceof scalar_expr)) {
if (!(b instanceof scalar_expr)) {
b = create_scalar(b);
}
this.min = a;
this.max = b;
this.interval_type = interval_type;
this.isEmpty = true;
if (a.value() < b.value() || a.value() == b.value() && interval_type === '[]') {
this.isEmpty = false;
} else {
this.min = undefined;
this.max = undefined;
}
}
equal(other) {
if (typeof other === 'number') {
other = new real_number(other);
}
return (this.value()==other.value());
}
// Add numbers.
add(other) {
if (typeof other === 'number') {
other = new real_number(other);
}
var sum = new real_number(this.number + other.value());
return(sum);
}
// Subtract this - other
subtract(other) {
if (typeof other === 'number') {
other = new real_number(other);
}
var sum = new real_number(this.number - other.value());
return(sum);
}
// Multiply this rational by another rational number and create new object.
multiply(other) {
if (typeof other === 'number') {
other = new real_number(other);
}
var product = new real_number(this.number * other.value());
return(product);
}
// Divide this rational by another rational number and create new object.
divide(other) {
if (typeof other === 'number') {
other = new real_number(other);
}
var product;
if (other.value != 0) {
product = new real_number(this.number / other.value());
} else {
product = new real_number(NaN);
}
return(product);
}
// Additive Inverse
addInverse() {
var inverse = new real_number(-this.number);
return(inverse);
}
// Multiplicative Inverse
multInverse() {
var inverse;
if (this.number != 0) {
inverse = new real_number(this.number);
} else {
inverse = new real_number(NaN);
}
return(inverse);
}
toString(leadSign) {
if (typeof leadSign == 'undefined') {
leadSign = false;
}
var str = (leadSign && this.number>0) ? '+' : '';
if (isNaN(this.number)) {
str = 'NaN';
} else {
str = str + Number(this.number.toFixed(10));
}
return(str);
}
// Format the rational number as TeX string.
toTeX(leadSign) {
if (typeof leadSign == 'undefined') {
leadSign = false;
}
var str = (leadSign && this.number>0) ? '+' : '';
if (isNaN(this.number)) {
str = '\\mathrm{NaN}';
} else {
str = str + Number(this.toString(leadSign));
}
return(str);
}
// Format as a root MathML element.
toMathML(leadSign) {
return("<cn>" + this.toString() + "</cn>");
}
}