001
002/*
003 * $Id: Monomial.java 5916 2018-08-29 20:21:02Z kredel $
004 */
005
006package edu.jas.poly;
007
008import java.util.Map;
009import java.util.SortedMap;
010import java.util.Iterator;
011
012import edu.jas.structure.RingElem;
013import edu.jas.structure.Element;
014import edu.jas.structure.ElemFactory;
015
016import edu.jas.poly.ExpVector;
017
018
019/**
020 * Monomial class. 
021 * Represents pairs of exponent vectors and coefficients.
022 * Adaptor for Map.Entry.
023 * @author Heinz Kredel
024 */
025public final class Monomial<C extends RingElem<C>> implements Element<Monomial<C>> {
026
027    /** 
028     * Exponent of monomial.
029     */
030    public final ExpVector e;
031
032
033    /** 
034     * Coefficient of monomial.
035     */
036    public final C c;
037
038
039    /** 
040     * Constructor of monomial.
041     * @param me a MapEntry.
042     */
043    public Monomial(Map.Entry<ExpVector,C> me){
044        this( me.getKey(), me.getValue() );
045    }
046
047
048    /** 
049     * Constructor of monomial.
050     * @param e exponent.
051     * @param c coefficient.
052     */
053    public Monomial(ExpVector e, C c) {
054        this.e = e;
055        this.c = c;
056    }
057
058
059    /** 
060     * Getter for exponent.
061     * @return exponent.
062     */
063    public ExpVector exponent() {
064        return e;
065    }
066
067
068    /** 
069     * Getter for coefficient.
070     * @return coefficient.
071     */
072    public C coefficient() {
073        return c;
074    }
075
076
077    /**
078     * Clone this Element.
079     * @return Creates and returns a copy of this Element.
080     */
081    public Monomial<C> copy() {
082        return new Monomial<C>(e,c);
083    }
084
085
086    /**
087     * String representation of Monomial.
088     * @see java.lang.Object#toString()
089     */
090    @Override
091    public String toString() {
092        return c.toString() + " " + e.toString();
093    }
094
095
096    /**
097     * Script representation of Monomial.
098     * @see edu.jas.structure.Element#toScript()
099     */
100    @Override
101    public String toScript() {
102        if (c.isZERO()) {
103            return "0";
104        }
105        StringBuffer sb = new StringBuffer();
106        if (!c.isONE()) {
107            sb.append(c.toScript());
108            sb.append(" * ");
109        }
110        sb.append(e.toScript());
111        return sb.toString();
112    }
113
114
115    /**
116     * Get a scripting compatible string representation of the factory.
117     * @return script compatible representation for this ElemFactory.
118     * @see edu.jas.structure.Element#toScriptFactory()
119     */
120    @Override
121    public String toScriptFactory() {
122        // Python and Ruby case
123        return ""; // TODO
124    }
125
126
127    /**
128     * Get the corresponding element factory.
129     * @return null, factory for this Element.
130     * @see edu.jas.structure.Element#factory()
131     */
132    public ElemFactory<Monomial<C>> factory() {
133        return null; // TODO
134    }
135
136
137    /**
138     * Comparison with any other object.
139     * @see java.lang.Object#equals(java.lang.Object)
140     */
141    @Override
142    @SuppressWarnings("unchecked")
143    public boolean equals(Object B) {
144        if (!(B instanceof Monomial)) {
145            return false;
146        }
147        Monomial<C> b = (Monomial<C>) B;
148        return (compareTo(b) == 0);
149    }
150
151
152    /**
153     * hashCode. 
154     * @see java.lang.Object#hashCode()
155     */
156    @Override
157    public int hashCode() {
158        int h = e.hashCode();
159        h = (h << 4) + c.hashCode();
160        return h;
161    }
162
163
164    /**
165     * Monomial comparison.
166     * @param S Monomial.
167     * @return SIGN(this-S).
168     */
169    @Override
170    public int compareTo(Monomial<C> S) {
171        if (S == null) {
172            return 1;
173        }
174        int s = e.compareTo(S.e);
175        if (s != 0) {
176            return s;
177        }
178        return c.compareTo(S.c);
179    }
180
181}