-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperator.java
More file actions
2896 lines (2361 loc) · 66 KB
/
Operator.java
File metadata and controls
2896 lines (2361 loc) · 66 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
public abstract class Operator implements FormulaBlock{
// Operators
public static final String PLUS = "+";
public static final String MINUS = "-";
public static final String TIMES = "*";
public static final String DIV = "/";
public static final String MOD = "%";
public static final String EQ = "=";
public static final String LESS = "<";
public static final String GTR = ">";
public static final String AND = "&";
public static final String OR = "|";
public static final String POW = "^";
public static final String LP = "(";
public static final String RP = ")";
public static final String LSP = "[";
public static final String RSP = "]";
public static final String LCP = "{";
public static final String RCP = "}";
public static final String NOT = "not";
public static final String SIN = "sin";
public static final String COS = "cos";
public static final String TAN = "tan";
public static final String ASIN = "asin";
public static final String ACOS = "acos";
public static final String ATAN = "atan";
public static final String LOG = "ln";
public static final String EXP = "exp";
public static final String SQRT = "sqrt";
public static final String ABS = "abs";
public static final String RND = "rnd";
public static final String FLOOR = "floor";
public static final String FREEV = "free";
public static final String PARTV = "coord";
public static final String COL = "col";
public static final String ROW = "row";
public static final String ELEM = "elem";
public static final String TR = "tp";
public static final String UNIT = "unit";
public static final String ZERO = "zero";
public static final String SUBST = "insert";
public static final String ARITH = "as";
public static final String GEOM = "gs";
public static final String NORM = "norm";
public static final String DOT = "dot";
public static final String CROSS = "cross";
public static final String SINGLEOP = ":";
public static final String COMMA = ",";
public static final String ALL_DELIMS = PLUS+MINUS+TIMES+DIV+MOD+
LP+RP+LSP+RSP+LCP+RCP+
POW+EQ+LESS+GTR+AND+OR+SINGLEOP+COMMA;
public static String[] OPERATORS = {PLUS,MINUS,TIMES,DIV,MOD,EQ,LESS,GTR,AND,OR,POW,
NOT,SIN,COS,TAN,ASIN,ACOS,ATAN,LOG,EXP,SQRT,ABS,RND,
FLOOR,FREEV,PARTV,COL,ROW,ELEM,TR,UNIT,ZERO,
SUBST,ARITH,GEOM,NORM,DOT,CROSS};
public static final int A_NOARG = 0;
public static final int A_SINGLE = 1;
public static final int A_DOUBLE = 2;
protected String opType;
protected int nArgs;
protected int argType;
protected String[] arguments;
protected String name;
protected String description;
//protected Calculator master;
//protected Structure geo;
protected boolean isSingle = false;
protected boolean isDouble = false;
protected boolean isParenthesis = false;
protected boolean isComparison = false;
protected boolean isLogic = false;
protected boolean isMultiplier = false;
protected boolean isPower = false;
protected boolean isAddition = false;
public Operator(){
}
public boolean isOperator(){
return true;
}
public boolean isCustomFunction(){
return false;
}
public String getType(){
return this.opType;
}
public int getArgType(){
return this.argType;
}
public String toString(){
return this.opType;
}
public int getArgumentCount(){
return this.nArgs;
}
public abstract Variable operate(FormulaBlock arg);
public abstract Variable operate(FormulaBlock a1, FormulaBlock a2);
public boolean isSingleOperator(){return isSingle;}
public boolean isDoubleOperator(){return isDouble;}
public boolean isParenthesisOperator(){return isParenthesis;}
public boolean isComparisonOperator(){return isComparison;}
public boolean isLogicOperator(){return isLogic;}
public boolean isMultiplierOperator(){return isMultiplier;}
public boolean isPowerOperator(){return isPower;}
public boolean isAdditionOperator(){return isAddition;}
public static boolean isSingleOperator(String type){
try{
return createOperator(type,1,null).isSingleOperator();
} catch (Exception error){
return false;
}
}
public static boolean isDoubleOperator(String type){
try{
return createOperator(type,2,null).isDoubleOperator();
} catch (Exception error){
return false;
}
}
public static boolean isParenthesisOperator(String type){
try{
return createOperator(type,0,null).isParenthesisOperator();
} catch (Exception error){
return false;
}
}
public static boolean isComparisonOperator(String type){
try{
return createOperator(type,2,null).isComparisonOperator();
} catch (Exception error){
return false;
}
}
public static boolean isLogicOperator(String type){
try{
return createOperator(type,2,null).isLogicOperator();
} catch (Exception error){
return false;
}
}
public static boolean isMultiplierOperator(String type){
try{
return createOperator(type,2,null).isMultiplierOperator();
} catch (Exception error){
return false;
}
}
public static boolean isAdditionOperator(String type){
try{
return createOperator(type,2,null).isAdditionOperator();
} catch (Exception error){
return false;
}
}
public static boolean isOperator(String type){
try{
createOperator(type,1,null);
return true;
} catch(Exception error){
return false;
}
}
public static Operator createOperator(String type, int argcount, Calculator master)
throws Exception{
if(type.equals(PLUS)){
return new PlusOperator();
} else if(type.equals(MINUS) && argcount == 1){
return new SingleMinusOperator();
} else if(type.equals(MINUS) && argcount == 2){
return new MinusOperator();
} else if(type.equals(TIMES)){
return new TimesOperator();
} else if(type.equals(DIV)){
return new DividedOperator();
} else if(type.equals(MOD)){
return new ModuloOperator();
} else if(type.equals(POW)){
return new PowerOperator();
} else if(type.equals(EQ)){
return new EqualsOperator();
} else if(type.equals(LESS)){
return new LessThanOperator();
} else if(type.equals(GTR)){
return new GreaterThanOperator();
} else if(type.equals(AND)){
return new LogicAndOperator();
} else if(type.equals(OR)){
return new LogicOrOperator();
} else if(type.equals(NOT)){
return new NotOperator();
} else if(type.equals(SIN)){
return new SinOperator();
} else if(type.equals(COS)){
return new CosOperator();
} else if(type.equals(TAN)){
return new TanOperator();
} else if(type.equals(ASIN)){
return new ArcsinOperator();
} else if(type.equals(ACOS)){
return new ArccosOperator();
} else if(type.equals(ATAN)){
return new ArctanOperator();
} else if(type.equals(LOG)){
return new LogarithmOperator();
} else if(type.equals(EXP)){
return new ExponentOperator();
} else if(type.equals(SQRT)){
return new SquareRootOperator();
} else if(type.equals(ABS)){
return new AbsoluteValueOperator();
} else if(type.equals(RND)){
return new RandomVariableOperator();
} else if(type.equals(TR)){
return new TransposeOperator();
} else if(type.equals(FLOOR)){
return new FloorOperator();
} else if(type.equals(PARTV)){
return new CoordinatesOperator(master.getMaster().getCurrentStructure());
} else if(type.equals(FREEV)){
return new ConstraintsOperator(master.getMaster().getCurrentStructure());
} else if(type.equals(COL)){
return new MatrixColumnOperator();
} else if(type.equals(ROW)){
return new MatrixRowOperator();
} else if(type.equals(UNIT)){
return new UnitMatrixOperator();
} else if(type.equals(ZERO)){
return new ZeroMatrixOperator();
} else if(type.equals(ELEM)){
return new MatrixElementOperator();
} else if(type.equals(SUBST)){
return new MatrixSubstitutionOperator();
} else if(type.equals(ARITH)){
return new ArithmeticSeriesOperator();
} else if(type.equals(GEOM)){
return new GeometricSeriesOperator();
} else if(type.equals(LP) ||
type.equals(RP) ||
type.equals(LSP) ||
type.equals(RSP) ||
type.equals(LCP) ||
type.equals(RCP) ||
type.equals(COMMA) ){
return new BracketOperator(type);
} else if(type.equals(SINGLEOP)){
return new FunctionOperator();
} else if(type.equals(NORM)){
return new VectorNormOperator();
} else if(type.equals(DOT)){
return new VectorDotOperator();
} else if(type.equals(CROSS)){
return new VectorCrossOperator();
}
throw new Exception();
}
public String getSyntax(){
String info = "";
if(argType == A_NOARG){
info += opType;
} else if(argType == A_SINGLE){
if(opType.equals(MINUS)){
info += opType+arguments[0];
} else {
info += opType+SINGLEOP;
if(nArgs > 1){
info += "{";
for(int i=0; i<nArgs; i++){
info += arguments[i];
if(i<nArgs-1){
info += ",";
}
}
info += "}";
} else {
info += arguments[0];
}
}
} else if(argType == A_DOUBLE){
info += arguments[0]+opType+arguments[1];
}
return info;
}
public String getName(){
return this.name;
}
public String getUsage(){
return this.description;
}
}
class PlusOperator extends Operator{
public PlusOperator(){
super();
this.opType = PLUS;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isAddition = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "addition";
this.description = "Adds the values of two variables.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]+b[i][j];
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
result[i][j] = a[0][0]+b[i][j];
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]+b[0][0];
}
}
} else {
System.out.println("Incompatible dimensions in matrix addition.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class MinusOperator extends Operator{
public MinusOperator(){
super();
this.opType = MINUS;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isAddition = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "subtraction";
this.description = "Subtracts the value of one variable from the other.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]-b[i][j];
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
result[i][j] = a[0][0]-b[i][j];
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]-b[0][0];
}
}
} else {
System.out.println("Incompatible dimensions in matrix subtraction.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class TimesOperator extends Operator{
public TimesOperator(){
super();
this.opType = TIMES;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isMultiplier = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "multiplication";
this.description = "Multiplication of two variables. For matrices, normal matrix multiplication is done.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw == bh){ // matrix multiplication
result = new double[ah][bw];
for(int i=0; i<ah; i++){
for(int j=0; j<bw; j++){
result[i][j] = 0;
for(int k=0; k<aw; k++){
result[i][j] += a[i][k]*b[k][j];
}
}
}
} else if(aw==bw && ah==bh && aw != ah){ // element-wise multiplication
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]*b[i][j];
}
}
} else if(aw==1 && ah == 1){ // scalar multiplication
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
result[i][j] = a[0][0]*b[i][j];
}
}
} else if(bw==1 && bh == 1){ // scalar multiplication
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]*b[0][0];
}
}
} else {
System.out.println("Incompatible dimensions in matrix multiplication.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class DividedOperator extends Operator{
public DividedOperator(){
super();
this.opType = DIV;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isMultiplier = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "addition";
this.description = "Divides the value of one variable by the other.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]/b[i][j];
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
result[i][j] = a[0][0]/b[i][j];
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = a[i][j]/b[0][0];
}
}
} else {
System.out.println("Incompatible dimensions in matrix division.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class ModuloOperator extends Operator{
public ModuloOperator(){
super();
this.opType = MOD;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isMultiplier = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "modulus";
this.description = "Calculates the remainder of the integer division x/y.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = (int)a[i][j]%(int)b[i][j];
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
result[i][j] = (int)a[0][0]%(int)b[i][j];
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = (int)a[i][j]%(int)b[0][0];
}
}
} else {
System.out.println("Incompatible dimensions in matrix modulus.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class PowerOperator extends Operator{
public PowerOperator(){
super();
this.opType = POW;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isPower = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "power";
this.description = "Calculates the power function for arbitrary real base and exponent.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = Math.pow(a[i][j],b[i][j]);
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
result[i][j] = Math.pow(a[0][0],b[i][j]);
}
}
} else if(bw==1 && bh == 1 && aw != ah){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = Math.pow(a[i][j],b[0][0]);
}
}
// Matrix power
} else if(bw==1 && bh == 1 && aw == ah){
result = new double[ah][aw];
double[][] c = new double[ah][aw];
// unit matrix
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(i==j){
c[i][j] = 1;
} else {
c[i][j] = 0;
}
}
}
int power = (int)b[0][0];
for(int n=0; n<power; n++){
// Matrix multiplication
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
result[i][j] = 0;
for(int k=0; k<aw; k++){
result[i][j] += a[i][k]*c[k][j];
}
}
}
if(n < power){
// copy result to c for the next multiplication
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
c[i][j] = result[i][j];
}
}
}
}
} else {
System.out.println("Incompatible dimensions in matrix power.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class EqualsOperator extends Operator{
public EqualsOperator(){
super();
this.opType = EQ;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isComparison = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "equality";
this.description = "Compares two values. If the values are equal, the equality is given value 1, and 0 otherwise.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[1][1];
result[0][0] = 1;
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(a[i][j] != b[i][j]){
result[0][0] = 0;
}
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
if(a[0][0] == b[i][j]){
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(a[i][j] == b[0][0]){
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
} else {
System.out.println("Incompatible dimensions in matrix comparison.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class LessThanOperator extends Operator{
public LessThanOperator(){
super();
this.opType = LESS;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isComparison = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "less than";
this.description = "Compares two values. If the first is strictly less than the second, the comparison is given the value 1, and 0 otherwise.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[1][1];
result[0][0] = 1;
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(a[i][j] >= b[i][j]){
result[0][0] = 0;
}
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
if(a[0][0] < b[i][j]){
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(a[i][j] < b[0][0]){
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
} else {
System.out.println("Incompatible dimensions in matrix comparison.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class GreaterThanOperator extends Operator{
public GreaterThanOperator(){
super();
this.opType = GTR;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isComparison = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "greater than";
this.description = "Compares two values. If the first is strictly greater than the first, the expression is given value 1, and 0 otherwise.";
}
public Variable operate(FormulaBlock arg){
return new Variable("NaN",Double.NaN);
}
public Variable operate(FormulaBlock a1, FormulaBlock a2){
double[][] a = ((Variable)a1).getMatrixValue();
double[][] b = ((Variable)a2).getMatrixValue();
int ah = a.length;
int aw = a[0].length;
int bh = b.length;
int bw = b[0].length;
double[][] result = {{Double.NaN}};
if(aw==bw && ah==bh){
result = new double[1][1];
result[0][0] = 1;
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(a[i][j] <= b[i][j]){
result[0][0] = 0;
}
}
}
} else if(aw==1 && ah == 1){
result = new double[bh][bw];
for(int i=0; i<bh; i++){
for(int j=0; j<bw; j++){
if(a[0][0] > b[i][j]){
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
} else if(bw==1 && bh == 1){
result = new double[ah][aw];
for(int i=0; i<ah; i++){
for(int j=0; j<aw; j++){
if(a[i][j] > b[0][0]){
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
} else {
System.out.println("Incompatible dimensions in matrix comparison.");
result = new double[0][0];
result[0][0] = Double.NaN;
}
return new Variable("r",result);
}
}
class LogicAndOperator extends Operator{
public LogicAndOperator(){
super();
this.opType = AND;
this.nArgs = 2;
this.argType = A_DOUBLE;
this.isDouble = true;
this.isLogic = true;
// Info
this.arguments = new String[2];
this.arguments[0] = "x";
this.arguments[1] = "y";
this.name = "logic and";
this.description = "Logical comparison. If the two values both equal 1, the expression is given value 1, and 0 otherwise.";
}