Skip to content

Commit

Permalink
fix index problem & add square matrix boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
DXie123 committed Nov 7, 2019
1 parent 95dcbda commit dd98e9f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
2 changes: 0 additions & 2 deletions assets/html/homepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@

function addString(message) {
mathField.write(message);
postLaTex();
}

function delString() {
Expand All @@ -97,7 +96,6 @@
function simulateKey(key) {
mathField.keystroke(key);
isClearable = false;
postLaTex();
postClearable();
}

Expand Down
21 changes: 15 additions & 6 deletions lib/src/latex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);

Expand All @@ -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;

Expand All @@ -396,6 +394,12 @@ class MatrixParser extends Parser {
i++;
}
}

if (_stream.length == 1 && _stream[0][1] == 'b') {
single = true;
} else {
single = false;
}
}

void matrixParse() {
Expand All @@ -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;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/mathbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
19 changes: 7 additions & 12 deletions lib/src/mathmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class MathModel with ChangeNotifier {
}

String checkHistory({@required toPrevious}) {
// TODO: Have index problem
if (toPrevious) {
if (_resultIndex>0) {
_resultIndex--;
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions lib/src/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class MatrixButton extends StatelessWidget {
child: Text('Calculate'),
),
Consumer<MatrixModel>(
builder: (_, model, child) => model.single?
builder: (_, model, child) => model.square?
SingleMatrixButton(
child: child,
onPressed: () {
Expand All @@ -124,30 +124,30 @@ class MatrixButton extends StatelessWidget {
child: Text('Invert'),
),
Consumer<MatrixModel>(
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<MatrixModel>(
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'),
Expand Down

0 comments on commit dd98e9f

Please sign in to comment.