-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathMatrix.js
More file actions
2124 lines (2037 loc) · 77.6 KB
/
Matrix.js
File metadata and controls
2124 lines (2037 loc) · 77.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module Math
*/
import { Vector } from '../p5.Vector';
import { MatrixInterface } from './MatrixInterface';
const isPerfectSquare = arr => {
const sqDimention = Math.sqrt(arr.length);
if (sqDimention % 1 !== 0) {
throw new Error('Array length must be a perfect square.');
}
return true;
};
export let GLMAT_ARRAY_TYPE = Array;
export let isMatrixArray = x => Array.isArray(x);
if (typeof Float32Array !== 'undefined') {
GLMAT_ARRAY_TYPE = Float32Array;
isMatrixArray = x => Array.isArray(x) || x instanceof Float32Array;
}
export class Matrix extends MatrixInterface {
matrix;
#sqDimention;
constructor(...args) {
super(...args);
// This is default behavior when object
// instantiated using createMatrix()
if (isMatrixArray(args[0]) && isPerfectSquare(args[0])) {
const sqDimention = Math.sqrt(args[0].length);
this.#sqDimention = sqDimention;
this.matrix = GLMAT_ARRAY_TYPE.from(args[0]);
} else if (typeof args[0] === 'number') {
this.#sqDimention = Number(args[0]);
this.matrix = this.#createIdentityMatrix(args[0]);
}
return this;
}
/**
* Returns the 3x3 matrix if the dimensions are 3x3, otherwise returns `undefined`.
*
* This method returns the matrix if its dimensions are 3x3.
* If the matrix is not 3x3, it returns `undefined`.
*
* @returns {Array|undefined} The 3x3 matrix or `undefined` if the matrix is not 3x3.
*/
get mat3() {
if (this.#sqDimention === 3) {
return this.matrix;
} else {
return undefined;
}
}
/**
* Returns the 4x4 matrix if the dimensions are 4x4, otherwise returns `undefined`.
*
* This method returns the matrix if its dimensions are 4x4.
* If the matrix is not 4x4, it returns `undefined`.
*
* @returns {Array|undefined} The 4x4 matrix or `undefined` if the matrix is not 4x4.
*/
get mat4() {
if (this.#sqDimention === 4) {
return this.matrix;
} else {
return undefined;
}
}
/**
* Adds the corresponding elements of the given matrix to this matrix, if the dimentions are the same.
*
* @param {Matrix} matrix - The matrix to add to this matrix. It must have the same dimensions as this matrix.
* @returns {Matrix} The resulting matrix after addition.
* @throws {Error} If the matrices do not have the same dimensions.
*
* @example
* const matrix1 = new p5.Matrix([1, 2, 3]);
* const matrix2 = new p5.Matrix([4, 5, 6]);
* matrix1.add(matrix2); // matrix1 is now [5, 7, 9]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix1 = new p5.Matrix([1, 2, 3, 4]);
* const matrix2 = new p5.Matrix([5, 6, 7, 8]);
* matrix1.add(matrix2);
* console.log(matrix1.matrix); // Output: [6, 8, 10, 12]
* }
* </code></div>
*/
add(matrix) {
if (this.matrix.length !== matrix.matrix.length) {
throw new Error('Matrices must be of the same dimension to add.');
}
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] += matrix.matrix[i];
}
return this;
}
/**
* Sets the value of a specific element in the matrix in column-major order.
*
* A matrix is stored in column-major order, meaning elements are arranged column by column.
* This function allows you to update or change the value of a specific element
* in the matrix by specifying its index in the column-major order and the new value.
*
* Parameters:
* - `index` (number): The position in the matrix where the value should be set.
* Indices start from 0 and follow column-major order.
* - `value` (any): The new value you want to assign to the specified element.
*
* Example:
* If you have the following 3x3 matrix stored in column-major order:
* ```
* [
* 1, 4, 7, // Column 1
* 2, 5, 8, // Column 2
* 3, 6, 9 // Column 3
* ]
* ```
* Calling `setElement(4, 10)` will update the element at index 4
* (which corresponds to row 2, column 2 in row-major order) to `10`.
* The updated matrix will look like this:
* ```
* [
* 1, 4, 7,
* 2, 10, 8,
* 3, 6, 9
* ]
* ```
*
* This function is useful for modifying specific parts of the matrix without
* having to recreate the entire structure.
*
* @param {Number} index - The position in the matrix where the value should be set.
* Must be a non-negative integer less than the length of the matrix.
* @param {Number} value - The new value to be assigned to the specified position in the matrix.
* @returns {Matrix} The current instance of the Matrix, allowing for method chaining.
*
* @example
* // Assuming matrix is an instance of Matrix with initial values [1, 2, 3, 4] matrix.setElement(2, 99);
* // Now the matrix values are [1, 2, 99, 4]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4]);
* matrix.setElement(2, 99);
* console.log(matrix.matrix); // Output: [1, 2, 99, 4]
* }
* </code></div>
*/
setElement(index, value) {
if (index >= 0 && index < this.matrix.length) {
this.matrix[index] = value;
}
return this;
}
/**
* Resets the current matrix to an identity matrix.
*
* This method replaces the current matrix with an identity matrix of the same dimensions.
* An identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere.
* This is useful for resetting transformations or starting fresh with a clean matrix.
*
* @returns {Matrix} The current instance of the Matrix class, allowing for method chaining.
*
* @example
* // Resetting a 4x4 matrix to an identity matrix
* const matrix = new p5.Matrix(4);
* matrix.scale(2, 2, 2); // Apply some transformations
* console.log(matrix.matrix); // Output: Transformed matrix
* matrix.reset(); // Reset to identity matrix
* console.log(matrix.matrix); // Output: Identity matrix
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix(4);
* matrix.scale(2, 2, 2); // Apply scaling transformation
* console.log("Before reset:", matrix.matrix);
* matrix.reset(); // Reset to identity matrix
* console.log("After reset:", matrix.matrix);
* }
* </code></div>
*/
reset() {
this.matrix = this.#createIdentityMatrix(this.#sqDimention);
return this;
}
/**
* Replace the entire contents of a NxN matrix.
*
* This method allows you to replace the values of the current matrix with
* those from another matrix, an array, or individual arguments. The input
* can be a `Matrix` instance, an array of numbers, or individual numbers
* that match the dimensions of the current matrix. The values are copied
* without referencing the source object, ensuring that the original input
* remains unchanged.
*
* If the input dimensions do not match the current matrix, an error will
* be thrown to ensure consistency.
*
* @param {Matrix|Float32Array|Number[]} [inMatrix] - The input matrix, array,
* or individual numbers to replace the current matrix values.
* @returns {Matrix} The current instance of the Matrix class, allowing for
* method chaining.
*
* @example
* // Replacing the contents of a matrix with another matrix
* const matrix1 = new p5.Matrix([1, 2, 3, 4]);
* const matrix2 = new p5.Matrix([5, 6, 7, 8]);
* matrix1.set(matrix2);
* console.log(matrix1.matrix); // Output: [5, 6, 7, 8]
*
* // Replacing the contents of a matrix with an array
* const matrix = new p5.Matrix([1, 2, 3, 4]);
* matrix.set([9, 10, 11, 12]);
* console.log(matrix.matrix); // Output: [9, 10, 11, 12]
*
* // Replacing the contents of a matrix with individual numbers
* const matrix = new p5.Matrix(4); // Creates a 4x4 identity matrix
* matrix.set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
* console.log(matrix.matrix); // Output: [1, 2, 3, ..., 16]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4]);
* console.log("Before set:", matrix.matrix);
* matrix.set([5, 6, 7, 8]);
* console.log("After set:", matrix.matrix); // Output: [5, 6, 7, 8]
* }
* </code></div>
*/
set(inMatrix) {
let refArray = GLMAT_ARRAY_TYPE.from([...arguments]);
if (inMatrix instanceof Matrix) {
refArray = GLMAT_ARRAY_TYPE.from(inMatrix.matrix);
} else if (isMatrixArray(inMatrix)) {
refArray = GLMAT_ARRAY_TYPE.from(inMatrix);
}
if (refArray.length !== this.matrix.length) {
p5._friendlyError(
`Expected same dimensions values but received different ${refArray.length}.`,
'p5.Matrix.set'
);
return this;
}
this.matrix = refArray;
return this;
}
/**
* Gets a copy of the matrix, returns a p5.Matrix object.
*
* This method creates a new instance of the `Matrix` class and copies the
* current matrix values into it. The returned matrix is independent of the
* original, meaning changes to the copy will not affect the original matrix.
*
* This is useful when you need to preserve the current state of a matrix
* while performing operations on a duplicate.
*
* @return {p5.Matrix} A new instance of the `Matrix` class containing the
* same values as the original matrix.
*
* @example
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const originalMatrix = new p5.Matrix([1, 2, 3, 4]);
* const copiedMatrix = originalMatrix.get();
* console.log("Original Matrix:", originalMatrix.matrix); // Output: [1, 2, 3, 4]
* console.log("Copied Matrix:", copiedMatrix.matrix); // Output: [1, 2, 3, 4]
*
* // Modify the copied matrix
* copiedMatrix.setElement(2, 99);
* console.log("Modified Copied Matrix:", copiedMatrix.matrix); // Output: [1, 2, 99, 4]
* console.log("Original Matrix remains unchanged:", originalMatrix.matrix); // Output: [1, 2, 3, 4]
* }
* </code></div>
*/
get() {
return new Matrix(this.matrix); // TODO: Pass p5
}
/**
* Return a copy of this matrix.
* If this matrix is 4x4, a 4x4 matrix with exactly the same entries will be
* generated. The same is true if this matrix is 3x3 or any NxN matrix.
*
* This method is useful when you need to preserve the current state of a matrix
* while performing operations on a duplicate. The returned matrix is independent
* of the original, meaning changes to the copy will not affect the original matrix.
*
* @return {p5.Matrix} The result matrix.
*
* @example
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const originalMatrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const copiedMatrix = originalMatrix.copy();
* console.log("Original Matrix:", originalMatrix.matrix);
* console.log("Copied Matrix:", copiedMatrix.matrix);
*
* // Modify the copied matrix
* copiedMatrix.setElement(4, 99);
* console.log("Modified Copied Matrix:", copiedMatrix.matrix);
* console.log("Original Matrix remains unchanged:", originalMatrix.matrix);
* }
* </code></div>
*/
copy() {
return new Matrix(this.matrix);
}
/**
* Creates a copy of the current matrix instance.
* This method is useful when you need a duplicate of the matrix
* without modifying the original one.
*
* @returns {Matrix} A new matrix instance that is a copy of the current matrix.
*
* @example
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const originalMatrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const clonedMatrix = originalMatrix.clone();
* console.log("Original Matrix:", originalMatrix.matrix);
* console.log("Cloned Matrix:", clonedMatrix.matrix);
*
* // Modify the cloned matrix
* clonedMatrix.setElement(4, 99);
* console.log("Modified Cloned Matrix:", clonedMatrix.matrix);
* console.log("Original Matrix remains unchanged:", originalMatrix.matrix);
* }
* </code></div>
*/
clone() {
return this.copy();
}
/**
* Returns the diagonal elements of the matrix in the form of an array.
* A NxN matrix will return an array of length N.
*
* This method extracts the diagonal elements of the matrix, which are the
* elements where the row index equals the column index. For example, in a
* 3x3 matrix:
* ```
* [
* 1, 2, 3,
* 4, 5, 6,
* 7, 8, 9
* ]
* ```
* The diagonal elements are [1, 5, 9].
*
* This is useful for operations that require the main diagonal of a matrix,
* such as calculating the trace of a matrix or verifying if a matrix is diagonal.
*
* @return {Number[]} An array obtained by arranging the diagonal elements
* of the matrix in ascending order of index.
*
* @example
* // Extracting the diagonal elements of a matrix
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const diagonal = matrix.diagonal(); // [1, 5, 9]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const diagonal = matrix.diagonal();
* console.log("Diagonal elements:", diagonal); // Output: [1, 5, 9]
* }
* </code></div>
*/
diagonal() {
const diagonal = [];
for (let i = 0; i < this.#sqDimention; i++) {
diagonal.push(this.matrix[i * (this.#sqDimention + 1)]);
}
return diagonal;
}
/**
* This function is only for 3x3 matrices A function that returns a row vector of a NxN matrix.
*
* This method extracts a specific row from the matrix and returns it as a `p5.Vector`.
* The row is determined by the `columnIndex` parameter, which specifies the column
* index of the matrix. This is useful for operations that require working with
* individual rows of a matrix, such as row transformations or dot products.
*
* @param {Number} columnIndex - The index of the column to extract as a row vector.
* Must be a non-negative integer less than the matrix dimension.
* @return {p5.Vector} A `p5.Vector` representing the extracted row of the matrix.
*
* @example
* // Extracting a row vector from a 3x3 matrix
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const rowVector = matrix.row(1); // Returns a vector [2, 5, 8]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const rowVector = matrix.row(1); // Extract the second row (index 1)
* console.log("Row Vector:", rowVector.toString()); // Output: Row Vector: [2, 5, 8]
* }
* </code></div>
*/
row(columnIndex) {
const columnVector = [];
for (let i = 0; i < this.#sqDimention; i++) {
columnVector.push(this.matrix[i * this.#sqDimention + columnIndex]);
}
return new Vector(...columnVector);
}
/**
* A function that returns a column vector of a NxN matrix.
*
* This method extracts a specific column from the matrix and returns it as a `p5.Vector`.
* The column is determined by the `rowIndex` parameter, which specifies the row index
* of the matrix. This is useful for operations that require working with individual
* columns of a matrix, such as column transformations or dot products.
*
* @param {Number} rowIndex - The index of the row to extract as a column vector.
* Must be a non-negative integer less than the matrix dimension.
* @return {p5.Vector} A `p5.Vector` representing the extracted column of the matrix.
*
* @example
* // Extracting a column vector from a 3x3 matrix
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const columnVector = matrix.column(1); // Returns a vector [4, 5, 6]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const columnVector = matrix.column(1); // Extract the second column (index 1)
* console.log("Column Vector:", columnVector.toString()); // Output: Column Vector: [4, 5, 6]
* }
* </code></div>
*/
column(rowIndex) {
const rowVector = [];
for (let i = 0; i < this.#sqDimention; i++) {
rowVector.push(this.matrix[rowIndex * this.#sqDimention + i]);
}
return new Vector(...rowVector);
}
/**
* Transposes the given matrix `a` based on the square dimension of the matrix.
*
* This method rearranges the elements of the matrix such that the rows become columns
* and the columns become rows. It handles matrices of different dimensions (4x4, 3x3, NxN)
* by delegating to specific transpose methods for each case.
*
* If no argument is provided, the method transposes the current matrix instance.
* If an argument is provided, it transposes the given matrix `a` and updates the current matrix.
*
* @param {Array} [a] - The matrix to be transposed. It should be a 2D array where each sub-array represents a row.
* If omitted, the current matrix instance is transposed.
* @returns {Matrix} - The current instance of the Matrix class, allowing for method chaining.
*
* @example
* // Transposing a 3x3 matrix
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* matrix.transpose();
* console.log(matrix.matrix); // Output: [1, 4, 7, 2, 5, 8, 3, 6, 9]
*
* // Transposing a 4x4 matrix
* const matrix4x4 = new p5.Matrix(4);
* matrix4x4.transpose();
* console.log(matrix4x4.matrix); // Output: Transposed 4x4 identity matrix
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* console.log("Before transpose:", matrix.matrix);
* matrix.transpose();
* console.log("After transpose:", matrix.matrix); // Output: [1, 4, 7, 2, 5, 8, 3, 6, 9]
* }
* </code></div>
*/
transpose(a) {
if (this.#sqDimention === 4) {
return this.#transpose4x4(a);
} else if (this.#sqDimention === 3) {
return this.#transpose3x3(a);
} else {
return this.#transposeNxN(a);
}
}
/**
* Multiplies the current matrix with another matrix or matrix-like array.
*
* This method supports several types of input:
* - Another Matrix instance
* - A matrix-like array (must be a perfect square, e.g., 4x4 or 3x3)
* - Multiple arguments that form a perfect square matrix
*
* If the input is the same as the current matrix, a copy is made to avoid modifying the original matrix.
*
* The method determines the appropriate multiplication strategy based on the dimensions of the current matrix
* and the input matrix. It supports 3x3, 4x4, and NxN matrices.
*
* @param {Matrix|Array|...number} multMatrix - The matrix or matrix-like array to multiply with.
* @returns {Matrix|undefined} The resulting matrix after multiplication, or undefined if the input is invalid.
* @chainable
*
* @example
* // Multiplying two 3x3 matrices
* const matrix1 = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const matrix2 = new p5.Matrix([9, 8, 7, 6, 5, 4, 3, 2, 1]);
* matrix1.mult(matrix2);
* console.log(matrix1.matrix); // Output: [30, 24, 18, 84, 69, 54, 138, 114, 90]
*
* // Multiplying a 4x4 matrix with another 4x4 matrix
* const matrix4x4_1 = new p5.Matrix(4); // Identity matrix
* const matrix4x4_2 = new p5.Matrix([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1]);
* matrix4x4_1.mult(matrix4x4_2);
* console.log(matrix4x4_1.matrix); // Output: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1]
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix1 = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const matrix2 = new p5.Matrix([9, 8, 7, 6, 5, 4, 3, 2, 1]);
* console.log("Before multiplication:", matrix1.matrix);
* matrix1.mult(matrix2);
* console.log("After multiplication:", matrix1.matrix); // Output: [30, 24, 18, 84, 69, 54, 138, 114, 90]
* }
* </code></div>
*/
mult(multMatrix) {
let _src;
if (multMatrix === this || multMatrix === this.matrix) {
_src = this.copy().matrix; // only need to allocate in this rare case
} else if (multMatrix instanceof Matrix) {
_src = multMatrix.matrix;
} else if (isMatrixArray(multMatrix) && isPerfectSquare(multMatrix)) {
_src = multMatrix;
} else if (isPerfectSquare(Array.from(arguments))) {
_src = Array.from(arguments);
} else {
return; // nothing to do.
}
if (this.#sqDimention === 4 && _src.length === 16) {
return this.#mult4x4(_src);
} else if (this.#sqDimention === 3 && _src.length === 9) {
return this.#mult3x3(_src);
} else {
return this.#multNxN(_src);
}
}
/**
* Takes a vector and returns the vector resulting from multiplying to that vector by this matrix from left. This function is only for 3x3 matrices.
*
* This method applies the current 3x3 matrix to a given vector, effectively
* transforming the vector using the matrix. The resulting vector is returned
* as a new vector or stored in the provided target vector.
*
* @param {p5.Vector} multVector - The vector to which this matrix applies.
* @param {p5.Vector} [target] - The vector to receive the result. If not provided,
* a copy of the input vector will be created and returned.
* @return {p5.Vector} - The transformed vector after applying the matrix.
*
* @example
* // Multiplying a 3x3 matrix with a vector
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const vector = new p5.Vector(1, 2, 3);
* const result = matrix.multiplyVec(vector);
* console.log(result.toString()); // Output: Transformed vector
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
* const vector = new p5.Vector(1, 2, 3);
* const result = matrix.multiplyVec(vector);
* console.log("Original Vector:", vector.toString()); // Output : Original Vector: [1, 2, 3]
* console.log("Transformed Vector:", result.toString()); // Output : Transformed Vector: [30, 36, 42]
* }
* </code></div>
*/
multiplyVec(multVector, target) {
if (target === undefined) {
target = multVector.copy();
}
for (let i = 0; i < this.#sqDimention; i++) {
target.values[i] = this.row(i).dot(multVector);
}
return target;
}
/**
* Inverts a given matrix.
*
* This method inverts a matrix based on its dimensions. Currently, it supports
* 3x3 and 4x4 matrices. If the matrix dimension is greater than 4, an error is thrown.
*
* For 4x4 matrices, it uses a specialized algorithm to compute the inverse.
* For 3x3 matrices, it uses a different algorithm optimized for smaller matrices.
*
* If the matrix is singular (non-invertible), the method will return `null`.
*
* @param {Array} a - The matrix to be inverted. It should be a 2D array representing the matrix.
* @returns {Array|null} - The inverted matrix, or `null` if the matrix is singular.
* @throws {Error} - Throws an error if the matrix dimension is greater than 4.
*
* @example
* // Inverting a 3x3 matrix
* const matrix = new p5.Matrix([1, 2, 3, 0, 1, 4, 5, 6, 0]);
* const invertedMatrix = matrix.invert();
* console.log(invertedMatrix.matrix); // Output: Inverted 3x3 matrix
*
* // Inverting a 4x4 matrix
* const matrix4x4 = new p5.Matrix(4); // Identity matrix
* matrix4x4.scale(2, 2, 2);
* const invertedMatrix4x4 = matrix4x4.invert();
* console.log(invertedMatrix4x4.matrix); // Output: Inverted 4x4 matrix
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix([1, 2, 3, 0, 1, 4, 5, 6, 0]);
* console.log("Original Matrix:", matrix.matrix);
* const invertedMatrix = matrix.invert();
* if (invertedMatrix) {
* console.log("Inverted Matrix:", invertedMatrix.matrix);
* } else {
* console.log("Matrix is singular and cannot be inverted.");
* }
* }
* </code></div>
*/
invert(a) {
if (this.#sqDimention === 4) {
return this.#invert4x4(a);
} else if (this.#sqDimention === 3) {
return this.#invert3x3(a);
} else {
throw new Error(
'Invert is not implemented for N>4 at the moment, we are working on it'
);
}
}
/**
* Creates a 3x3 matrix whose entries are the top left 3x3 part and returns it. This function is only for 4x4 matrices.
*
* This method extracts the top-left 3x3 portion of a 4x4 matrix and creates a new
* 3x3 matrix from it. This is particularly useful in 3D graphics for operations
* that require only the rotational or scaling components of a transformation matrix.
*
* If the current matrix is not 4x4, an error is thrown to ensure the method is used
* correctly. The resulting 3x3 matrix is independent of the original matrix, meaning
* changes to the new matrix will not affect the original.
*
* @return {p5.Matrix} A new 3x3 matrix containing the top-left portion of the original 4x4 matrix.
* @throws {Error} If the current matrix is not 4x4.
*
* @example
* // Extracting a 3x3 submatrix from a 4x4 matrix
* const matrix4x4 = new p5.Matrix(4); // Creates a 4x4 identity matrix
* matrix4x4.scale(2, 2, 2); // Apply scaling transformation
* const subMatrix3x3 = matrix4x4.createSubMatrix3x3();
* console.log("Original 4x4 Matrix:", matrix4x4.matrix);
* console.log("Extracted 3x3 Submatrix:", subMatrix3x3.matrix);
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix4x4 = new p5.Matrix(4); // Creates a 4x4 identity matrix
* matrix4x4.scale(2, 2, 2); // Apply scaling transformation
* console.log("Original 4x4 Matrix:", matrix4x4.matrix);
*
* const subMatrix3x3 = matrix4x4.createSubMatrix3x3();
* console.log("Extracted 3x3 Submatrix:", subMatrix3x3.matrix);
* }
* </code></div>
*/
createSubMatrix3x3() {
if (this.#sqDimention === 4) {
const result = new Matrix(3);
result.mat3[0] = this.matrix[0];
result.mat3[1] = this.matrix[1];
result.mat3[2] = this.matrix[2];
result.mat3[3] = this.matrix[4];
result.mat3[4] = this.matrix[5];
result.mat3[5] = this.matrix[6];
result.mat3[6] = this.matrix[8];
result.mat3[7] = this.matrix[9];
result.mat3[8] = this.matrix[10];
return result;
} else {
throw new Error('Matrix dimension must be 4 to create a 3x3 submatrix.');
}
}
/**
* Converts a 4×4 matrix to its 3×3 inverse transpose transform.
* This is commonly used in MVMatrix to NMatrix conversions, particularly
* in 3D graphics for transforming normal vectors.
*
* This method extracts the top-left 3×3 portion of a 4×4 matrix, inverts it,
* and then transposes the result. If the matrix is singular (non-invertible),
* the resulting matrix will be zeroed out.
*
* @param {p5.Matrix} mat4 - The 4×4 matrix to be converted.
* @returns {Matrix} The current instance of the Matrix class, allowing for method chaining.
* @throws {Error} If the current matrix is not 3×3.
*
* @example
* // Converting a 4×4 matrix to its 3×3 inverse transpose
* const mat4 = new p5.Matrix(4); // Create a 4×4 identity matrix
* mat4.scale(2, 2, 2); // Apply scaling transformation
* const mat3 = new p5.Matrix(3); // Create a 3×3 matrix
* mat3.inverseTranspose4x4(mat4);
* console.log("Converted 3×3 Matrix:", mat3.matrix);
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const mat4 = new p5.Matrix(4); // Create a 4×4 identity matrix
* mat4.scale(2, 2, 2); // Apply scaling transformation
* console.log("Original 4×4 Matrix:", mat4.matrix);
*
* const mat3 = new p5.Matrix(3); // Create a 3×3 matrix
* mat3.inverseTranspose4x4(mat4);
* console.log("Converted 3×3 Matrix:", mat3.matrix);
* }
* </code></div>
*/
inverseTranspose4x4({ mat4 }) {
if (this.#sqDimention !== 3) {
throw new Error('This function only works with 3×3 matrices.');
} else {
// Convert mat4 -> mat3 by extracting the top-left 3×3 portion
this.matrix[0] = mat4[0];
this.matrix[1] = mat4[1];
this.matrix[2] = mat4[2];
this.matrix[3] = mat4[4];
this.matrix[4] = mat4[5];
this.matrix[5] = mat4[6];
this.matrix[6] = mat4[8];
this.matrix[7] = mat4[9];
this.matrix[8] = mat4[10];
}
const inverse = this.invert();
// Check if inversion succeeded
if (inverse) {
inverse.transpose(this.matrix);
} else {
// In case of singularity, zero out the matrix
for (let i = 0; i < 9; i++) {
this.matrix[i] = 0;
}
}
return this;
}
/**
* Applies a transformation matrix to the current matrix.
*
* This method multiplies the current matrix by another matrix, which can be provided
* in several forms: another Matrix instance, an array representing a matrix, or as
* individual arguments representing the elements of a 4x4 matrix.
*
* This operation is useful for combining transformations such as translation, rotation,
* scaling, and perspective projection into a single matrix. By applying a transformation
* matrix, you can modify the current matrix to represent a new transformation.
*
* @param {Matrix|Array|number} multMatrix - The matrix to multiply with. This can be:
* - An instance of the Matrix class.
* - An array of 16 numbers representing a 4x4 matrix.
* - 16 individual numbers representing the elements of a 4x4 matrix.
* @returns {Matrix} The current matrix after applying the transformation.
*
* @example
* <div class="norender"><code>
* function setup() {
*
* // Assuming `matrix` is an instance of Matrix
* const anotherMatrix = new p5.Matrix(4);
* const anotherMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
* matrix.apply(anotherMatrix);
*
* // Applying a transformation using an array
* const matrixArray = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
* matrix.apply(matrixArray);
*
* // Applying a transformation using individual arguments
* matrix.apply(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
*
*
* // Create a 4x4 identity matrix
* const matrix = new p5.Matrix(4);
* console.log("Original Matrix:", matrix.matrix);
*
* // Create a scaling transformation matrix
* const scalingMatrix = new p5.Matrix([2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1]);
*
* // Apply the scaling transformation
* matrix.apply(scalingMatrix);
* console.log("After Scaling Transformation:", matrix.matrix);
*
* // Apply a translation transformation using an array
* const translationMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 5, 5, 5, 1];
* matrix.apply(translationMatrix);
* console.log("After Translation Transformation:", matrix.matrix);
* }
* </code></div>
*/
apply(multMatrix) {
let _src;
if (multMatrix === this || multMatrix === this.matrix) {
_src = this.copy().matrix; // only need to allocate in this rare case
} else if (multMatrix instanceof Matrix) {
_src = multMatrix.matrix;
} else if (isMatrixArray(multMatrix)) {
_src = multMatrix;
} else if (arguments.length === 16) {
_src = arguments;
} else {
return; // nothing to do.
}
const mat4 = this.matrix;
// each row is used for the multiplier
const m0 = mat4[0];
const m4 = mat4[4];
const m8 = mat4[8];
const m12 = mat4[12];
mat4[0] = _src[0] * m0 + _src[1] * m4 + _src[2] * m8 + _src[3] * m12;
mat4[4] = _src[4] * m0 + _src[5] * m4 + _src[6] * m8 + _src[7] * m12;
mat4[8] = _src[8] * m0 + _src[9] * m4 + _src[10] * m8 + _src[11] * m12;
mat4[12] = _src[12] * m0 + _src[13] * m4 + _src[14] * m8 + _src[15] * m12;
const m1 = mat4[1];
const m5 = mat4[5];
const m9 = mat4[9];
const m13 = mat4[13];
mat4[1] = _src[0] * m1 + _src[1] * m5 + _src[2] * m9 + _src[3] * m13;
mat4[5] = _src[4] * m1 + _src[5] * m5 + _src[6] * m9 + _src[7] * m13;
mat4[9] = _src[8] * m1 + _src[9] * m5 + _src[10] * m9 + _src[11] * m13;
mat4[13] = _src[12] * m1 + _src[13] * m5 + _src[14] * m9 + _src[15] * m13;
const m2 = mat4[2];
const m6 = mat4[6];
const m10 = mat4[10];
const m14 = mat4[14];
mat4[2] = _src[0] * m2 + _src[1] * m6 + _src[2] * m10 + _src[3] * m14;
mat4[6] = _src[4] * m2 + _src[5] * m6 + _src[6] * m10 + _src[7] * m14;
mat4[10] = _src[8] * m2 + _src[9] * m6 + _src[10] * m10 + _src[11] * m14;
mat4[14] = _src[12] * m2 + _src[13] * m6 + _src[14] * m10 + _src[15] * m14;
const m3 = mat4[3];
const m7 = mat4[7];
const m11 = mat4[11];
const m15 = mat4[15];
mat4[3] = _src[0] * m3 + _src[1] * m7 + _src[2] * m11 + _src[3] * m15;
mat4[7] = _src[4] * m3 + _src[5] * m7 + _src[6] * m11 + _src[7] * m15;
mat4[11] = _src[8] * m3 + _src[9] * m7 + _src[10] * m11 + _src[11] * m15;
mat4[15] = _src[12] * m3 + _src[13] * m7 + _src[14] * m11 + _src[15] * m15;
return this;
}
/**
* Scales a p5.Matrix by scalars or a vector.
*
* This method applies a scaling transformation to the current matrix.
* Scaling is a transformation that enlarges or shrinks objects by a scale factor
* along the x, y, and z axes. The scale factors can be provided as individual
* numbers, an array, or a `p5.Vector`.
*
* If a `p5.Vector` or an array is provided, the x, y, and z components are extracted
* from it. If the z component is not provided, it defaults to 1 (no scaling along the z-axis).
*
* @param {p5.Vector|Float32Array|Number[]} s - The vector or scalars to scale by.
* Can be a `p5.Vector`, an array, or individual numbers.
* @returns {Matrix} The current instance of the Matrix class, allowing for method chaining.
*
* @example
* // Scaling a matrix by individual scalars
* const matrix = new p5.Matrix(4); // Create a 4x4 identity matrix
* matrix.scale(2, 3, 4); // Scale by 2 along x, 3 along y, and 4 along z
* console.log(matrix.matrix);
*
* // Scaling a matrix by a p5.Vector
* const scaleVector = new p5.Vector(2, 3, 4);
* matrix.scale(scaleVector);
* console.log(matrix.matrix);
*
* // Scaling a matrix by an array
* const scaleArray = [2, 3, 4];
* matrix.scale(scaleArray);
* console.log(matrix.matrix);
*
* // p5.js script example
* <div class="norender"><code>
* function setup() {
*
* const matrix = new p5.Matrix(4); // Create a 4x4 identity matrix
* console.log("Original Matrix:", matrix.matrix);
*
* // Scale the matrix by individual scalars
* matrix.scale(2, 3, 4);
* console.log("Scaled Matrix (2, 3, 4):", matrix.matrix);
*
* // Scale the matrix by a p5.Vector
* const scaleVector = new p5.Vector(1.5, 2.5, 3.5);
* matrix.scale(scaleVector);
* console.log("Scaled Matrix (Vector):", matrix.matrix);
*
* // Scale the matrix by an array
* const scaleArray = [0.5, 0.5, 0.5];
* matrix.scale(scaleArray);
* console.log("Scaled Matrix (Array):", matrix.matrix);
* }
* </code></div>
*/
scale(x, y, z) {
if (x instanceof Vector) {
// x is a vector, extract the components from it.
y = x.y;
z = x.z;
x = x.x; // must be last
} else if (x instanceof Array) {
// x is an array, extract the components from it.
y = x[1];
z = x[2];
x = x[0]; // must be last
}
this.matrix[0] *= x;
this.matrix[1] *= x;
this.matrix[2] *= x;
this.matrix[3] *= x;
this.matrix[4] *= y;
this.matrix[5] *= y;
this.matrix[6] *= y;
this.matrix[7] *= y;
this.matrix[8] *= z;
this.matrix[9] *= z;
this.matrix[10] *= z;
this.matrix[11] *= z;
return this;
}
/**
* Rotate the Matrix around a specified axis by a given angle.
*
* This method applies a rotation transformation to the matrix, modifying its orientation
* in 3D space. The rotation is performed around the provided axis, which can be defined
* as a `p5.Vector` or an array of numbers representing the x, y, and z components of the axis.
* Rotate our Matrix around an axis by the given angle.
* @param {Number} a The angle of rotation in radians.
* Angles in radians are a measure of rotation, where 2π radians
* represent a full circle (360 degrees). For example:
* - π/2 radians = 90 degrees (quarter turn)
* - π radians = 180 degrees (half turn)