-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgebraicManipulator.java
146 lines (95 loc) · 4.15 KB
/
AlgebraicManipulator.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
import java.util.List;
import java.util.LinkedList;
import java.util.Arrays;
import java.util.Iterator;
public class AlgebraicManipulator{
public int compareOperations(char a, char b){
return a-b;
}
public static BinaryTreeList<Term> expansion(Expression e){
BinaryTreeList<Term> list = new BinaryTreeList<Term>();
for(Term t : e.terms())
list.addAll(expansion(t));
return list;
}
public static List<Term> expansion(Term t){
//System.out.println("Trying to expand: " +t);
List<Term> termsToReturn = new LinkedList<Term>();
Term nonExpressionFactors = new Term();
List<Expression> expressionList = new LinkedList<Expression>();
for(Factor f : t.factors()){
if(f instanceof Expression && f.operations().size() == 0){
expressionList.add((Expression)f);
}else{
nonExpressionFactors.addFactor(f);
}
}
nonExpressionFactors.addFactors(t.constants());
System.out.println("Expression list: " +expressionList);
System.out.println("Expression list size: " +expressionList.size());
System.out.println("Non-expression factors: " +nonExpressionFactors);
if(expressionList.isEmpty()){
System.out.println("This expression list is empty, so returning.");
return new LinkedList<Term>(Arrays.asList(nonExpressionFactors));
}
LinkedList<Expression> expandedExpressions = new LinkedList<Expression>();
List<Term> termListPtr;
for(Expression toExpand : expressionList){
Expression expanded = new Expression();
if(toExpand.operations().size() > 0){
throw new UnsupportedOperationException("How did a factor with operations get here?");
}
//expandedExpressions.addLast(new Expression());
System.out.println("Looking to expand terms in: " +toExpand);
for(Term termToExpand : toExpand.terms()){
System.out.println("In "+toExpand+", " +termToExpand+", is about to be expanded.");
expanded.addTerms(expansion(termToExpand));
}
expandedExpressions.add(expanded);
}
while(expandedExpressions.size() > 1){
System.out.println("Expanded expressions are: " +expandedExpressions);
//pop two elements off, multiply them and add them back in.
expandedExpressions.addFirst(new Expression(multiply(expandedExpressions.pop(), expandedExpressions.pop())));
}
System.out.println("Expanded expressions are: " +expandedExpressions);
if(nonExpressionFactors.isEmpty())
return expandedExpressions.getFirst().terms();
return multiply(nonExpressionFactors, expandedExpressions.getFirst()).asList();
}
public static Expression multiplyEntities(/*MathematicalEntity a, MathematicalEntity b*/){
return null;
}
private static Constant multiplyConstants(Constant a, Constant b){
return new Constant(a.doubleValue() * b.doubleValue());
}
public static Term multiply(Term a, Term b){
System.out.print("Multiplying: (T) " + a + " * (T) " + b);
Term t = new Term();
// System.out.println("So, combining: " +a.allFactors() + " and " + b.allFactors());
t.addFactors(a.allFactors());
t.addFactors(b.allFactors());
//System.out.print(" = " +t+"\n");
return t;
}
public static BinaryTreeList<Term> multiply(Term t, Expression exp){
// System.out.println("TERM VS. EXP");
BinaryTreeList<Term> toReturn = new BinaryTreeList<Term>();
for(Term toMultiply : exp.terms()){
toReturn.add(multiply(t, toMultiply));
}
return toReturn;
}
public static BinaryTreeList<Term> multiply(Expression a, Expression b){
System.out.println("Multiplying: " + a + " * " + b);
List<Term> aTerms = a.terms();
List<Term> bTerms = b.terms();
BinaryTreeList<Term> product = new BinaryTreeList<Term>();
for(Term p : aTerms)
for(Term q : bTerms){
product.add(multiply(p, q));
}
System.out.println("The product is: " +product);
return product;
}
}