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