-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolveForVar.java
2465 lines (2351 loc) · 167 KB
/
SolveForVar.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Calculus;
import static Calculus.DiffrIntegrSep.MyFuncDiff;
import Calculus.MathsContxtLAv;
import static Calculus.MathsContxtLAv.BackupStateAll;
import static Calculus.MathsContxtLAv.ComplexAssignFlag;
import static Calculus.MathsContxtLAv.ContainsIndConstFlag;
import static Calculus.MathsContxtLAv.ContainsXFlag;
import static Calculus.MathsContxtLAv.GotExpntNumFlag;
import static Calculus.MathsContxtLAv.GotIndConstFlag;
import static Calculus.MathsContxtLAv.GotIndConstPos;
import static Calculus.MathsContxtLAv.GotParenPos;
import static Calculus.MathsContxtLAv.GotVariableFlag;
import static Calculus.MathsContxtLAv.GotVariablePos;
import static Calculus.MathsContxtLAv.IsNotANumber;
import static Calculus.MathsContxtLAv.IsNotAnOperator;
import static Calculus.MathsContxtLAv.IsNumber;
import static Calculus.MathsContxtLAv.IsOperator;
import static Calculus.MathsContxtLAv.IsVariable;
import static Calculus.MathsContxtLAv.LookAheadOperator;
import static Calculus.MathsContxtLAv.MyFuncExpress;
import static Calculus.MathsContxtLAv.MyFuncSimple;
import static Calculus.MathsContxtLAv.ParenthesisFlag;
import static Calculus.MathsContxtLAv.ParenthesisOperator;
import static Calculus.MathsContxtLAv.ParseLog;
import static Calculus.MathsContxtLAv.ParseMap;
import static Calculus.MathsContxtLAv.RestoreStateAll;
import static Calculus.MathsContxtLAv.SavedOperator;
import static Calculus.MathsContxtLAv.Str;
import static Calculus.MathsContxtLAv.StrIsAConstant;
import static Calculus.MathsContxtLAv.TermOutputIndConst;
import static Calculus.MathsContxtLAv.TermOutputSetFlag;
import static Calculus.MathsContxtLAv.chTerm;
import static Calculus.MathsContxtLAv.functions;
import static Calculus.MathsContxtLAv.functionsSimple;
import static Calculus.MathsContxtLAv.log;
import static Calculus.MathsContxtLAv.parseExpr;
import static Calculus.MathsContxtLAv.parseIndConst;
import static Calculus.MathsContxtLAv.parseLA;
import static Calculus.MathsContxtLAv.parseSimple;
import static Calculus.MathsContxtLAv.parseVar;
import static Calculus.MathsContxtLAv.pos;
import static Calculus.MathsContxtLAv.posTerm;
import java.util.HashMap;
import java.util.Map;
import java.util.function.DoubleUnaryOperator;
import org.apache.log4j.Logger;
/**
*
* @author Administrator
*/
public class SolveForVar {
public static Logger log = Logger.getLogger(SolveForVar.class.getName());
//Important Mapping Variables
static Map<String, Double> variables = new HashMap<>();
private static Map<String, String> functionsSimple = new HashMap<>();
private static Map<String, DoubleUnaryOperator> functions = new HashMap<>();
// Simple Expression & Simple Term character Index and Character Variable
public static int posSolve = -1, chSolve;
public static int chNextSolve, chTermSolve, posTermSolve = -1;
public static String StrSolve;
// Algebra & AlgebraTerm character Index and Character Variable
public static int posFunFSolve = -1, chFunFSolve;
public static int posFunGSolve = -1, chFunGSolve;
public static String StrFunFSolve;
public static String StrFunGSolve;
public static String[] StrTermFunFSolve;
public static String[] StrTermFunGSolve;
public static String[] StrUnknownSolve;
public static String[] StrCoeffSolve;
public static int IndexUnknownSolve = 0;
public static String[] FuncFStrUnknownSolve;
public static Double[] FuncFStrCoeffSolve;
public static String[] FuncGStrUnknownSolve;
public static Double[] FuncGStrCoeffSolve;
public static String[] ResultStrUnknownSolve;
public static Double[] ResultStrCoeffSolve;
//IsAConstant Return Type
public static String StrIsAConstant = "";
public static String NumberREGex = "[\\-\\+\\*/]*[0-9\\.]+";
public static String StrVar = "";
public static int posPVar = -1, chPVar;
public static int chNextPVar;
// Unknown Lambda Variables Parser State Variables
private static int posVarUnk = -1, chVarUnk;
private static int posIndConstUnk = -1, chIndConstUnk;
private static String StrVarUnk;
private static String StrIndConstUnk;
// Unknown Lambda Variables Parser Control State Variables
// SimpleTerm Interface Variables
public static boolean ComplexAssignFlag = false;
public static boolean GotVariableFlag = false;
public static int GotVariablePos = 0;
public static boolean GotExpntNumFlag = false;
private static int TermOperatorPos = -1;
private static double TermOutput = 0.0;
public static double TermOutputIndConst = 0.0;
public static boolean GotExtremityFlag = false;
public static boolean GotIndConstFlag = false;
public static int GotIndConstPos = 0;
private static String TempIndConstBase = "";
private static boolean TempIndConstStrSet = false;
private static boolean TrailConstantFlag = false;
private static int TrailConstantOperator = -1;
private static int timesLoop = 0;
public static boolean ContainsXFlag = false;
public static boolean ContainsIndConstFlag = false;
public static boolean TermOutputSetFlag = false;
private static int TermOutputOperator = -1;
private static int LATermOperatorBraces = -1;
private static int LAPreTermOperator = -1;
private static int LATermOperator = -1;
private static int LAVarTermOperator = -1;
private static int LAPreIndConstOperator = -1;
private static int LAIndConstOperator = -1;
private static int LAMAPOperator = -1;
private static int LADyYByDxMAPOperator = -1;
private static boolean LAVarSimpleMapFlag = false;
private static boolean LAVarComplexMapFlag = false;
public static int DiffTermOperator = -1;
public static int LADiffFactorOperator = -1;
public static int LADiffIndConstOperator = -1;
private static String LastInvokedFlag = "";
private static int LastInvokedPos = -1;
public static int LastLADiffIndConstAidPos = -1;
public static int LastLADiffExtremityAidPos = -1;
public static int LastLADiffConstCoeffAidPos = -1;
private static boolean ParseMapposModFlag = false;
private static boolean ZeroTermSignFlag = false;
// Maximum Order of the Term or Expression
private static double MaxTermOutput = 0.0;
private static String FunctionStr = "";
private static boolean ContainsFuncFlag = false;
private static boolean LAConstSimpleMapFlag = false;
private static boolean LAConstComplexMapFlag = false;
//Function Argument Flag
private static boolean GotBracesVarFlag = false;
public static int GotParenPos = -1;
private static int GotBracesCount = -1;
private static int GotBracesCountCurr = -1;
static Double Slope = 0.0;
static Double Constant = 0.0;
static int CoeffVar = -1;
static int EliminatedVar = -1;
static int AnalyticIgnoreNumEqn = 0;
static int NoiseyNumEqn = 0;
static int NoNoiseNumEqn = 0;
static int OverallNoNoiseNumEqn=0;
static int OverallNoiseyNumEqn=0;
static MathsContxtLAv.Expression expValue;
public static enum LogLevel {
All(7), Debug(6), Info(5), Warn(4), Error(3), Fatal(2), Off(1);
private int level = 0;
LogLevel(int level1) {
this.level = level1;
}
}
public static void MyFuncExpressUnk() {
functions.put("sqrt", x -> Math.sqrt(x));
functions.put("sin", x -> Math.sin(Math.toRadians(x)));
functions.put("cos", x -> Math.cos(Math.toRadians(x)));
functions.put("tan", x -> Math.tan(Math.toRadians(x)));
functions.put("round", x -> Math.round(x));
functions.put("abs", x -> Math.abs(x));
functions.put("ceil", x -> Math.ceil(x));
functions.put("floor", x -> Math.floor(x));
functions.put("log", x -> Math.log10(x));
functions.put("ln", x -> Math.log(x));
functions.put("exp", x -> Math.exp(x));
//TODO:More Unary Functions to be added
}
public static void MyFuncSimpleUnk() {
functionsSimple.put("sin", "sin");
functionsSimple.put("cos", "cos");
functionsSimple.put("tan", "tan");
functionsSimple.put("log", "log");
functionsSimple.put("ln", "ln");
functionsSimple.put("exp", "exp");
//TODO:More Simple Unary Functions to be added
}
static boolean IsAConstant(String StrX1, String DiffWithRespTo) {
try {
StrIsAConstant = "Unknown";
String StrX = StrX1;
//log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant);
log.info("IsAConstant:StrX=" + StrX);
log.info("IsAConstant:DiffWithRespTo=" + DiffWithRespTo);
// if ((StrX != null)
// && (!StrX.equalsIgnoreCase(""))
// && (!StrX.contains(DiffWithRespTo))
// && (StrX.matches("[-+]+"))) {
// StrIsAConstant = "Number";
// log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant);
// return true;
// } else
if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (!StrX.contains(DiffWithRespTo))
&& (StrX.matches("[\\-\\+]*[0-9\\.]+"))
//&& (!StrX.matches("[\\-\\+]*[a-zA-Z0-9]+"))
&& (Double.isFinite(Double.valueOf(StrX)))) {
StrIsAConstant = "Number";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return true;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (!StrX.contains(DiffWithRespTo))
&& (StrX.matches("[\\+\\-\\*/]*[a-zA-Z0-9]+"))) {
StrIsAConstant = "IndConstant";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (!StrX.contains(DiffWithRespTo))
&& (StrX.matches("[\\-\\+\\*/]*[0-9\\.]+"))) {
StrIsAConstant = "NumberExpression";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return true;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (!StrX.equalsIgnoreCase(DiffWithRespTo))
&& (!StrX.contains(DiffWithRespTo))
&& (StrX.matches("[\\+\\-\\*/]*[a-zA-Z0-9\\.]+[\\^]*[0-9\\.]+"))) {
StrIsAConstant = "IndConstantExpression";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (StrX.equalsIgnoreCase(DiffWithRespTo))
&& (StrX.matches("[\\+\\-\\*\\^/]*[a-zA-Z0-9]+"))) {
StrIsAConstant = "Var";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& ((StrX.equalsIgnoreCase(DiffWithRespTo))
|| ((StrX.contains(DiffWithRespTo)) && ((!StrX.contains(DiffWithRespTo + "^")))))
&& (StrX.matches("[\\+\\-\\*\\^/]*[a-zA-Z0-9]+"))) {
StrIsAConstant = "Var";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (StrX.contains(DiffWithRespTo))) {
StrIsAConstant = "VarExpression";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
} else if ((StrIsAConstant.equalsIgnoreCase("Unknown")) && (StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (!StrX.contains(DiffWithRespTo))
&& ((StrX.contains("E-")) || (StrX.contains("E+")) || (StrX.contains("e+")) || (StrX.contains("e-")))
&& (Double.isFinite(Double.valueOf(StrX)))) {
StrIsAConstant = "Number";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return true;
} else if (StrIsAConstant.equalsIgnoreCase("Unknown") && (StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (StrX.matches("[\\+\\-]*[a-zA-Z0-9\\.]+"))
&& (StrX.matches(DiffWithRespTo))) {
StrIsAConstant = "VarExpression";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
} else if ((StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& (!StrX.contains(DiffWithRespTo))
&& ((StrX.matches("[\\+\\-]*[0-9\\.\\*\\^/]+[0-9\\.]*"))
|| (StrX.matches("[\\+\\-]*[0-9\\.\\*/]*\\^[0-9\\.\\*\\^/]+")))) {
StrIsAConstant = "NumberExpression";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return true;
} else if ((StrIsAConstant.equalsIgnoreCase("Unknown") && (StrX != null)
&& (!StrX.equalsIgnoreCase(""))
&& ((StrX.matches("[\\+\\-]*[a-zA-Z0-9\\.\\*\\^/]+[\\+\\-]*[0-9\\.]*"))
|| (StrX.matches("[\\+\\-]*[0-9\\.\\*/]*\\^[a-zA-Z0-9\\.\\*\\^/]+"))))) {
StrIsAConstant = "IndConstantExpression";
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
}
} catch (NumberFormatException ConstExp) {
log.info("IsAConstant:Exception:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
}
log.info("IsAConstant:StrIsAConstant=" + StrIsAConstant + " DiffWithRespTo=" + DiffWithRespTo);
return false;
}
public static boolean IsNumber(int Mych) {
return ((((Mych >= '0') && (Mych <= '9')) || (Mych == '.')));
}
public static boolean IsVariable(int Mych) {
return (((Mych >= 'a') && (Mych <= 'z')) || ((Mych >= 'A') && (Mych <= 'Z')));
}
public static boolean IsOperator(int Mych) {
return (((Mych == '+') || (Mych == '-') || (Mych == '*') || (Mych == '/') || (Mych == '^')));
}
public static String eatAll(String Temp, int CharToEat) {
String MyTempString = "";
//if (Temp.equalsIgnoreCase("")) {
// Temp = Str;
//}
for (int i = 0; (i < Temp.length());) {
if (CharToEat == Temp.charAt(i)) {
i++;
} else {
MyTempString = MyTempString + Temp.charAt(i);
i++;
}
}
return MyTempString;
}
//Created by Rajesh Pai
// Solves for unknown variables in 2 equations using Elimination Method
//Copyright (c) 2018 by Rajesh V. Pai
@FunctionalInterface
public interface SolveSimulElim {
Double Eliminate();
}
public static SolveSimulElim parseSolveElim(String FuncF, Double ResultF, String FuncG, Double ResultG) {
return new Object() {
void ExtractUnknownVar(String TermF, String DiffWithRespTo, String[] StrUnknownSolve) {
if ((IsAConstant(TermF, DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("Number"))
|| (StrIsAConstant.equalsIgnoreCase("NumberExpression"))))
; else if ((StrIsAConstant.equalsIgnoreCase("IndConstant"))
|| (StrIsAConstant.equalsIgnoreCase("IndConstantExpression"))) {
String[] Temp = TermF.split("[\\*/]");
for (int i = 0; i < Temp.length; i++) {
log.fatal("SolveElim:parseTermSimple:InsideLoop: are:" + Temp[i]);
if ((IsAConstant(Temp[i], DiffWithRespTo))
&& (StrIsAConstant.equalsIgnoreCase("Number")))
; else {
log.fatal("SolveElim:parseTermSimple:Assigned: are:" + Temp[i]);
StrUnknownSolve[IndexUnknownSolve++] = Temp[i];
}
}
}
}
Double EliminateByAdd(String TermF, int indexF, String TermG, int indexG, String TermToElim, int IndexUnknownSolve) {
System.out.println("SolveElim:EliminateByAdd:IndexUnknownSolve=" + IndexUnknownSolve + " TermF=" + TermF + " indexF=" + indexF);
System.out.println("SolveElim:EliminateByAdd:FuncF=" + FuncF);
System.out.println("SolveElim:EliminateByAdd:FuncG=" + FuncG);
for (int i = 0; i < IndexUnknownSolve; i++) {
ResultStrCoeffSolve[i] = FuncFStrCoeffSolve[i] + FuncGStrCoeffSolve[i];
}
return (ResultF + ResultG);
}
Double EliminateByMinus(String TermF, int indexF, String TermG, int indexG, String TermToElim, int IndexUnknownSolve) {
System.out.println("SolveElim:EliminateByMinus:IndexUnknownSolve=" + IndexUnknownSolve + " TermF=" + TermF + " indexF=" + indexF);
System.out.println("SolveElim:EliminateByMinus:FuncF=" + FuncF);
System.out.println("SolveElim:EliminateByMinus:FuncG=" + FuncG);
for (int i = 0; i < IndexUnknownSolve; i++) {
if (FuncGStrCoeffSolve[i] < FuncFStrCoeffSolve[i]) {
ResultStrCoeffSolve[i] = FuncFStrCoeffSolve[i] - FuncGStrCoeffSolve[i];
} else {
ResultStrCoeffSolve[i] = -1.0 * (FuncGStrCoeffSolve[i] - FuncFStrCoeffSolve[i]);
}
System.out.println("SolveElim:EliminateByMinus:ResultStrCoeffSolve[i]=" + ResultStrCoeffSolve[i] + " FuncFStrCoeffSolve[i]=" + FuncFStrCoeffSolve[i] + " FuncGStrCoeffSolve[i]=" + FuncGStrCoeffSolve[i]);
}
return (ResultF - ResultG);
}
Double EliminateBySubstitution(String TermF, int indexF, String TermG, int indexG, String TermToElim, int IndexUnknownSolve) {
Double[] TempFResultStrCoeffSolve = new Double[IndexUnknownSolve];
Double[] TempGResultStrCoeffSolve = new Double[IndexUnknownSolve];
System.out.println("SolveElim:EliminateBySubstitution:FuncF=" + FuncF);
System.out.println("SolveElim:EliminateBySubstitution:FuncG=" + FuncG);
//Multiply Func Eqn Coefficients by First Coeff of FuncG
Double Multiplicand1 = FuncGStrCoeffSolve[1];
Double Multiplicand2 = FuncFStrCoeffSolve[1];
System.out.println("SolveElim:EliminateBySubstitution:IndexUnknownSolve=" + IndexUnknownSolve + " TermF=" + TermF + " indexF=" + indexF + " Multiplicand1=" + Multiplicand1);
for (int i = 0; i < IndexUnknownSolve; i++) {
TempFResultStrCoeffSolve[i] = FuncFStrCoeffSolve[i] * Multiplicand1;
}
final Double MyTemp = ResultF;
final Double MyTemp1 = Multiplicand1;
Double ResultF1 = MyTemp * MyTemp1;
//Multiply Func Eqn Coefficients by First Coeff of FuncG
System.out.println("SolveElim:EliminateBySubstitution:IndexUnknownSolve=" + IndexUnknownSolve + " TermF=" + TermF + " indexF=" + indexF + " Multiplicand2=" + Multiplicand2);
for (int i = 0; i < IndexUnknownSolve; i++) {
TempGResultStrCoeffSolve[i] = FuncGStrCoeffSolve[i] * Multiplicand2;
}
final Double MyTemp2 = ResultG;
final Double MyTemp3 = Multiplicand2;
Double ResultG1 = MyTemp2 * MyTemp3;
//Subtract the 2 Eqn to eliminate 1st Unknown
for (int i = 0; i < IndexUnknownSolve; i++) {
if (TempGResultStrCoeffSolve[i] < TempFResultStrCoeffSolve[i]) {
ResultStrCoeffSolve[i] = TempFResultStrCoeffSolve[i] - TempGResultStrCoeffSolve[i];
} else {
ResultStrCoeffSolve[i] = -1.0 * (TempGResultStrCoeffSolve[i] - TempFResultStrCoeffSolve[i]);
}
System.out.println("SolveElim:EliminateBySubstitution:ResultStrCoeffSolve[" + i + "]=" + ResultStrCoeffSolve[i] + " FuncFStrCoeffSolve[" + i + "]=" + FuncFStrCoeffSolve[i] + " FuncGStrCoeffSolve[" + i + "]=" + FuncGStrCoeffSolve[i]);
}
//Multiply LHS Result also by Multiplcand
return ((ResultF1 - ResultG1));
}
SolveSimulElim parseSolveElim() {
SolveSimulElim x = (() -> Double.NaN);
double ExprnexpValue = 0;
ResultStrCoeffSolve = new Double[IndexUnknownSolve];
for (int i = 0; i < IndexUnknownSolve; i++) {
ResultStrCoeffSolve[i] = Double.NaN;
}
String OutputExprn = "";
x = parseTermSimple();
log.fatal("SolveElim:parseSimple:x.SimpleExpr()=" + x.Eliminate());
return x;
}
//Grammar:
//Parenthesis = '(' expression ')' | function '(' expression ')'
//expression = term | expression '+' term |expression '-' term
//term = factor | term '*' factor | term '/' factor
//factor = base | base ^ base
//base = '-' base | '+' base | number | identifier |function expression | '(' expression ')'
//
SolveSimulElim parseTermSimple() {
log.fatal("SolveElim:parseTermSimple:Topmost:posSolve=" + posSolve + " chSolve=" + (char) chSolve + " chSolve=" + chSolve);
SolveSimulElim x = (() -> Double.NaN);
String TermF = "";
String TermG = "";
Double Result = Double.NaN;
//Eliminate Variables
for (int i = 0; i < (IndexUnknownSolve); i++) {
if (FuncFStrUnknownSolve[i] == null) {
FuncFStrUnknownSolve[i] = "";
}
if (FuncGStrUnknownSolve[i] == null) {
FuncGStrUnknownSolve[i] = "";
}
TermF = FuncFStrUnknownSolve[i];
TermG = FuncGStrUnknownSolve[i];
log.fatal("SolveElim:parseTermSimple:TermF=" + TermF + " TermG=" + TermG);
log.fatal("SolveElim:parseTermSimple:FuncFStrCoeffSolve[" + i + "]=" + FuncFStrCoeffSolve[i] + " FuncGStrCoeffSolve[" + i + "]=" + FuncGStrCoeffSolve[i]);
if ((TermF.equalsIgnoreCase(TermG)) && (IndexUnknownSolve == 2)
&& (FuncFStrCoeffSolve[i] != null)
&& (FuncGStrCoeffSolve[i] != null)
&& (FuncFStrCoeffSolve[i].doubleValue() != 0)
&& (FuncGStrCoeffSolve[i].doubleValue() != 0)
&& (FuncFStrCoeffSolve[i].doubleValue() == FuncGStrCoeffSolve[i].doubleValue())) {
Result = EliminateByMinus(TermF, i, TermG, i, TermF, IndexUnknownSolve);
} else if ((TermF.equalsIgnoreCase(TermG)) && (IndexUnknownSolve == 2)
&& (FuncFStrCoeffSolve[i] != null)
&& (FuncGStrCoeffSolve[i] != null)
&& (FuncFStrCoeffSolve[i].doubleValue() != 0)
&& (FuncGStrCoeffSolve[i].doubleValue() != 0)
&& (FuncFStrCoeffSolve[i].doubleValue() == -FuncGStrCoeffSolve[i].doubleValue())) {
variables.put(FuncFStrUnknownSolve[i], FuncFStrCoeffSolve[i]);
variables.put(StrUnknownSolve[i + 1], FuncFStrCoeffSolve[i + 1]);
Result = EliminateByAdd(TermF, i, TermG, i, TermF, IndexUnknownSolve);
} else if ((TermF.equalsIgnoreCase(TermG)) && (IndexUnknownSolve == 2)
&& (FuncFStrCoeffSolve[i] != null)
&& (FuncGStrCoeffSolve[i] != null)
&& (FuncFStrCoeffSolve[i].doubleValue() != 0)
&& (FuncGStrCoeffSolve[i].doubleValue() != 0)
&& (FuncFStrCoeffSolve[i].doubleValue() != FuncGStrCoeffSolve[i].doubleValue())) {
Result = EliminateBySubstitution(TermF, i, TermG, i, TermF, IndexUnknownSolve);
i++;
}
}
final Double Temp = Result;
x = (() -> Temp);
return x;
}
}.parseSolveElim();
}
// Created by Rajesh Pai
// Extract unknown variables in 2 equations
// Copyright (c) 2018 by Rajesh V. Pai
@FunctionalInterface
public interface SimulExtractUnk {
String Extract();
}
public static SimulExtractUnk parseExtract(String TermEqnName, String FunctionF, String DiffWithRespTo, boolean NoConstructFlag) {
return new Object() {
SimulExtractUnk ExtractUnknownVar(String TermF, String DiffWithRespTo, String[] StrUnknownSolve) {
int SimulCoeffIndex = -1;
String Result = "";
System.out.println("SimulExtractUnk:ExtractUnknownVar:TermF=" + TermF + " FunctionF=" + FunctionF);
String Mname = TermF;
if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '+')) {
Mname = Mname.replace('+', ' ').trim();
} else if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '-')) {
Mname = Mname.replace('-', ' ').trim();
}
TermF = Mname;
if ((IsAConstant(TermF, DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("Number"))
|| (StrIsAConstant.equalsIgnoreCase("NumberExpression"))))
; else if ((StrIsAConstant.equalsIgnoreCase("IndConstant"))
|| (StrIsAConstant.equalsIgnoreCase("IndConstantExpression"))) {
String[] Temp = TermF.split("[\\+\\-\\*/]");
if ((Temp.length == 1) && (Temp[0].equalsIgnoreCase(TermF)) && ((!IsAConstant(TermF, DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("IndConstant"))
|| (StrIsAConstant.equalsIgnoreCase("IndConstantExpression"))))) {
StrUnknownSolve[IndexUnknownSolve++] = TermF;
System.out.println("SimulExtractUnk:ExtractUnknownVar:IndConstant:Final:Result=" + StrUnknownSolve[IndexUnknownSolve - 1]);
final String TempResult = Result;
return (() -> TempResult);
}
for (int i = 0; (i < Temp.length) && (Temp.length > 1); i++) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:Loop:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
log.fatal("SimulExtractUnk:ExtractUnknownVar:InsideLoop: are:" + Temp[i]);
if ((IsAConstant(Temp[i], DiffWithRespTo))
&& (StrIsAConstant.equalsIgnoreCase("Number"))) {
StrCoeffSolve[IndexUnknownSolve] = Temp[i];
}
if ((!IsAConstant(Temp[i], DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("IndConstant"))
|| (StrIsAConstant.equalsIgnoreCase("IndConstantExpression")))) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:IndConstant:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
System.out.println("SimulExtractUnk:ExtractUnknownVar:Assigned: are:" + Temp[i]);
StrUnknownSolve[IndexUnknownSolve] = Temp[i];
if (StrCoeffSolve[IndexUnknownSolve] == null) {
IndexUnknownSolve++;
}
}
if ((!IsAConstant(Temp[i], DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("Var"))
|| (StrIsAConstant.equalsIgnoreCase("VarExpression")))) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Var:Assigned:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
log.fatal("SimulExtractUnk:ExtractUnknownVar:Assigned: are:" + Temp[i]);
//CODE for writing eqn as col2*m2+c instead of m2*col2+c:Begins
//Supports :col1*m1+col2*m2+m7*col7 OR m1*col1+*m2*col2+m7*col7
if ((SimulCoeffIndex > -1) && ((StrCoeffSolve[SimulCoeffIndex] == null) || (StrCoeffSolve[SimulCoeffIndex].equalsIgnoreCase("")))) {
StrCoeffSolve[SimulCoeffIndex] = Temp[i];
log.fatal("SimulExtractUnk:ExtractUnknownVar:IndConstant:If:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
} else if (IndexUnknownSolve >= 1) {
StrCoeffSolve[IndexUnknownSolve] = Temp[i];
log.fatal("SimulExtractUnk:ExtractUnknownVar:IndConstant:Else If:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
} else {
StrCoeffSolve[i] = Temp[i];
log.fatal("SimulExtractUnk:ExtractUnknownVar:IndConstant:Else:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
}
//CODE for writing eqn as col2*m2+c instead of m2*col2+c :Ends
Result = Result + "@" + Temp[i];
}
}
} else if ((StrIsAConstant.equalsIgnoreCase("Var"))
|| (StrIsAConstant.equalsIgnoreCase("VarExpression"))) {
String[] Temp = TermF.split("[\\+\\-\\*/]");
if ((Temp.length == 1) && (Temp[0].equalsIgnoreCase(TermF)) && ((!IsAConstant(TermF, DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("Var"))
|| (StrIsAConstant.equalsIgnoreCase("VarExpression"))))) {
StrCoeffSolve[IndexUnknownSolve++] = TermF;
Result = Result + "@" + Temp[0];
final String TempResult = Result;
System.out.println("SimulExtractUnk:ExtractUnknownVar:Var:Final:Result=" + Result);
return (() -> TempResult);
}
for (int i = 0; (i < Temp.length) && (Temp.length > 1); i++) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
log.fatal("SimulExtractUnk:ExtractUnknownVar:InsideLoop: are:" + Temp[i]);
System.out.println("SimulExtractUnk:ExtractUnknownVar:Var:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
if ((IsAConstant(Temp[i], DiffWithRespTo))
&& (StrIsAConstant.equalsIgnoreCase("Number"))) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Number:Assigned:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
StrCoeffSolve[IndexUnknownSolve] = Temp[i];
StrUnknownSolve[IndexUnknownSolve++] = "";
}
if ((!IsAConstant(Temp[i], DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("IndConstant"))
|| (StrIsAConstant.equalsIgnoreCase("IndConstantExpression")))) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:IndConstant:Assigned:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
//Store m1 Index
SimulCoeffIndex = IndexUnknownSolve;
log.fatal("SimulExtractUnk:ExtractUnknownVar:Assigned: are:" + Temp[i]);
StrUnknownSolve[IndexUnknownSolve] = Temp[i];
//if((StrCoeffSolve[IndexUnknownSolve] != null)&&(IndexUnknownSolve==0)) {
IndexUnknownSolve++;
//}
}
if ((!IsAConstant(Temp[i], DiffWithRespTo))
&& ((StrIsAConstant.equalsIgnoreCase("Var"))
|| (StrIsAConstant.equalsIgnoreCase("VarExpression")))) {
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Var:Assigned:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo);
log.fatal("SimulExtractUnk:ExtractUnknownVar:Assigned: are:" + Temp[i]);
//CODE for writing eqn as col2*m2+c instead of m2*col2+c:Begins
//Supports :col1*m1+col2*m2+m7*col7 OR m1*col1+*m2*col2+m7*col7
if ((SimulCoeffIndex > -1) && ((StrCoeffSolve[SimulCoeffIndex] == null) || (StrCoeffSolve[SimulCoeffIndex].equalsIgnoreCase("")))) {
StrCoeffSolve[SimulCoeffIndex] = Temp[i];
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:If:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
} else if (IndexUnknownSolve >= 1) {
StrCoeffSolve[IndexUnknownSolve] = Temp[i];
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Else If:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
} else {
StrCoeffSolve[i] = Temp[i];
log.fatal("SimulExtractUnk:ExtractUnknownVar:Var:Else:Temp[" + i + "]=" + Temp[i] + " DiffWithRespTo=" + DiffWithRespTo + " SimulCoeffIndex=" + SimulCoeffIndex + " IndexUnknownSolve=" + IndexUnknownSolve);
}
//CODE for writing eqn as col2*m2+c instead of m2*col2+c :Ends
Result = Result + "@" + Temp[i];
}
}
}
System.out.println("SimulExtractUnk:ExtractUnknownVar:Final:Result=" + Result);
final String TempResult = Result;
return (() -> TempResult);
}
SimulExtractUnk ExtractUnknownVarN(String TermF, String DiffWithRespTo, String[] StrUnknownSolve) {
int SimulCoeffIndex = -1;
String Result = "";
System.out.println("SimulExtractUnk:ExtractUnknownVarN:TermF=" + TermF + " FunctionF=" + FunctionF);
String Mname = TermF;
if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '+')) {
Mname = Mname.replace('+', ' ').trim();
} else if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '-')) {
Mname = Mname.replace('-', ' ').trim();
}
TermF = Mname;
if ((TermF.contains("*")) || (TermF.contains("/"))) {
MathsContxtLAv.ChaosPrevention();
MathsContxtLAv.LookAheadConst MyCoeffConst;
MyCoeffConst = parseLA(TermF, 0, variables, DiffWithRespTo,false);
String StrConst = "" + MyCoeffConst.eval();
log.info("SimulExtractUnk:ExtractUnknownVarN:Number:MyCoeff=" + StrConst);
if ((!StrConst.equalsIgnoreCase("") && (!StrConst.equalsIgnoreCase("1.0")))) {
StrUnknownSolve[IndexUnknownSolve] = StrConst;
}
System.out.println("SimulExtractUnk:ExtractUnknownVarN:Constant:Final:StrConst=" + StrConst);
if ((TermF.contains(DiffWithRespTo))) {
LookAheadVarUnk MyCoeffVar;
MyCoeffVar = parseVarUnk(TermF, variables, DiffWithRespTo);
String nameVar = MyCoeffVar.eval();
log.info("SimulExtractUnk:ExtractUnknownVarN:Variable:nameVar=" + nameVar);
if (!nameVar.equalsIgnoreCase("")) {
StrCoeffSolve[IndexUnknownSolve] = nameVar;
}
System.out.println("SimulExtractUnk:ExtractUnknownVarN:Var:Final:nameVar=" + nameVar);
}
if (StrConst.equalsIgnoreCase("") || (StrConst.equalsIgnoreCase("1.0"))) {
LookAheadIndConstUnk MyCoeffIndConst;
MyCoeffIndConst = parseIndConstUnk(TermF, DiffWithRespTo);
String nameIndConst = MyCoeffIndConst.eval();
log.fatal("SimulExtractUnk:ExtractUnknownVar:Assigned: are:nameIndConst=" + nameIndConst);
StrUnknownSolve[IndexUnknownSolve] = nameIndConst;
IndexUnknownSolve++;
System.out.println("SimulExtractUnk:ExtractUnknownVarN:IndConst:Final:nameIndConst=" + nameIndConst);
}
} else {
SimulExtractUnk b = ExtractUnknownVar(TermF, DiffWithRespTo, StrUnknownSolve);
return b;
}
System.out.println("SimulExtractUnk:ExtractUnknownVar:Final:Result=" + Result);
final String TempResult = Result;
return (() -> TempResult);
}
SimulExtractUnk parseExtract() {
StrSolve = FunctionF;
StrFunFSolve = StrSolve;
Str = StrSolve;
posFunFSolve = 0;
posSolve = 0;
pos = 0;
IndexUnknownSolve = 0;
SimulExtractUnk x = (() -> "");
if ((Str == null) || (Str.equalsIgnoreCase("")) || (StrSolve == null) || (StrSolve.equalsIgnoreCase(""))) {
return x;
}
String OutputExprn = "";
int startpos = posSolve;
x = parseTermSimple();
log.fatal("SimulExtractUnk:parseExtract:x.SimpleExpr()=" + x.Extract());
log.fatal("SimulExtractUnk:parseExtract:IndexUnknownSolve=" + IndexUnknownSolve);
//CopyVarNCoeff(TermEqnName);
log.info("SimulExtractUnk:parseExtract:x.Extract()=" + x.Extract());
// if ((pos != -1)&&(str != null)&&(!str.equalsIgnoreCase(""))&&(pos <= str.length())) {
// throw new RuntimeException("Simple:parse:Unexpected: " + (char) ch + " int=" + (int) ch + " pos=" + pos + " str=" + str.substring(pos, str.length()));
// }
return x;
}
//Grammar:
//Parenthesis = '(' expression ')' | function '(' expression ')'
//expression = term | expression '+' term |expression '-' term
//term = factor | term '*' factor | term '/' factor
//factor = base | base ^ base
//base = '-' base | '+' base | number | identifier |function expression | '(' expression ')'
//
SimulExtractUnk parseTermSimple() {
log.fatal("SimulExtractUnk:parseTermSimple:Topmost:posSolve=" + posSolve + " chSolve=" + (char) chSolve + " chSolve=" + chSolve);
SimulExtractUnk x = (() -> "");
SimulExtractUnk a = (() -> "");
SimulExtractUnk b = (() -> "");
int c = 0;
int d = 0;
Calculus.MathsContxtLAv.SimpleTerm u = (() -> "");
Calculus.MathsContxtLAv.SimpleTerm v = (() -> "");
boolean Flag = false;
String TermF = "";
String TermG = "";
// Split StrFunFSolve into its Terms
StrTermFunFSolve = new String[StrFunFSolve.length()];
StrUnknownSolve = new String[StrFunFSolve.length()];
StrCoeffSolve = new String[StrFunFSolve.length()];
for (c = 0;;) {
a = x;
System.out.println("SimulExtractUnk:parseTermSimple:StrFunFSolve.substring=" + StrFunFSolve.substring(posFunFSolve));
v = Calculus.MathsContxtLAv.parseSimpleTerm(StrFunFSolve.substring(posFunFSolve), variables, DiffWithRespTo);
TermF = v.SimpleTerm();
System.out.println("SimulExtractUnk:parseTermSimple:TermF=" + TermF);
if ((TermF.equalsIgnoreCase("")) && (c == 0)) {
String Mname = StrSolve;
if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '+')) {
Mname = Mname.replace('+', ' ').trim();
} else if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '-')) {
Mname = Mname.replace('-', ' ').trim();
}
StrTermFunFSolve[c] = Mname;
TermF = Mname;
} else {
String Mname = TermF;
if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '+')) {
Mname = Mname.replace('+', ' ').trim();
} else if ((NoConstructFlag == true) && (!Mname.equalsIgnoreCase("")) && (Mname.charAt(0) == '-')) {
Mname = Mname.replace('-', ' ').trim();
}
StrTermFunFSolve[c++] = Mname;
TermF = Mname;
}
System.out.println("SimulExtractUnk:parseTermSimple:TermF are:" + TermF);
final String aTemp = a.Extract();
b = ExtractUnknownVarN(TermF, DiffWithRespTo, StrUnknownSolve);
final String bTemp = b.Extract();
x = (() -> bTemp);
log.fatal("SimulExtractUnk:parseTermSimple:Primary Loop parseTermSimple:posSolve=" + posSolve + " chSolve=" + (char) chSolve);
log.fatal("SimulExtractUnk:parseTermSimple:Primary Loop parseTermSimple:TermF=" + TermF + " c=" + c + " posFunFSolve=" + posFunFSolve + " chFunFSolve=" + (char) chFunFSolve);
posSolve = pos;
posFunFSolve = posSolve;
if (posFunFSolve == -1) {
break;
}
}
for (int i = 0; i < IndexUnknownSolve; i++) {
if (StrUnknownSolve[i] == null) {
IndexUnknownSolve--;
}
//log.fatal("ExtractUnk:parseTermSimple:Unknown Vars are:i=" + i + ":" + StrUnknownSolve[i] + " IndexUnknownSolve=" + IndexUnknownSolve);
System.out.println("SimulExtractUnk:parseTermSimple:Unknown Vars are:i=" + i + ":" + StrUnknownSolve[i] + " StrCoeffSolve[i]=" + StrCoeffSolve[i] + " IndexUnknownSolve=" + IndexUnknownSolve);
}
log.fatal("SimulExtractUnk:parseTermSimple:Result: x.Extract()=" + x.Extract());
return x;
}
}.parseExtract();
}
//Created by Rajesh Pai
// Solves for unknown variables in 2 equations using Elimination Method
//Copyright (c) 2018 by Rajesh V. Pai
@FunctionalInterface
public interface SolveSimulGauss {
String Eliminate();
}
public static SolveSimulGauss parseSolveSimul(int row, int row1,
int NumOfEqn, int NumOfUnknown, int eqn, int kspace,
String ColumnVariables, String BaseDiffWithRespTo,
boolean SolveFlag, boolean SolveNoConstantFlag,
String FuncF, Double ResultF, String FuncG, Double ResultG) {
return new Object() {
SolveSimulGauss parseSolveSimulGauss() {
SolveSimulGauss x = (() -> "");
double ExprnexpValue = 0;
ResultStrCoeffSolve = new Double[IndexUnknownSolve];
for (int i = 0; i < IndexUnknownSolve; i++) {
ResultStrCoeffSolve[i] = Double.NaN;
}
String OutputExprn = "";
x = parseTermSimple();
log.fatal("SolveElim:parseSimple:x.SimpleExpr()=" + x.Eliminate());
return x;
}
SolveSimulGauss parseTermSimple() {
log.fatal("SolveElim:parseTermSimple:Topmost:posSolve=" + posSolve + " chSolve=" + (char) chSolve + " chSolve=" + chSolve);
SolveSimulGauss x = (() -> "");
String TermF = "";
String TermG = "";
Double Result = Double.NaN;
Double Slope = 0.0;
Double Constant = 0.0;
int CoeffVar = -1;
int EliminatedVar = -1;
SolveSimulElim F = (() -> 0.0);
log.fatal("SolveSimulGauss:SolveSimulEqn:Solving Equations:Random Row Selected 1:row=" + row);
log.fatal("SolveSimulGauss:SolveSimulEqn:Solving Equations:Random Row Selected 2:row1=" + row1);
System.out.println("DOEOFATAnalyzer:SolveSimulEqn:Solving Equations:ColumnVariables=" + ColumnVariables);
if (SolveFlag == true) {
//log.fatal("DOEOFATAnalyzer:SolveSimulEqn:Solving Equations: Level row=" + Level1 + " Value=" + LvlValue1);
//log.fatal("DOEOFATAnalyzer:SolveSimulEqn:Solving Equations: Leve2 row1=" + Level2 + " Value=" + LvlValue2);
log.fatal("SolveSimulGauss:SolveSimulEqn:Solving Equations:FuncF=" + FuncF);
log.fatal("SolveSimulGauss:SolveSimulEqn:Solving Equations:FuncG=" + FuncG);
log.fatal("SolveSimulGauss:SolveSimulEqn:ResultsArrayGet(0, row, kspace)=" + ResultF);
log.fatal("SolveSimulGauss:SolveSimulEqn:ResultsArrayGet(0, row1, kspace)=" + ResultG);
if (FuncF.equalsIgnoreCase(FuncG)) {
System.out.println("DOEOFATAnalyzer:SolveSimulEqn:Solving Equations:Both Equations are the same. Coefficients must be different.");
System.out.println("DOEOFATAnalyzer:SolveSimulEqn:Solving Equations:Choose Different Rows where Levels of column are different");
AnalyticIgnoreNumEqn++;
} else {
//for (row = 0; row < NumOfEqn; row += 2) {
F = parseSolveElim(FuncF, ResultF, FuncG, ResultG);
System.out.println("DOEOFATAnalyzer:SolveEquations:F.Eliminate()=" + F.Eliminate());
int OneAssignOnlyFlag = 0;
for (int i = 0; i < NumOfUnknown; i++) {
if (ResultStrCoeffSolve[i] == 0) {
OneAssignOnlyFlag++;
EliminatedVar = i;
log.fatal("SolveSimulGauss:SolveSimulEqn:0.0:Equation=" + ResultStrCoeffSolve[i] + " Var=" + StrUnknownSolve[i] + " EliminatedVar=" + EliminatedVar);
if (OneAssignOnlyFlag == 2) {
System.out.println("SolveSimulGauss:SolveSimulEqn:CANNOT SOLVE:Both Variables are eliminated");
EliminatedVar = -1;
CoeffVar = -1;
AnalyticIgnoreNumEqn++;
break;
}
} else {
CoeffVar = i;
log.fatal("SolveSimulGauss:SolveSimulEqn:NOT 0.0:Equation=" + ResultStrCoeffSolve[i] + " Var=" + StrUnknownSolve[i] + " CoeffVar=" + CoeffVar + " i=" + i);
}
}
log.fatal("SolveSimulGauss:SolveSimulEqn:FuncF=" + FuncF + " ResultsArrayGet(0, row, kspace)=" + ResultF);
log.fatal("SolveSimulGauss:SolveSimulEqn:FuncG=" + FuncG + " ResultsArrayGet(0, row1, kspace)=" + ResultG);
// Determine the Value of the CoeffVar Variable (as other one has been eliminated)
if (CoeffVar != -1) {
log.fatal("SolveSimulGauss:SolveSimulEqn:OneAssignOnlyFlag:Result=" + F.Eliminate() + " SlopeName=" + FuncFStrUnknownSolve[CoeffVar] + " ResultStrCoeffSolve[CoeffVar]=" + ResultStrCoeffSolve[CoeffVar]);
Slope = F.Eliminate() / ResultStrCoeffSolve[CoeffVar];
log.fatal("SolveSimulGauss:SolveSimulEqn:OneAssignOnlyFlag:Var=" + FuncFStrUnknownSolve[CoeffVar] + "=" + F.Eliminate() / ResultStrCoeffSolve[CoeffVar]);
}
// Determine the Value of the EliminatedVar(which had been eliminated)
if ((SolveNoConstantFlag == false) && (EliminatedVar != -1) && (CoeffVar != -1)) {
log.fatal("SolveSimulGauss:SolveSimulEqn:!SolveNoConstantFlag:EliminatedVar:Result=" + ResultF + " cName=" + StrUnknownSolve[EliminatedVar] + " Coeff=" + FuncFStrCoeffSolve[EliminatedVar]);
Constant = (ResultF - (Slope * FuncFStrCoeffSolve[CoeffVar]));
log.fatal("SolveSimulGauss:SolveSimulEqn:!SolveNoConstantFlag:EliminatedVar:" + StrUnknownSolve[EliminatedVar] + "=" + Constant);
} else if ((SolveNoConstantFlag == true) && (EliminatedVar != -1) && (CoeffVar != -1)) {
log.fatal("SolveSimulGauss:SolveSimulEqn:SolveNoConstantFlag:EliminatedVar:Result=" + ResultF + " Slope=" + Slope + " Slope Coeff=" + FuncFStrCoeffSolve[CoeffVar] + " CName=" + StrUnknownSolve[EliminatedVar] + " C Coeff=" + FuncFStrCoeffSolve[EliminatedVar]);
Constant = ((ResultF - Slope * FuncFStrCoeffSolve[CoeffVar]) / FuncFStrCoeffSolve[EliminatedVar]);
log.fatal("SolveSimulGauss:SolveSimulEqn:SolveNoConstantFlag:EliminatedVar:" + StrUnknownSolve[EliminatedVar] + "=" + Constant);
}
//}
}
}
if ((CoeffVar >= 0) && (EliminatedVar >= 0)) {
final String Temp = "ValidResult"
+ "@" + FuncFStrUnknownSolve[CoeffVar] + ":" + Slope
+ "@" + StrUnknownSolve[EliminatedVar] + ":" + Constant
+ "@" + "CoeffVar" + ":" + CoeffVar
+ "@" + "EliminatedVar" + ":" + EliminatedVar;
x = (() -> Temp);
return x;
} else {
final String Temp = "InvalidResult"
+ "@" + StrUnknownSolve[0] + ":" + Slope
+ "@" + StrUnknownSolve[1] + ":" + Constant
+ "@" + "CoeffVar" + ":" + CoeffVar
+ "@" + "EliminatedVar" + ":" + EliminatedVar;
x = (() -> Temp);
return x;
}
}
}.parseSolveSimulGauss();
}
//Created by Rajesh Pai
// Solves for unknown variables in 2 equations using Elimination Method
//Copyright (c) 2018 by Rajesh V. Pai
@FunctionalInterface
public interface VariableCol {
int Index();
}
public static VariableCol parseVariableCol(String Exprn, String BaseDiffWithRespTo, String Silent, LogLevel MyLogLevel) {
return new Object() {
void nextCharVar() {
++posPVar;
chPVar = (posPVar < StrVar.length()) ? StrVar.charAt(posPVar) : -1;
if ((chPVar == -1) && (posPVar >= StrVar.length())) {
posPVar = StrVar.length();
} else if ((chPVar == -1) || ((chPVar == 0))) {
throw new RuntimeException("nextChar:nextChar:Unexpected: " + (char) chPVar + " int=" + (int) chPVar + " Cannot Format:" + StrVar.substring(posPVar, StrVar.length()));
}
}
boolean eat(int CharToEat) {
while (chPVar == ' ') {
nextCharVar();
}
if (chPVar == CharToEat) {
nextCharVar();
return true;
}
return false;
}
VariableCol parseVariableCol() {
VariableCol x = (() -> -1);
chPVar = -1;
posPVar = 0;
StrVar = Exprn;
if ((StrVar == null) || (StrVar.equalsIgnoreCase(""))) {
return x;
}
chPVar = StrVar.charAt(posPVar);
x = parseExpression();
System.out.println("VariableCol:parseSimple:Exprn=" + Exprn + " x.Index()=" + x.Index());
log.fatal("VariableCol:parseSimple:x.SimpleExpr()=" + x.Index());
return x;
}
VariableCol parseExpression() {
log.fatal("VariableCol:parseTermSimple:Topmost:posPVar=" + posPVar + " chPVar=" + (char) chPVar + " chPVar=" + chPVar);
VariableCol x = (() -> -1);
VariableCol a = (() -> -1);
for (;;) {
if ((chPVar != 0) && (chPVar != -1)) {
a = parseBase();
if (a.Index() >= 0) {
final int Result = a.Index();
x = () -> Result;
}
} else {
log.info("VariableCol:parseTerm:Return Value:y.Index()=" + x.Index());
return x;
}
}
}
VariableCol parseBase() {
int startPos = posPVar;
VariableCol x = (() -> -1);
if (posPVar >= StrVar.length()) {
posPVar = -1;
chPVar = -1;
return x;
}
if ((eat('-')) && (startPos != 0)) { // unary minus
VariableCol b = parseBase();
x = (() -> b.Index());
if (MyLogLevel.level >= LogLevel.Info.level) {
log.info("VariableCol:parseBase:-:Return Value:x.Index=" + x.Index() + " startPos=" + startPos);
}
return x;
} else if (eat('+')) { // unary plus
x = parseBase();
if (MyLogLevel.level >= LogLevel.Info.level) {
log.info("VariableCol:parseBase:+:Return Value:x.Index=" + x.Index());