KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > symbolic > Product


1 package JSci.maths.symbolic;
2  
3 import JSci.maths.*;
4 import JSci.maths.groups.*;
5 import JSci.maths.fields.*;
6
7 import java.util.*;
8
9 class Product extends Expression {
10
11     private final List terms;
12
13     public Product(Expression [] a) {
14     terms = Arrays.asList(a);
15     }
16
17     public Product(List a) {
18     terms = a;
19     }
20
21     public Product(Expression a,Expression b) {
22     terms = new ArrayList();
23     terms.add(a);
24     terms.add(b);
25     }
26
27     public String JavaDoc toString() {
28     String JavaDoc r = "";
29     Expression f;
30     for (int j=0;j<terms.size();j++) {
31         if (j>0) r+="*";
32         f=(Expression)terms.get(j);
33         if (f.getPriority()<getPriority()) r+="("+f+")";
34         else r+=""+f;
35     }
36     return r;
37     }
38
39     public Expression differentiate(Variable x) {
40     List r = new ArrayList();
41     List p;
42     for (int j=0;j<terms.size();j++) {
43         p = new ArrayList();
44         for (int k=0;k<terms.size();k++) {
45         if (j==k) p.add(((Expression)terms.get(k)).differentiate(x));
46         else p.add(((Expression)terms.get(k)));
47         }
48         r.add(new Product(p));
49     }
50     return new Sum(r);
51     }
52
53     public Expression evaluate() {
54     // svolge i prodotti contenuti -> t
55
List t = new ArrayList();
56     for (int j=0;j<terms.size();j++) {
57         Expression f=((Expression)terms.get(j)).evaluate(); // recursive
58
if (Product.class.isInstance(f))
59         for (int k=0;k<((Product)f).terms.size();k++)
60             t.add(((Product)f).terms.get(k));
61         else t.add(f);
62     }
63     // raccoglie le costanti -> s
64
Ring.Member c = null;
65     List s = new ArrayList();
66     for (int j=0;j<t.size();j++) {
67         Expression f=(Expression)t.get(j);
68         if (f instanceof Constant) {
69         if (c==null) c=(Ring.Member)(((Constant)f).getValue());
70         else c = c.multiply((Ring.Member)((Constant)f).getValue());
71         }
72         else s.add(f);
73     }
74     if (c!=null && ((AbelianGroup)(c.getSet())).isZero(c))
75         return new Constant(c);
76     if (c!=null && ! (((Ring)(c.getSet())).isOne(c)))
77         s.add(new Constant(c));
78     // raduna i termini con base uguale -> h
79
Hashtable h = new Hashtable();
80     for (int j=0;j<s.size();j++) {
81         Expression b;
82         int e;
83         if (s.get(j) instanceof Power) {
84         b = ((Power)s.get(j)).getBase();
85         e = ((Power)s.get(j)).getExponent();
86         }
87         else {
88         b = (Expression) s.get(j);
89         e = 1;
90         }
91         if (h.containsKey(b))
92         e+=((Integer JavaDoc)h.get(b)).intValue();
93         h.put(b,new Integer JavaDoc(e));
94     }
95     // traduce h -> s
96
s = new ArrayList();
97     for (Enumeration fe=h.keys();fe.hasMoreElements();) {
98         Expression b = (Expression)fe.nextElement();
99         int e = ((Integer JavaDoc)h.get(b)).intValue();
100         if (e!=0) {
101         if (e==1) s.add(b);
102         else if (e!=0) s.add(new Power(b,e));
103         }
104     }
105     // ritorno
106
if (s.size()==0) return new Constant(((Ring)getSet()).one());
107     if (s.size()==1) return (Expression)s.get(0);
108     return new Product(s);
109     }
110
111     protected int getPriority() {return 10;}
112
113     public Object JavaDoc getSet() { return ((Member)terms.get(0)).getSet(); }
114
115 }
116
117
118
Popular Tags