001/* 002 * $Id$ 003 */ 004 005package edu.jas.root; 006 007 008import java.io.Serializable; 009import java.util.ArrayList; 010import java.util.List; 011 012import edu.jas.arith.BigDecimal; 013import edu.jas.arith.BigRational; 014import edu.jas.arith.Rational; 015import edu.jas.structure.GcdRingElem; 016 017 018/** 019 * RealAlgebraicNumber root tuple. 020 * @param <C> coefficient type. 021 * @author Heinz Kredel 022 */ 023public class RealRootTuple<C extends GcdRingElem<C> & Rational> implements Serializable { 024 025 026 /** 027 * Tuple of RealAlgebraicNumbers. 028 */ 029 public final List<RealAlgebraicNumber<C>> tuple; 030 031 032 /** 033 * Constructor. 034 * @param t list of roots. 035 */ 036 public RealRootTuple(List<RealAlgebraicNumber<C>> t) { 037 if (t == null) { 038 throw new IllegalArgumentException("null tuple not allowed"); 039 } 040 tuple = t; 041 } 042 043 044 /** 045 * String representation of tuple. 046 * @see java.lang.Object#toString() 047 */ 048 @Override 049 public String toString() { 050 return tuple.toString(); 051 } 052 053 054 /** 055 * Get a scripting compatible string representation. 056 * @return script compatible representation for this Rectangle. 057 */ 058 public String toScript() { 059 // Python case 060 StringBuffer sb = new StringBuffer("["); 061 boolean first = true; 062 for (RealAlgebraicNumber<C> r : tuple) { 063 if (first) { 064 first = false; 065 } else { 066 sb.append(","); 067 } 068 sb.append(r.toScript()); 069 } 070 sb.append("]"); 071 return sb.toString(); 072 } 073 074 075 /** 076 * Contains a point. 077 * @param c real root tuple representing a point. 078 * @return true if c is contained in this root tuple, else false. 079 */ 080 public boolean contains(RealRootTuple<C> c) { 081 return contains(c.tuple); 082 } 083 084 085 /** 086 * Contains a point. 087 * @param c list of real algebraic numbers representing a point. 088 * @return true if c is contained in this root tuple, else false. 089 */ 090 public boolean contains(List<RealAlgebraicNumber<C>> c) { 091 int i = 0; 092 for (RealAlgebraicNumber<C> r : tuple) { 093 RealAlgebraicNumber<C> cn = c.get(i++); 094 boolean t = r.ring.root.contains(cn.ring.root); 095 if (!t) { 096 return false; 097 } 098 } 099 return true; 100 } 101 102 103 /** 104 * Random point of real root tuple. 105 * @return a random point contained in this real root tuple. 106 */ 107 public List<C> randomPoint() { 108 List<C> tp = new ArrayList<C>(tuple.size()); 109 for (RealAlgebraicNumber<C> r : tuple) { 110 C rp = r.ring.root.randomPoint(); 111 tp.add(rp); 112 } 113 return tp; 114 } 115 116 117 /** 118 * Refine root isolating intervals. 119 * @param eps desired interval length. 120 */ 121 public void refineRoot(BigRational eps) { 122 for (RealAlgebraicNumber<C> r : tuple) { 123 r.ring.refineRoot(eps); 124 } 125 return; 126 } 127 128 129 /** 130 * Copy this. 131 * @return a copy of this. 132 */ 133 public RealRootTuple<C> copy() { 134 return new RealRootTuple<C>(new ArrayList<RealAlgebraicNumber<C>>(tuple)); 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 RealRootTuple<C> a = null; 146 try { 147 a = (RealRootTuple<C>) b; 148 } catch (ClassCastException e) { 149 } 150 if (a == null) { 151 return false; 152 } 153 return tuple.equals(a.tuple); 154 } 155 156 157 /** 158 * Hash code for this Rectangle. 159 * @see java.lang.Object#hashCode() 160 */ 161 @Override 162 public int hashCode() { 163 return tuple.hashCode(); 164 } 165 166 167 /** 168 * Rational approximation of each coordinate. 169 * @return list of coordinate points. 170 */ 171 public List<BigRational> getRational() { 172 List<BigRational> center = new ArrayList<BigRational>(tuple.size()); 173 for (RealAlgebraicNumber<C> rr : tuple) { 174 BigRational r = rr.getRational(); 175 center.add(r); 176 } 177 return center; 178 } 179 180 181 /** 182 * Decimal approximation of each coordinate. 183 * @return list of coordinate points. 184 */ 185 public List<BigDecimal> decimalMagnitude() { 186 List<BigDecimal> center = new ArrayList<BigDecimal>(tuple.size()); 187 for (RealAlgebraicNumber<C> rr : tuple) { 188 BigDecimal r = rr.decimalMagnitude(); 189 center.add(r); 190 } 191 return center; 192 } 193 194 195 /** 196 * Rational Length. 197 * @return max |v_i|; 198 */ 199 public BigRational rationalLength() { 200 BigRational len = new BigRational(); 201 for (RealAlgebraicNumber<C> rr : tuple) { 202 BigRational r = rr.ring.root.rationalLength(); 203 int s = len.compareTo(r); 204 if (s < 0) { 205 len = r; 206 } 207 } 208 return len; 209 } 210 211 212 /** 213 * Signum. 214 * @return ?; 215 */ 216 public int signum() { 217 int s = 0; 218 for (RealAlgebraicNumber<C> rr : tuple) { 219 int rs = rr.signum(); 220 if (rs != 0) { 221 s = rs; 222 } 223 } 224 return s; 225 } 226 227}