Skip to content

Commit 9d0994f

Browse files
New method simplify
1 parent a417d75 commit 9d0994f

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/interpreter.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ JXG.extend(JXG.JessieCode.prototype, /** @lends JXG.JessieCode.prototype */ {
819819
_genericParse: function (code, cmd, geonext, dontstore) {
820820
var i, setTextBackup, ast, result,
821821
ccode = code.replace(/\r\n/g, '\n').split('\n'),
822-
// options = {},
822+
options = {},
823823
cleaned = [];
824824

825825
if (!dontstore) {
@@ -850,9 +850,10 @@ JXG.extend(JXG.JessieCode.prototype, /** @lends JXG.JessieCode.prototype */ {
850850
ast = this.CA.removeTrivialNodes(ast);
851851
}
852852
if (this.CAS) {
853-
// Search for expression of form `D(f, x)` and determine the
853+
// Search for expression of form `D(f, x)` and determine the
854854
// the derivative symbolically.
855855
ast = this.CAS.expandDerivatives(ast, null, ast);
856+
856857
// options.method = options.method || "strong";
857858
// options.form = options.form || "fractions";
858859
// options.steps = options.steps || [];
@@ -866,6 +867,18 @@ JXG.extend(JXG.JessieCode.prototype, /** @lends JXG.JessieCode.prototype */ {
866867
case 'manipulate':
867868
result = this.compile(ast);
868869
break;
870+
case 'simplify':
871+
if (Type.exists(this.CAS)) {
872+
options.method = options.method || "strong";
873+
options.form = options.form || "fractions";
874+
options.steps = options.steps || [];
875+
options.iterations = options.iterations || 1000;
876+
ast = this.CAS.simplify(ast, options);
877+
result = this.CAS.compile(ast);
878+
} else {
879+
result = this.compile(ast);
880+
}
881+
break;
869882
case 'getAst':
870883
result = ast;
871884
break;
@@ -916,6 +929,21 @@ JXG.extend(JXG.JessieCode.prototype, /** @lends JXG.JessieCode.prototype */ {
916929
return this._genericParse(code, 'manipulate', geonext, dontstore);
917930
},
918931

932+
/**
933+
* Manipulate JessieCode.
934+
* This consists of generating an AST with parser.parse,
935+
* apply simplifying rules from CAS
936+
* and compile the AST back to JessieCode with minimal number of parentheses.
937+
*
938+
* @param {String} code JessieCode code to be parsed
939+
* @param {Boolean} [geonext=false] Geonext compatibility mode.
940+
* @param {Boolean} [dontstore=false] If false, the code string is stored in this.code.
941+
* @return {String} Simplified JessieCode code
942+
*/
943+
simplify: function (code) {
944+
return this._genericParse(code, 'simplify');
945+
},
946+
919947
/**
920948
* Get abstract syntax tree (AST) from JessieCode code.
921949
* This consists of generating an AST with parser.parse.

0 commit comments

Comments
 (0)