001 /* 002 * $Id: RealRootTuple.java 3603 2011-04-25 18:31:43Z kredel $ 003 */ 004 005 package edu.jas.root; 006 007 008 import java.util.List; 009 import java.util.ArrayList; 010 011 import edu.jas.arith.BigDecimal; 012 import edu.jas.arith.BigRational; 013 import edu.jas.arith.Rational; 014 import edu.jas.structure.ElemFactory; 015 import edu.jas.structure.RingElem; 016 import edu.jas.structure.GcdRingElem; 017 import edu.jas.structure.RingFactory; 018 019 020 /** 021 * RealAlgebraicNumber root tuple. 022 * @param <C> coefficient type. 023 * @author Heinz Kredel 024 */ 025 public class RealRootTuple<C extends GcdRingElem<C> & Rational> { 026 027 028 /** 029 * Tuple of RealAlgebraicNumbers. 030 */ 031 public final List<RealAlgebraicNumber<C>> tuple; 032 033 034 /** 035 * Constructor. 036 * @param t list of roots. 037 */ 038 public RealRootTuple(List<RealAlgebraicNumber<C>> t) { 039 if ( t == null ) { 040 throw new IllegalArgumentException("null tuple not allowed"); 041 } 042 tuple = t; 043 } 044 045 046 /** 047 * String representation of tuple. 048 * @see java.lang.Object#toString() 049 */ 050 @Override 051 public String toString() { 052 return tuple.toString(); 053 } 054 055 056 /** 057 * Get a scripting compatible string representation. 058 * @return script compatible representation for this Rectangle. 059 */ 060 public String toScript() { 061 // Python case 062 StringBuffer sb = new StringBuffer("["); 063 boolean first = true; 064 for (RealAlgebraicNumber<C> r : tuple) { 065 if ( first ) { 066 first = false; 067 } else { 068 sb.append(","); 069 } 070 sb.append(r.toScript()); 071 } 072 return sb.toString(); 073 } 074 075 076 /* 077 * Contains a point. 078 * @param c point. 079 * @return true if c is contained in this rectangle, else false. 080 public boolean contains(Complex<C> c) { 081 Complex<C> ll = getSW(); 082 Complex<C> ur = getSW(); 083 return c.getRe().compareTo(ll.getRe()) < 0 || 084 c.getIm().compareTo(ll.getIm()) < 0 || 085 c.getRe().compareTo(ur.getRe()) > 0 || 086 c.getIm().compareTo(ur.getIm()) > 0; 087 } 088 */ 089 090 091 /* 092 * Random point of recatangle. 093 * @return a random point contained in this rectangle. 094 public Complex<C> randomPoint() { 095 Complex<C> sw = getSW(); 096 Complex<C> se = getSE(); 097 Complex<C> nw = getNW(); 098 Complex<C> r = sw.factory().random(13); 099 C dr = se.getRe().subtract(sw.getRe()); // >= 0 100 C di = nw.getIm().subtract(sw.getIm()); // >= 0 101 C rr = r.getRe().abs(); 102 C ri = r.getIm().abs(); 103 C one = ((RingFactory<C>)dr.factory()).getONE(); 104 if ( !rr.isZERO() ) { 105 if ( rr.compareTo(one) > 0 ) { 106 rr = rr.inverse(); 107 } 108 } 109 if ( !ri.isZERO() ) { 110 if ( ri.compareTo(one) > 0 ) { 111 ri = ri.inverse(); 112 } 113 } 114 // 0 <= rr, ri <= 1 115 rr = rr.multiply(dr); 116 ri = ri.multiply(di); 117 Complex<C> rp = new Complex<C>(sw.factory(),rr,ri); 118 //System.out.println("rp = " + rp); 119 rp = sw.sum(rp); 120 return rp; 121 } 122 */ 123 124 125 /** 126 * Clone this. 127 * @see java.lang.Object#clone() 128 */ 129 @Override 130 public RealRootTuple<C> clone() { 131 return new RealRootTuple<C>(new ArrayList<RealAlgebraicNumber<C>>(tuple)); 132 } 133 134 135 /** 136 * Comparison with any other object. 137 * @see java.lang.Object#equals(java.lang.Object) 138 */ 139 @Override 140 @SuppressWarnings("unchecked") 141 public boolean equals(Object b) { 142 if (!(b instanceof RealRootTuple)) { 143 return false; 144 } 145 RealRootTuple<C> a = null; 146 try { 147 a = (RealRootTuple<C>) b; 148 } catch (ClassCastException e) { 149 } 150 return tuple.equals(a.tuple); 151 } 152 153 154 /** 155 * Hash code for this Rectangle. 156 * @see java.lang.Object#hashCode() 157 */ 158 @Override 159 public int hashCode() { 160 return tuple.hashCode(); 161 } 162 163 164 /** 165 * Rational approximation of each coordinate. 166 * @return list of coordinate points. 167 */ 168 public List<BigRational> getRational() { 169 List<BigRational> center = new ArrayList<BigRational>(tuple.size()); 170 for ( RealAlgebraicNumber<C> rr : tuple ) { 171 BigRational r = rr.getRational(); 172 center.add(r); 173 } 174 return center; 175 } 176 177 178 /** 179 * Decimal approximation of each coordinate. 180 * @return list of coordinate points. 181 */ 182 public List<BigDecimal> decimalMagnitude() { 183 List<BigDecimal> center = new ArrayList<BigDecimal>(tuple.size()); 184 for ( RealAlgebraicNumber<C> rr : tuple ) { 185 BigDecimal r = rr.decimalMagnitude(); 186 center.add(r); 187 } 188 return center; 189 } 190 191 192 /** 193 * Rational Length. 194 * @return max |v_i|; 195 */ 196 public BigRational rationalLength() { 197 BigRational len = new BigRational(); 198 for ( RealAlgebraicNumber<C> rr : tuple ) { 199 BigRational r = rr.ring.root.rationalLength(); 200 int s = len.compareTo(r); 201 if ( s < 0 ) { 202 len = r; 203 } 204 } 205 return len; 206 } 207 208 209 /** 210 * Signum. 211 * @return ?; 212 */ 213 public int signum() { 214 int s = 0; 215 for ( RealAlgebraicNumber<C> rr : tuple ) { 216 int rs = rr.signum(); 217 if ( rs != 0 ) { 218 s = rs; 219 } 220 } 221 return s; 222 } 223 224 }