Skip to content

Commit

Permalink
Changed matrix indexes of expression parser to one-based (Fixed #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Aug 29, 2013
1 parent c8346b2 commit 9746dc3
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 628 deletions.
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
https://github.com/josdejong/mathjs


## 2013-08-22, version 0.13.0

- Changed matrix indexes of the expression parser to one-based with the
upper-bound included, similar to most math applications. Note that on a
JavaScript level, math.js uses zero-based indexes with excluded upper-bound.


## 2013-08-22, version 0.12.1

- Fixed outdated version of README.md.
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,21 @@ var c = math.multiply(a, b); // Matrix, [[19, 22], [43, 50]]
var d = c.get(math.index(1, 0)); // 43
```

Matrices are supported by the parser:
Matrices are supported by the parser. *IMPORTANT:* matrix indexes and ranges work
different from the indexes in JavaScript: They are one-based with an included
upper-bound, similar to most math applications.


```js
parser = math.parser();

parser.eval('a = [1, 2; 3, 4]'); // Matrix, [[1, 2], [3, 4]]
parser.eval('b = zeros(2, 2)'); // Matrix, [[0, 0], [0, 0]]
parser.eval('b[0, 0:2] = [5, 6]'); // Matrix, [[5, 6], [0, 0]]
parser.eval('b[1, :] = [7, 8]'); // Matrix, [[5, 6], [7, 8]]
parser.eval('b(1, 1:2) = [5, 6]'); // Matrix, [[5, 6], [0, 0]]
parser.eval('b(2, :) = [7, 8]'); // Matrix, [[5, 6], [7, 8]]
parser.eval('c = a * b'); // Matrix, [[19, 22], [43, 50]]
parser.eval('d = c[1, 0]'); // 43
parser.eval('e = c[1, 0:end]'); // Matrix, [[43, 50]]
parser.eval('d = c(2, 1)'); // 43
parser.eval('e = c(2, 1:end)'); // Matrix, [[43, 50]]
```


Expand Down
555 changes: 220 additions & 335 deletions dist/math.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions dist/math.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions examples/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ print(parser.eval('x + 3')); // 6.5
print(parser.eval('function f(x, y) = x^y')); // f(x, y)
print(parser.eval('f(2, 3)')); // 8

// manipulate matrices
// Note that matrix indexes in the expression parser are one-based with the
// upper-bound included. On a JavaScript level however, math.js uses zero-based
// indexes with an excluded upper-bound.
console.log('\nmanipulate matrices');
print(parser.eval('k = [1, 2; 3, 4]')); // [[1, 2], [3, 4]]
print(parser.eval('l = zeros(2, 2)')); // [[0, 0], [0, 0]]
print(parser.eval('l(1, 1:2) = [5, 6]')); // [[5, 6], [0, 0]]
print(parser.eval('l(2, :) = [7, 8]')); // [[5, 6], [7, 8]]
print(parser.eval('m = k * l')); // [[19, 22], [43, 50]]
print(parser.eval('n = m(2, 1)')); // 43
print(parser.eval('n = m(:, 1)')); // [[19], [43]]

// get and set variables and functions
console.log('\nget and set variables and function in the scope of the parser');
var x = parser.get('x'); // x = 7
Expand Down
11 changes: 0 additions & 11 deletions examples/matrices.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,3 @@ print(math.range(0, 18, 3)); // [0, 3, 6, 9, 12, 15]
print(math.range('2:-1:-3')); // [2, 1, 0, -1, -2]
print(math.factorial(math.range('1:6'))); // [1, 2, 6, 24, 120]
console.log();

console.log('use the expression parser');
var parser = math.parser();
print(parser.eval('d = [1, 2; 3, 4]')); // [[1, 2], [3, 4]]
print(parser.eval('e = zeros(2, 2)')); // [[0, 0], [0, 0]]
print(parser.eval('e[0, 0:2] = [5, 6]')); // [[5, 6], [0, 0]]
print(parser.eval('e[1, :] = [7, 8]')); // [[5, 6], [7, 8]]
print(parser.eval('f = d * e')); // [[19, 22], [43, 50]]
print(parser.eval('g = f[1, 0]')); // 43
print(parser.eval('g = f[:, 0]')); // [[19], [43]]
console.log();
4 changes: 2 additions & 2 deletions lib/docs/function/matrix/diag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = {
],
'description': 'Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned.When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.',
'examples': [
'diag(1:4)',
'diag(1:4, 1)',
'diag(1:3)',
'diag(1:3, 1)',
'a = [1, 2, 3; 4, 5, 6; 7, 8, 9]',
'diag(a)'
],
Expand Down
8 changes: 4 additions & 4 deletions lib/docs/function/matrix/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module.exports = {
'Create a range. Lower bound of the range is included, upper bound is excluded.',
'examples': [
'1:5',
'3:-1:-4',
'3:-1:-3',
'range(3, 7)',
'range(0, 12, 2)',
'range("4:11")',
'a = [0, 1, 2, 3; 4, 5, 6]',
'a(1:2)'
'range("4:10")',
'a = [1, 2, 3, 4; 5, 6, 7, 8]',
'a(1:2, 1:2)'
],
'seealso': [
'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'size', 'squeeze', 'subset', 'transpose', 'zeros'
Expand Down
8 changes: 4 additions & 4 deletions lib/docs/function/matrix/subset.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module.exports = {
'examples': [
'd = [1, 2; 3, 4]',
'e = []',
'e[0, 0:2] = [5, 6]',
'e[1, :] = [7, 8]',
'e(1, 1:2) = [5, 6]',
'e(2, :) = [7, 8]',
'f = d * e',
'f[1, 0]',
'f[:, 0]'
'f(2, 1)',
'f(:, 1)'
],
'seealso': [
'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'transpose', 'zeros'
Expand Down
141 changes: 0 additions & 141 deletions lib/expr/node/IndexNode.js

This file was deleted.

34 changes: 33 additions & 1 deletion lib/expr/node/ParamsNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,39 @@ ParamsNode.prototype.eval = function() {
return obj.apply(this, results);
}
else {
throw new TypeError('Function expected');
// evaluate the parameters as index
params = this.params;
results = [];
for (i = 0, len = this.params.length; i < len; i++) {
var param = params[i];
var result;

if (param instanceof RangeNode) {
result = param.toRange();
}
else {
result = param.eval();
}

// change from one-based to zero-based range
if (result instanceof Range) {
result.start --;
result.end --;
}
else if (isNumber(result)) {
// number
result--;
}
else {
throw new TypeError('Number or Range expected');
}

results[i] = result;
}

// get a subset of the object
var index = Index.create(results);
return this.math.subset(obj, index);
}
};

Expand Down
Loading

0 comments on commit 9746dc3

Please sign in to comment.