diff --git a/lib/matrix.js b/lib/matrix.js index b232d36..62fade9 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -8,14 +8,14 @@ class Matrix { this.data = Array(this.rows).fill().map(() => Array(this.cols).fill(0)); } - copy() { - let m = new Matrix(this.rows, this.cols); - for (let i = 0; i < this.rows; i++) { - for (let j = 0; j < this.cols; j++) { - m.data[i][j] = this.data[i][j]; + static copy(matrix) { + let matrixCopy = new Matrix(matrix.rows, matrix.cols); + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.cols; j++) { + matrixCopy.data[i][j] = matrix.data[i][j]; } } - return m; + return matrixCopy; } static fromArray(arr) { diff --git a/lib/matrix.test.js b/lib/matrix.test.js index 49fc653..9b6fdee 100644 --- a/lib/matrix.test.js +++ b/lib/matrix.test.js @@ -381,7 +381,7 @@ test('matrix copy', () => { let m = new Matrix(5, 5); m.randomize(); - let n = m.copy(); + let n = Matrix.copy(m); expect(n).toEqual({ rows: m.rows, diff --git a/lib/nn.js b/lib/nn.js index 0cb4527..2ceccb8 100644 --- a/lib/nn.js +++ b/lib/nn.js @@ -30,11 +30,10 @@ class NeuralNetwork { this.hidden_nodes = a.hidden_nodes; this.output_nodes = a.output_nodes; - this.weights_ih = a.weights_ih.copy(); - this.weights_ho = a.weights_ho.copy(); - - this.bias_h = a.bias_h.copy(); - this.bias_o = a.bias_o.copy(); + this.weights_ih = Matrix.copy(a.weights_ih); + this.weights_ho = Matrix.copy(a.weights_ho); + this.bias_h = Matrix.copy(a.bias_h); + this.bias_o = Matrix.copy(a.bias_o); } else { this.input_nodes = in_nodes; this.hidden_nodes = hid_nodes;