001/* 002 * $Id: RealRootTuple.java 5611 2016-10-03 22:06:32Z kredel $ 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 return sb.toString(); 071 } 072 073 074 /** 075 * Contains a point. 076 * @param c real root tuple representing a point. 077 * @return true if c is contained in this root tuple, else false. 078 */ 079 public boolean contains(RealRootTuple<C> c) { 080 return contains(c.tuple); 081 } 082 083 084 /** 085 * Contains a point. 086 * @param c list of real algebraic numbers representing a point. 087 * @return true if c is contained in this root tuple, else false. 088 */ 089 public boolean contains(List<RealAlgebraicNumber<C>> c) { 090 int i = 0; 091 for (RealAlgebraicNumber<C> r : tuple) { 092 RealAlgebraicNumber<C> cn = c.get(i++); 093 boolean t = r.ring.root.contains(cn.ring.root); 094 if (!t) { 095 return false; 096 } 097 } 098 return true; 099 } 100 101 102 /** 103 * Random point of real root tuple. 104 * @return a random point contained in this real root tuple. 105 */ 106 public List<C> randomPoint() { 107 List<C> tp = new ArrayList<C>(tuple.size()); 108 for (RealAlgebraicNumber<C> r : tuple) { 109 C rp = r.ring.root.randomPoint(); 110 tp.add(rp); 111 } 112 return tp; 113 } 114 115 116 /** 117 * Refine root isolating intervals. 118 * @param eps desired interval length. 119 */ 120 public void refineRoot(BigRational eps) { 121 for (RealAlgebraicNumber<C> r : tuple) { 122 r.ring.refineRoot(eps); 123 } 124 return; 125 } 126 127 128 /** 129 * Copy this. 130 * @return a copy of this. 131 */ 132 public RealRootTuple<C> copy() { 133 return new RealRootTuple<C>(new ArrayList<RealAlgebraicNumber<C>>(tuple)); 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 RealRootTuple<C> a = null; 145 try { 146 a = (RealRootTuple<C>) b; 147 } catch (ClassCastException e) { 148 } 149 if (a == null) { 150 return false; 151 } 152 return tuple.equals(a.tuple); 153 } 154 155 156 /** 157 * Hash code for this Rectangle. 158 * @see java.lang.Object#hashCode() 159 */ 160 @Override 161 public int hashCode() { 162 return tuple.hashCode(); 163 } 164 165 166 /** 167 * Rational approximation of each coordinate. 168 * @return list of coordinate points. 169 */ 170 public List<BigRational> getRational() { 171 List<BigRational> center = new ArrayList<BigRational>(tuple.size()); 172 for (RealAlgebraicNumber<C> rr : tuple) { 173 BigRational r = rr.getRational(); 174 center.add(r); 175 } 176 return center; 177 } 178 179 180 /** 181 * Decimal approximation of each coordinate. 182 * @return list of coordinate points. 183 */ 184 public List<BigDecimal> decimalMagnitude() { 185 List<BigDecimal> center = new ArrayList<BigDecimal>(tuple.size()); 186 for (RealAlgebraicNumber<C> rr : tuple) { 187 BigDecimal r = rr.decimalMagnitude(); 188 center.add(r); 189 } 190 return center; 191 } 192 193 194 /** 195 * Rational Length. 196 * @return max |v_i|; 197 */ 198 public BigRational rationalLength() { 199 BigRational len = new BigRational(); 200 for (RealAlgebraicNumber<C> rr : tuple) { 201 BigRational r = rr.ring.root.rationalLength(); 202 int s = len.compareTo(r); 203 if (s < 0) { 204 len = r; 205 } 206 } 207 return len; 208 } 209 210 211 /** 212 * Signum. 213 * @return ?; 214 */ 215 public int signum() { 216 int s = 0; 217 for (RealAlgebraicNumber<C> rr : tuple) { 218 int rs = rr.signum(); 219 if (rs != 0) { 220 s = rs; 221 } 222 } 223 return s; 224 } 225 226}