2020-12-22 00:01:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* Logarithm Graph Creator - Create graphs with logarithm scales.
|
|
|
|
|
* Copyright (C) 2020 Ad5001
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
.pragma library
|
|
|
|
|
|
|
|
|
|
.import "expr-eval.js" as ExprEval
|
2020-12-22 15:47:48 +00:00
|
|
|
|
.import "utils.js" as Utils
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
2020-12-25 19:42:13 +00:00
|
|
|
|
|
2020-12-23 23:06:52 +00:00
|
|
|
|
var evalVariables = { // Variables not provided by expr-eval.js, needs to be provided manualy
|
|
|
|
|
"pi": Math.PI,
|
|
|
|
|
"π": Math.PI,
|
|
|
|
|
"inf": Infinity,
|
|
|
|
|
"Infinity": Infinity,
|
|
|
|
|
"∞": Infinity,
|
2020-12-25 19:42:13 +00:00
|
|
|
|
"e": Math.E,
|
|
|
|
|
"E": Math.E
|
2020-12-23 23:06:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 19:48:11 +00:00
|
|
|
|
var currentVars = {}
|
|
|
|
|
|
|
|
|
|
const parser = new ExprEval.Parser()
|
|
|
|
|
parser.functions.integral = function(a, b, f, variable) {
|
|
|
|
|
// https://en.wikipedia.org/wiki/Simpson%27s_rule
|
|
|
|
|
f = parser.parse(f).toJSFunction(variable, currentVars)
|
|
|
|
|
return (b-a)/6*(f(a)+4*f((a+b)/2)+f(b))
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
class Expression {
|
|
|
|
|
constructor(expr) {
|
|
|
|
|
this.expr = expr
|
|
|
|
|
this.calc = parser.parse(expr).simplify()
|
2020-12-23 23:06:52 +00:00
|
|
|
|
this.cached = this.isConstant()
|
|
|
|
|
this.cachedValue = this.cached ? this.calc.evaluate(evalVariables) : null
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConstant() {
|
2020-12-26 18:16:42 +00:00
|
|
|
|
return !this.expr.includes("x") && !this.expr.includes("n")
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
execute(x = 1) {
|
2020-12-23 23:06:52 +00:00
|
|
|
|
if(this.cached) return this.cachedValue
|
2021-01-10 19:48:11 +00:00
|
|
|
|
currentVars = Object.assign({'x': x}, evalVariables)
|
|
|
|
|
return this.calc.evaluate(currentVars)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
simplify(x) {
|
2020-12-23 23:06:52 +00:00
|
|
|
|
var expr = this.calc.substitute('x', x).simplify()
|
|
|
|
|
if(expr.evaluate(evalVariables) == 0) return '0'
|
2021-03-08 22:12:10 +00:00
|
|
|
|
var str = Utils.makeExpressionReadable(expr.toString());
|
|
|
|
|
if(str != undefined && str.match(/^\d*\.\d+$/)) {
|
|
|
|
|
if(str.split('.')[1].split('0').length > 7) {
|
|
|
|
|
// Likely rounding error
|
|
|
|
|
str = parseFloat(str.substring(0, str.length-1)).toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return str
|
2020-12-22 23:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
duplicate() {
|
|
|
|
|
return new Expression(this.toEditableString())
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
toEditableString() {
|
|
|
|
|
return this.calc.toString()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 15:47:48 +00:00
|
|
|
|
toString(forceSign=false) {
|
2020-12-25 19:42:13 +00:00
|
|
|
|
var str = Utils.makeExpressionReadable(this.calc.toString())
|
2020-12-22 15:47:48 +00:00
|
|
|
|
if(str[0] != '-' && forceSign) str = '+' + str
|
2020-12-22 00:01:36 +00:00
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
function executeExpression(expr){
|
|
|
|
|
return (new Expression(expr.toString())).execute()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 19:42:13 +00:00
|
|
|
|
class Sequence extends Expression {
|
2020-12-25 22:42:31 +00:00
|
|
|
|
constructor(name, baseValues = {}, valuePlus = 1, expr = "") {
|
|
|
|
|
// u[n+valuePlus] = expr
|
2020-12-25 19:42:13 +00:00
|
|
|
|
super(expr)
|
|
|
|
|
this.name = name
|
|
|
|
|
this.baseValues = baseValues
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.calcValues = Object.assign({}, baseValues)
|
|
|
|
|
for(var n in this.calcValues)
|
|
|
|
|
if(['string', 'number'].includes(typeof this.calcValues[n]))
|
2021-01-05 09:54:59 +00:00
|
|
|
|
this.calcValues[n] = parser.parse(this.calcValues[n].toString()).simplify().evaluate(evalVariables)
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.valuePlus = parseInt(valuePlus)
|
2020-12-25 19:42:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConstant() {
|
|
|
|
|
return this.expr.indexOf("n") == -1
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 18:16:42 +00:00
|
|
|
|
execute(n = 1) {
|
|
|
|
|
if(n in this.calcValues)
|
2021-01-05 09:54:59 +00:00
|
|
|
|
return this.calcValues[n]
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.cache(n)
|
2021-01-05 09:54:59 +00:00
|
|
|
|
return this.calcValues[n]
|
2020-12-26 18:16:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simplify(n = 1) {
|
|
|
|
|
if(n in this.calcValues)
|
|
|
|
|
return Utils.makeExpressionReadable(this.calcValues[n].toString())
|
|
|
|
|
this.cache(n)
|
|
|
|
|
return Utils.makeExpressionReadable(this.calcValues[n].toString())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache(n = 1) {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
var str = Utils.simplifyExpression(this.calc.substitute('n', n-this.valuePlus).toString())
|
2020-12-26 18:16:42 +00:00
|
|
|
|
var expr = parser.parse(str).simplify()
|
2021-01-10 19:48:11 +00:00
|
|
|
|
var l = {'n': n-this.valuePlus} // Just in case, add n (for custom functions)
|
2021-01-05 09:54:59 +00:00
|
|
|
|
l[this.name] = this.calcValues
|
2021-01-10 19:48:11 +00:00
|
|
|
|
currentVars = Object.assign(l, evalVariables)
|
|
|
|
|
this.calcValues[n] = expr.evaluate(currentVars)
|
2020-12-26 18:16:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString(forceSign=false) {
|
|
|
|
|
var str = Utils.makeExpressionReadable(this.calc.toString())
|
|
|
|
|
if(str[0] != '-' && forceSign) str = '+' + str
|
|
|
|
|
var subtxt = this.valuePlus == 0 ? 'ₙ' : Utils.textsub('n+' + this.valuePlus)
|
|
|
|
|
var ret = `${this.name}${subtxt} = ${str}${this.baseValues.length == 0 ? '' : "\n"}`
|
|
|
|
|
ret += Object.keys(this.baseValues).map(
|
|
|
|
|
n => `${this.name}${Utils.textsub(n)} = ${this.baseValues[n]}`
|
|
|
|
|
).join('; ')
|
|
|
|
|
return ret
|
2020-12-25 19:42:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 18:35:38 +00:00
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
// Domains
|
2020-12-25 00:20:57 +00:00
|
|
|
|
class Domain {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
|
|
includes(x) { return false }
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
toString() { return '???' }
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
union(domain) { return domain }
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
intersection(domain) { return this }
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
static import(frm) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
switch(frm.trim().toUpperCase()) {
|
|
|
|
|
case "R":
|
|
|
|
|
case "ℝ":
|
|
|
|
|
return Domain.R
|
|
|
|
|
break;
|
|
|
|
|
case "RE":
|
|
|
|
|
case "R*":
|
|
|
|
|
case "ℝ*":
|
|
|
|
|
return Domain.RE
|
|
|
|
|
break;
|
|
|
|
|
case "RP":
|
|
|
|
|
case "R+":
|
|
|
|
|
case "ℝ⁺":
|
|
|
|
|
return Domain.RP
|
|
|
|
|
break;
|
|
|
|
|
case "RM":
|
|
|
|
|
case "R-":
|
|
|
|
|
case "ℝ⁻":
|
|
|
|
|
return Domain.RM
|
|
|
|
|
break;
|
|
|
|
|
case "RPE":
|
|
|
|
|
case "REP":
|
|
|
|
|
case "R+*":
|
|
|
|
|
case "R*+":
|
|
|
|
|
case "ℝ*⁺":
|
|
|
|
|
case "ℝ⁺*":
|
|
|
|
|
return Domain.RPE
|
|
|
|
|
break;
|
|
|
|
|
case "RME":
|
|
|
|
|
case "REM":
|
|
|
|
|
case "R-*":
|
|
|
|
|
case "R*-":
|
|
|
|
|
case "ℝ⁻*":
|
|
|
|
|
case "ℝ*⁻":
|
|
|
|
|
return Domain.RME
|
|
|
|
|
break;
|
2020-12-25 18:30:19 +00:00
|
|
|
|
case "ℕ":
|
|
|
|
|
case "N":
|
|
|
|
|
case "ZP":
|
|
|
|
|
case "ℤ⁺":
|
|
|
|
|
return Domain.N
|
|
|
|
|
break;
|
2020-12-25 18:55:47 +00:00
|
|
|
|
case "NLOG":
|
|
|
|
|
case "ℕˡᵒᵍ":
|
|
|
|
|
return Domain.NLog
|
|
|
|
|
break;
|
2020-12-25 18:30:19 +00:00
|
|
|
|
case "NE":
|
|
|
|
|
case "NP":
|
|
|
|
|
case "N*":
|
|
|
|
|
case "N+":
|
|
|
|
|
case "ℕ*":
|
|
|
|
|
case "ℕ⁺":
|
|
|
|
|
case "ZPE":
|
|
|
|
|
case "ZEP":
|
|
|
|
|
case "Z+*":
|
|
|
|
|
case "Z*+":
|
|
|
|
|
case "ℤ⁺*":
|
|
|
|
|
case "ℤ*⁺":
|
|
|
|
|
return Domain.NE
|
|
|
|
|
break;
|
|
|
|
|
case "Z":
|
|
|
|
|
case "ℤ":
|
|
|
|
|
return Domain.Z
|
|
|
|
|
break;
|
|
|
|
|
case "ZM":
|
|
|
|
|
case "Z-":
|
|
|
|
|
case "ℤ⁻":
|
|
|
|
|
return Domain.ZM
|
|
|
|
|
break;
|
|
|
|
|
case "ZME":
|
|
|
|
|
case "ZEM":
|
|
|
|
|
case "Z-*":
|
|
|
|
|
case "Z*-":
|
|
|
|
|
case "ℤ⁻*":
|
|
|
|
|
case "ℤ*⁻":
|
|
|
|
|
return Domain.ZME
|
|
|
|
|
break;
|
|
|
|
|
case "ZE":
|
|
|
|
|
case "Z*":
|
|
|
|
|
case "ℤ*":
|
|
|
|
|
return Domain.ZE
|
|
|
|
|
break;
|
2020-12-22 00:01:36 +00:00
|
|
|
|
default:
|
2020-12-25 18:30:19 +00:00
|
|
|
|
return new EmptySet()
|
2020-12-22 00:01:36 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-25 00:20:57 +00:00
|
|
|
|
|
|
|
|
|
class EmptySet extends Domain {
|
|
|
|
|
|
|
|
|
|
includes(x) { return false }
|
|
|
|
|
|
|
|
|
|
toString() { return "∅" }
|
|
|
|
|
|
|
|
|
|
union(domain) { return domain }
|
|
|
|
|
|
|
|
|
|
intersection(domain) { return this }
|
|
|
|
|
|
|
|
|
|
static import(frm) { return new EmptySet() }
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 13:49:48 +00:00
|
|
|
|
class Range extends Domain {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
constructor(begin, end, openBegin, openEnd) {
|
|
|
|
|
super()
|
|
|
|
|
if(typeof begin == 'number' || typeof begin == 'string') begin = new Expression(begin.toString())
|
|
|
|
|
this.begin = begin
|
|
|
|
|
if(typeof end == 'number' || typeof end == 'string') end = new Expression(end.toString())
|
|
|
|
|
this.end = end
|
|
|
|
|
this.openBegin = openBegin
|
|
|
|
|
this.openEnd = openEnd
|
|
|
|
|
this.displayName = (openBegin ? "]" : "[") + begin.toString() + ";" + end.toString() + (openEnd ? "[" : "]")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(x) {
|
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
|
|
|
|
return ((this.openBegin && x > this.begin.execute()) || (!this.openBegin && x >= this.begin.execute())) &&
|
|
|
|
|
((this.openEnd && x < this.end.execute()) || (!this.openEnd && x <= this.end.execute()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
return this.displayName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
union(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return this
|
|
|
|
|
if(domain instanceof DomainSet) return domain.union(this)
|
|
|
|
|
if(domain instanceof UnionDomain) return domain.union(this)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return new UnionDomain(this, domain)
|
|
|
|
|
if(domain instanceof MinusDomain) return new UnionDomain(this, domain)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) return new UnionDomain(this, domain)
|
2020-12-25 00:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intersection(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return domain
|
|
|
|
|
if(domain instanceof DomainSet) return domain.intersection(this)
|
|
|
|
|
if(domain instanceof UnionDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return domain.intersection(this)
|
|
|
|
|
if(domain instanceof MinusDomain) return new IntersectionDomain(this, domain)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) return new IntersectionDomain(this, domain)
|
2020-12-25 00:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static import(frm) {
|
|
|
|
|
var openBegin = frm.trim().charAt(0) == "]"
|
|
|
|
|
var openEnd = frm.trim().charAt(frm.length -1) == "["
|
|
|
|
|
var [begin, end] = frm.substr(1, frm.length-2).split(";")
|
2020-12-26 13:49:48 +00:00
|
|
|
|
return new Range(begin.trim(), end.trim(), openBegin, openEnd)
|
2020-12-25 00:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
2020-12-25 18:30:19 +00:00
|
|
|
|
class SpecialDomain extends Domain {
|
|
|
|
|
// For special domains (N, Z...)
|
|
|
|
|
// isValidExpr is function returning true when number is in domain
|
|
|
|
|
// false when it isn't.
|
|
|
|
|
// nextValue provides the next positive value in the domain
|
|
|
|
|
// after the one given.
|
|
|
|
|
constructor(displayName, isValid, next = x => true, previous = x => true,
|
|
|
|
|
moveSupported = true) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
super()
|
2020-12-25 18:30:19 +00:00
|
|
|
|
this.displayName = displayName
|
|
|
|
|
this.isValid = isValid
|
|
|
|
|
this.nextValue = next
|
|
|
|
|
this.prevValue = previous
|
|
|
|
|
this.moveSupported = moveSupported
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(x) {
|
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
|
|
|
|
return this.isValid(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(x) {
|
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
|
|
|
|
return this.nextValue(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
previous(x) {
|
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
|
|
|
|
return this.prevValue(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
return this.displayName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
union(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return this
|
|
|
|
|
if(domain instanceof DomainSet) return domain.union(this)
|
|
|
|
|
if(domain instanceof UnionDomain) return new UnionDomain(this, domain)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return new UnionDomain(this, domain)
|
|
|
|
|
if(domain instanceof MinusDomain) return new UnionDomain(this, domain)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) return new UnionDomain(this, domain)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intersection(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return domain
|
|
|
|
|
if(domain instanceof DomainSet) return domain.intersection(this)
|
|
|
|
|
if(domain instanceof UnionDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
if(domain instanceof MinusDomain) return new IntersectionDomain(this, domain)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) return new IntersectionDomain(this, domain)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DomainSet extends SpecialDomain {
|
|
|
|
|
constructor(values) {
|
|
|
|
|
super('', x => true, x => x, true)
|
|
|
|
|
console.log(values)
|
|
|
|
|
var newVals = {}
|
|
|
|
|
this.executedValues = []
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var value of values) {
|
|
|
|
|
var expr = new Expression(value.toString())
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var ex = expr.execute()
|
|
|
|
|
newVals[ex] = expr
|
|
|
|
|
this.executedValues.push(ex)
|
|
|
|
|
}
|
|
|
|
|
this.executedValues.sort((a,b) => a-b)
|
|
|
|
|
this.values = this.executedValues.map(val => newVals[val])
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(x) {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var value of this.values)
|
|
|
|
|
if(x == value.execute()) return true
|
2020-12-25 00:20:57 +00:00
|
|
|
|
return false
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 18:30:19 +00:00
|
|
|
|
next(x) {
|
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
|
|
|
|
if(x < this.executedValues[0]) return this.executedValues[0]
|
|
|
|
|
for(var i = 1; i < this.values.length; i++) {
|
|
|
|
|
var prevValue = this.executedValues[i-1]
|
|
|
|
|
var value = this.executedValues[i]
|
|
|
|
|
if(x >= prevValue && x < value) return value
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
previous(x) {
|
|
|
|
|
if(typeof x == 'string') x = executeExpression(x)
|
|
|
|
|
if(x > this.executedValues[this.executedValues.length-1])
|
|
|
|
|
return this.executedValues[this.executedValues.length-1]
|
|
|
|
|
for(var i = 1; i < this.values.length; i++) {
|
|
|
|
|
var prevValue = this.executedValues[i-1]
|
|
|
|
|
var value = this.executedValues[i]
|
|
|
|
|
if(x > prevValue && x <= value) return prevValue
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
toString() {
|
|
|
|
|
return "{" + this.values.join(";") + "}"
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
union(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return this
|
|
|
|
|
if(domain instanceof DomainSet) {
|
|
|
|
|
var newValues = []
|
|
|
|
|
var values = this.values.concat(domain.values).filter(function(val){
|
|
|
|
|
newValues.push(val.execute())
|
|
|
|
|
return newValues.indexOf(val.execute()) == newValues.length - 1
|
|
|
|
|
})
|
|
|
|
|
return new DomainSet(values)
|
|
|
|
|
}
|
|
|
|
|
var notIncludedValues = []
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var value in this.values) {
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var value = this.executedValues[i]
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
if(domain.begin.execute() == value && domain.openBegin) {
|
|
|
|
|
domain.openBegin = false
|
|
|
|
|
}
|
|
|
|
|
if(domain.end.execute() == value && domain.openEnd) {
|
|
|
|
|
domain.openEnd = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!domain.includes(value))
|
|
|
|
|
notIncludedValues.push(this.values[i].toEditableString())
|
|
|
|
|
}
|
|
|
|
|
if(notIncludedValues.length == 0) return domain
|
|
|
|
|
return new UnionDomain(domain, new DomainSet(notIncludedValues))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intersection(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return domain
|
|
|
|
|
if(domain instanceof DomainSet) {
|
|
|
|
|
var domValues = domain.values.map(expr => expr.execute())
|
|
|
|
|
this.values = this.values.filter(function(val){
|
|
|
|
|
return domValues.indexOf(val.execute()) >= 0
|
|
|
|
|
})
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
var includedValues = []
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var i in this.values) {
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var value = this.executedValues[i]
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
if(domain.begin.execute() == value && !domain.openBegin) {
|
|
|
|
|
domain.openBegin = false
|
|
|
|
|
}
|
|
|
|
|
if(domain.end.execute() == value && !domain.openEnd) {
|
|
|
|
|
domain.openEnd = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(domain.includes(value))
|
|
|
|
|
includedValues.push(this.values[i].toEditableString())
|
|
|
|
|
}
|
|
|
|
|
if(includedValues.length == 0) return new EmptySet()
|
|
|
|
|
if(includedValues.length == this.values.length) return this
|
|
|
|
|
return new IntersectionDomain(domain, new DomainSet(includedValues))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static import(frm) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
return new DomainSet(frm.substr(1, frm.length-2).split(";"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
class UnionDomain extends Domain {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
constructor(dom1, dom2) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
super()
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.dom1 = dom1
|
|
|
|
|
this.dom2 = dom2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(x) {
|
|
|
|
|
return this.dom1.includes(x) || this.dom2.includes(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
return this.dom1.toString() + " ∪ " + this.dom2.toString()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
union(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return this
|
|
|
|
|
if(domain instanceof DomainSet) return domain.union(this)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) return domain.union(this)
|
2020-12-25 00:20:57 +00:00
|
|
|
|
if(domain instanceof UnionDomain) return new UnionDomain(this, domain)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return new UnionDomain(this, domain)
|
|
|
|
|
if(domain instanceof MinusDomain) return new MinusDomain(this, domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intersection(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return domain
|
|
|
|
|
if(domain instanceof DomainSet) return domain.intersection(this)
|
|
|
|
|
if(domain instanceof UnionDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return this.dom1.intersection(domain.dom1).intersection(this.dom2).intersection(domain.dom2)
|
|
|
|
|
if(domain instanceof MinusDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static import(frm) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
var domains = frm.trim().split("∪")
|
|
|
|
|
if(domains.length == 1) domains = frm.trim().split("U") // Fallback
|
2020-12-25 00:20:57 +00:00
|
|
|
|
var dom1 = parseDomain(domains.pop())
|
|
|
|
|
var dom2 = parseDomain(domains.join('∪'))
|
|
|
|
|
return dom1.union(dom2)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
class IntersectionDomain extends Domain {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
constructor(dom1, dom2) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
super()
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.dom1 = dom1
|
|
|
|
|
this.dom2 = dom2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(x) {
|
|
|
|
|
return this.dom1.includes(x) && this.dom2.includes(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
return this.dom1.toString() + " ∩ " + this.dom2.toString()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
union(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return this
|
|
|
|
|
if(domain instanceof DomainSet) return domain.union(this)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain instanceof Range) return domain.union(this)
|
2020-12-25 00:20:57 +00:00
|
|
|
|
if(domain instanceof UnionDomain) return this.dom1.union(domain.dom1).union(this.dom2).union(domain.dom2)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return new UnionDomain(this, domain)
|
|
|
|
|
if(domain instanceof MinusDomain) return new MinusDomain(this, domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intersection(domain) {
|
|
|
|
|
if(domain instanceof EmptySet) return domain
|
|
|
|
|
if(domain instanceof DomainSet) return domain.intersection(this)
|
|
|
|
|
if(domain instanceof UnionDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
if(domain instanceof IntersectionDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
if(domain instanceof MinusDomain) return new IntersectionDomain(this, domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static import(frm) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
var domains = frm.trim().split("∩")
|
2020-12-25 00:20:57 +00:00
|
|
|
|
var dom1 = parseDomain(domains.pop())
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var dom2 = parseDomain(domains.join('∩'))
|
2020-12-25 00:20:57 +00:00
|
|
|
|
return dom1.intersection(dom2)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
class MinusDomain extends Domain {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
constructor(dom1, dom2) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
super()
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.dom1 = dom1
|
|
|
|
|
this.dom2 = dom2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(x) {
|
|
|
|
|
return this.dom1.includes(x) && !this.dom2.includes(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
return this.dom1.toString() + "∖" + this.dom2.toString()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
static import(frm) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
var domains = frm.trim().split("∖")
|
|
|
|
|
if(domains.length == 1) domains = frm.trim().split("\\") // Fallback
|
2020-12-25 00:20:57 +00:00
|
|
|
|
var dom1 = parseDomain(domains.pop())
|
|
|
|
|
var dom2 = parseDomain(domains.join('∪'))
|
|
|
|
|
return new MinusDomain(dom1, dom2)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Domain.RE = new MinusDomain("R", "{0}")
|
|
|
|
|
Domain.RE.displayName = "ℝ*"
|
|
|
|
|
|
2020-12-26 13:49:48 +00:00
|
|
|
|
Domain.R = new Range(-Infinity,Infinity,true,true)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
Domain.R.displayName = "ℝ"
|
2020-12-26 13:49:48 +00:00
|
|
|
|
Domain.RP = new Range(0,Infinity,true,false)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
Domain.RP.displayName = "ℝ⁺"
|
2020-12-26 13:49:48 +00:00
|
|
|
|
Domain.RM = new Range(-Infinity,0,true,false)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
Domain.RM.displayName = "ℝ⁻"
|
2020-12-26 13:49:48 +00:00
|
|
|
|
Domain.RPE = new Range(0,Infinity,true,true)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
Domain.RPE.displayName = "ℝ⁺*"
|
2020-12-26 13:49:48 +00:00
|
|
|
|
Domain.RME = new Range(-Infinity,0,true,true)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
Domain.RME.displayName = "ℝ⁻*"
|
|
|
|
|
Domain.N = new SpecialDomain('ℕ', x => x%1==0 && x >= 0,
|
|
|
|
|
x => Math.max(Math.floor(x)+1, 0),
|
|
|
|
|
x => Math.max(Math.ceil(x)-1, 0))
|
|
|
|
|
Domain.NE = new SpecialDomain('ℕ*', x => x%1==0 && x > 0,
|
|
|
|
|
x => Math.max(Math.floor(x)+1, 1),
|
|
|
|
|
x => Math.max(Math.ceil(x)-1, 1))
|
|
|
|
|
Domain.Z = new SpecialDomain('ℤ', x => x%1==0, x => Math.floor(x)+1, x => Math.ceil(x)-1)
|
|
|
|
|
Domain.ZE = new SpecialDomain('ℤ*', x => x%1==0 && x != 0,
|
|
|
|
|
x => Math.floor(x)+1 == 0 ? Math.floor(x)+2 : Math.floor(x)+1,
|
|
|
|
|
x => Math.ceil(x)-1 == 0 ? Math.ceil(x)-2 : Math.ceil(x)-1)
|
|
|
|
|
Domain.ZM = new SpecialDomain('ℤ⁻', x => x%1==0 && x <= 0,
|
|
|
|
|
x => Math.min(Math.floor(x)+1, 0),
|
|
|
|
|
x => Math.min(Math.ceil(x)-1, 0))
|
|
|
|
|
Domain.ZME = new SpecialDomain('ℤ⁻*', x => x%1==0 && x < 0,
|
|
|
|
|
x => Math.min(Math.floor(x)+1, -1),
|
|
|
|
|
x => Math.min(Math.ceil(x)-1, -1))
|
2020-12-25 18:55:47 +00:00
|
|
|
|
Domain.NLog = new SpecialDomain('ℕˡᵒᵍ',
|
|
|
|
|
x => x/Math.pow(10, x.toString().length-1) % 1 == 0 && x > 0,
|
|
|
|
|
function(x) {
|
|
|
|
|
var x10pow = Math.pow(10, x.toString().length-1)
|
|
|
|
|
return Math.max(1, (Math.floor(x/x10pow)+1)*x10pow)
|
|
|
|
|
},
|
|
|
|
|
function(x) {
|
|
|
|
|
var x10pow = Math.pow(10, x.toString().length-1)
|
|
|
|
|
return Math.max(1, (Math.ceil(x/x10pow)-1)*x10pow)
|
|
|
|
|
})
|
2020-12-25 18:30:19 +00:00
|
|
|
|
|
2020-12-25 00:20:57 +00:00
|
|
|
|
var refedDomains = []
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
|
|
|
|
function parseDomain(domain) {
|
2020-12-25 00:20:57 +00:00
|
|
|
|
if(!domain.includes(')') && !domain.includes('(')) return parseDomainSimple(domain)
|
|
|
|
|
var domStr
|
|
|
|
|
while((domStr = /\(([^)(]+)\)/.exec(domain)) !== null) {
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var dom = parseDomainSimple(domStr[1].trim());
|
2020-12-25 00:20:57 +00:00
|
|
|
|
domain = domain.replace(domStr[0], 'D' + refedDomains.length)
|
|
|
|
|
refedDomains.push(dom)
|
|
|
|
|
}
|
|
|
|
|
return parseDomainSimple(domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseDomainSimple(domain) {
|
2020-12-25 18:30:19 +00:00
|
|
|
|
domain = domain.trim()
|
|
|
|
|
if(domain.includes("U") || domain.includes("∪")) return UnionDomain.import(domain)
|
|
|
|
|
if(domain.includes("∩")) return IntersectionDomain.import(domain)
|
|
|
|
|
if(domain.includes("∖") || domain.includes("\\")) return MinusDomain.import(domain)
|
2020-12-25 00:20:57 +00:00
|
|
|
|
if(domain.charAt(0) == "{" && domain.charAt(domain.length -1) == "}") return DomainSet.import(domain)
|
2020-12-26 13:49:48 +00:00
|
|
|
|
if(domain.includes("]") || domain.includes("[")) return Range.import(domain)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
if(["R", "ℝ", "N", "ℕ", "Z", "ℤ"].some(str => domain.toUpperCase().includes(str)))
|
|
|
|
|
return Domain.import(domain)
|
|
|
|
|
if(domain[0] == 'D') return refedDomains[parseInt(domain.substr(1))]
|
2020-12-22 00:01:36 +00:00
|
|
|
|
return new EmptySet()
|
|
|
|
|
}
|