Skip to content

Commit

Permalink
sum, product, min, max, version 0.5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
doleron committed Jun 30, 2020
1 parent 9042ea0 commit d1671e5
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ const { Matrix } = require('matrix-reef.js');
- accessing: [get, set](examples/accessing.md#constructors), row, col, diagonal and path.
- arithmetics: [add, subtract](examples/arithmetics.md#get-and-set), [add scalar](examples/arithmetics.md#adding-scalar-to-matrix), [add row](examples/arithmetics.md#adding-row-or-column-to-matrix) to matrix and [add column](examples/arithmetics.md#adding-row-or-column-to-matrix) to matrix.
- multiplication: [multiply by scalar](multiplication.md#multiply-by-scalar), [dot multiply](multiplication.md#dot-multiplication) and [matrix multiplication](multiplication.md#matrix-multiplication).
- functional: map, max, min, sum, product, rowWise, colWise and chaining calls.
- matrices: [dimension](examples/matrices.md#dimension), [rows, cols](examples/matrices.md#rows-and-cols), transpose, determinant, inverse, reshape, flatten, rotate90, rotate180 and rank.
- query: isSquared, isDiagonal, isUpperTriangular, isLowerTriangular
- functional: [map](examples/functional.md#map), [max](examples/functional.md#max), [min](examples/functional.md#min), [sum](examples/functional.md#sum), [product](examples/functional.md#product), rowWise, colWise and [chaining](examples/functional.md#chaining) calls.
- matrices: [dimension](examples/matrices.md#dimension), [rows, cols](examples/matrices.md#rows-and-cols), [transpose](examples/matrices.md#transpose), determinant, inverse, reshape, flatten, rotate90, rotate180 and rank.
- query: isSquared, isDiagonal, isUpperTriangular and isLowerTriangular.
- memory saving: in-place operations, slices and views.
- optimization: Matrix2D, Matrix3D, Matrix4D, Vector, RowVector, ColumnVector, Vector2D, Vector3D, Vector4D and Sparse Matrix.
- miscellaneous: print out, convert to array, shuffle and comparing.
Expand Down
80 changes: 80 additions & 0 deletions examples/functional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# matrix-specific features

## map
Map applies a function f(number) => number to each element of the matrix
```javascript
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);

const B = A.map(Math.exp);
// B is [ 2.718281828459045, 7.3890560989306, 20.0855369231876],
// [54.598150033144236, 148.4131591025766, 403.4287934927351]]
```
## max
max returns an array [maxValue, i, j] where i,j are the index of the first max occurrence.
```javascript
const A = new Matrix([
[1, 2, 3],
[4, 9, 6],
]);

const result = A.max();
// result is [9, 1, 1]

```
## min
min returns an array [minValue, i, j] where i,j are the index of the first min occurrence.
```javascript
const A = new Matrix([
[1, 2, 0],
[4, 9, 6],
]);

const result = A.min();
// result is [0, 0, 2]

```
## sum

```javascript
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);

const s = A.sum();
// s is the sum of all elements, that is 21

```
## product
```javascript
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);

const p = A.product();
// p is the product of all elements, that is 720
```
## chaining
```javascript
const A = new Matrix([
[1, 2],
[3, 4],
[5, 6]
]);
const B = new Matrix([
[2, 1],
[2, 0]
]);
const C = new Matrix([
[2, 2],
[3, 3],
[4, 4]
]);

const D = A.multiply(B).add(C);
// D is [[8, 3 ], [17, 6], [26, 9]]
```
12 changes: 12 additions & 0 deletions examples/matrices.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ const dimension = A.dim();
// dimension is [2, 3]

```
## transpose

```javascript
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);

const B = A.transpose();
// B is [[1, 4], [2, 5], [3, 6]]

```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "matrix-reef.js",
"version": "0.5.1",
"version": "0.5.5",
"description": "Fast Matrix Library for Java Script",
"main": "index.js",
"directories": {
Expand Down
16 changes: 16 additions & 0 deletions src/api/highlevelapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ function _Matrix(data, rows, cols) {
return [this._rows, this._cols];
}

this.max = function() {
return lowLevelApi.max(this.storage, this._rows, this._cols);
}

this.min = function() {
return lowLevelApi.min(this.storage, this._rows, this._cols);
}

this.sum = function() {
return lowLevelApi.sum(this.storage);
}

this.product = function() {
return lowLevelApi.product(this.storage);
}

}

function _repeat(rows, cols, value) {
Expand Down
49 changes: 49 additions & 0 deletions src/api/lowlevelapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,55 @@ module.exports = {
set : function(data, rows, cols, r, c, val) {
const index = cols * r + c;
data[index] = val;
},

max : function(storage, rows, cols) {
if (storage.length > 0) {
let index = 0;
let maxValue = storage[0]
for (var r = 1, size = storage.length; r < size; ++r) {
const val = storage[r];
if (maxValue < val) {
index = r;
maxValue = val;
}
}
const i = Math.floor(index / cols);
return [maxValue, i, index - (i * cols)];
} else return false;
},

min : function(storage, rows, cols) {
if (storage.length > 0) {
let index = 0;
let minValue = storage[0]
for (var r = 1, size = storage.length; r < size; ++r) {
const val = storage[r];
if (minValue > val) {
index = r;
minValue = val;
}
}
const i = Math.floor(index / cols);
return [minValue, i, index - (i * cols)];
} else return false;
},

sum : function(storage) {
let result = 0.;
for (var r = 0, size = storage.length; r < size; ++r) {
result += storage[r];
}
return result;
},

product : function(storage) {
const size = storage.length;
let result = size ? 1. : 0.;
for (var r = 0; r < size; ++r) {
result *= storage[r];
}
return result;
}

}
2 changes: 1 addition & 1 deletion test/arithmetics/multiplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('basic multiplications', function () {
*/
describe('blind check multiplication', function () {

const size = 100000;
const size = 1000;

this.timeout(size / 2);

Expand Down
102 changes: 101 additions & 1 deletion test/operations/operations.js → test/functional/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('map', function () {
const expected = [[2, 8], [4, 10], [6, 12]];
const A = new Matrix([[1, 4], [2, 5], [3, 6]]);
const C = zeros(3, 2);
A.map(function(val) {return val * 2;}, C);
A.map(function (val) { return val * 2; }, C);
assert.ok(testUtils.compare(C, expected, 1e-8));
} catch (e) {
assert.fail(e.message);
Expand Down Expand Up @@ -131,4 +131,104 @@ describe('get set', function () {

});

});

describe('sum, product', function () {

it("basic", function () {

const A = new Matrix([
[1, 2],
[3, 4],
[5, 6]
]);

const B = new Matrix([
[2, 1],
[2, -11]
]);

const s = A.sum();
const p = B.product();

assert.equal(21, s);
assert.equal(-44, p);

assert.equal(0, new Matrix([]).product());

});

});

describe('max', function () {

it("max", function () {
const A = new Matrix([
[1, 2, 3],
[4, 0, 6],
[4, 9, 9],
]);

const result = A.max();
assert.equal(9, result[0]);
assert.equal(2, result[1]);
assert.equal(1, result[2]);
});

it("linear", function () {
const A = new Matrix([
1, 2, 3,
4, 0, 6,
4, 9, 9,
]);

const result = A.max();
assert.equal(9, result[0]);
assert.equal(7, result[1]);
assert.equal(0, result[2]);
});

it("empty", function () {
const A = new Matrix([]);
const result = A.max();
assert.ok(!result);
});

});

describe('min', function () {

it("min", function () {
const A = new Matrix([
[1, -2, 3],
[4, 0, -3],
[4, -3, 1],
]);

const result = A.min();
assert.equal(-3, result[0]);
assert.equal(1, result[1]);
assert.equal(2, result[2]);

});

it("linear", function () {
const A = new Matrix([
1, 2, 3,
4, 0, 6,
4, 9, 9,
]);

const result = A.min();
assert.equal(0, result[0]);
assert.equal(4, result[1]);
assert.equal(0, result[2]);
});

it("empty", function () {
const A = new Matrix([]);
const result = A.min();
assert.ok(!result);
});

});

0 comments on commit d1671e5

Please sign in to comment.