diff --git a/assets/html/homepage.html b/assets/html/homepage.html index 896f2ea..0390a8c 100644 --- a/assets/html/homepage.html +++ b/assets/html/homepage.html @@ -77,7 +77,6 @@ function addString(message) { mathField.write(message); - postLaTex(); } function delString() { @@ -97,7 +96,6 @@ function simulateKey(key) { mathField.keystroke(key); isClearable = false; - postLaTex(); postClearable(); } diff --git a/lib/src/latex.dart b/lib/src/latex.dart index 25cdbcd..5c4089a 100644 --- a/lib/src/latex.dart +++ b/lib/src/latex.dart @@ -365,16 +365,14 @@ class MatrixParser extends Parser { final String inputString; final bool isRadMode; final int precision; - - int get length => _stream.length; + bool square; + bool single; MatrixParser(this.inputString, {this.isRadMode = true, this.precision = 10}) : super(inputString, isRadMode); @override void tokenize() { - final matrix = (string('\\begin{bmatrix}') & any().starLazy(string('\\end{bmatrix}')).flatten() & string('\\end{bmatrix}')).pick(1); - - final basic = matrix.map((v)=>[v, 'b']); + final matrix = (string('\\begin{bmatrix}') & any().starLazy(string('\\end{bmatrix}')).flatten() & string('\\end{bmatrix}')).pick(1).map((v)=>[v, 'b']); final plus = char('+').map((v)=>[v, ['o', 2]]); @@ -386,7 +384,7 @@ class MatrixParser extends Parser { final oper = plus | minus | times | divide; - final tokenize = (basic | oper).star().end(); + final tokenize = (matrix | oper).star().end(); _stream = tokenize.parse(inputString).value; @@ -396,6 +394,12 @@ class MatrixParser extends Parser { i++; } } + + if (_stream.length == 1 && _stream[0][1] == 'b') { + single = true; + } else { + single = false; + } } void matrixParse() { @@ -414,6 +418,11 @@ class MatrixParser extends Parser { } } _stream[i][0] = Matrix(source); + if (single && _stream[i][0].m == _stream[i][0].n) { + square = true; + } else { + square = false; + } } } } diff --git a/lib/src/mathbox.dart b/lib/src/mathbox.dart index 4cefa45..28a71ec 100644 --- a/lib/src/mathbox.dart +++ b/lib/src/mathbox.dart @@ -106,7 +106,7 @@ class MathBox extends StatelessWidget { onMessageReceived: (JavascriptMessage message) { if (message.message.contains('matrix')) { matrixModel.updateExpression(message.message); - } else { + } else if (message.message.isNotEmpty) { mathModel.updateExpression(message.message); mathModel.calcNumber(); } diff --git a/lib/src/mathmodel.dart b/lib/src/mathmodel.dart index a0fd12b..98cb653 100644 --- a/lib/src/mathmodel.dart +++ b/lib/src/mathmodel.dart @@ -63,7 +63,6 @@ class MathModel with ChangeNotifier { } String checkHistory({@required toPrevious}) { - // TODO: Have index problem if (toPrevious) { if (_resultIndex>0) { _resultIndex--; @@ -98,43 +97,39 @@ class MatrixModel with ChangeNotifier { int _precision; bool _isRadMode; bool single = true; + bool square = true; + Matrix matrix; - get result => _result.last; + bool get result => _result.last; void updateExpression(String expression) { _matrixExpression.last = expression; - final mp = MatrixParser(_matrixExpression.last)..tokenize(); - single = mp.length>1?false:true; + final mp = MatrixParser(_matrixExpression.last, precision: _precision); + matrix = mp.parse(); + single = mp.single; + square = mp.square; notifyListeners(); } void calc() { - final mp = MatrixParser(_matrixExpression.last, precision: _precision); - Matrix matrix = mp.parse(); _result.last = matrix; _matrixExpression.add(_matrixExpression.last); _result.add(_result.last); } void norm() { - final mp = MatrixParser(_matrixExpression.last, precision: _precision); - Matrix matrix = mp.parse(); _result.last = matrix.det(); _matrixExpression.add(_matrixExpression.last); _result.add(_result.last); } void transpose() { - final mp = MatrixParser(_matrixExpression.last, precision: _precision); - Matrix matrix = mp.parse(); _result.last = matrix.transpose(); _matrixExpression.add(_matrixExpression.last); _result.add(_result.last); } void invert() { - final mp = MatrixParser(_matrixExpression.last, precision: _precision); - Matrix matrix = mp.parse(); _result.last = matrix.inverse(); _matrixExpression.add(_matrixExpression.last); _result.add(_result.last); diff --git a/lib/src/result.dart b/lib/src/result.dart index 597eda4..6bd6436 100644 --- a/lib/src/result.dart +++ b/lib/src/result.dart @@ -111,7 +111,7 @@ class MatrixButton extends StatelessWidget { child: Text('Calculate'), ), Consumer( - builder: (_, model, child) => model.single? + builder: (_, model, child) => model.square? SingleMatrixButton( child: child, onPressed: () { @@ -124,30 +124,30 @@ class MatrixButton extends StatelessWidget { child: Text('Invert'), ), Consumer( - builder: (_, model, child) => model.single? + builder: (_, model, child) => model.square? SingleMatrixButton( child: child, onPressed: () { - model.transpose(); + model.norm(); mathBoxController.deleteAllExpression(); - mathBoxController.addString(model.display()); + mathBoxController.addString(model.result.toString()); }, ): SizedBox(height: 0.0,), - child: Text('Transpose'), + child: Text('Norm'), ), Consumer( builder: (_, model, child) => model.single? SingleMatrixButton( child: child, onPressed: () { - model.norm(); + model.transpose(); mathBoxController.deleteAllExpression(); - mathBoxController.addString(model.result.toString()); + mathBoxController.addString(model.display()); }, ): SizedBox(height: 0.0,), - child: Text('Norm'), + child: Text('Transpose'), ), SingleMatrixButton( child: Text('Add Row'),