-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFunction.java
More file actions
1132 lines (968 loc) · 39.5 KB
/
Function.java
File metadata and controls
1132 lines (968 loc) · 39.5 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package parser;
import interfaces.Savable;
import parser.methods.Method;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import math.matrix.expressParser.Matrix;
import util.FunctionManager;
import util.Serializer;
import util.VariableManager;
/**
*
* @author JIBOYE OLUWAGBEMIRO OLAOLUWA
*/
public class Function implements Savable {
/**
* The dependent variable
*/
private Variable dependentVariable;
/**
* The independent variables.
*/
private ArrayList<Variable> independentVariables = new ArrayList<>();
/**
* The type of the function
*/
private int type = ALGEBRAIC;
public static final int ALGEBRAIC = 1;
public static final int MATRIX = 2;
public static final int LIST = 3;
/**
* If the object is an algebraic expression, its details are stored here.
* The math expression on the RHS.
*/
private MathExpression mathExpression;
/**
* If the object is a {@link Matrix} its data is stored here.
*/
private Matrix matrix;
/**
*
* @param matrix A Matrix to be used to initialize the function..
*/
public Function(Matrix matrix) {
this.matrix = matrix;
this.type = MATRIX;
FunctionManager.add(this);
}
/**
*
* @param input The user input into the system, usually of the form:
* F(x,y,z,w,....)=mathexpr; or F= @(x,y,z,w,...)mathexpr ...where mathexpr
* is an algebraic expression in terms of x,y,z,w,...
*
*/
public Function(String input) throws InputMismatchException {
try {
input = STRING.purifier(input);
int openIndex = input.indexOf("(");
int equalsIndex = input.indexOf("=");
int atIndex = input.indexOf("@");
if (equalsIndex == -1) {
boolean anonymous = input.startsWith("@");
if (anonymous) {
parseInput(input);
return;
}
throw new InputMismatchException("Bad function syntax!");
}
/**
* F=@(x,y,z,w,...)mathexpr OR F(x,y,z,w,...)=mathexpr
*/
String tokenAfterEquals = input.substring(equalsIndex + 1, equalsIndex + 2);
if (atIndex != -1 && atIndex < openIndex) {
//The enclosing if assumes that the user is creating a function using the anonymous function assignment format.
if (atIndex != openIndex - 1) {
throw new InputMismatchException("Error in function format... anonymous function assignment format must have the `@` preceding the `(`");
//error...token between at symbol and param list
} else if (!tokenAfterEquals.equals("@")) {
//Avoid this nonsense: f=kdkdk@(x,...)expr
throw new InputMismatchException("Error in function format... anonymous function assignment format must have the `=` preceding the `@`");
//cool... function created with anonymous function assignment
}
}
if (openIndex == -1 || equalsIndex == -1) {
throw new InputMismatchException("Bad function format!");
}
int close = Bracket.getComplementIndex(true, openIndex, input);
String name = null;
/**
* If function is in this format: //f(...) = expr Force it to be in
* this format: //f=@(...)expr
*/
if (openIndex < equalsIndex) {
name = input.substring(0, openIndex);
input = name + "=@" + input.substring(openIndex, close + 1) + input.substring(equalsIndex + 1);
}
openIndex = input.indexOf("(");
equalsIndex = input.indexOf("=");
close = Bracket.getComplementIndex(true, openIndex, input);
if (name == null) {
name = input.substring(0, equalsIndex);
}
if (!Variable.isVariableString(name)) {
throw new InputMismatchException("Bad name for Function.");
}
String paramsList = input.substring(openIndex + 1, close);
List<String> params = new CustomScanner(paramsList, false, ",").scan();
int size = params.size();
boolean notAlgebraic = true;
/**
* This loop should check if all arguments in the params list are
* numbers... This is necessary for the input to be a Matrix
* function or a List
*/
for (String p : params) {
try {
Double.parseDouble(p);
} catch (Exception e) {
notAlgebraic = false;//algebraic argument found....exit
break;
}
}//end for loop
if (notAlgebraic) {
if (size == 1) {
int listSize = Integer.parseInt(params.get(0));
type = LIST;
} else if (size == 2) {
//A matrix definition...A(2,3)=(3,2,4,5,3,1)------A=@(3,3)(3,4,32,3,4,4,3,3,4)
int rows = Integer.parseInt(params.get(0));
int cols = Integer.parseInt(params.get(1));
int indexOfLastCloseBrac = input.lastIndexOf(")");
int compIndexOfLastCloseBrac = Bracket.getComplementIndex(false, indexOfLastCloseBrac, input);
String list = input.substring(compIndexOfLastCloseBrac, indexOfLastCloseBrac + 1);
if (!list.startsWith("(") || !list.endsWith(")")) {
throw new InputMismatchException("Invalid Matrix Format...Circular Parentheses missing");
}
list = list.substring(1, list.length() - 1);
List<String> matrixData = new CustomScanner(list, false, ",").scan();
if (rows * cols == matrixData.size()) {
matrixData.add(0, cols + "");
matrixData.add(0, rows + "");
//Validate the entries
for (int i = 0; i < matrixData.size(); i++) {
try {
Double.parseDouble(matrixData.get(i));
} catch (Exception e) {
throw new InputMismatchException("Invalid Matrix Data..." + matrixData.get(i) + " found.");
}
}
this.matrix = listToMatrix(matrixData);
type = MATRIX;
this.matrix.setName(name);
} else {
throw new InputMismatchException("Invalid number of entries found in Matrix Data---for input: " + input);
}
}//end else if params-list size == 2
}//if not an algebraic function
else {//input is algebraic like this..f(a,b..)=expr
parseInput(input);
}
} catch (Exception e) {
e.printStackTrace();
throw new InputMismatchException("Bad Function Syntax--" + input);
}
}//end constructor
public void setType(int type) {
this.type = type;
}
public int getType() {
return type;
}
/**
*
* @param x A list of variable values to set for the function. The supplied
* value list is applied to the function's parameter list in the order they
* were supplied in the original question.
* @return the value of the function with these variables set.
*/
public double calc(double... x) {
int i = 0;
if (x.length == independentVariables.size()) {
for (Variable var : independentVariables) {
mathExpression.setValue(var.getName(), String.valueOf(x[i++]));
}
return Double.parseDouble(mathExpression.solve());
}
return Double.NaN;
}
public static boolean assignObject(String input, Class mathExpClass) {
/**
* Check if it is a function assignment operation...e.g:
* f=matrix_mul(A,B)
*
*/
input = STRING.purifier(input);
int equalsIndex = input.indexOf("=");
int semiColonIndex = input.indexOf(";");
int indexOfOpenBrac = input.indexOf("(");
if (equalsIndex == -1 && semiColonIndex == -1 && indexOfOpenBrac == -1) {
throw new InputMismatchException("Wrong Input!");
}
/**
* Check if the user used the form f(x)=.... instead of f=@(x).... If so
* convert to the latter format, and then recompute the necessary
* indexes.
*/
if (indexOfOpenBrac != -1 && indexOfOpenBrac < equalsIndex) {
input = input.substring(0, indexOfOpenBrac)
+ "=@" + input.substring(indexOfOpenBrac, Bracket.getComplementIndex(true, indexOfOpenBrac, input) + 1) + input.substring(equalsIndex + 1);
//recompute the indexes.
equalsIndex = input.indexOf("=");
semiColonIndex = input.indexOf(";");
}
boolean success = false;
if (equalsIndex != -1 && semiColonIndex != -1) {
String newFuncName = input.substring(0, equalsIndex);
boolean isVarNamesList = isParameterList("(" + newFuncName + ")");
boolean hasCommas = newFuncName.contains(",");
String rhs = input.substring(equalsIndex + 1, semiColonIndex);
if (Number.validNumber(rhs)) {
if (Variable.isVariableString(newFuncName)) {
VariableManager.VARIABLES.put(newFuncName, new Variable(newFuncName, rhs, false));
} else if (isVarNamesList) {
List<String> vars = new CustomScanner(newFuncName, false, ",").scan();
for (String var : vars) {
VariableManager.VARIABLES.put(var, new Variable(var, rhs, false));
}
}
success = true;
} else {
MathExpression expr = mathExpClass == BigMathExpression.class ? new BigMathExpression(rhs) : new MathExpression(rhs);
String val = expr.solve();
String referenceName = expr.getReturnObjectName();
//System.out.println("rhs: "+rhs+", mathExpClass: "+mathExpClass+", expr.class: "+expr.getClass()+", val: "+val+", type: "+expr.getReturnType());
if (Variable.isVariableString(newFuncName) || isVarNamesList) {
Function f;
switch (expr.getReturnType()) {
case MATRIX:
if (isVarNamesList && hasCommas) {
throw new InputMismatchException("Initialize a function at a time!");
}
f = FunctionManager.lookUp(referenceName);
FunctionManager.FUNCTIONS.put(newFuncName, new Function(newFuncName + "=" + f.expressionForm()));
success = true;
break;
case ALGEBRAIC_EXPRESSION:
if (isVarNamesList && hasCommas) {
throw new InputMismatchException("Initialize a function at a time!");
}
f = FunctionManager.lookUp(referenceName);
FunctionManager.FUNCTIONS.put(newFuncName, new Function(newFuncName + "=" + f.expressionForm()));
success = true;
break;
case LIST:
if (isVarNamesList && hasCommas) {
throw new InputMismatchException("Initialize a function at a time!");
}
f = FunctionManager.lookUp(referenceName);
FunctionManager.FUNCTIONS.put(newFuncName, new Function(newFuncName + "=" + f.expressionForm()));
success = true;
break;
case STRING://for now, this only confirms that a comma separated list has been returned
break;
case NUMBER:
if (isVarNamesList && hasCommas) {
List<String> vars = new CustomScanner(newFuncName, false, ",").scan();
for (String var : vars) {
VariableManager.VARIABLES.put(var, new Variable(var, val, false));
}
success = true;
} else {
VariableManager.VARIABLES.put(newFuncName, new Variable(newFuncName, val, false));
success = true;
}
break;
case VOID:
break;
default:
break;
}//end switch statement
}//end if
else {
throw new InputMismatchException("Syntax Error---" + newFuncName);
}
}//end else
}
if (success) {
FunctionManager.update();
VariableManager.update();
}
return success;
}
/**
*
* @param input The user input into the system, usually of the form:
* F=@(x,y,z,w)mathexpr;..where mathexpr is an algebraic expression in terms
* of x,y,z,...
*
*/
private void parseInput(String input) {
input = input.trim();
if (input.contains("@")) {
boolean anonymous = input.startsWith("@");
if (anonymous) {
input = "anon" + (FunctionManager.countAnonymousFunctions() + 1) + "=".concat(input);
}
String[] cutUpInput = new String[3];
cutUpInput[0] = input.substring(0, input.indexOf("=")).trim();//---function-name
cutUpInput[1] = input.substring(input.indexOf("@") + 1, input.indexOf(")") + 1).trim();//---(x,y,....)..params list
cutUpInput[2] = input.substring(input.indexOf(")") + 1);//--the expression
CustomScanner cs = new CustomScanner(cutUpInput[1], false, ",", "(", ")");
List<String> scan = cs.scan();
if (Variable.isVariableString(cutUpInput[0]) && isParameterList(cutUpInput[1])) {
if (cutUpInput[0].startsWith("anon") && !anonymous) {
throw new InputMismatchException("Function Name Cannot Start With \'anon\'.\n \'anon\' is a reserved name for anonymous functions.");
} else if (Method.isInBuiltMethod(cutUpInput[0])) {
throw new InputMismatchException(cutUpInput[0] + " is a reserved name for inbuilt methods.");
} else {
setDependentVariable(new Variable(cutUpInput[0]));
}
String vars = "";
for (int i = 0; i < scan.size(); i++) {
try {
if (Variable.isVariableString(scan.get(i))) {
independentVariables.add(new Variable(scan.get(i), "0.0", false));
vars = vars.concat(scan.get(i) + "=" + "0.0;");//build variable command list
}//end if
}//end try
catch (IndexOutOfBoundsException boundsException) {
break;
}//end catch
}//end for
while (cutUpInput[2].startsWith("(") && cutUpInput[2].endsWith(")") && Bracket.getComplementIndex(true, 0, cutUpInput[2]) == cutUpInput[2].length() - 1) {
cutUpInput[2] = cutUpInput[2].substring(1, cutUpInput[2].length() - 1).trim();
}
setMathExpression(new MathExpression(vars.concat(cutUpInput[2].trim())));
if (!mathExpression.isCorrectFunction()) {
throw new InputMismatchException("SYNTAX ERROR IN FUNCTION");
}
}//end if
else {
if (isDimensionsList(cutUpInput[1])) {
Function f = new Function(input);
this.matrix = f.matrix;
this.type = f.type;
return;
}
throw new InputMismatchException("Syntax Error: Format Is: F=@(x,y,z,...)mathexpr");
}//end else
}//end if
else {
throw new InputMismatchException("Syntax Error: Format Is: F=@(x,y,z,...)mathexpr");
}//end else
}//end method
public void setDependentVariable(Variable dependentVariable) {
this.dependentVariable = dependentVariable;
}
public Variable getDependentVariable() {
return dependentVariable;
}
public void setMathExpression(MathExpression mathExpression) {
this.mathExpression = mathExpression;
}
public MathExpression getMathExpression() {
return mathExpression;
}
public void setIndependentVariables(ArrayList<Variable> independentVariables) {
this.independentVariables = independentVariables;
}
public ArrayList<Variable> getIndependentVariables() {
return independentVariables;
}
/**
*
* @return the number of independent variables possessed by this Function
* object.
*/
public int numberOfParameters() {
if (type == LIST) {
return 1;
}
if (type == MATRIX) {
return 2;
}
return independentVariables.size();
}
/**
*
* @param list A string containing info. about the arguments to be passed to
* the Function. The format is (num1,num2,num3,....) The information should
* be numbers only
* @return true if its format is valid.
*/
private static boolean isDimensionsList(String list) {
list = STRING.purifier(list);
CustomScanner cs = new CustomScanner(list, true, "(", ",", ")");
List<String> scan = cs.scan();
if (scan.get(0).equals("(") && scan.get(scan.size() - 1).equals(")")) {
scan.remove(0);
scan.remove(scan.size() - 1);
} else {
return false;
}
int sz = scan.size();
for (int i = 0; i < sz; i++) {
try {
if (!Number.validNumber(scan.get(i)) && !Operator.isComma(scan.get(i))) {
return false;
}//end if
if ((i == 0 || i == scan.size() - 1)) {
if (!Number.validNumber(scan.get(i))) {
return false;
}
}//end if
else {
if (Number.validNumber(scan.get(i)) && !Operator.isComma(scan.get(i + 1))) {
return false;
}//end if
if (Operator.isComma(scan.get(i)) && !Number.validNumber(scan.get(i + 1))) {
return false;
}//end if
}
}//end try
catch (IndexOutOfBoundsException ioobe) {
}//end catch
}//end for
return true;
}//end method
/**
*
* @param list A string containing info. about the arguments to be passed to
* the Function. The format is (var1,var2,var3,....)
* @return true if its format is valid.
*/
private static boolean isParameterList(String list) {
list = STRING.purifier(list);
CustomScanner cs = new CustomScanner(list, true, "(", ",", ")");
List<String> scan = cs.scan();
if (scan.get(0).equals("(") && scan.get(scan.size() - 1).equals(")")) {
scan.remove(0);
scan.remove(scan.size() - 1);
} else {
return false;
}
int sz = scan.size();
if (sz == 0) {
return false;
} else if (sz == 1) {
if (Variable.isVariableString(scan.get(0))) {
return true;
}
}
for (int i = 0; i < sz; i++) {
try {
//Only allowd tokens are variables and commas
if (!Variable.isVariableString(scan.get(i)) && !Operator.isComma(scan.get(i))) {
return false;
}//end if
//The first and the last tokens must be variables
if (i == 0 || i == sz - 1) {
if (!Variable.isVariableString(scan.get(i))) {
return false;
}
}//end if
else {
if (Variable.isVariableString(scan.get(i)) && !Operator.isComma(scan.get(i + 1))) {
return false;
}//end if
if (Operator.isComma(scan.get(i)) && !Variable.isVariableString(scan.get(i + 1))) {
return false;
}//end if
}
}//end try
catch (IndexOutOfBoundsException ioobe) {
}//end catch
}//end for
return true;
}//end method
public Matrix getMatrix() {
return matrix;
}
/**
*
* @param name The name of the variable.
* @return the Variable if it exists in the parameter list of this Function.
* Returns null if no Variable having that name is to be found in the
* parameter list of this Function.
*
*/
public Variable getIndependentVariable(String name) {
if (type != ALGEBRAIC) {
return null;
}
int sz = independentVariables.size();
for (int i = 0; i < sz; i++) {
if (independentVariables.get(i).getName().equalsIgnoreCase(name)) {
return independentVariables.get(i);
}//end if
}//end for loop
return null;
}//end method
/**
*
* @param var The name of the Variable to check.
* @return true if this object has an independent variable that goes by the
* given name.
*/
public boolean hasIndependentVariable(String var) {
if (type != ALGEBRAIC) {
return false;
}
Variable v = new Variable(var);
return independentVariables.contains(v);
}//end method
/**
*
* @param paramList A string containing info. about the arguments to be
* passed to the Function. The format is (var1,var2,var3,....)
* @return an array containing the parameters, if its format is valid.
*/
public static String[] getParameters(String paramList) {
paramList = STRING.purifier(paramList);
CustomScanner cs = new CustomScanner(paramList, false, "(", ",", ")");
List<String> scan = cs.scan();
String str[] = new String[scan.size()];
if (isParameterList(paramList)) {
return scan.toArray(str);
} else {
throw new InputMismatchException("Syntax Error In Parameter List " + paramList);
}
}
/**
*
* @param name The name of the Variable object to check, whether or not it
* is a parameter of this Function object.
* @return true if a Variable of the specified name exists in the parameter
* list of this Function object.
*/
public boolean isParam(String name) {
return getIndependentVariable(name) != null;
}
/**
*
* @param matrix The {@link Matrix} object to be wrapped in a function
* @return the name assigned to the anonymous function created.
*/
public static synchronized String storeAnonymousMatrixFunction(Matrix matrix) {
int num = FunctionManager.countAnonymousFunctions();
String name = "anon" + (num + 1);
matrix.setName(name);
FunctionManager.add(new Function(matrix));
return name;
}
/**
*
* @param expression The expression used to create the function...e.g @(x)sin(x-1)^cos(x)
* @return the name assigned to the anonymous function created.
*/
public static synchronized String storeAnonymousFunction(String expression) {
int num = FunctionManager.countAnonymousFunctions();
String name = "anon" + (num + 1);
String tempName = "temp"+System.nanoTime();
Function f = new Function(tempName+"="+expression);
f.dependentVariable.setName(name);
FunctionManager.add(f);
return name;
}
/**
* @param args
* @return the value of a function when valid arguments are passed into its
* parentheses. e.g if the fullname of the Function is f(x,y,c), this method
* could be passed..f(3,-4,9)
*/
public String evalArgs(String args) {
if (type != ALGEBRAIC) {
return null;
}
String str = getFullName();
//StringBuilder;
CustomScanner cs = new CustomScanner(args, false, "(", ")", ",");
List<String> l = cs.scan();
if (l.get(0).equals(dependentVariable.getName())) {
l.remove(0);
int sz = l.size();
int sz1 = independentVariables.size();
if (sz == sz1) {
String vars = "";
for (int i = 0; i < sz; i++) {
String token = l.get(i);
if (!Number.validNumber(token) && !Variable.isVariableString(token)) {
throw new NumberFormatException("Unrecognized Value or Variable: " + l.get(i));
}//end if
else {
vars = vars.concat(independentVariables.get(i).getName() + "=" + l.get(i) + ";");//build variable command.
}
}//end for
mathExpression.getVariableManager().parseCommand(vars);
return mathExpression.solve();
}//end if
else {
throw new InputMismatchException("Invalid Argument List! " + sz1 + " arguments expected!");
}//end else
}//end if
else {
throw new InputMismatchException("Pass Arguments To The Format: " + str);
}//end else
}//end method
/**
*
* @param rangeDescr Describes the range between which this Function object
* should be plotted. e.g. x:-10:10:0.0001
* @return an 2D array containing two 1d arrays. The first array contains
* the values that the Function object will have for all values specified
* for the range of the independent variable. The second array contains the
* values that the independent variable will assume in its given range. If
* the rangeDescr parameter is not valid.. it returns a2D array containing 2
* null arrays.
*/
public String[][] evalRange(String rangeDescr) {
//x:0:10:xStep
String variableName = rangeDescr.substring(0, rangeDescr.indexOf(":"));
rangeDescr = rangeDescr.substring(rangeDescr.indexOf(":") + 1);
if (isParam(variableName)) {
String xStart = rangeDescr.substring(0, rangeDescr.indexOf(":"));
rangeDescr = rangeDescr.substring(rangeDescr.indexOf(":") + 1);
String xEnd = rangeDescr.substring(0, rangeDescr.indexOf(":"));
rangeDescr = rangeDescr.substring(rangeDescr.indexOf(":") + 1);
double xStep = Double.valueOf(rangeDescr);
double x1 = Double.valueOf(xStart);
double x2 = Double.valueOf(xEnd);
int sz = (int) ((x2 - x1) / xStep);
String[][] results = new String[2][sz + 1];
if (x1 > x2) {
double p = x1;
x1 = x2;
x2 = p;
}
int i = 0;
int len = sz + 1;
for (double x = x1; i < len && x <= x2; x += xStep, i++) {
String xStr = String.valueOf(x);
mathExpression.setValue(variableName, xStr);
results[0][i] = mathExpression.solve();
results[1][i] = xStr;
}//end for
return results;
}//end if
return new String[][]{null, null};
}//end method
/**
*
* @param xLower The lower limit from which plotting begins.
* @param xUpper The upper limit from which plotting begins.
* @param xStep The plot step.
* @param variableName The name of the independent(the horizontal axis
* variable..usually x)
* @param DRG States whether the function should be evaluated in Degrees,
* Radians, and Grad.
* @return an 2D array containing two 1d arrays. The first array contains
* the values that the Function object will have for all values specified
* for the range of the independent variable. The second array contains the
* values that the independent variable will assume in its given range. If
* the rangeDescr parameter is not valid.. it returns a2D array containing 2
* null arrays.
*/
public double[][] evalRange(double xLower, double xUpper, double xStep, String variableName, int DRG) {
if (xLower > xUpper) {
double p = xLower;
xLower = xUpper;
xUpper = p;
}
int sz = (int) ((xUpper - xLower) / xStep);
double[][] results = new double[2][sz + 1];
int len = sz + 1;
int i = 0;
for (double x = xLower; i < len && x <= xUpper; x += xStep, i++) {
mathExpression.setValue(variableName, String.valueOf(x));
results[0][i] = Double.parseDouble(mathExpression.solve());
results[1][i] = x;
}//end for
return results;
}//end method
/**
* Prints the content of a 2D array
*/
public static void print2DArray(Object[][] obj) {
int rows = obj.length;
for (int j = 0; j < rows; j++) {
Object[] values = obj[j];
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}//end for loop.
}//end for loop.
}//end method
/**
*
* @return the value of the function based on assigned values of its
* variables and constants.
*/
public String eval() {
return mathExpression.solve();
}
/**
*
* @param str The input string to check if or not it is of the format of the
* full name of a Function. e.g. F(x),p(x,y) e.t.c.
* @return true if the input string has the format of the full name of a
* Function. This method will be used to identify when the input into a
* calculator is to be treated as a Function problem...i.e. identify when
* the user is trying to create or modify a Function. F(x,y,x1,c,e3,u)
*/
public static boolean isFunctionFullName(String str) {
if (str.contains("(") && str.contains(")")) {
str = STRING.purifier(str);
CustomScanner cs = new CustomScanner(str, false, "(", ")", ",");
List<String> l = cs.scan();
if (Variable.isVariableString(l.get(0))) {
int sz = l.size();
for (int i = 1; i < sz; i++) {
if (!Variable.isVariableString(l.get(i))) {
return false;
}//end false
}//end for loop
return true;
}//end if
else {
return false;
}//end else
}//end if
else {
return false;
}//end else
}//end method
/**
*
* @return the standard math form of this Function object.
*/
@Override
public String toString() {
String fullname = getFullName();
String paramList = fullname.substring(fullname.indexOf("("));
switch (type) {
case ALGEBRAIC:
return getName() + "=@" + paramList + mathExpression.getExpression();
case MATRIX:
return getName() + "=@" + paramList + "(" + matrixToCommaList(matrix) + ")";
case LIST:
return getName() + "=@" + paramList + "(" + matrixToCommaList(matrix) + ")";
default:
return "";
}
}//end method
public boolean isAnonymous() {
return isAnonymous(this);
}
public static boolean isAnonymous(Function f) {
switch (f.type) {
case ALGEBRAIC:
return f.dependentVariable.getName().startsWith("anon");
case MATRIX:
return f.matrix.getName().startsWith("anon");
case LIST:
return f.matrix.getName().startsWith("anon");
default:
return false;
}
}
public static boolean isAnonymous(String name) {
return name.startsWith("anon");
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Function) {
Function f = (Function) obj;
return toString().equals(f.toString());
}
return false;
}
/**
* @return the dependent variable together with its independent variables
* within its circular parentheses. e.g if the Function is y=x^2, this
* method will return y(x)
*/
public String getFullName() {
switch (type) {
case ALGEBRAIC:
String str = dependentVariable.getName() + "(";
int sz = independentVariables.size();
for (int i = 0; i < sz; i++) {
str += (independentVariables.get(i).getName() + ",");
}//end for
str = str.substring(0, str.length() - 1);
return str + ")";
case MATRIX:
return getName() + "(" + matrix.getRows() + "," + matrix.getCols() + ")";
case LIST:
return getName() + "(" + matrix.getRows() + "," + matrix.getCols() + ")";
default:
return "";
}
}
/**
*
* @return the simple name of the function. e.g if the function is
* y=@(x)cos(x), then this method returns 'y'.
*/
public String getName() {
switch (type) {
case ALGEBRAIC:
return dependentVariable.getName();
case MATRIX:
return matrix.getName();
case LIST:
return matrix.getName();
default:
return "";
}
}
/**
*
* @return the determinant of the function if it is of type
* {@link Function#MATRIX} Otherwise it returns {@link Double#NaN}
*/
public double calcDet() {
if (type == MATRIX) {
return matrix.determinant();
}
return Double.NaN;
}
/**
*
* @return the inverse of the matrix-function if it is of type
* {@link Function#MATRIX} Otherwise it returns null
*/
public Matrix calcInverse() {
if (type == MATRIX) {
return matrix.inverse();
}
return null;
}
/**
*
* @return the triangular Matrix of the function if it is of type
* {@link Function#MATRIX} Otherwise it returns null
*/
public Matrix triangularMatrix() {
if (type == MATRIX) {
return matrix.reduceToTriangularMatrix();
}
return null;
}
/**
*
* @return the row-reduced echelon matrix of the function if it is of type
* {@link Function#MATRIX} Otherwise it returns null
*/
public Matrix reduceToEchelon() {
if (type == MATRIX) {
return matrix.reduceToRowEchelonMatrix();
}
return null;
}