Skip to content

Commit 1a972de

Browse files
committed
Adding generate functionality
1 parent f5d4edf commit 1a972de

6 files changed

Lines changed: 69 additions & 5 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,14 @@ B = matrix([[3,4], [1,2]]);
132132
A.equals(B);
133133
// returns false
134134
```
135+
136+
#### 19. Generate
137+
Generates a matrix with the value passed across the entire matrix or just the diagonal.
138+
```javascript
139+
matrix.gen(4).size(2,3); // returns [[4,4,4],[4,4,4]]
140+
matrix.gen(2).size(2); // returns [[2,2],[2,2]]
141+
matrix.gen(1).diag(3); // return [[1,0,0],[0,1,0],[0,0,1]], identity matrix
142+
// Diagonal matrices are normally square. Here, only diagonal elements are filled where row and column indices are same.
143+
matrix.gen(1).diag(3,2); // returns [[1,0],[0,1],[0,0]]
144+
matrix.gen(2).diag(3,4); // returns [[2,0,0,0],[0,2,0,0],[0,0,2,0]
145+
```

lib/generate.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
function generate(val) {
4+
return {
5+
size: (row, col) => size(val, row, col),
6+
diag: (row, col) => diag(val, row, col)
7+
}
8+
}
9+
10+
function size(val, row, col) {
11+
if (!col) {
12+
col = row;
13+
}
14+
let rows = [];
15+
for (let i = 0; i < row; i++) {
16+
let cols = [];
17+
for (let j = 0; j < col; j++) {
18+
cols[j] = val;
19+
}
20+
rows[i] = cols;
21+
}
22+
return rows;
23+
}
24+
25+
function diag(val, row, col) {
26+
if (!col) {
27+
col = row;
28+
}
29+
let rows = [];
30+
for (let i = 0; i < row; i++) {
31+
let cols = [];
32+
for (let j = 0; j < col; j++) {
33+
cols[j] = 0;
34+
}
35+
rows[i] = cols;
36+
if (i < col || row == col) {
37+
rows[i][i] = val;
38+
}
39+
}
40+
return rows;
41+
}
42+
43+
module.exports = generate;

lib/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const rational = require('./rational');
44
const merge = require('./merge');
5+
const generate = require('./generate');
56

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

24+
matrix.gen = generate;
2325

2426
/**
2527
* Private function that returns an object containing methods
@@ -511,7 +513,7 @@ function derationalize(mat) {
511513
* @param val specified value
512514
* @returns square matrix
513515
*/
514-
function generate(size, val) {
516+
function _generate(size, val) {
515517
let dim = 2;
516518
while (dim > 0) {
517519
var arr = [];
@@ -535,7 +537,7 @@ function generate(size, val) {
535537
* @returns identity matrix
536538
*/
537539
function identity(size) {
538-
let result = generate(size, 0);
540+
let result = _generate(size, 0);
539541
result.forEach((row, index) => {
540542
row[index] = 1;
541543
});

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "matrix-js",
3-
"version": "1.6.1",
3+
"version": "1.7.0",
44
"description": "A Javascript Library to perform basic matrix operations using the functional nature of Javascript",
55
"main": "lib",
66
"directories": {

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,12 @@ describe('Matrix operations', () => {
153153
assert.equal(A.equals(B), false);
154154
assert.equal(C.equals(C), true);
155155
});
156+
157+
it('should generate a matrix', () => {
158+
assert.deepEqual(matrix.gen(4).size(2,3), [[4,4,4],[4,4,4]]);
159+
assert.deepEqual(matrix.gen(2).size(2), [[2,2],[2,2]]);
160+
assert.deepEqual(matrix.gen(1).diag(3,2), [[1,0],[0,1],[0,0]]);
161+
assert.deepEqual(matrix.gen(2).diag(3,4), [[2,0,0,0],[0,2,0,0],[0,0,2,0]]);
162+
assert.deepEqual(matrix.gen(1).diag(3), [[1,0,0],[0,1,0],[0,0,1]]);
163+
});
156164
});

0 commit comments

Comments
 (0)