Skip to content

Bump braces from 3.0.2 to 3.0.3 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,14 @@ B = matrix([[3,4], [1,2]]);
A.equals(B);
// returns false
```

#### 19. Generate
Generates a matrix with the value passed across the entire matrix or just the diagonal.
```javascript
matrix.gen(4).size(2,3); // returns [[4,4,4],[4,4,4]]
matrix.gen(2).size(2); // returns [[2,2],[2,2]]
matrix.gen(1).diag(3); // return [[1,0,0],[0,1,0],[0,0,1]], identity matrix
// Rectangular matrices' diagonal elements are filled with the remaining rows and columns as zero. But diagonal matrices are normally square.
matrix.gen(1).diag(3,2); // returns [[1,0],[0,1],[0,0]]
matrix.gen(2).diag(3,4); // returns [[2,0,0,0],[0,2,0,0],[0,0,2,0]
```
43 changes: 43 additions & 0 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

function generate(val) {
return {
size: (row, col) => size(val, row, col),
diag: (row, col) => diag(val, row, col)
}
}

function size(val, row, col) {
if (!col) {
col = row;
}
let rows = [];
for (let i = 0; i < row; i++) {
let cols = [];
for (let j = 0; j < col; j++) {
cols[j] = val;
}
rows[i] = cols;
}
return rows;
}

function diag(val, row, col) {
if (!col) {
col = row;
}
let rows = [];
for (let i = 0; i < row; i++) {
let cols = [];
for (let j = 0; j < col; j++) {
cols[j] = 0;
}
rows[i] = cols;
if (i < col || row == col) {
rows[i][i] = val;
}
}
return rows;
}

module.exports = generate;
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const rational = require('./rational');
const merge = require('./merge');
const generate = require('./generate');

/**
* Pass a 2-dimensional array that will return a function accepting indices to access the matrix
Expand All @@ -20,6 +21,7 @@ function matrix(mat) {
return Object.assign(_matrix, _mat(mat));
}

matrix.gen = generate;

/**
* Private function that returns an object containing methods
Expand Down Expand Up @@ -511,7 +513,7 @@ function derationalize(mat) {
* @param val specified value
* @returns square matrix
*/
function generate(size, val) {
function _generate(size, val) {
let dim = 2;
while (dim > 0) {
var arr = [];
Expand All @@ -535,7 +537,7 @@ function generate(size, val) {
* @returns identity matrix
*/
function identity(size) {
let result = generate(size, 0);
let result = _generate(size, 0);
result.forEach((row, index) => {
row[index] = 1;
});
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "matrix-js",
"version": "1.6.1",
"version": "1.7.0",
"description": "A Javascript Library to perform basic matrix operations using the functional nature of Javascript",
"main": "lib",
"directories": {
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,12 @@ describe('Matrix operations', () => {
assert.equal(A.equals(B), false);
assert.equal(C.equals(C), true);
});

it('should generate a matrix', () => {
assert.deepEqual(matrix.gen(4).size(2,3), [[4,4,4],[4,4,4]]);
assert.deepEqual(matrix.gen(2).size(2), [[2,2],[2,2]]);
assert.deepEqual(matrix.gen(1).diag(3,2), [[1,0],[0,1],[0,0]]);
assert.deepEqual(matrix.gen(2).diag(3,4), [[2,0,0,0],[0,2,0,0],[0,0,2,0]]);
assert.deepEqual(matrix.gen(1).diag(3), [[1,0,0],[0,1,0],[0,0,1]]);
});
});