5
5
import javacalculus .core .CALC ;
6
6
import javacalculus .evaluator .extend .CalcFunctionEvaluator ;
7
7
import javacalculus .exception .CalcWrongParametersException ;
8
+ import javacalculus .struct .CalcDouble ;
8
9
import javacalculus .struct .CalcFunction ;
9
10
import javacalculus .struct .CalcInteger ;
10
11
import javacalculus .struct .CalcObject ;
@@ -49,43 +50,58 @@ public CalcObject expand(CalcObject object) {
49
50
if (obj .getHeader ().equals (CALC .POWER )) {
50
51
CalcFunction function = (CalcFunction ) obj ;
51
52
CalcObject firstObj = CALC .SYM_EVAL (function .get (0 ));
52
- CalcObject secondObj = CALC .SYM_EVAL (function .get (1 ));
53
- //System.out.println("This is a function in the power branch: " + function);
54
- if (firstObj instanceof CalcFunction && secondObj .isNumber () && secondObj instanceof CalcInteger ) {//f(x)^k
55
- int pow = ((CalcInteger ) secondObj ).intValue ();
56
- boolean isPowNegative = pow < 0 ;
57
- //System.out.println("WE ARE IN THE f(x)^k branch");
58
- if (isPowNegative ) {
59
- //System.out.println("OH SNAP, this is the bottom part of a fraction!");
60
- pow = Math .abs (pow );
61
- }
62
- if (pow == 1 ) {
63
- return obj ;
53
+ CalcObject secondObjTemp = CALC .SYM_EVAL (function .get (1 ));
54
+ CalcInteger secondObj = null ;
55
+ if (secondObjTemp .isNumber ()) {
56
+ if (secondObjTemp instanceof CalcInteger ) {
57
+ secondObj = (CalcInteger ) secondObjTemp ;
58
+ } else {
59
+ CalcDouble temp = (CalcDouble ) secondObjTemp ;
60
+ if (temp .isInteger ()) {
61
+ //CalcDouble
62
+ secondObj = new CalcInteger (((CalcDouble ) secondObjTemp ).bigDecimalValue ().toBigInteger ());
63
+ }
64
64
}
65
- ArrayList <CalcObject > resultFunc = new ArrayList <>();
66
- //System.out.println("This is the first part of the function " + firstObj);
67
- //System.out.println("This is the second part of the function " + secondObj);
68
- if (firstObj .getHeader ().equals (CALC .ADD )) {
69
- Iterator iter = ((CalcFunction ) firstObj ).iterator ();
70
- while (iter .hasNext ()) {
71
- resultFunc .add (CALC .SYM_EVAL (CALC .EXPAND .createFunction (CALC .MULTIPLY .createFunction ((CalcObject ) iter .next (), firstObj ))));
65
+ //System.out.println("This is a function in the power branch: " + function);
66
+ if (secondObj != null && firstObj instanceof CalcFunction ) {//f(x)^k
67
+ int pow = ((CalcInteger ) secondObj ).intValue ();
68
+ boolean isPowNegative = pow < 0 ;
69
+ //System.out.println("WE ARE IN THE f(x)^k branch");
70
+ if (isPowNegative ) {
71
+ //System.out.println("OH SNAP, this is the bottom part of a fraction!");
72
+ pow = Math .abs (pow );
73
+ }
74
+ if (pow == 1 ) {
75
+ return obj ;
76
+ }
77
+ ArrayList <CalcObject > resultFunc = new ArrayList <>();
78
+ //System.out.println("This is the first part of the function " + firstObj);
79
+ //System.out.println("This is the second part of the function " + secondObj);
80
+ if (firstObj .getHeader ().equals (CALC .ADD )) {
81
+ Iterator iter = ((CalcFunction ) firstObj ).iterator ();
82
+ while (iter .hasNext ()) {
83
+ resultFunc .add (CALC .SYM_EVAL (CALC .EXPAND .createFunction (CALC .MULTIPLY .createFunction ((CalcObject ) iter .next (), firstObj ))));
84
+ }
85
+ } else {
86
+ //System.out.println("not adding: " + firstObj);
87
+ return obj ;
88
+ }
89
+ ////System.err.println(resultFunc);
90
+ for (CalcObject temp : resultFunc ) {
91
+ factored = CALC .SYM_EVAL (CALC .ADD .createFunction (factored , temp ));
72
92
}
93
+ for (int i = 0 ; i < pow - 2 ; i ++) {
94
+ factored = CALC .SYM_EVAL (CALC .EXPAND .createFunction (CALC .MULTIPLY .createFunction (firstObj , factored )));
95
+ }
96
+ if (isPowNegative ) {
97
+ factored = CALC .POWER .createFunction (factored , CALC .NEG_ONE );
98
+ }
99
+ //System.out.println("RESULT of f(x)^k: " + factored);
100
+ return factored ;
73
101
} else {
74
- //System.out.println("not adding: " + firstObj );
75
- return obj ;
102
+ //System.out.println("SECOND NUM " + secondObj.getHeader() );
103
+ //System.out.println((firstObj instanceof CalcFunction) + " && " + (secondObj.isNumber()) + " && " + (secondObj instanceof CalcInteger)) ;
76
104
}
77
- ////System.err.println(resultFunc);
78
- for (CalcObject temp : resultFunc ) {
79
- factored = CALC .SYM_EVAL (CALC .ADD .createFunction (factored , temp ));
80
- }
81
- for (int i = 0 ; i < pow - 2 ; i ++) {
82
- factored = CALC .SYM_EVAL (CALC .EXPAND .createFunction (CALC .MULTIPLY .createFunction (firstObj , factored )));
83
- }
84
- if (isPowNegative ) {
85
- factored = CALC .POWER .createFunction (factored , CALC .NEG_ONE );
86
- }
87
- //System.out.println("RESULT of f(x)^k: " + factored);
88
- return factored ;
89
105
}
90
106
} else if (obj .getHeader ().equals (CALC .MULTIPLY )) {
91
107
ArrayList <CalcObject > allParts = giveList (CALC .MULTIPLY , obj );
@@ -144,6 +160,8 @@ public CalcObject expand(CalcObject object) {
144
160
//System.out.println("RESULT of g(x)*f(x): " + factored + "\n" + CALC.SYM_EVAL(factored));
145
161
return factored ;
146
162
}
163
+ } else {
164
+ //System.out.println("NOPE");
147
165
}
148
166
return obj ;
149
167
}
@@ -153,17 +171,15 @@ private ArrayList<CalcObject> giveList(CalcSymbol operator, CalcObject func) {
153
171
////System.out.println(func);
154
172
if (func instanceof CalcFunction && func .getHeader ().equals (operator )) {
155
173
ArrayList <CalcObject > funcParts = ((CalcFunction ) func ).getAll ();
156
- for (int i = 0 ; i < funcParts .size (); i ++) {
157
- CalcObject firstObj = funcParts .get (i );
174
+ for (CalcObject firstObj : funcParts ) {
158
175
//if (firstObj instanceof CalcFunction && ((CalcFunction) firstObj).getHeader().equals(operator)) {
159
176
list .addAll (giveList (operator , firstObj ));
160
177
//}
161
178
}
162
- ////System.out.println("LIST in loop" + list);
163
179
} else {
164
180
list .add (func );
165
181
////System.out.println("LIST" + list);
166
182
}
167
183
return list ;
168
184
}
169
- }
185
+ }
0 commit comments