Fixing bugs.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Adsooi 2025-01-06 16:23:06 +01:00
parent fdcc8105ba
commit 6025742e86
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
3 changed files with 132 additions and 69 deletions

View file

@ -24,6 +24,7 @@ import { Expression, executeExpression } from "./expression.mjs"
*/ */
export class Domain { export class Domain {
constructor() { constructor() {
this.latexMarkup = "#INVALID"
} }
/** /**
@ -206,8 +207,8 @@ export class Range extends Domain {
} }
includes(x) { includes(x) {
if(x instanceof Expression) x = x.execute()
if(typeof x == "string") x = executeExpression(x) if(typeof x == "string") x = executeExpression(x)
if(x instanceof Expression) x = x.execute()
return ((this.openBegin && x > this.begin.execute()) || (!this.openBegin && x >= this.begin.execute())) && 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())) ((this.openEnd && x < this.end.execute()) || (!this.openEnd && x <= this.end.execute()))
} }
@ -249,15 +250,17 @@ export class SpecialDomain extends Domain {
/** /**
* @constructs SpecialDomain * @constructs SpecialDomain
* @param {string} displayName * @param {string} displayName
* @param {string} latexMarkup - markup representing the domain.
* @param {function} isValid - function returning true when number is in domain false when it isn't. * @param {function} isValid - function returning true when number is in domain false when it isn't.
* @param {function} next - function provides the next positive value in the domain after the one given. * @param {function} next - function provides the next positive value in the domain after the one given.
* @param {function} previous - function provides the previous positive value in the domain before the one given. * @param {function} previous - function provides the previous positive value in the domain before the one given.
* @param {boolean} moveSupported - Only true if next and previous functions are valid. * @param {boolean} moveSupported - Only true if next and previous functions are valid.
*/ */
constructor(displayName, isValid, next = () => true, previous = () => true, constructor(displayName, latexMarkup, isValid, next = () => true, previous = () => true,
moveSupported = true) { moveSupported = true) {
super() super()
this.displayName = displayName this.displayName = displayName
this.latexMarkup = latexMarkup
this.isValid = isValid this.isValid = isValid
this.nextValue = next this.nextValue = next
this.prevValue = previous this.prevValue = previous
@ -562,39 +565,54 @@ Domain.RPE.latexMarkup = "\\mathbb{R}^{+*}"
Domain.RME = new Range(-Infinity, 0, true, true) Domain.RME = new Range(-Infinity, 0, true, true)
Domain.RME.displayName = "ℝ⁻*" Domain.RME.displayName = "ℝ⁻*"
Domain.RME.latexMarkup = "\\mathbb{R}^{+*}" Domain.RME.latexMarkup = "\\mathbb{R}^{+*}"
Domain.N = new SpecialDomain("", x => x % 1 === 0 && x >= 0, Domain.N = new SpecialDomain(
"", "\\mathbb{N}",
x => x % 1 === 0 && x >= 0,
x => Math.max(Math.floor(x) + 1, 0), x => Math.max(Math.floor(x) + 1, 0),
x => Math.max(Math.ceil(x) - 1, 0)) x => Math.max(Math.ceil(x) - 1, 0)
Domain.N.latexMarkup = "\\mathbb{N}" )
Domain.NE = new SpecialDomain("*", x => x % 1 === 0 && x > 0, Domain.NE = new SpecialDomain(
"*", "\\mathbb{N}^{*}",
x => x % 1 === 0 && x > 0,
x => Math.max(Math.floor(x) + 1, 1), x => Math.max(Math.floor(x) + 1, 1),
x => Math.max(Math.ceil(x) - 1, 1)) x => Math.max(Math.ceil(x) - 1, 1)
Domain.NE.latexMarkup = "\\mathbb{N}^{*}" )
Domain.Z = new SpecialDomain("", x => x % 1 === 0, x => Math.floor(x) + 1, x => Math.ceil(x) - 1) Domain.Z = new SpecialDomain(
Domain.Z.latexMarkup = "\\mathbb{Z}" "", "\\mathbb{Z}",
Domain.ZE = new SpecialDomain("*", x => x % 1 === 0 && x !== 0, x => x % 1 === 0,
x => Math.floor(x) + 1,
x => Math.ceil(x) - 1
)
Domain.ZE = new SpecialDomain(
"*", "\\mathbb{Z}^{*}",
x => x % 1 === 0 && x !== 0,
x => Math.floor(x) + 1 === 0 ? Math.floor(x) + 2 : Math.floor(x) + 1, 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) x => Math.ceil(x) - 1 === 0 ? Math.ceil(x) - 2 : Math.ceil(x) - 1
Domain.ZE.latexMarkup = "\\mathbb{Z}^{*}" )
Domain.ZM = new SpecialDomain("ℤ⁻", x => x % 1 === 0 && x <= 0, Domain.ZM = new SpecialDomain(
"ℤ⁻", "\\mathbb{Z}^{-}",
x => x % 1 === 0 && x <= 0,
x => Math.min(Math.floor(x) + 1, 0), x => Math.min(Math.floor(x) + 1, 0),
x => Math.min(Math.ceil(x) - 1, 0)) x => Math.min(Math.ceil(x) - 1, 0)
Domain.ZM.latexMarkup = "\\mathbb{Z}^{-}" )
Domain.ZME = new SpecialDomain("ℤ⁻*", x => x % 1 === 0 && x < 0, Domain.ZME = new SpecialDomain(
"ℤ⁻*", "\\mathbb{Z}^{-*}",
x => x % 1 === 0 && x < 0,
x => Math.min(Math.floor(x) + 1, -1), x => Math.min(Math.floor(x) + 1, -1),
x => Math.min(Math.ceil(x) - 1, -1)) x => Math.min(Math.ceil(x) - 1, -1)
Domain.ZME.latexMarkup = "\\mathbb{Z}^{-*}" )
Domain.NLog = new SpecialDomain("ℕˡᵒᵍ", Domain.NLog = new SpecialDomain(
"ℕˡᵒᵍ", "\\mathbb{N}^{log}",
x => x / Math.pow(10, Math.ceil(Math.log10(x))) % 1 === 0 && x > 0, x => x / Math.pow(10, Math.ceil(Math.log10(x))) % 1 === 0 && x > 0,
function(x) { x => {
let x10pow = Math.pow(10, Math.ceil(Math.log10(x))) let x10pow = Math.pow(10, Math.ceil(Math.log10(x)))
return Math.max(1, (Math.floor(x / x10pow) + 1) * x10pow) return Math.max(1, (Math.floor(x / x10pow) + 1) * x10pow)
}, },
function(x) { x => {
let x10pow = Math.pow(10, Math.ceil(Math.log10(x))) let x10pow = Math.pow(10, Math.ceil(Math.log10(x)))
return Math.max(1, (Math.ceil(x / x10pow) - 1) * x10pow) return Math.max(1, (Math.ceil(x / x10pow) - 1) * x10pow)
}) }
Domain.NLog.latexMarkup = "\\mathbb{N}^{log}" )
let refedDomains = [] let refedDomains = []
@ -626,7 +644,7 @@ export function parseDomainSimple(domain) {
if(domain.includes("U") || domain.includes("")) return UnionDomain.import(domain) if(domain.includes("U") || domain.includes("")) return UnionDomain.import(domain)
if(domain.includes("∩")) return IntersectionDomain.import(domain) if(domain.includes("∩")) return IntersectionDomain.import(domain)
if(domain.includes("") || domain.includes("\\")) return MinusDomain.import(domain) if(domain.includes("") || domain.includes("\\")) return MinusDomain.import(domain)
if(domain.charAt(0) === "{" && domain.charAt(domain.length - 1) === "}") return DomainSet.import(domain) if(domain.at(0) === "{" && domain.at(-1) === "}") return DomainSet.import(domain)
if(domain.includes("]") || domain.includes("[")) return Range.import(domain) if(domain.includes("]") || domain.includes("[")) return Range.import(domain)
if(["R", "", "N", "", "Z", ""].some(str => domain.toUpperCase().includes(str))) if(["R", "", "N", "", "Z", ""].some(str => domain.toUpperCase().includes(str)))
return Domain.import(domain) return Domain.import(domain)

View file

@ -19,46 +19,90 @@
import { describe, it } from "mocha" import { describe, it } from "mocha"
import { expect } from "chai" import { expect } from "chai"
// import { Domain, parseDomainSimple } from "../../src/math/domain.mjs" import { Domain, EmptySet, parseDomainSimple } from "../../src/math/domain.mjs"
//
// describe("math.domain", function() { describe("math.domain", function() {
// describe("#parseDomainSimple", function() { describe("#parseDomainSimple", function() {
// it("returns predefined domains", function() { it("returns empty sets when a domain cannot be parsed", function() {
// const predefinedToCheck = [ expect(parseDomainSimple("∅")).to.be.an.instanceof(EmptySet)
// // Real domains expect(parseDomainSimple("O")).to.be.an.instanceof(EmptySet)
// { domain: Domain.R, shortcuts: ["R", ""] }, expect(parseDomainSimple("AAAAAAAAA")).to.be.an.instanceof(EmptySet)
// // Zero exclusive real domains expect(parseDomainSimple("???")).to.be.an.instanceof(EmptySet)
// { domain: Domain.RE, shortcuts: ["RE", "R*", "*"] }, expect(parseDomainSimple("∅").latexMarkup).to.equal("\\emptyset")
// // Real positive domains expect(parseDomainSimple("???").toString()).to.equal("∅")
// { domain: Domain.RP, shortcuts: ["RP", "R+", "ℝ⁺", "+"] }, expect(parseDomainSimple("∅").includes(0)).to.be.false
// // Zero-exclusive real positive domains expect(parseDomainSimple("∅").includes(Infinity)).to.be.false
// { domain: Domain.RPE, shortcuts: ["RPE", "REP", "R+*", "R*+", "*⁺", "ℝ⁺*", "*+", "+*"] }, expect(parseDomainSimple("∅").includes(-3)).to.be.false
// // Real negative domain
// { domain: Domain.RM, shortcuts: ["RM", "R-", "ℝ⁻", "-"] }, })
// // Zero-exclusive real negative domains
// { domain: Domain.RME, shortcuts: ["RME", "REM", "R-*", "R*-", "ℝ⁻*", "*⁻", "-*", "*-"] }, it("returns predefined domains", function() {
// // Natural integers domain const predefinedToCheck = [
// { domain: Domain.N, shortcuts: ["", "N", "ZP", "Z+", "ℤ⁺", "+"] }, // Real domains
// // Zero-exclusive natural integers domain { domain: Domain.R, shortcuts: ["R", ""] },
// { domain: Domain.NE, shortcuts: ["NE", "NP", "N*", "N+", "*", "ℕ⁺", "+", "ZPE", "ZEP", "Z+*", "Z*+", "ℤ⁺*", "*⁺", "+*", "*+"] }, // Zero exclusive real domains
// // Logarithmic natural domains { domain: Domain.RE, shortcuts: ["RE", "R*", "*"] },
// { domain: Domain.NLog, shortcuts: ["NLOG", "ℕˡᵒᵍ", "LOG"] }, // Real positive domains
// // All integers domains { domain: Domain.RP, shortcuts: ["RP", "R+", "ℝ⁺", "+"] },
// { domain: Domain.Z, shortcuts: ["Z", ""] }, // Zero-exclusive real positive domains
// // Zero-exclusive all integers domain { domain: Domain.RPE, shortcuts: ["RPE", "REP", "R+*", "R*+", "*⁺", "ℝ⁺*", "*+", "+*"] },
// { domain: Domain.ZE, shortcuts: ["ZE", "Z*", "*"] }, // Real negative domain
// // Negative integers domain { domain: Domain.RM, shortcuts: ["RM", "R-", "ℝ⁻", "-"] },
// { domain: Domain.ZM, shortcuts: ["ZM", "Z-", "ℤ⁻", "-"] }, // Zero-exclusive real negative domains
// // Zero-exclusive negative integers domain { domain: Domain.RME, shortcuts: ["RME", "REM", "R-*", "R*-", "ℝ⁻*", "*⁻", "-*", "*-"] },
// { domain: Domain.ZME, shortcuts: ["ZME", "ZEM", "Z-*", "Z*-", "ℤ⁻*", "*⁻", "-*", "*-"] }, // Natural integers domain
// ] { domain: Domain.N, shortcuts: ["", "N", "ZP", "Z+", "ℤ⁺", "+"] },
// // Zero-exclusive natural integers domain
// // Real domains { domain: Domain.NE, shortcuts: ["NE", "NP", "N*", "N+", "*", "ℕ⁺", "+", "ZPE", "ZEP", "Z+*", "Z*+", "ℤ⁺*", "*⁺", "+*", "*+"] },
// for(const { domain, shortcuts } of predefinedToCheck) // Logarithmic natural domains
// for(const shortcut of shortcuts) { domain: Domain.NLog, shortcuts: ["NLOG", "ℕˡᵒᵍ", "LOG"] },
// expect(parseDomainSimple(shortcut)).to.be.equal(domain) // All integers domains
// }) { domain: Domain.Z, shortcuts: ["Z", ""] },
// // Zero-exclusive all integers domain
// it("") { domain: Domain.ZE, shortcuts: ["ZE", "Z*", "*"] },
// }) // Negative integers domain
// }) { domain: Domain.ZM, shortcuts: ["ZM", "Z-", "ℤ⁻", "-"] },
// Zero-exclusive negative integers domain
{ domain: Domain.ZME, shortcuts: ["ZME", "ZEM", "Z-*", "Z*-", "ℤ⁻*", "*⁻", "-*", "*-"] },
]
// Real domains
for(const { domain, shortcuts } of predefinedToCheck)
for(const shortcut of shortcuts)
expect(parseDomainSimple(shortcut)).to.be.equal(domain)
})
it("returns parsed ranges", function() {
const parsedClosed = parseDomainSimple("[1;3]")
expect(parsedClosed.includes(1)).to.be.true
expect(parsedClosed.includes(2.4)).to.be.true
expect(parsedClosed.includes(3)).to.be.true
expect(parsedClosed.includes(3.01)).to.be.false
expect(parsedClosed.includes(0.99)).to.be.false
const parsedOpen = parseDomainSimple("]1;3[")
expect(parsedOpen.includes(1)).to.be.false
expect(parsedOpen.includes(3)).to.be.false
expect(parsedOpen.includes(2.4)).to.be.true
expect(parsedOpen.includes(1.01)).to.be.true
expect(parsedOpen.includes(2.99)).to.be.true
const parsedOpenBefore = parseDomainSimple("]1;3]")
expect(parsedOpenBefore.includes(1)).to.be.false
expect(parsedOpenBefore.includes(3)).to.be.true
expect(parsedOpenBefore.includes(2.4)).to.be.true
expect(parsedOpenBefore.includes(1.01)).to.be.true
expect(parsedOpenBefore.includes(3.01)).to.be.false
const parsedOpenAfter = parseDomainSimple("[1;3[")
expect(parsedOpenAfter.includes(1)).to.be.true
expect(parsedOpenAfter.includes(3)).to.be.false
expect(parsedOpenAfter.includes(2.4)).to.be.true
expect(parsedOpenAfter.includes(0.99)).to.be.false
expect(parsedOpenAfter.includes(2.99)).to.be.true
})
it("does not parse invalid ranges", function() {
expect(() => parseDomainSimple("]1;2;3[")).to.throw
expect(() => parseDomainSimple("]1,2;3[")).to.throw
expect(() => parseDomainSimple("](12);3[")).to.throw
})
})
})

View file

@ -111,6 +111,8 @@ Popup {
model.append({ 'chr': chr }) model.append({ 'chr': chr })
} }
} }
Keys.onEscapePressed: parent.close()
} }
function setFocus() { function setFocus() {
@ -118,5 +120,4 @@ Popup {
insertGrid.forceActiveFocus() insertGrid.forceActiveFocus()
} }
Keys.onEscapePressed: close()
} }