-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1621 lines (1516 loc) · 54.5 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>mat.js</title>
<!-- MathJax CDN -->
<script async src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_CHTML"></script>
<!-- The mat.js source code -->
<script type="text/javascript" src="src/mat.js"></script>
</head>
<style>
body {
font-family: 'Helvetica Neue', Helvetica, Arial, Serif;
font-size: 12px;
color: #222;
margin: 0;
padding: 0;
width 100%;
background: #fefefe;
}
table {
margin: 0;
padding: 0;
width: 300px;
border: 1px solid #ccc;
border-radius: 3px;
}
table thead tr th {
background: #eee;
margin: 0;
height: 24px;
}
table tbody tr td {
margin: 0;
padding-left: 10px;
padding-right: 10px;
height: 20px;
text-align: right;
border-top: 1px solid #ccc;
}
table tbody tr td:first-child {
border-right: 1px solid #ccc;
text-align: left;
}
table tbody tr td:last-child {
border-left: 1px solid #ccc;
}
p {
line-height: 16px;
}
a {
text-decoration: none;
}
ol li {
line-height: 16px;
}
#container {
width: 960px;
margin: 0 auto;
}
.left {
float: left;
width: 240px;
}
.right {
float: right;
width: 700px;
}
ul#menu {
list-style-type: none;
margin: 0;
margin-top: 1.245em;
padding: 0;
}
ul#menu li {
width: 100%;
position: relative;
display: block;
}
ul#menu li a {
width: 100%;
display: block;
padding-left: 10px;
line-height: 24px;
}
ul#menu li span {
font-weight: bold;
font-size: 16px;
color: #444;
}
div.section h2 {
border-bottom: 1px dashed #ccc;
}
div.section a.top {
display: none;
position: relative;
float: right;
margin-top: 5px;
}
div.section:hover a.top {
display: block;
}
</style>
<body>
<div id="container">
<div class="left">
<ul id="menu">
<li><span>Table of contents</span></li>
<li><a href="#introduction">Introduction</a></li>
<li><span>Attributes</span></li>
<li><a href="#error">error</a></li>
<li><a href="#maxrounds">maxrounds</a></li>
<li><a href="#overwrite">overwrite</a></li>
<li><span>Methods</span></li>
<li><a href="#fromArray">fromArray</a></li>
<li><a href="#toArray">toArray</a></li>
<li><a href="#toString">toString</a></li>
<li><a href="#toLatex">toLatex</a></li>
<li><a href="#getColumn">getColumn</a></li>
<li><a href="#getColumns">getColumns</a></li>
<li><a href="#getLength">getLength</a></li>
<li><a href="#getRow">getRow</a></li>
<li><a href="#getRows">getRows</a></li>
<li><a href="#getShape">getShape</a></li>
<li><a href="#getValue">getValue</a></li>
<li><a href="#getValues">getValues</a></li>
<li><a href="#setRows">setRows</a></li>
<li><a href="#setColumns">setColumns</a></li>
<li><a href="#setValue">setValue</a></li>
<li><a href="#isSquare">isSquare</a></li>
<li><a href="#isColumnVector">isColumnVector</a></li>
<li><a href="#isRowVector">isRowVector</a></li>
<li><a href="#isVector">isVector</a></li>
<li><a href="#isSameSize">isSameSize</a></li>
<li><a href="#add">add</a></li>
<li><a href="#subtract">subtract</a></li>
<li><a href="#product">product</a></li>
<li><a href="#strassenProduct">strassenProduct</a></li>
<li><a href="#hadamardProduct">hadamardProduct</a></li>
<li><a href="#scalarProduct">scalarProduct</a></li>
<li><a href="#transpose">transpose</a></li>
<li><a href="#hermitian">hermitian</a></li>
<li><a href="#trace">trace</a></li>
<li><a href="#upperTrace">upperTrace</a></li>
<li><a href="#lowerTrace">lowerTrace</a></li>
<li><a href="#minor">minor</a></li>
<li><a href="#determinant">determinant</a></li>
<li><a href="#det">det</a></li>
<li><a href="#gaussElimination">gaussElimination</a></li>
<li><a href="#solve">solve</a></li>
<li><a href="#gramSchmidt">gramSchmidt</a></li>
<li><a href="#qrDecomposition">qrDecomposition</a></li>
<li><a href="#qr">qr</a></li>
<li><a href="#luDecomposition">luDecomposition</a></li>
<li><a href="#lu">lu</a></li>
<li><a href="#eigenValues">eigenValues</a></li>
<li><a href="#eigenVectors">eigenVectors</a></li>
<li><a href="#identity">identity</a></li>
<li><a href="#diagonal">diagonal</a></li>
<li><a href="#pNorm">pNorm</a></li>
<li><a href="#norm">norm</a></li>
<li><a href="#toToeplitz">toToeplitz</a></li>
<li><a href="#inverse">inverse</a></li>
<li><a href="#pseudoInverse">pseudoInverse</a></li>
<li><a href="#toVandermonde">toVandermonde</a></li>
<li><a href="#zero">zero</a></li>
</ul>
</div>
<div class="right">
<div class="section" id="introduction">
<h2>Introduction</h2>
<p>This library is not intended to be taken seriously, as it is made just for fun and learning purposes. One may find it useful but any use in production environments, research studies, etc. is discouraged.</p>
<p>This javascript matrix library comes from the need to perform various numeric matrix operations during the course of some assignments at college. The lack of one of those fancy graphic calculators, my reluctance in spending money buying one and the growing interest in javascript took me to start writing most of the code found here. After six months or so I have managed to put this sort of library and learned some more linear algebra and algorithms during the process.</p>
<p>In case you find this useful, or end up ignoring my first advice and use it somewhere, I will be delighted to know it so feel free to mail me at the email address found in the source code. Moreover, if you happen to find any mistakes (I am pretty sure there are a ton of them) in the source code or in this document, you can issue them at its github repository at <a href="https://github.com/dhuertas/mat.js" target="_blank">https://github.com/dhuertas/mat.js</a>.</p>
<p>Happy coding!</p>
</div>
<div class="section" id="error">
<a class="top" href="#">top</a>
<h2>error</h2>
<p class="description">(float) - Sets the error resolution for the iterative algorithms (e.g. <a href="#eigenValues">eigenValues</a>, <a href="#eigenVectors">eigenVectors</a>).</p>
<h3>Default:</h3>
<pre class="brush: js">0.000001</pre>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(2, 2, [1, 2, 3, 4]);
A.error = 0.000001;</pre>
<span>or</span>
<pre class="brush: js">
var A = new MAT({
rows: 2,
columns: 2,
values: [1, 2, 3, 4],
error: 0.000001});</pre>
</div>
<div class="section" id="maxrounds">
<a class="top" href="#">top</a>
<h2>maxrounds</h2>
<p class="description">(integer) - Sets the number of maximum rounds for the iterative algorithms (e.g. <a href="#eigenValues">eigenValues</a>, <a href="#eigenVectors">eigenVectors</a>).</p>
<h3>Default:</h3>
<pre class="brush: js">1000</pre>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(2, 2, [1, 2, 3, 4]);
A.maxrounds = 1000;</pre>
<span>or</span>
<pre class="brush: js">
var A = new MAT({
rows: 2,
columns: 2,
values: [1, 2, 3, 4],
maxrounds: 1000});</pre>
</div>
<div class="section" id="overwrite">
<a class="top" href="#">top</a>
<h2>overwrite</h2>
<p class="description">When set to <code>true</code> the result values will be stored in the same matrix instance, overwriting the previous ones (not to be confused with in-place algorithms). The aim is to reduce the amount of memory and time used to perform iterative operations.</p>
<h3>Default:</h3>
<pre class="brush: js">false</pre>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(2,2,[1, 2, 3, 4]);
A.overwrite = true;</pre>
<span>or</span>
<pre class="brush: js">
var A = new MAT({
rows: 2,
columns: 2,
values: [1, 2, 3, 4],
overwrite: true});</pre>
</div>
<div class="section" id="fromArray">
<a class="top" href="#">top</a>
<h2>fromArray</h2>
<p class="description">Creates a new matrix using array notation.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.fromArray(array);</pre>
<h3>Arguments:</h3>
<ol>
<li>(array) - A 2D array containing the matrix values.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix.</li>
</ul>
<h3>Example:</h3>
<p>This example creates a 2x3 matrix object:</p>
<pre class="brush: js">
var A = new MAT({
maxrounds: 1000,
overwrite: true
}).fromArray([
[1, 2, 3],
[4, 5, 6]
]);</pre>
</div>
<div class="section" id="toArray">
<a class="top" href="#">top</a>
<h2>toArray</h2>
<p class="description">Returns an array containing all the matrix values, ordered from left to right and from top to bottom.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.toArray()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(array) - A copy of the array containing the matrix values.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">var values = A.toArray();</pre>
</div>
<div class="section" id="toString">
<a class="top" href="#">top</a>
<h2>toString</h2>
<p class="description">Returns a string containing all the matrix values, using the toString method of the javascript <code>Array</code> object.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.toString()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(string) - The toString method applied to the matrix array.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">document.write(A.toString());</pre>
</div>
<div class="section" id="toLatex">
<a class="top" href="#">top</a>
<h2>toLatex</h2>
<p class="description">Returns the matrix converted into a LaTeX formatted string.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.toLatex([closure])</pre>
<h3>Arguments:</h3>
<ol>
<li>(string) - The closure format. Options are: b, B, v, V, p ([ ], { }, | |, || ||, ( ) respectively). Default closure is p ( ).</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(string) - A LaTeX formatted string.</li>
</ul>
<h3>Example:</h3>
<p>The following example uses the MathJax library, which enables the browser to format text using LaTeX:</p>
<pre class="brush: js">
var A = new MAT(2, 2, [1, 2, -1, 2]);
document.getElementById("output").innerHTML += "The determinant $$" +
A.toLatex('v') + "$$ equals " + A.det() + ".";
// Typeset the element:
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"output"]);</pre>
<p>If mathjax loeaded correctly from the CDN, should look something like this:</p>
<div id="toLatexExample"></div>
<script type="text/javascript">
var A = new MAT(2, 2, [1, 2, -1, 2]);
document.getElementById("toLatexExample").innerHTML += "The determinant $$" +
A.toLatex('v') + "$$ equals " + A.det() + ".";
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"toLatexExample"]);
</script>
</div>
<div class="section" id="getColumn">
<a class="top" href="#">top</a>
<h2>getColumn</h2>
<p class="description">Returns a column (or column vector) from the matrix.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getColumn(column);</pre>
<h3>Arguments:</h3>
<ol>
<li>(integer) - The column position (starting from <code>0</code>, to <code>columns-1</code>).</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix containing the specified column.</li>
</ul>
<h3>Example:</h3>
<p>The following example will print the column vector <code>[0, 0, 1]</code>:</p>
<pre class="brush: js">
var A = new MAT().fromArray([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
var column = A.getColumn(2);
document.write(column.toString());</pre>
</div>
<div class="section" id="getColumns">
<a class="top" href="#">top</a>
<h2>getColumns</h2>
<p class="description">Returns the number of columns.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getColumns()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(integer) - The number of columns.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT().fromArray([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
document.write(A.getColumns());</pre>
<p>Will print the number 3.</p>
</div>
<div class="section" id="getLength">
<a class="top" href="#">top</a>
<h2>getLength</h2>
<p class="description">Returns the number of elements (or length) of the matrix array.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getLength()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(integer) - The number of elements of the matrix array.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT().fromArray([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
document.write(A.getLength());</pre>
<p>Will print the number 9.</p>
</div>
<div class="section" id="getRow">
<a class="top" href="#">top</a>
<h2>getRow</h2>
<p class="description">Returns a row (row vector) from the matrix.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getRow(row)</pre>
<h3>Arguments:</h3>
<ol>
<li>(integer) - The row position (starting from <code>0</code>, to <code>rows-1</code>).</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix containing the specified row.</li>
</ul>
<h3>Example:</h3>
<p>The following example will print the row vector <code>[0, 1, 0]</code>.</p>
<pre class="brush: js">
var A = new MAT().fromArray([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
var row = A.getRow(1);
document.write(row.toString());</pre>
</div>
<div class="section" id="getRows">
<a class="top" href="#">top</a>
<h2>getRows</h2>
<p class="description">Returns the number of rows.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getRows()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(integer) - The number of rows.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT().fromArray([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
document.write(A.getRows());</pre>
<p>Will print the number 3.</p>
</div>
<div class="section" id="getShape">
<a class="top" href="#">top</a>
<h2>getShape</h2>
<p class="description">Returns the shape (or the dimensions) of the matrix.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getShape()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(array) - An array of the form <code>[rows, columns]</code>.</li>
</ul>
<h3>Example:</h3>
<p>For a 3x4 matrix the following code will print the array <code>[3, 4]</code>:</p>
<pre class="brush: js">
var A = new MAT(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
document.write(A.getShape());</pre>
</div>
<div class="section" id="getValue">
<a class="top" href="#">top</a>
<h2>getValue</h2>
<p class="description">Returns the value at the specified <code>row</code> and <code>column</code> values.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getValue(row, column)</pre>
<h3>Arguments:</h3>
<ol>
<li>(integer) - The row position (from <code>0</code> to <code>rows-1</code>).</li>
<li>(integer) - The column position (from <code>0</code> to <code>columns-1</code>).</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(float|array) - The value at the specified position (complex numbers are treated as duplex of the form <code>[real,imag]</code>).</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
document.write(A.getValue(2, 3));</pre>
<p>The previous code will print the number <code>12</code>.</p>
</div>
<div class="section" id="getValues">
<a class="top" href="#">top</a>
<h2>getValues</h2>
<p class="description">Returns a copy of the matrix values, ordered from left to right and from top to bottom. This is the same as the <code>toString</code> method.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.getValues()</pre>
<h3>Arguments:</h3>
<p>None</p>
<h3>Returns:</h3>
<ul>
<li>(array) - A copy of the array containing the matrix values.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
document.write(A.getValues());</pre>
<p>The previous code will print the array <code>[1, 2, 3, 4, 5, 6, 7, 8, 9]</code>.</p>
</div>
<div class="section" id="setRows">
<a class="top" href="#">top</a>
<h2>setRows</h2>
<p class="description">Sets the number of rows of the matrix. This method should be used in conjuction with the <a href="#setColumns">setColumns</a> method in order to avoid undesired behaviours.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.setRows(rows)</pre>
<h3>Arguments:</h3>
<ol>
<li>(integer) - The number of rows.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - The matrix object.</li>
</ul>
<h3>Example:</h3>
<p>Let's say we want to transform a 3x2 matrix to a 1x6 matrix (or row vector):</p>
<pre class="brush: js">
var A = new MAT(3, 2, [1, 2, 3, 4, 5, 6]);
// do some stuff here
A.setRows(1).setColumns(6);
document.write(A.getShape());</pre>
<p>The previous code will print the array <code>[1, 6]</code>.</p>
</div>
<div class="section" id="setColumns">
<a class="top" href="#">top</a>
<h2>setColumns</h2>
<p class="description">Sets the number of columns of the matrix. This method should be used in conjuction with the <a href="#setRows">setRows</a> method in order to avoid undesired behaviours.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.setColumns(columns)</pre>
<h3>Arguments:</h3>
<ol>
<li>(integer) - The number of columns.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - The matrix object.</li>
</ul>
<h3>Example:</h3>
<p>Let's say we want to transform a 3x2 matrix to a 6x1 matrix (or column vector):</p>
<pre class="brush: js">
var A = new MAT(3, 2,[1, 2, 3, 4, 5, 6]);
// do some stuff here
A.setColumns(1).setRows(6);
document.write(A.getShape());</pre>
<p>The previous code will print the array <code>[6, 1]</code>.</p>
</div>
<div class="section" id="setValue">
<a class="top" href="#">top</a>
<h2>setValue</h2>
<p class="description">Sets the value at the specified position of the matrix.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.setValue(row, column, value)</pre>
<h3>Arguments:</h3>
<ol>
<li>(integer) - The row position (from <code>0</code> to <code>rows-1</code>).</li>
<li>(integer) - The column position (from <code>0</code> to <code>columns-1</code>).</li>
<li>(float|array) - The desired value.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - The matrix object.</li>
</ul>
<h3>Example:</h3>
<p>The following example sets the center value of a 3x3 matrix to 0.</p>
<pre class="brush: js">matrix.setValue(1, 1, 0).</pre>
</div>
<div class="section" id="isSquare">
<a class="top" href="#">top</a>
<h2>isSquare</h2>
<p class="description">Tells whether the matrix is a square matrix or not.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.isSquare()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(boolean) - <code>true</code> if the matrix is square, <code>false</code> otherwise.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
if (matrix.isSquare()) {
// execute this code when the matrix is square
} else {
// :(
}</pre>
</div>
<div class="section" id="isColumnVector">
<a class="top" href="#">top</a>
<h2>isColumnVector</h2>
<p class="description">Tells whether the matrix is a column vector or not.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.isColumnVector()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(boolean) - <code>true</code> when the matrix is a column vector, <code>false</code> otherwise.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
if (matrix.isColumnVector()) {
// matrix is a column vector
} else {
// other code
}</pre>
</div>
<div class="section" id="isRowVector">
<a class="top" href="#">top</a>
<h2>isRowVector</h2>
<p class="description">Tells whether the matrix is a row vector or not.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.isRowVector()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(boolean) - <code>true</code> when the matrix is a row vector, <code>false</code> otherwise.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
if (matrix.isColumnVector()) {
// matrix is a row vector
} else {
// other code
}</pre>
</div>
<div class="section" id="isVector">
<a class="top" href="#">top</a>
<h2>isVector</h2>
<p class="description">Tells whether the matrix is a vector (1 row or 1 column) or not.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.isVector()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(boolean) - <code>true</code> when the matrix is a vector, <code>false</code> otherwise.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
if (matrix.isVector()) {
// the matrix is a vector
} else {
// other code
}</pre>
</div>
<div class="section" id="isSameSize">
<a class="top" href="#">top</a>
<h2>isSameSize</h2>
<p class="description">Compares the shape of the matrix against the one in the argument.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.isSameSize(matrix)</pre>
<h3>Arguments:</h3>
<ol>
<li>(object) - Another matrix</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(boolean) - <code>true</code> when both matrices have the same shape (same number of rows and columns).</li>
</ul>
<h3>Example:</h3>
<p>The following example compares two matrices that have the same number of rows and columns and outputs the string <i>same size</i>:</p>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]);
if (A.isSameSize(B)) {
document.write('same size');
} else {
documenet.write('different size');
}</pre>
</div>
<div class="section" id="add">
<a class="top" href="#">top</a>
<h2>add</h2>
<p class="description">Performs the addition of two matrices (<strong>A</strong> + <strong>B</strong>).</p>
<h3>Syntax:</h3>
<pre class="brush: js">.add(matrix)</pre>
<h3>Arguments:</h3>
<ol>
<li>(object) - A matrix object.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix with the result.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]);
var C = A.add(B);
// [1, 2, 3] [9, 8, 7]
// C = [4, 5, 6] + [6, 5, 4]
// [7, 8, 9] [3, 2, 1]
document.write(C.toString()); // outputs "10, 10, 10, 10, 10, 10, 10, 10, 10"
document.write(A.toString()); // outputs "1, 2, 3, 4, 5, 6, 7, 8, 9"
// Now with the overwrite attribute set to true
A.overwrite = true;
A.add(B);
document.write(A.toString()); // outputs "10, 10, 10, 10, 10, 10, 10, 10, 10"</pre>
</div>
<div class="section" id="subtract">
<a class="top" href="#">top</a>
<h2>subtract</h2>
<p class="description">Performs the subtraction of two matrices from left to right (<strong>A</strong> - <strong>B</strong>).</p>
<h3>Syntax:</h3>
<pre class="brush: js">.subtract(matrix)</pre>
<h3>Arguments:</h3>
<ol>
<li>(object) - A matrix object.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix with the result.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]);
var C = A.subtract(B);
// [1, 2, 3] [9, 8, 7]
// C = [4, 5, 6] - [6, 5, 4]
// [7, 8, 9] [3, 2, 1]
document.write(C.toString()); // outputs "-8, -6, -4, -2, 0, 2, 4, 6, 8"
document.write(A.toString()); // outputs "1, 2, 3, 4, 5, 6, 7, 8, 9"
// Now with the overwrite attribute set to true
A.overwrite = true;
A.subtract(B);
document.write(A.toString()); // outputs "-8, -6, -4, -2, 0, 2, 4, 6, 8"</pre>
</div>
<div class="section" id="product">
<a class="top" href="#">top</a>
<h2>product</h2>
<p class="description">Performs the product of two matrices (from left to right <strong>AB</strong>). The number of columns of the first matrix must match the number of rows of the second one.</p>
<p class="description">This is the naive product algorithm. The <a href="#strassenProduct">strassenProduct</a> is asymptotically faster for large matrices.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.product(matrix)</pre>
<h3>Arguments:</h3>
<ol>
<li>(object) - A matrix object.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix with the result.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
var B = new MAT(3, 3, [1, 0, 1, 0, 1, 0, 0, 1, 1]);
var C = A.product(B);
// [1, 2, 3] [1, 0, 1]
// C = [4, 5, 6] * [0, 1, 0]
// [7, 8, 9] [0, 1, 1]
document.write(C.toString()); // outputs "1, 5, 4, 4, 11, 10, 7, 17, 16"
// Now with the overwrite attribute set to true
A.overwrite = true;
A.product(B);
document.write(A.toString()); // outputs "1, 5, 4, 4, 11, 10, 7, 17, 16"</pre>
</div>
<div class="section" id="strassenProduct">
<a class="top" href="#">top</a>
<h2>strassenProduct</h2>
<p class="description">Performs the product of two matrices (from left to right <strong>AB</strong>) using the Strassen's algorithm. This algorithm is asymptotically faster than the naive method for large matrices.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.strassenProduct(matrix)</pre>
<h3>Arguments:</h3>
<ol>
<li>(object) - A matrix object.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix with the result.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]);
var C = A.strassenProduct(B);
document.write(C.toString()); // outputs "30,24,18,84,69,54,138,114,90"
// [ 30, 24, 18]
// [ 84, 69, 54]
// [138, 114, 90]</pre>
<p>The next table shows some average running times for different matrix sizes:</p>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Size</th>
<th>Naive</th>
<th>Strassen</th>
</tr>
</thead>
<tbody>
<tr>
<td>4x4</td>
<td>- ms</td>
<td>1 ms</td>
</tr>
<tr>
<td>8x8</td>
<td>- ms</td>
<td>1 ms</td>
</tr>
<tr>
<td>16x16</td>
<td>2 ms</td>
<td>9.5 ms</td>
</tr>
<tr>
<td>32x32</td>
<td>9.5 ms</td>
<td>14.5 ms</td>
</tr>
<tr>
<td>64x64</td>
<td>75 ms</td>
<td>93.5 ms</td>
</tr>
<tr>
<td>128x128</td>
<td>587 ms</td>
<td>661 ms</td>
</tr>
<tr>
<td>256x256</td>
<td>4835.5 ms</td>
<td>4670 ms</td>
</tr>
<tr>
<td>512x512</td>
<td>39319 ms</td>
<td>34827.5 ms</td>
</tr>
<tr>
<td>1024x1024</td>
<td>426434.5 ms</td>
<td>243014 ms</td>
</tr>
</tbody>
</table>
<p>It seems that at a size between 128 and 256 Strassen's algorithm starts to perform better than the naive algorithm, although the memory required for the Strassen's algorithm should be taken into account.</p>
</div>
<div class="section" id="hadamardProduct">
<a class="top" href="#">top</a>
<h2>hadamardProduct</h2>
<p class="description">Performs the Hadamard product of two matrices (from left to right <strong>AB</strong>).</p>
<h3>Syntax:</h3>
<pre class="brush: js">.hadamardProduct(matrix)</pre>
<h3>Arguments:</h3>
<ol>
<li>(object) - A matrix object.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix with the result.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
var B = new MAT(3, 3, [1, 0, 1, 0, 1, 0, 0, 1, 1]);
var C = A.hadamardProduct(B);
// [1, 2, 3] [1, 0, 1]
// C = [4, 5, 6] o [0, 1, 0]
// [7, 8, 9] [0, 1, 1]
document.write(C.toString()); // outputs "1,0,3,0,5,0,0,8,9"
// [1, 0, 3]
// [0, 5, 0]
// [0, 8, 9]</pre>
</div>
<div class="section" id="scalarProduct">
<a class="top" href="#">top</a>
<h2>scalarProduct</h2>
<p class="description">Performs the scalar product between the matrix and a scalar.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.scalarProduct(value)</pre>
<h3>Arguments:</h3>
<ol>
<li>(float|array) - A scalar value.</li>
</ol>
<h3>Returns:</h3>
<ul>
<li>(object) - A new matrix with the result.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(2, 2, [1, 2, 3, 4]);
document.write(A.scalarProduct(2).toString()); // outputs "1,4,6,8"</pre>
</div>
<div class="section" id="transpose">
<a class="top" href="#">top</a>
<h2>transpose</h2>
<p class="description">Returns the transpose matrix.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.transpose()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(object) - The matrix transposed.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
// [1, 2, 3, 4]
// A = [5, 6, 7, 8]
// [9,10,11,12]
var C = A.transpose();
// [1, 5, 9]
// C = [2, 6,10]
// [3, 7,11]
// [4, 8,12]
document.write(C.toString()); // outputs "1,5,9,2,6,10,3,7,11,4,8,12"</pre>
</div>
<div class="section" id="hermitian">
<a class="top" href="#">top</a>
<h2>hermitian</h2>
<p class="description">Returns the hermitian matrix (conjugate transpose).</p>
<h3>Syntax:</h3>
<pre class="brush: js">.hermitian()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(object) - The hermitian matrix.</li>
</ul>
<p class="notify"><strong>Note:</strong> when the <code>overwrite</code> attribute is set to <code>true</code> the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.</p>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(2, 2, [3, [2,3], [2,-1], 1]);
document.write(A.hermitian().toString()); // outputs "3,[2,1],[2,-3],1"</pre>
</div>
<div class="section" id="trace">
<a class="top" href="#">top</a>
<h2>trace</h2>
<p class="description">Returns the trace of the matrix. That is, the sum of all the elements in the main diagonal.</p>
<h3>Syntax:</h3>
<pre class="brush: js">.trace()</pre>
<h3>Arguments:</h3>
<p>None.</p>
<h3>Returns:</h3>
<ul>
<li>(float|array) - The trace of the matrix.</li>
</ul>
<h3>Example:</h3>
<pre class="brush: js">
var A = new MAT(2, 2, [3, [2,3], [2,-1], 1]);
document.write(A.trace());</pre>