Latex markup for sequences and bode phases

This commit is contained in:
Adsooi 2022-03-06 18:31:03 +01:00
parent 650e43894c
commit 8251504fbe
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
3 changed files with 44 additions and 44 deletions

View file

@ -21,6 +21,8 @@
.import "common.js" as C
.import "expression.js" as Expr
.import "../utils.js" as Utils
.import "../math/latex.js" as Latex
/**
* Represents mathematical object for sequences.
@ -32,9 +34,13 @@ class Sequence extends Expr.Expression {
this.name = name
this.baseValues = baseValues
this.calcValues = Object.assign({}, baseValues)
this.latexValues = Object.assign({}, baseValues)
for(var n in this.calcValues)
if(['string', 'number'].includes(typeof this.calcValues[n]))
this.calcValues[n] = C.parser.parse(this.calcValues[n].toString()).simplify().evaluate(C.evalVariables)
if(['string', 'number'].includes(typeof this.calcValues[n])) {
let parsed = C.parser.parse(this.calcValues[n].toString()).simplify()
this.latexValues[n] = Latex.expressionToLatex(parsed.tokens)
this.calcValues[n] = parsed.evaluate(C.evalVariables)
}
this.valuePlus = parseInt(valuePlus)
}
@ -75,4 +81,15 @@ class Sequence extends Expr.Expression {
).join('; ')
return ret
}
toLatexString(forceSign=false) {
var str = this.latexMarkup
if(str[0] != '-' && forceSign) str = '+' + str
var subtxt = '_{n' + (this.valuePlus == 0 ? '' : '+' + this.valuePlus) + '}'
var ret = `\\begin{array}{l}${Latex.variable(this.name)}${subtxt} = ${str}${this.latexValues.length == 0 ? '' : "\n"}\\\\`
ret += Object.keys(this.latexValues).map(
n => `${this.name}_{${n}} = ${this.latexValues[n]}`
).join('; ') + "\\end{array}"
return ret
}
}

View file

@ -23,19 +23,13 @@
.import "../mathlib.js" as MathLib
.import "../historylib.js" as HistoryLib
.import "../parameters.js" as P
.import "../math/latex.js" as Latex
class PhaseBode extends Common.ExecutableObject {
static type(){return 'Phase Bode'}
static displayType(){return qsTr('Bode Phase')}
static displayTypeMultiple(){return qsTr('Bode Phases')}
/*static properties() {return {
'om_0': new P.ObjectType('Point'),
'phase': 'Expression',
'unit': new P.Enum('°', 'deg', 'rad'),
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
'labelX': 'number'
}}*/
static properties() {return {
[QT_TRANSLATE_NOOP('prop','om_0')]: new P.ObjectType('Point'),
[QT_TRANSLATE_NOOP('prop','phase')]: 'Expression',
@ -82,6 +76,10 @@ class PhaseBode extends Common.ExecutableObject {
return `${this.name}: ${this.phase.toString(true)}${this.unit} at ${this.om_0.name} = ${this.om_0.x}`
}
getLatexString() {
return `${Latex.variable(this.name)}: ${this.phase.latexMarkup}\\textrm{${this.unit} at }${Latex.variable(this.om_0.name)} = ${this.om_0.x.latexMarkup}`
}
execute(x=1) {
if(typeof x == 'string') x = MathLib.executeExpression(x)
if(x < this.om_0.x) {
@ -120,37 +118,7 @@ class PhaseBode extends Common.ExecutableObject {
canvas.drawLine(ctx, Math.max(0, baseX), augmtY, canvas.canvasSize.width, augmtY)
// Label
var text = this.getLabel()
ctx.font = `${canvas.textsize}px sans-serif`
var textSize = canvas.measureText(ctx, text)
var posX = canvas.x2px(this.labelX)
var posY = canvas.y2px(this.execute(this.labelX))
switch(this.labelPosition) {
case 'above':
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
break;
case 'below':
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
break;
case 'left':
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
break;
case 'right':
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
break;
case 'above-left':
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
break;
case 'above-right':
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
break;
case 'below-left':
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
break;
case 'below-right':
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
break;
}
this.drawLabel(canvas, ctx, this.labelPosition, canvas.x2px(this.labelX), canvas.y2px(this.execute(this.labelX)))
}
update() {

View file

@ -22,6 +22,7 @@
.import "function.js" as F
.import "../mathlib.js" as MathLib
.import "../parameters.js" as P
.import "../math/latex.js" as Latex
class Sequence extends Common.ExecutableObject {
@ -76,11 +77,14 @@ class Sequence extends Common.ExecutableObject {
)
}
getReadableString() {
return this.sequence.toString()
}
getLatexString() {
return this.sequence.toLatexString()
}
execute(x = 1) {
if(x % 1 == 0)
return this.sequence.execute(x)
@ -103,7 +107,17 @@ class Sequence extends Common.ExecutableObject {
return this.getReadableString()
case 'null':
return ''
}
}
getLatexLabel() {
switch(this.labelContent) {
case 'name':
return `(${Latex.variable(this.name)}_n)`
case 'name + value':
return this.getLatexString()
case 'null':
return ''
}
}
@ -111,7 +125,8 @@ class Sequence extends Common.ExecutableObject {
F.Function.drawFunction(canvas, ctx, this.sequence, canvas.logscalex ? MathLib.Domain.NE : MathLib.Domain.N, MathLib.Domain.R, this.drawPoints, this.drawDashedLines)
// Label
var text = this.getLabel()
this.drawLabel(canvas, ctx, this.labelPosition, canvas.x2px(this.labelX), canvas.y2px(this.execute(this.labelX)))
/*var text = this.getLabel()
ctx.font = `${canvas.textsize}px sans-serif`
var textSize = canvas.measureText(ctx, text)
var posX = canvas.x2px(this.labelX)
@ -141,7 +156,7 @@ class Sequence extends Common.ExecutableObject {
case 'below-right':
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
break;
}
}*/
}
}