Skip to content

Commit 6025742

Browse files
committed
Fixing bugs.
1 parent fdcc810 commit 6025742

File tree

3 files changed

+132
-69
lines changed

3 files changed

+132
-69
lines changed

common/src/math/domain.mjs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Expression, executeExpression } from "./expression.mjs"
2424
*/
2525
export class Domain {
2626
constructor() {
27+
this.latexMarkup = "#INVALID"
2728
}
2829

2930
/**
@@ -206,8 +207,8 @@ export class Range extends Domain {
206207
}
207208

208209
includes(x) {
209-
if(x instanceof Expression) x = x.execute()
210210
if(typeof x == "string") x = executeExpression(x)
211+
if(x instanceof Expression) x = x.execute()
211212
return ((this.openBegin && x > this.begin.execute()) || (!this.openBegin && x >= this.begin.execute())) &&
212213
((this.openEnd && x < this.end.execute()) || (!this.openEnd && x <= this.end.execute()))
213214
}
@@ -249,15 +250,17 @@ export class SpecialDomain extends Domain {
249250
/**
250251
* @constructs SpecialDomain
251252
* @param {string} displayName
253+
* @param {string} latexMarkup - markup representing the domain.
252254
* @param {function} isValid - function returning true when number is in domain false when it isn't.
253255
* @param {function} next - function provides the next positive value in the domain after the one given.
254256
* @param {function} previous - function provides the previous positive value in the domain before the one given.
255257
* @param {boolean} moveSupported - Only true if next and previous functions are valid.
256258
*/
257-
constructor(displayName, isValid, next = () => true, previous = () => true,
259+
constructor(displayName, latexMarkup, isValid, next = () => true, previous = () => true,
258260
moveSupported = true) {
259261
super()
260262
this.displayName = displayName
263+
this.latexMarkup = latexMarkup
261264
this.isValid = isValid
262265
this.nextValue = next
263266
this.prevValue = previous
@@ -562,39 +565,54 @@ Domain.RPE.latexMarkup = "\\mathbb{R}^{+*}"
562565
Domain.RME = new Range(-Infinity, 0, true, true)
563566
Domain.RME.displayName = "ℝ⁻*"
564567
Domain.RME.latexMarkup = "\\mathbb{R}^{+*}"
565-
Domain.N = new SpecialDomain("ℕ", x => x % 1 === 0 && x >= 0,
568+
Domain.N = new SpecialDomain(
569+
"ℕ", "\\mathbb{N}",
570+
x => x % 1 === 0 && x >= 0,
566571
x => Math.max(Math.floor(x) + 1, 0),
567-
x => Math.max(Math.ceil(x) - 1, 0))
568-
Domain.N.latexMarkup = "\\mathbb{N}"
569-
Domain.NE = new SpecialDomain("ℕ*", x => x % 1 === 0 && x > 0,
572+
x => Math.max(Math.ceil(x) - 1, 0)
573+
)
574+
Domain.NE = new SpecialDomain(
575+
"ℕ*", "\\mathbb{N}^{*}",
576+
x => x % 1 === 0 && x > 0,
570577
x => Math.max(Math.floor(x) + 1, 1),
571-
x => Math.max(Math.ceil(x) - 1, 1))
572-
Domain.NE.latexMarkup = "\\mathbb{N}^{*}"
573-
Domain.Z = new SpecialDomain("ℤ", x => x % 1 === 0, x => Math.floor(x) + 1, x => Math.ceil(x) - 1)
574-
Domain.Z.latexMarkup = "\\mathbb{Z}"
575-
Domain.ZE = new SpecialDomain("ℤ*", x => x % 1 === 0 && x !== 0,
578+
x => Math.max(Math.ceil(x) - 1, 1)
579+
)
580+
Domain.Z = new SpecialDomain(
581+
"ℤ", "\\mathbb{Z}",
582+
x => x % 1 === 0,
583+
x => Math.floor(x) + 1,
584+
x => Math.ceil(x) - 1
585+
)
586+
Domain.ZE = new SpecialDomain(
587+
"ℤ*", "\\mathbb{Z}^{*}",
588+
x => x % 1 === 0 && x !== 0,
576589
x => Math.floor(x) + 1 === 0 ? Math.floor(x) + 2 : Math.floor(x) + 1,
577-
x => Math.ceil(x) - 1 === 0 ? Math.ceil(x) - 2 : Math.ceil(x) - 1)
578-
Domain.ZE.latexMarkup = "\\mathbb{Z}^{*}"
579-
Domain.ZM = new SpecialDomain("ℤ⁻", x => x % 1 === 0 && x <= 0,
590+
x => Math.ceil(x) - 1 === 0 ? Math.ceil(x) - 2 : Math.ceil(x) - 1
591+
)
592+
Domain.ZM = new SpecialDomain(
593+
"ℤ⁻", "\\mathbb{Z}^{-}",
594+
x => x % 1 === 0 && x <= 0,
580595
x => Math.min(Math.floor(x) + 1, 0),
581-
x => Math.min(Math.ceil(x) - 1, 0))
582-
Domain.ZM.latexMarkup = "\\mathbb{Z}^{-}"
583-
Domain.ZME = new SpecialDomain("ℤ⁻*", x => x % 1 === 0 && x < 0,
596+
x => Math.min(Math.ceil(x) - 1, 0)
597+
)
598+
Domain.ZME = new SpecialDomain(
599+
"ℤ⁻*", "\\mathbb{Z}^{-*}",
600+
x => x % 1 === 0 && x < 0,
584601
x => Math.min(Math.floor(x) + 1, -1),
585-
x => Math.min(Math.ceil(x) - 1, -1))
586-
Domain.ZME.latexMarkup = "\\mathbb{Z}^{-*}"
587-
Domain.NLog = new SpecialDomain("ℕˡᵒᵍ",
602+
x => Math.min(Math.ceil(x) - 1, -1)
603+
)
604+
Domain.NLog = new SpecialDomain(
605+
"ℕˡᵒᵍ", "\\mathbb{N}^{log}",
588606
x => x / Math.pow(10, Math.ceil(Math.log10(x))) % 1 === 0 && x > 0,
589-
function(x) {
607+
x => {
590608
let x10pow = Math.pow(10, Math.ceil(Math.log10(x)))
591609
return Math.max(1, (Math.floor(x / x10pow) + 1) * x10pow)
592610
},
593-
function(x) {
611+
x => {
594612
let x10pow = Math.pow(10, Math.ceil(Math.log10(x)))
595613
return Math.max(1, (Math.ceil(x / x10pow) - 1) * x10pow)
596-
})
597-
Domain.NLog.latexMarkup = "\\mathbb{N}^{log}"
614+
}
615+
)
598616

599617
let refedDomains = []
600618

@@ -626,7 +644,7 @@ export function parseDomainSimple(domain) {
626644
if(domain.includes("U") || domain.includes("∪")) return UnionDomain.import(domain)
627645
if(domain.includes("∩")) return IntersectionDomain.import(domain)
628646
if(domain.includes("∖") || domain.includes("\\")) return MinusDomain.import(domain)
629-
if(domain.charAt(0) === "{" && domain.charAt(domain.length - 1) === "}") return DomainSet.import(domain)
647+
if(domain.at(0) === "{" && domain.at(-1) === "}") return DomainSet.import(domain)
630648
if(domain.includes("]") || domain.includes("[")) return Range.import(domain)
631649
if(["R", "ℝ", "N", "ℕ", "Z", "ℤ"].some(str => domain.toUpperCase().includes(str)))
632650
return Domain.import(domain)

common/test/math/domain.mjs

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,90 @@
1919
import { describe, it } from "mocha"
2020
import { expect } from "chai"
2121

22-
// import { Domain, parseDomainSimple } from "../../src/math/domain.mjs"
23-
//
24-
// describe("math.domain", function() {
25-
// describe("#parseDomainSimple", function() {
26-
// it("returns predefined domains", function() {
27-
// const predefinedToCheck = [
28-
// // Real domains
29-
// { domain: Domain.R, shortcuts: ["R", "ℝ"] },
30-
// // Zero exclusive real domains
31-
// { domain: Domain.RE, shortcuts: ["RE", "R*", "ℝ*"] },
32-
// // Real positive domains
33-
// { domain: Domain.RP, shortcuts: ["RP", "R+", "ℝ⁺", "ℝ+"] },
34-
// // Zero-exclusive real positive domains
35-
// { domain: Domain.RPE, shortcuts: ["RPE", "REP", "R+*", "R*+", "ℝ*⁺", "ℝ⁺*", "ℝ*+", "ℝ+*"] },
36-
// // Real negative domain
37-
// { domain: Domain.RM, shortcuts: ["RM", "R-", "ℝ⁻", "ℝ-"] },
38-
// // Zero-exclusive real negative domains
39-
// { domain: Domain.RME, shortcuts: ["RME", "REM", "R-*", "R*-", "ℝ⁻*", "ℝ*⁻", "ℝ-*", "ℝ*-"] },
40-
// // Natural integers domain
41-
// { domain: Domain.N, shortcuts: ["ℕ", "N", "ZP", "Z+", "ℤ⁺", "ℤ+"] },
42-
// // Zero-exclusive natural integers domain
43-
// { domain: Domain.NE, shortcuts: ["NE", "NP", "N*", "N+", "ℕ*", "ℕ⁺", "ℕ+", "ZPE", "ZEP", "Z+*", "Z*+", "ℤ⁺*", "ℤ*⁺", "ℤ+*", "ℤ*+"] },
44-
// // Logarithmic natural domains
45-
// { domain: Domain.NLog, shortcuts: ["NLOG", "ℕˡᵒᵍ", "ℕLOG"] },
46-
// // All integers domains
47-
// { domain: Domain.Z, shortcuts: ["Z", "ℤ"] },
48-
// // Zero-exclusive all integers domain
49-
// { domain: Domain.ZE, shortcuts: ["ZE", "Z*", "ℤ*"] },
50-
// // Negative integers domain
51-
// { domain: Domain.ZM, shortcuts: ["ZM", "Z-", "ℤ⁻", "ℤ-"] },
52-
// // Zero-exclusive negative integers domain
53-
// { domain: Domain.ZME, shortcuts: ["ZME", "ZEM", "Z-*", "Z*-", "ℤ⁻*", "ℤ*⁻", "ℤ-*", "ℤ*-"] },
54-
// ]
55-
//
56-
// // Real domains
57-
// for(const { domain, shortcuts } of predefinedToCheck)
58-
// for(const shortcut of shortcuts)
59-
// expect(parseDomainSimple(shortcut)).to.be.equal(domain)
60-
// })
61-
//
62-
// it("")
63-
// })
64-
// })
22+
import { Domain, EmptySet, parseDomainSimple } from "../../src/math/domain.mjs"
23+
24+
describe("math.domain", function() {
25+
describe("#parseDomainSimple", function() {
26+
it("returns empty sets when a domain cannot be parsed", function() {
27+
expect(parseDomainSimple("∅")).to.be.an.instanceof(EmptySet)
28+
expect(parseDomainSimple("O")).to.be.an.instanceof(EmptySet)
29+
expect(parseDomainSimple("AAAAAAAAA")).to.be.an.instanceof(EmptySet)
30+
expect(parseDomainSimple("???")).to.be.an.instanceof(EmptySet)
31+
expect(parseDomainSimple("∅").latexMarkup).to.equal("\\emptyset")
32+
expect(parseDomainSimple("???").toString()).to.equal("∅")
33+
expect(parseDomainSimple("∅").includes(0)).to.be.false
34+
expect(parseDomainSimple("∅").includes(Infinity)).to.be.false
35+
expect(parseDomainSimple("∅").includes(-3)).to.be.false
36+
37+
})
38+
39+
it("returns predefined domains", function() {
40+
const predefinedToCheck = [
41+
// Real domains
42+
{ domain: Domain.R, shortcuts: ["R", "ℝ"] },
43+
// Zero exclusive real domains
44+
{ domain: Domain.RE, shortcuts: ["RE", "R*", "ℝ*"] },
45+
// Real positive domains
46+
{ domain: Domain.RP, shortcuts: ["RP", "R+", "ℝ⁺", "ℝ+"] },
47+
// Zero-exclusive real positive domains
48+
{ domain: Domain.RPE, shortcuts: ["RPE", "REP", "R+*", "R*+", "ℝ*⁺", "ℝ⁺*", "ℝ*+", "ℝ+*"] },
49+
// Real negative domain
50+
{ domain: Domain.RM, shortcuts: ["RM", "R-", "ℝ⁻", "ℝ-"] },
51+
// Zero-exclusive real negative domains
52+
{ domain: Domain.RME, shortcuts: ["RME", "REM", "R-*", "R*-", "ℝ⁻*", "ℝ*⁻", "ℝ-*", "ℝ*-"] },
53+
// Natural integers domain
54+
{ domain: Domain.N, shortcuts: ["ℕ", "N", "ZP", "Z+", "ℤ⁺", "ℤ+"] },
55+
// Zero-exclusive natural integers domain
56+
{ domain: Domain.NE, shortcuts: ["NE", "NP", "N*", "N+", "ℕ*", "ℕ⁺", "ℕ+", "ZPE", "ZEP", "Z+*", "Z*+", "ℤ⁺*", "ℤ*⁺", "ℤ+*", "ℤ*+"] },
57+
// Logarithmic natural domains
58+
{ domain: Domain.NLog, shortcuts: ["NLOG", "ℕˡᵒᵍ", "ℕLOG"] },
59+
// All integers domains
60+
{ domain: Domain.Z, shortcuts: ["Z", "ℤ"] },
61+
// Zero-exclusive all integers domain
62+
{ domain: Domain.ZE, shortcuts: ["ZE", "Z*", "ℤ*"] },
63+
// Negative integers domain
64+
{ domain: Domain.ZM, shortcuts: ["ZM", "Z-", "ℤ⁻", "ℤ-"] },
65+
// Zero-exclusive negative integers domain
66+
{ domain: Domain.ZME, shortcuts: ["ZME", "ZEM", "Z-*", "Z*-", "ℤ⁻*", "ℤ*⁻", "ℤ-*", "ℤ*-"] },
67+
]
68+
69+
// Real domains
70+
for(const { domain, shortcuts } of predefinedToCheck)
71+
for(const shortcut of shortcuts)
72+
expect(parseDomainSimple(shortcut)).to.be.equal(domain)
73+
})
74+
75+
it("returns parsed ranges", function() {
76+
const parsedClosed = parseDomainSimple("[1;3]")
77+
expect(parsedClosed.includes(1)).to.be.true
78+
expect(parsedClosed.includes(2.4)).to.be.true
79+
expect(parsedClosed.includes(3)).to.be.true
80+
expect(parsedClosed.includes(3.01)).to.be.false
81+
expect(parsedClosed.includes(0.99)).to.be.false
82+
const parsedOpen = parseDomainSimple("]1;3[")
83+
expect(parsedOpen.includes(1)).to.be.false
84+
expect(parsedOpen.includes(3)).to.be.false
85+
expect(parsedOpen.includes(2.4)).to.be.true
86+
expect(parsedOpen.includes(1.01)).to.be.true
87+
expect(parsedOpen.includes(2.99)).to.be.true
88+
const parsedOpenBefore = parseDomainSimple("]1;3]")
89+
expect(parsedOpenBefore.includes(1)).to.be.false
90+
expect(parsedOpenBefore.includes(3)).to.be.true
91+
expect(parsedOpenBefore.includes(2.4)).to.be.true
92+
expect(parsedOpenBefore.includes(1.01)).to.be.true
93+
expect(parsedOpenBefore.includes(3.01)).to.be.false
94+
const parsedOpenAfter = parseDomainSimple("[1;3[")
95+
expect(parsedOpenAfter.includes(1)).to.be.true
96+
expect(parsedOpenAfter.includes(3)).to.be.false
97+
expect(parsedOpenAfter.includes(2.4)).to.be.true
98+
expect(parsedOpenAfter.includes(0.99)).to.be.false
99+
expect(parsedOpenAfter.includes(2.99)).to.be.true
100+
})
101+
102+
it("does not parse invalid ranges", function() {
103+
expect(() => parseDomainSimple("]1;2;3[")).to.throw
104+
expect(() => parseDomainSimple("]1,2;3[")).to.throw
105+
expect(() => parseDomainSimple("](12);3[")).to.throw
106+
})
107+
})
108+
})

runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Popup/InsertCharacter.qml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ Popup {
111111
model.append({ 'chr': chr })
112112
}
113113
}
114+
115+
Keys.onEscapePressed: parent.close()
114116
}
115117

116118
function setFocus() {
117119
insertGrid.currentIndex = 0
118120
insertGrid.forceActiveFocus()
119121
}
120122

121-
Keys.onEscapePressed: close()
122123
}

0 commit comments

Comments
 (0)