-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.matrix.class.js
More file actions
1725 lines (1545 loc) · 61.8 KB
/
Copy pathcomplex.matrix.class.js
File metadata and controls
1725 lines (1545 loc) · 61.8 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
/*
JS Complex Matrix class Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Code by Alessandro Rosa - zandor_zz@yahoo.it
var _CM_UNDEF = "undefined" ;
var _CM_NO_FILL = 0 ;
var _CM_FILL = 1 ;
if ( typeof is_array != "function" ) function is_array( _obj ) { return _obj instanceof Array ? 1 : 0 ; }
if ( typeof is_complex != "function" ) function is_complex( _obj ) { return _obj instanceof complex ? 1 : 0 ; }
if ( typeof is_integer != "function" ) function is_integer( _obj ) { return is_number( _obj ) ? ( Math.floor( _obj ) == _obj ? 1 : 0 ) : 0 ; }
if ( typeof is_number != "function" ) function is_number( _obj ) { return ( _obj instanceof Number || typeof _obj == "number" ) ? 1 : 0 ; }
if ( typeof is_rational != "function" ) function is_rational( _obj ) { return is_number( _obj ) ? !is_integer( _obj ) : 0 ; }
if ( typeof is_string != "function" ) function is_string( _obj ) { return ( typeof _obj == "string" || _obj instanceof String ) ; }
if ( typeof safe_string != "function" ) function safe_string( _obj, _default_str ) { return ( typeof _obj == "string" || _obj instanceof String ) ? new String( _obj ).trim() : new String( _default_str + "" ).trim() ; }
if ( typeof safe_int != "function" ) function safe_int( _val, _set_if_nan ) { _val = parseInt( _val, 10 ); return isNaN( _val ) ? ( isNaN( _set_if_nan ) ? 0 : _set_if_nan ) : _val ; }
if ( typeof safe_float != "function" ) function safe_float( _val, _set_if_nan ) { _val = parseFloat( _val ); return isNaN( _val ) ? ( isNaN( _set_if_nan ) ? 0 : _set_if_nan ) : _val ; }
if ( typeof safe_size != "function" )
{
function safe_size( _obj, _ret_val )
{
if ( _ret_val == "undefined" || _ret_val == null ) _ret_val = 0 ;
if ( _obj == null || _obj == "undefined" ) return _ret_val ;
else if ( typeof _obj == "string" || _obj instanceof String || is_array( _obj ) || _obj instanceof Object ) return _obj.length ;
else return _ret_val ;
}
}
if ( typeof is_number != "function" ) function is_number( _obj ) { return ( typeof _obj == "number" || _obj instanceof Number ) ; }
if ( typeof is_complex != "function" ) function is_complex( _obj ){ return ( _obj instanceof complex ) ; }
if ( typeof is_string != "function" ) function is_string( _obj ) { return ( typeof _obj == "string" || _obj instanceof String ) ; }
if ( typeof is_complex_matrix != "function" ) function is_complex_matrix( _obj ) { return _obj instanceof complex_matrix ? 1 : 0 ; }
// COMPLEX NUMBERS MATRIX
function complex_matrix()
{
this.customclass = arguments.callee.name ;
// #01. new complex_matrix( [ 1, 2, 3, 4 ] ); --> a square matrix 2x2 is constructed
// #02. new complex_matrix( [ 1, 2, 3, 4, 5 ] ); --> a square matrix 2x2 is constructed (truncated at 4)
// #02. new complex_matrix( [ 1, 2, 3, 4, 5, 6 ], 2, 3 ); --> a matrix 2x3 is constructed
// #03. new complex_matrix( [ 1, 2, 3, 4, 5 ], "exceed" ); --> a square matrix 3x3 is constructed: missing values are set to zero
// #04. new complex_matrix( [ 1, 2, 3, 4, 5 ], "exceed", "1+i" ); --> a square matrix 3x3 is constructed: missing values are set to complex 1+i
// #05. new complex_matrix( [ 1, 2, 3, 4, 5 ], "exceed", new complex( 1, 1 ) ); --> equivalent to previous example, but using a complex object
// #06. new complex_matrix( 2, 2 ); --> a square matrix 2x2 is constructed and filled with 0
// #07. new complex_matrix( 2, 2, _CM_NO_FILL ); --> a square grid 2x2 is set but the matrix is not initialized
// #08. new complex_matrix( 4, 4, _CM_FILL, 1 ); --> a square matrix 4x4 is constructed and filled (argument indexed at 2 is 1) with 1
// #09. new complex_matrix( 4, 4, _CM_FILL, new complex( 1, 1 ) ); --> a square matrix 4x4 is constructed and filled with complex obj equalling 1+i
// #10. new complex_matrix( 4, 4, _CM_FILL, "2+2i" ); --> a square matrix 4x4 is constructed and filled with complex obj equalling 1+i
// #11. new complex_matrix( 2, 2, _CM_FILL, [ 1, 2, 3, 4 ] ); --> a square matrix 2x2 is constructed and filled with this input array (1, 2, 3, 4)
// #12. new complex_matrix( 2, 2, _CM_FILL, [ 1, new complex( 2, 1 ), "i", 1 ] ); --> a square matrix 2x2 is constructed and filled with complex entries
this.grid = [];
if ( is_array( arguments[0] ) )
{
this.grid = arguments[0].clone();
if ( is_number( arguments[1] ) && is_number( arguments[2] ) )
{
this.rows = safe_int( arguments[1], 0 ), this.cols = safe_int( arguments[2], 1 ) ;
}
else
{
// in this case we assume that the input matrix is a square matrix
this.rows = Math.sqrt( this.grid.length );
this.rows = ( is_string( arguments[1] ) && arguments[1] === "exceed" ) ? Math.ceil( this.rows ) : Math.floor( this.rows );
this.cols = this.rows ;
}
this.grid = this.grid.trunc( this.rows * this.cols );
for( var _i = 0 ; _i < this.grid.length ; _i++ )
{
if ( is_number( this.grid[_i] ) ) this.grid[_i] = new complex( this.grid[_i], 0 ) ;
else if ( is_string( this.grid[_i] ) ) this.grid[_i] = parse_complex_from_string( this.grid[_i] );
}
// at last, it must be a complex number
var _fill_val = arguments[1] === "exceed" ? arguments[2] : arguments[3] ;
if ( is_string( _fill_val ) ) _fill_val = parse_complex_from_string( _fill_val );
else if ( is_number( _fill_val ) ) _fill_val = new complex( _fill_val, 0.0 ) ;
else if ( !is_complex( _fill_val ) ) _fill_val = new complex( 0, 0 ) ;
var _diff = Math.abs( this.cols * this.rows ) - this.grid.length ;
this.grid = this.grid.append( _fill_val, _diff ) ;
}
else if ( is_number( arguments[0] ) && is_number( arguments[1] ) )
{
this.rows = safe_int( arguments[0], 0 ), this.cols = safe_int( arguments[1], 0 ) ;
var _fill_bool = safe_int( arguments[2], 1 ), _fill_val = arguments[3] ;
var _size = this.size(), _default_value = new complex( 0, 0 ) ;
if ( is_array( _fill_val ) )
{
this.grid = _fill_val.clone();
var _diff = _size - this.grid.length ;
if ( _diff < 0 ) this.grid = _fill_val.trunc( _size ) ;
else if ( _diff > 0 ) for( var _d = 0 ; _d < _diff ; _d++ ) this.grid = this.grid.append( _default_value ) ;
for( var _i = 0 ; _i < this.grid.length ; _i++ )
{
if ( is_number( this.grid[_i] ) ) this.grid[_i] = new complex( this.grid[_i], 0 ) ;
else if ( is_string( this.grid[_i] ) ) this.grid[_i] = parse_complex_from_string( this.grid[_i] );
}
}
else if ( is_number( _fill_val ) || is_complex( _fill_val ) || is_string( _fill_val ) )
{
// at last, it must be a complex number
if ( is_string( _fill_val ) ) _default_value = parse_complex_from_string( _fill_val );
else if ( is_number( _fill_val ) ) _default_value = new complex( _fill_val, 0.0 ) ;
else if ( is_complex( _fill_val ) ) _default_value = _fill_val ;
else _default_value = new complex( 0, 0 ) ;
if ( _fill_bool ) for( var _i = 0 ; _i < _size ; _i++ ) this.grid.push( _default_value ) ;
}
else if ( _fill_bool ) for( var _i = 0 ; _i < _size ; _i++ ) this.grid.push( _default_value ) ;
}
else
{
this.rows = this.cols = 0 ;
this.grid = [] ;
}
this.err_log = [] ;
this.stream_lines = [] ;
this.parser_obj_ref = null ;
this.parser_eval_fn_tag = null ;
}
complex_matrix.prototype.get_stream = function() { return this.stream_lines.clone() ; }
complex_matrix.prototype.get_log = function() { return this.err_log.clone() ; }
complex_matrix.prototype.copy = function() { var _cm = new complex_matrix( this.rows, this.cols ) ; _cm.grid = this.grid.clone() ; return _cm ; }
complex_matrix.prototype.size = function() { return safe_int( this.rows * this.cols, 0 ) ; }
complex_matrix.prototype.count_rows = function() { return safe_int( this.rows, 0 ) ; }
complex_matrix.prototype.count_cols = function() { return safe_int( this.cols, 0 ) ; }
complex_matrix.prototype.set_parser_refs = function( _parser_obj_ref, _parser_eval_fn_tag )
{
this.parser_obj_ref = _parser_obj_ref ;
this.parser_eval_fn_tag = _parser_eval_fn_tag ;
}
// -- START -- grid management
complex_matrix.prototype.insert_row = function( _i, _input_row, _overwrite )
{
if ( !is_array( _input_row ) )
{
this.err_log.push( arguments.callee.caller + " : Input row must be of array type" );
return null ;
}
else if ( _input_row.length == 0 )
{
this.err_log.push( arguments.callee.caller + " : Input row is empty" );
return null ;
}
else if ( _input_row.length != this.cols )
{
this.err_log.push( arguments.callee.caller + " : Input row size does not match complex matrix dimensions" );
return null ;
}
_i = Math.min( Math.max( 0, safe_int( _i, 0 ) ), this.rows );
_overwrite = safe_int( _overwrite, 0 ) ;
var _r, _row, _x, _cm = new complex_matrix( this.rows, this.cols ), _inserted = 0 ;
_cm.grid.flush();
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r );
if ( _r == _i )
{
for( _x = 0 ; _x < _input_row.length ; _x++ ) _cm.grid.push( _input_row[_x] );
_inserted = 1 ;
}
for( _x = 0 ; _x < _row.length ; _x++ ) _cm.grid.push( _row[_x] );
}
if ( _inserted ) _cm.rows += 1 ;
if ( _overwrite )
{
this.grid = _cm.grid.clone();
this.rows = _cm.rows ;
this.cols = _cm.cols ;
}
else return _cm ;
}
complex_matrix.prototype.insert_col = function( _i, _input_col, _overwrite )
{
_old_size = safe_int( this.grid.length, 0 ) ;
if ( !is_array( _input_col ) )
{
this.err_log.push( arguments.callee.caller + " : Input column must be of array type" );
return null ;
}
else if ( _input_col.length == 0 )
{
this.err_log.push( arguments.callee.caller + " : Input column is empty" );
return null ;
}
else if ( _input_col.length != this.rows )
{
this.err_log.push( arguments.callee.caller + " : Input column size does not match complex matrix dimensions" );
return null ;
}
_i = Math.min( Math.max( 0, safe_int( _i, 0 ) ), this.cols );
_overwrite = safe_int( _overwrite, 0 ) ;
var _r, _row, _x, _cm = new complex_matrix( this.rows, this.cols ), _inserted = 0 ;
_cm.grid.flush();
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r );
for( _x = 0 ; _x < _row.length ; _x++ )
{
if ( _x == _i ) _cm.grid.push( _input_col[_r] );
_cm.grid.push( _row[_x] );
}
}
_inserted = ( _old_size + _input_col.length == _cm.grid.length ) ? 1 : 0 ;
if ( _inserted ) _cm.cols += 1 ;
if ( _overwrite )
{
this.grid = _cm.grid.clone();
this.rows = _cm.rows ;
this.cols = _cm.cols ;
}
else return _cm ;
}
complex_matrix.prototype.delete_row = function()
{
_overwrite = safe_int( arguments[0], 0 ) ;
_old_size = safe_int( this.grid.length, 0 ) ;
if ( _old_size == 0 )
{
this.err_log.push( arguments.callee.caller + " : this complex matrix is empty" );
return null ;
}
var _a, _i, _r, _row, _x, _cm = new complex_matrix( this.rows, this.cols ), _deleted = 0 ;
for( _a = 1 ; _a < arguments.length ; _a++ )
{
_i = Math.min( Math.max( 0, safe_int( arguments[_a], 0 ) ), this.rows );
_overwrite = safe_int( _overwrite, 0 ) ;
_cm.grid.flush();
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r );
if ( _r != _i ) for( _x = 0 ; _x < _row.length ; _x++ ) _cm.grid.push( _row[_x] );
else _deleted = 1 ;
}
if ( _deleted ) _cm.rows -= 1 ;
}
if ( _overwrite )
{
this.grid = _cm.grid.clone();
this.rows = _cm.rows, this.cols = _cm.cols ;
}
else return _cm ;
}
complex_matrix.prototype.delete_col = function( _i, _overwrite )
{
_overwrite = safe_int( arguments[0], 0 ) ;
_old_size = safe_int( this.grid.length, 0 ) ;
if ( _old_size == 0 )
{
this.err_log.push( arguments.callee.caller + " : this complex matrix is empty" );
return null ;
}
var _a, _i, _r, _row, _x, _cm = new complex_matrix( this.rows, this.cols ), _deleted = 0 ;
for( _a = 1 ; _a < arguments.length ; _a++ )
{
_i = Math.min( Math.max( 0, safe_int( arguments[_a], 0 ) ), this.rows );
_overwrite = safe_int( _overwrite, 0 ) ;
_cm.grid.flush();
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r );
for( _x = 0 ; _x < _row.length ; _x++ ) if ( _x != _i ) _cm.grid.push( _row[_x] );
}
_deleted = ( _old_size - this.rows == _cm.grid.length ) ? 1 : 0 ;
if ( _deleted ) _cm.cols -= 1 ;
}
if ( _overwrite )
{
this.grid = _cm.grid.clone();
this.rows = _cm.rows, this.cols = _cm.cols ;
}
else return _cm ;
}
// -- START -- check operations --
complex_matrix.prototype.is_binary_matrix = function() { return this.is_boolean_matrix(); }
complex_matrix.prototype.is_singular_matrix = function() { return this.det().radius() == 0 ? 1 : 0 ; }
complex_matrix.prototype.is_logical_matrix = function() { return this.is_boolean_matrix() ; }
complex_matrix.prototype.is_consistent_matrix = function() { return ( ( this.rows + this.cols ) % 2 == 0 ) ; }
complex_matrix.prototype.is_row_matrix = function() { return ( this.rows == 1 && this.cols == 0 ) ? 1 : 0 ; }
complex_matrix.prototype.is_column_matrix = function() { return ( this.rows == 0 && this.cols == 1 ) ? 1 : 0 ; }
complex_matrix.prototype.is_rectangular_matrix = function() { return this.rows != this.cols ? 1 : 0 ; }
complex_matrix.prototype.is_square_matrix = function() { return this.rows == this.cols ? 1 : 0 ; }
complex_matrix.prototype.is_symmetric_matrix = function() { return this.is_equal_to( this.get_transpose_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_skewsymmetric_matrix = function() { return this.is_equal_to( this.get_transpose_matrix().get_negative_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_nilpotent_matrix_of_order = function( _k ) { return this.pow( _k ).is_equal_to( this.get_zero_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_periodic_matrix_of_order = function( _k ) { return this.is_equal_to( this.pow( _k ) ) ? 1 : 0 ; }
complex_matrix.prototype.is_idempotent_matrix = function() { return this.is_periodic_matrix_of_order( 2 ) ? 1 : 0 ; }
complex_matrix.prototype.is_hermitian_matrix = function() { return this.is_equal_to( this.get_adjoint_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_antihermitian_matrix = function() { return this.get_negative_matrix().is_equal_to( this.get_adjoint_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_involutive_matrix = function() { return this.pow( 2 ).is_equal_to( this.get_identity_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_orthogonal_matrix = function() { return this.mul( this.get_transpose_matrix() ).is_equal_to( this.get_identity_matrix() ) ? 1 : 0 ; }
complex_matrix.prototype.is_normal_matrix = function() { return this.mul( this.get_transpose_matrix() ).is_equal_to( this.get_transpose_matrix().mul( this ) ) ? 1 : 0 ; }
complex_matrix.prototype.is_zero_matrix = function()
{
var _b_ok = 1, _r ;
for( _r = 0 ; _r < this.grid.length ; _r++ )
{
if ( this.grid[ _r ].radius() > 0 )
{
_b_ok = 0 ;
break ;
}
}
return _b_ok ;
}
complex_matrix.prototype.is_boolean_matrix = function()
{
var _b_ok = 1, _unit = new complex( 1, 0 ), _zero = new complex( 0, 0 ), _r ;
for( _r = 0 ; _r < this.grid.length ; _r++ )
{
if ( !this.grid[ _r ].is_equal_to( _unit ) && !this.grid[ _r ].is_equal_to( _zero ) )
{
_b_ok = 0 ;
break ;
}
}
return _b_ok ;
}
complex_matrix.prototype.is_identity_matrix = function()
{
var _b_ok = 1, _item, _r, _c ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
for( _c = 0 ; _c < this.cols ; _c++ )
{
_item = this.grid[ _r * this.cols + _c ] ;
if ( ( !_item.is_zero() && _r != _c ) || ( !_item.is_real_unit() && _r == _c ) )
{
_b_ok = 0 ;
break ;
}
}
}
return _b_ok ;
}
complex_matrix.prototype.is_permutation_matrix = function()
{
var _b_ok = 1, _row, _r, _count = 0, _runner, _unit = new complex( 1, 0 ), _zero = new complex( 0, 0 ) ;
mainfor:
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r ), _count = 0;
for( _runner = 0 ; _runner < _row.length ; _runner++ )
{
if ( _row[ _runner ].is_equal_to( _unit ) ) _count++ ;
else if ( !( _row[ _runner ].is_equal_to( _zero ) ) )
{
_b_ok = 0 ;
break mainfor ;
}
}
if ( _count != 1 )
{
_b_ok = 0 ;
break mainfor ;
}
}
return _b_ok ;
}
complex_matrix.prototype.is_diagonal_matrix = function()
{
var _b_ok = 1, _item, _r, _c ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
for( _c = 0 ; _c < this.cols ; _c++ )
{
_item = this.grid[ _r * this.cols + _c ] ;
if ( !_item.is_zero() && _r != _c )
{
_b_ok = 0 ;
break ;
}
}
}
return _b_ok ;
}
complex_matrix.prototype.is_antidiagonal_matrix = function()
{
var _b_ok = 1, _item, _r, _c ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
for( _c = 0 ; _c < this.cols ; _c++ )
{
_item = this.grid[ _r * this.cols + _c ] ;
if ( !_item.is_zero() && _c != ( this.rows - _r - 1 ) )
{
_b_ok = 0 ;
break ;
}
}
}
return _b_ok ;
}
complex_matrix.prototype.is_scalar_matrix = function()
{
if ( !this.is_diagonal ) return 0 ;
var _b_ok = 1, _item = null, _rec_item = null, _r, _c ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
for( _c = 0 ; _c < this.cols ; _c++ )
{
_item = this.grid[ _r * this.cols + _c ] ;
if ( !_item.is_zero() && _r != _c )
{
_b_ok = 0 ;
break ;
}
else if ( _r == _c )
{
if ( _rec_item == null ) _rec_item = _item ;
else if ( !_item.is_equal_to( _rec_item ) )
{
_b_ok = 0 ;
break ;
}
}
}
}
return _b_ok ;
}
complex_matrix.prototype.is_equal_to = function( _cm )
{
if ( !is_complex_matrix( _cm ) ) return 0 ;
else if ( _cm.rows != this.rows || _cm.cols != this.cols ) return 0 ;
else
{
var _b_ok = 1, _r, _c ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
for( _c = 0 ; _c < this.cols ; _c++ )
{
if ( !( _cm.grid[ _r * this.cols + _c ].is_equal_to( this.grid[ _r * this.cols + _c ] ) ) )
{
_b_ok = 0 ;
break ;
}
}
}
return _b_ok ;
}
}
complex_matrix.prototype.commute_with = function( _cm ) { return ( is_complex_matrix( _cm ) ) ? ( ( this.mul( _cm ).is_equal_to( _cm.mul( this ) ) ) ? 1 : 0 ) : 0 ; }
complex_matrix.prototype.anticommute_with = function( _cm ) { return ( is_complex_matrix( _cm ) ) ? ( ( this.mul( _cm ).is_equal_to( _cm.mul( this ).get_negative_matrix() ) ) ? 1 : 0 ) : 0 ; }
// -- END -- check operations --
complex_matrix.prototype.set_in = function( _input_val ) { for( var _i = 0 ; _i < arguments.length ; _i++ ) if ( is_complex_matrix( arguments[_i] ) ) this.grid.push( arguments[_i] ) ; }
complex_matrix.prototype.set_params = function()
{
var _c = null, _a, _i ;
this.grid.flush();
if ( is_complex_matrix( arguments[0] ) )
{
this.grid = arguments[0].grid.clone();
this.cols = arguments[0].cols, this.rows = arguments[0].rows ;
return 1 ;
}
else if ( safe_size( arguments, 0 ) > 0 )
{
for( _a = 0 ; _a < arguments.length ; _a++ )
{
if ( is_array( arguments[_a] ) )
{
for( _i = 0 ; _i < arguments[_a].length ; _i++ )
{
if ( is_string( arguments[_a][_i] ) ) arguments[_a][_i] = parse_complex_from_string( arguments[_a][_i] );
_c = is_number( arguments[_a][_i] ) ? new complex( safe_float( arguments[_a][_i], 0 ), 0 ) : ( is_complex( arguments[_a][_i] ) ? arguments[_a][_i] : null ) ;
if ( _c == null ) this.err_log.push( arguments.callee.caller + " : Invalid input " + arguments[_a] );
this.grid.push( _c == null ? new complex( 0, 0 ) : _c );
}
}
else
{
_c = is_number( arguments[_a] ) ? new complex( arguments[_a], 0 ) : ( is_string( arguments[_a] ) ? parse_complex_from_string( arguments[_a] ) : null ) ;
if ( _c == null ) this.err_log.push( arguments.callee.caller + " : Invalid input " + arguments[_a] );
this.grid.push( _c == null ? new complex( 0, 0 ) : _c );
}
}
return 1 ;
}
else return 0 ;
}
complex_matrix.prototype.set_formal_params = function()
{
var _c = null, _a, _i ;
if ( is_complex_matrix( arguments[0] ) )
{
this.grid.flush();
this.grid = arguments[0].grid.clone();
this.cols = arguments[0].cols, this.rows = arguments[0].rows ;
return 1 ;
}
else if ( safe_size( arguments, 0 ) > 0 )
{
this.grid.flush();
for( _a = 0 ; _a < arguments.length ; _a++ ) this.grid.push( arguments[_a] );
return 1 ;
}
else return 0 ;
}
complex_matrix.prototype.resolve_parametrization = function( _params_tags, _params_complex_vals )
{
if ( !is_array( _params_tags ) && !is_array( _params_complex_vals ) ) return null ;
var _cm_copy = this.copy(), _grid_len = safe_size( _cm_copy.grid, 0 );
var _i, _p ;
// 1. it replaces the formal parameters with input values
for( _i = 0 ; _i < _grid_len ; _i++ )
{
if ( is_string( _cm_copy.grid[_i] ) )
{
for( _p = 0 ; _p < _params_tags.length ; _p++ )
_cm_copy.grid[_i] = _cm_copy.grid[_i].replaceAll( _params_tags[_p], "("+_params_complex_vals[_p].formula()+")" );
}
}
// 2. evaluates complex matrix elements
if ( this.parser_obj_ref != null && this.parser_eval_fn_tag != null )
{
var _parser = this.parser_obj_ref, _fn = this.parser_eval_fn_tag, _result ;
for( _i = 0 ; _i < _grid_len ; _i++ )
{
eval( "_result = _parser."+_fn+"( '"+_cm_copy.grid[_i]+"' );" ) ;
_cm_copy.grid[_i] = _result.re != null ? new complex( _result.re, _result.im ) : new complex( _result, 0 ) ;
}
return _cm_copy ;
}
else return null ;
}
complex_matrix.prototype.set_value_at = function( _x, _y, _val )
{
if ( this.grid[ _y * this.rows + _x ] != null && is_complex( _val ) )
{
this.grid[ _y * this.rows + _x ] = _val ;
return 1 ;
}
else
{
if ( !is_complex( _val ) ) this.err_log.push( arguments.callee.caller + " : Invalid input complex value" );
else this.err_log.push( arguments.callee.caller + " : Invalid coords " + _x + ", " + _y );
return 0 ;
}
}
complex_matrix.prototype.work = function( fn )
{
if ( typeof fn != "function" ) throw new TypeError();
var _cm = new complex_matrix( this.cols, this.rows, 0 ), _r, _c ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
for( _c = 0 ; _c < this.cols ; _c++ ) _cm.grid.push( fn.call( null, _r, _c, this.grid[ _r * this.cols + _c ] ) );
}
return _cm ;
};
// START - arithmetics
complex_matrix.prototype.add = function( _add2 )
{
if ( is_complex_matrix( _add2 ) )
{
var _add1 = this.copy(), _i;
for( _i = 0 ; _i < _add1.grid.length ; _i++ ) _add1.grid[_i] = _add1.grid[_i].add( _add2.grid[_i] );
return _add1 ;
}
else
{
this.err_log.push( arguments.callee.caller + " : input is not of complex matrix type" );
return null ;
}
}
complex_matrix.prototype.sub = function( _sub2 )
{
if ( is_complex_matrix( _sub2 ) )
{
var _sub1 = this.copy(), _i ;
for( _i = 0 ; _i < _sub1.grid.length ; _i++ ) _sub1.grid[_i] = _sub1.grid[_i].sub( _sub2.grid[_i] );
return _sub1 ;
}
else
{
this.err_log.push( arguments.callee.caller + " : input is not of complex matrix type" );
return null ;
}
}
// MATRIX MULTIPLICATION
complex_matrix.prototype.mul = function( _m2 )
{
if ( !is_complex_matrix( _m2 ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of complex matrix type" );
return null ;
}
else if ( this.rows != _m2.cols || this.cols != _m2.rows )
{
this.err_log.push( arguments.callee.caller + " : input matrix dimensions are not congruent to caller matrix size" );
return null ;
}
var _ret_matrix = new complex_matrix( this.rows, this.cols );
var _row, _col, _ret_complex = new complex( 0, 0 ), _r, _c, _runner ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r );
for( _c = 0 ; _c < _m2.cols ; _c++ )
{
_col = _m2.get_col( _c );
_ret_complex.real = 0, _ret_complex.imag = 0 ;
for( _runner = 0 ; _runner < _row.length ; _runner++ ) _ret_complex = _ret_complex.add( _row[ _runner ].mul( _col[ _runner ] ) );
_ret_matrix.set_value_at( _c, _r, _ret_complex.copy() );
}
}
return _ret_matrix ;
}
complex_matrix.prototype.mul_formal = function( _m2 )
{
if ( ! is_complex_matrix( _m2 ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of complex matrix type" );
return null ;
}
else if ( this.rows != _m2.cols || this.cols != _m2.rows )
{
this.err_log.push( arguments.callee.caller + " : input matrix dimensions are not congruent to caller matrix size" );
return null ;
}
var _ret_matrix = new complex_matrix( this.rows, this.cols );
var _row, _col, _ret_complex_array = [], _ret_1, _ret_2, _r, _c, _runner ;
for( _r = 0 ; _r < this.rows ; _r++ )
{
_row = this.get_row( _r );
for( _c = 0 ; _c < _m2.cols ; _c++ )
{
_col = _m2.get_col( _c );
_ret_complex_array = [] ;
for( _runner = 0 ; _runner < _row.length ; _runner++ )
{
_ret_1 = "("+( is_complex( _row[ _runner ] ) ? _row[ _runner ].formula() : _row[ _runner ] )+")" ;
_ret_2 = "("+( is_complex( _col[ _runner ] ) ? _col[ _runner ].formula() : _col[ _runner ] )+")" ;
_ret_complex_array.push( _ret_1 +"*"+ _ret_2 ) ;
}
_ret_matrix.set_value_at( _c, _r, _ret_complex_array.join("+") );
}
}
return _ret_matrix ;
}
complex_matrix.prototype.hadamard_product = function( _m2 )
{
if ( is_array( _m2 ) ) _m2 = new complex_matrix( this.rows, this.cols, _m2 ) ;
if ( ! is_complex_matrix( _m2 ) ) return null ;
else if ( this.size() == _m2.size() )
{
var _ret_matrix = new complex_matrix( this.rows, this.cols );
for( var _runner = 0 ; _runner < this.size() ; _runner++ )
_ret_matrix.grid[_runner] = this.grid[_runner].mul( _m2.grid[_runner] );
return _ret_matrix ;
}
else return null ;
}
complex_matrix.prototype.dot_product = function( _m2 )
{
if ( ! is_complex_matrix( _m2 ) ) return null ;
else if ( this.size() == _m2.size() )
{
var _ret = new complex( 0, 0 ) ;
for( var _runner = 0 ; _runner < this.size() ; _runner++ )
_ret = _ret.add( this.grid[_runner].mul( _m2[_runner] ) );
return _ret ;
}
else return null ;
}
// END - arithmetics
complex_matrix.prototype.trace = function()
{
if ( this.is_square_matrix() )
{
var _tr = new complex( 0, 0 ), _y = 0, _i ;
for( _i = 0 ; _i < this.rows ; _i++, _y++ )
{
_pos = _i * this.cols + _y ;
_tr = _tr.add( this.grid[ _pos ] );
}
return _tr ;
}
else return null ;
}
complex_matrix.prototype.antitrace = function()
{
if ( this.is_square_matrix() )
{
var _tr = new complex( 0, 0 ), _y = this.count_cols()-1, _i ;
for( _i = 0 ; _i < this.rows ; _i++, _y-- )
{
_pos = _i * this.cols + _y ;
_tr = _tr.add( this.grid[ _pos ] );
}
return _tr ;
}
else return null ;
}
complex_matrix.prototype.det = function()
{
// temporary code ... looking forward to implement a more performant algorithm
//return ( this.rows == 2 && this.cols == 2 ) ? this.grid[0].mul( this.grid[3] ).sub( this.grid[1].mul( this.grid[2] ) ) : new complex( 0, 0 ) ;
var _array_of_indexes = [];
for( var _i = 0 ; _i < this.cols ; _i++ ) _array_of_indexes.push( _i );
var _all_permutations = _array_of_indexes.permute(), _sign, _determinant = new complex( 0, 0 ), _row_product, _permutation ;
var _p1, _p2, _row_pos ;
for( _p1 = 0 ; _p1 < _all_permutations.length ; _p1++ )
{
_permutation = _all_permutations[_p1] ;
_sign = _p1 % 2 == 0 ? 1 : -1 ;
_row_product = new complex( 1, 0 ).mul( _sign ) ;
for( _p2 = 0, _row = 0 ; _p2 < _permutation.length ; _p2++, _row++ )
_row_product = _row_product.mul( this.grid[ _row * this.cols + _permutation[_p2] ] );
_determinant = _determinant.add( _row_product );
}
return _determinant ;
}
complex_matrix.prototype.normalize = function( _overwrite )
{
_overwrite = safe_int( _overwrite, 0 );
var _cm = this.copy(), _det = this.det() ;
if ( _det.radius() == 0 ) return this ;
for( var _i = 0 ; _i < _cm.grid.length ; _i++ ) _cm.grid[_i] = _cm.grid[_i].div( _det );
if ( !_overwrite ) return _cm ;
else
{
this.grid = _cm.grid.clone();
return null ;
}
}
// -- START -- elementary operations --
complex_matrix.prototype.get_item_at = function( _x, _y ) { _x = parseInt( _x ); _y = parseInt( _y ); return this.grid[ _y * this.rows + _x ] ; }
complex_matrix.prototype.get_row = function( _nth_row )
{
if ( _nth_row < 0 || _nth_row > this.rows )
{
this.err_log.push( arguments.callee.caller + " : input row index is out of range" );
return null ;
}
var _out = [], _i ;
for( _i = 0 ; _i < this.cols ; _i++ ) _out.push( this.grid[ _nth_row * this.cols + _i ] );
return _out ;
}
complex_matrix.prototype.get_col = function( _nth_col )
{
if ( _nth_col < 0 || _nth_col > this.cols )
{
this.err_log.push( arguments.callee.caller + " : input columns index is out of range" );
return null ;
}
var _out = [], _r ;
for( _r = 0 ; _r < this.rows ; _r++ ) _out.push( this.grid[ _r * this.cols + _nth_col ] );
return _out ;
}
complex_matrix.prototype.set_row = function( _i, _arr )
{
if ( is_array( _arr ) )
{
if ( _i < 0 || _i > this.rows )
{
this.err_log.push( arguments.callee.caller + " : input row index is out of range" );
return 0 ;
}
else if ( _arr.length == this.cols )
{
var _pos = _i * this.cols, _a ;
for( _a = 0 ; _a < _arr.length ; _a++, _pos++ )
{
if ( is_complex( _arr[_a] ) ) this.grid[_pos] = _arr[_a] ;
else
{
this.err_log.push( arguments.callee.caller + " : input item, with index "+_a+", is not of complex type" );
return 0 ;
}
}
return 1 ;
}
else
{
this.err_log.push( arguments.callee.caller + " : undetermined error" );
return 0 ;
}
}
else
{
this.err_log.push( arguments.callee.caller + " : input is not of array type" );
return 0 ;
}
}
complex_matrix.prototype.set_col = function( _i, _arr )
{
if ( is_array( _arr ) )
{
if ( _i < 0 || _i > this.cols ) return 0 ;
else if ( _arr.length == this.rows )
{
var _pos = _i, _a ;
for( _a = 0 ; _a < _arr.length ; _a++ )
{
if ( is_complex( _arr[_a] ) ) this.grid[_pos+_a*this.rows] = _arr[_a] ;
else
{
this.err_log.push( arguments.callee.caller + " : input item, with index "+_a+", is not of complex type" );
return 0 ;
}
}
return 1 ;
}
else
{
this.err_log.push( arguments.callee.caller + " : undetermined error" );
return 0 ;
}
}
else
{
this.err_log.push( arguments.callee.caller + " : input is not of array type" );
return 0 ;
}
}
complex_matrix.prototype.add_to_row = function( _row_index, _value, _overwrite )
{
if ( is_array( _value ) )
{
if ( _value.length != this.cols )
{
this.err_log.push( arguments.callee.caller + " : incongruent row size" );
return null ;
}
}
else if ( !is_complex( _value ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of allowed type (complex or array of complexes)" );
return null ;
}
_overwrite = safe_int( _overwrite, 1 );
var _grid = [], _c ;
for( _c = 0 ; _c < this.cols ; _c++ )
{
if ( _overwrite )
this.grid[ _row_index * this.cols + _c ] = this.grid[ _row_index * this.cols + _c ].add( is_array( _value ) ? _value[_c] : _value );
else
_grid.push( this.grid[ _row_index * this.cols + _c ].add( is_array( _value ) ? _value[_c] : _value ) );
}
return _overwrite ? null : _grid.clone();
}
complex_matrix.prototype.sub_from_row = function( _row_index, _value, _overwrite )
{
if ( is_array( _value ) )
{
if ( _value.length != this.cols )
{
this.err_log.push( arguments.callee.caller + " : incongruent column size" );
return null ;
}
}
else if ( !is_complex( _value ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of allowed type (complex or array of complexes)" );
return null ;
}
_overwrite = safe_int( _overwrite, 1 );
var _grid = [], _c ;
for( _c = 0 ; _c < this.cols ; _c++ )
{
if ( _overwrite ) this.grid[ _row_index * this.cols + _c ] = this.grid[ _row_index * this.cols + _c ].sub( is_array( _value ) ? _value[_c] : _value );
else _grid.push( this.grid[ _row_index * this.cols + _c ].sub( is_array( _value ) ? _value[_c] : _value ) );
}
return _overwrite ? null : _grid.clone();
}
complex_matrix.prototype.mul_row_by = function( _row_index, _value, _overwrite )
{
if ( !is_complex( _value ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of complex type" );
return null ;
}
_overwrite = safe_int( _overwrite, 1 );
var _grid = [], _c ;
for( _c = 0 ; _c < this.cols ; _c++ )
{
if ( _overwrite ) this.grid[ _row_index * this.cols + _c ] = this.grid[ _row_index * this.cols + _c ].mul( _value );
else _grid.push( this.grid[ _row_index * this.cols + _c ].mul( _value ) );
}
return _overwrite ? null : _grid.clone();
}
complex_matrix.prototype.div_row_by = function( _row_index, _value, _overwrite )
{
if ( !is_complex( _value ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of complex type" );
return null ;
}
_overwrite = safe_int( _overwrite, 1 );
var _grid = [], _c ;
for( _c = 0 ; _c < this.cols ; _c++ )
{
if ( _overwrite ) this.grid[ _row_index * this.cols + _c ] = this.grid[ _row_index * this.cols + _c ].div( _value );
else _grid.push( this.grid[ _row_index * this.cols + _c ].div( _value ) );
}
return _overwrite ? null : _grid.clone();
}
complex_matrix.prototype.add_to_col = function( _col_index, _value, _overwrite )
{
if ( !is_complex( _value ) )
{
this.err_log.push( arguments.callee.caller + " : input is not of complex type" );