KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > values > Permutation


1 /* @author rich
2  * Created on 15-Mar-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.groupJep.values;
9 import org.lsmp.djep.groupJep.*;
10 /**
11  * @author Rich Morris
12  * Created on 15-Mar-2004
13  */

14 public class Permutation extends Number JavaDoc {
15
16     protected GroupI group;
17     protected Integer JavaDoc perm[];
18     protected int len;
19     /**
20      *
21      */

22     public Permutation(GroupI group,Integer JavaDoc perm[]) {
23         super();
24         this.group = group;
25         this.perm = perm;
26         this.len = perm.length;
27     }
28
29     public Permutation add(Permutation p1)
30     {
31         Integer JavaDoc res[] = new Integer JavaDoc[p1.perm.length];
32         for(int i=0;i<len;++i)
33             res[i]= p1.perm[this.perm[i].intValue()-1];
34         return valueOf(res);
35     }
36
37     
38     public Permutation getInverse() {
39         Integer JavaDoc res[] = new Integer JavaDoc[len];
40         for(int i=0;i<len;++i)
41             res[this.perm[i].intValue()-1]= new Integer JavaDoc(i+1);
42         return valueOf(res);
43     }
44     
45     public Permutation sub(Permutation p1)
46     {
47         return this.add(p1.getInverse());
48     }
49
50     public boolean equals(Permutation p1)
51     {
52         for(int i=0;i<len;++i)
53             if(this.perm[i] != p1.perm[i])
54                 return false;
55         return true;
56     }
57
58     public Permutation valueOf(Integer JavaDoc p[])
59     {
60         return new Permutation(this.group,p);
61     }
62
63     public Permutation valueOf(Number JavaDoc p[])
64     {
65         Integer JavaDoc res[] = new Integer JavaDoc[p.length];
66         for(int i=0;i<p.length;++i)
67             res[i]=(Integer JavaDoc) p[i];
68         return new Permutation(this.group,res);
69     }
70     
71     public String JavaDoc toString()
72     {
73         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
74         sb.append("(");
75         for(int i=0;i<this.perm.length;++i)
76         {
77             if(i>0) sb.append(",");
78             sb.append(this.perm[i].toString());
79         }
80         sb.append(")");
81         return sb.toString();
82     }
83     /** Just returns 0. Minimal implematation for compatability with Number. */
84     public double doubleValue() {return 0; }
85     public float floatValue() {return 0; }
86     public int intValue() { return 0; }
87     public long longValue() {return 0; }
88 }
89
Popular Tags