From 05e51fdd7374e9540bb165f5eb0c986ff0b608eb Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Fri, 25 Dec 2020 20:42:13 +0100 Subject: [PATCH] Starting Sequences! --- qml/ComboBoxSetting.qml | 2 +- qml/LogGraph.qml | 2 +- qml/ObjectLists.qml | 16 +++++++++++----- qml/TextSetting.qml | 2 +- qml/js/mathlib.js | 39 ++++++++++++++++++++++++++++++++++----- qml/js/objects.js | 5 +++-- qml/js/utils.js | 9 ++++++--- 7 files changed, 57 insertions(+), 18 deletions(-) diff --git a/qml/ComboBoxSetting.qml b/qml/ComboBoxSetting.qml index 4e79ce3..7fa5037 100644 --- a/qml/ComboBoxSetting.qml +++ b/qml/ComboBoxSetting.qml @@ -41,7 +41,7 @@ Item { anchors.top: parent.top verticalAlignment: TextInput.AlignVCenter color: sysPalette.windowText - text: " "+ control.label +": " + text: control.label +": " } ComboBox { diff --git a/qml/LogGraph.qml b/qml/LogGraph.qml index 5a8b2f5..818982d 100644 --- a/qml/LogGraph.qml +++ b/qml/LogGraph.qml @@ -140,7 +140,7 @@ ApplicationWindow { function loadDiagram(filename) { var data = JSON.parse(Helper.load(filename)) - if(Object.keys(data).indexOf("type") != -1 && data["type"] == "logplotv1") { + if("type" in Object.keys(data) && data["type"] == "logplotv1") { settings.xzoom = data["xzoom"] settings.yzoom = data["yzoom"] settings.xmin = data["xmin"] diff --git a/qml/ObjectLists.qml b/qml/ObjectLists.qml index cb2402d..2ab1047 100644 --- a/qml/ObjectLists.qml +++ b/qml/ObjectLists.qml @@ -263,13 +263,22 @@ ListView { width: dlgProperties.width property string label: Utils.camelCase2readable(modelData[0]) + Text { + id: customPropComment + height: 30 + width: parent.width + visible: modelData[0].startsWith('comment') + text: modelData[1] + color: sysPalette.windowText + } + TextSetting { id: customPropText height: 30 width: parent.width label: parent.label isDouble: modelData[1] == 'number' - visible: ['Expression', 'Domain', 'string', 'number'].indexOf(modelData[1]) >= 0 + visible: modelData[1] in ['Expression', 'Domain', 'string', 'number'] defValue: visible ? { 'Expression': () => Utils.simplifyExpression(objEditor.obj[modelData[0]].toEditableString()), 'Domain': () => objEditor.obj[modelData[0]].toString(), @@ -286,9 +295,6 @@ ListView { Objects.currentObjects[objEditor.objType][objEditor.objIndex].update() objectListList.update() } - Component.onCompleted: { - //console.log(modelData[0], objEditor.obj[modelData[0]],modelData[1], defValue) - } } CheckBox { @@ -310,7 +316,7 @@ ListView { width: dlgProperties.width label: parent.label // True to select an object of type, false for enums. - property bool selectObjMode: (typeof modelData[1] == "string" && Object.keys(Objects.types).indexOf(modelData[1]) >= 0) + property bool selectObjMode: (typeof modelData[1] == "string" && modelData[1] in Object.keys(Objects.types)) model: visible ? (selectObjMode ? Objects.getObjectsName(modelData[1]).concat(['+ Create new ' + modelData[1]]) : modelData[1]) : [] diff --git a/qml/TextSetting.qml b/qml/TextSetting.qml index 7e19764..6439f03 100644 --- a/qml/TextSetting.qml +++ b/qml/TextSetting.qml @@ -37,7 +37,7 @@ Item { anchors.top: parent.top verticalAlignment: TextInput.AlignVCenter color: sysPalette.windowText - text: " "+ control.label +": " + text: control.label +": " } diff --git a/qml/js/mathlib.js b/qml/js/mathlib.js index f5515e4..e9e02e6 100644 --- a/qml/js/mathlib.js +++ b/qml/js/mathlib.js @@ -23,13 +23,17 @@ const parser = new ExprEval.Parser() +var u = {1: 1, 2: 2, 3: 3} +console.log(parser.parse('u[n]+u[n+1]+u[n+2]').simplify().evaluate({"u": u, 'n': 1})) + var evalVariables = { // Variables not provided by expr-eval.js, needs to be provided manualy "pi": Math.PI, "π": Math.PI, "inf": Infinity, "Infinity": Infinity, "∞": Infinity, - "e": Math.E + "e": Math.E, + "E": Math.E } class Expression { @@ -64,10 +68,7 @@ class Expression { } toString(forceSign=false) { - var str = this.calc.toString() - if(str[0] == "(") str = str.substr(1) - if(str[str.length - 1] == ")") str = str.substr(0, str.length - 1) - str = Utils.makeExpressionReadable(str) + var str = Utils.makeExpressionReadable(this.calc.toString()) if(str[0] != '-' && forceSign) str = '+' + str return str } @@ -77,6 +78,34 @@ function executeExpression(expr){ return (new Expression(expr.toString())).execute() } +class Sequence extends Expression { + constructor(name, baseValues = {}, expr) { + console.log('Expression', expr) + super(expr) + this.name = name + this.baseValues = baseValues + } + + isConstant() { + return this.expr.indexOf("n") == -1 + } + + execute(n = 0) { + if(this.cached) return this.cachedValue + if(n in this.baseValues) return this.baseValues[n] + var vars = Object.assign({'n': n-1}, evalVariables) + vars[this.name] = this.baseValues + var un = this.calc.evaluate(vars) + this.baseValues[n] = un + return un + } +} + +var test = new Sequence('u', {0: 0}, '3*u[n]+3') +console.log(test) +for(var i=0; i<20; i++) + console.log('u' + Utils.textsub(i) + ' = ' + test.execute(i)) + // Domains class Domain { constructor() {} diff --git a/qml/js/objects.js b/qml/js/objects.js index b7fbf24..4665631 100644 --- a/qml/js/objects.js +++ b/qml/js/objects.js @@ -203,6 +203,7 @@ class Function extends ExecutableObject { 'expression': 'Expression', 'inDomain': 'Domain', 'outDomain': 'Domain', + 'comment1': 'Ex: R+* (ℝ⁺*), N* (ℕ*), Z-* (ℤ⁻*), ]0;1[, {3;4;5}', 'labelPosition': ['above', 'below'], 'displayMode': ['application', 'function'], 'labelX': 'number' @@ -280,8 +281,6 @@ class Function extends ExecutableObject { var pxprecision = 2 var previousX = canvas.px2x(0) var previousY; - var draw = function(currentX) { - } if(inDomain instanceof MathLib.SpecialDomain && inDomain.moveSupported) { previousX = inDomain.previous(previousX) if(previousX === null) previousX = inDomain.next(canvas.px2x(0)) @@ -924,6 +923,8 @@ class CursorX extends DrawableObject { } } + + const types = { 'Point': Point, 'Function': Function, diff --git a/qml/js/utils.js b/qml/js/utils.js index dc7cc4a..0851c63 100644 --- a/qml/js/utils.js +++ b/qml/js/utils.js @@ -207,8 +207,10 @@ function makeExpressionReadable(str) { [/ \^ /g, '^'], [/\^\(([^\^]+)\)/g, function(match, p1) { return textsup(p1) }], [/\^([^ ]+)/g, function(match, p1) { return textsup(p1) }], + [/_\(([^_]+)\)/g, function(match, p1) { return textsub(p1) }], + [/_([^ ]+)/g, function(match, p1) { return textsub(p1) }], [/(\d|\))×/g, '$1'], - [/×(\d|\()/g, '$1'], + //[/×(\d|\()/g, '$1'], [/\(([^)(+.\/-]+)\)/g, "$1"], ] @@ -258,8 +260,10 @@ function parseName(str, removeUnallowed = true) { [/([^a-z]|^)gps(i)?([^a-z]|$)/g, '$1Ψ$3'], [/([^a-z]|^)gom(ega)?([^a-z]|$)/g, '$1Ω$3'], // Underscores - [/_\(([^\^]+)\)/g, function(match, p1) { return textsub(p1) }], + [/_\(([^_]+)\)/g, function(match, p1) { return textsub(p1) }], [/_([^ ]+)/g, function(match, p1) { return textsub(p1) }], + // Array elements + [/\[([^\]\[]+)\]/g, function(match, p1) { return textsub(p1) }], // Removing [/[xπℝℕ\\∪∩\]\[ ()^/÷*×+=\d-]/g , ''], ] @@ -271,7 +275,6 @@ function parseName(str, removeUnallowed = true) { return str } - String.prototype.toLatinUppercase = function() { return this.replace(/[a-z]/g, function(match){return match.toUpperCase()}) }