001/* 002 * $Id: RealRootTuple.java 4125 2012-08-19 19:05:22Z kredel $ 003 */ 004 005package edu.jas.root; 006 007 008import java.io.Serializable; 009import java.util.List; 010import java.util.ArrayList; 011 012import edu.jas.arith.BigDecimal; 013import edu.jas.arith.BigRational; 014import edu.jas.arith.Rational; 015import edu.jas.structure.ElemFactory; 016import edu.jas.structure.RingElem; 017import edu.jas.structure.GcdRingElem; 018import edu.jas.structure.RingFactory; 019 020 021/** 022 * RealAlgebraicNumber root tuple. 023 * @param <C> coefficient type. 024 * @author Heinz Kredel 025 */ 026public class RealRootTuple<C extends GcdRingElem<C> & Rational> implements Serializable { 027 028 029 /** 030 * Tuple of RealAlgebraicNumbers. 031 */ 032 public final List<RealAlgebraicNumber<C>> tuple; 033 034 035 /** 036 * Constructor. 037 * @param t list of roots. 038 */ 039 public RealRootTuple(List<RealAlgebraicNumber<C>> t) { 040 if ( t == null ) { 041 throw new IllegalArgumentException("null tuple not allowed"); 042 } 043 tuple = t; 044 } 045 046 047 /** 048 * String representation of tuple. 049 * @see java.lang.Object#toString() 050 */ 051 @Override 052 public String toString() { 053 return tuple.toString(); 054 } 055 056 057 /** 058 * Get a scripting compatible string representation. 059 * @return script compatible representation for this Rectangle. 060 */ 061 public String toScript() { 062 // Python case 063 StringBuffer sb = new StringBuffer("["); 064 boolean first = true; 065 for (RealAlgebraicNumber<C> r : tuple) { 066 if ( first ) { 067 first = false; 068 } else { 069 sb.append(","); 070 } 071 sb.append(r.toScript()); 072 } 073 return sb.toString(); 074 } 075 076 077 /* 078 * Contains a point. 079 * @param c point. 080 * @return true if c is contained in this rectangle, else false. 081 public boolean contains(Complex<C> c) { 082 Complex<C> ll = getSW(); 083 Complex<C> ur = getSW(); 084 return c.getRe().compareTo(ll.getRe()) < 0 || 085 c.getIm().compareTo(ll.getIm()) < 0 || 086 c.getRe().compareTo(ur.getRe()) > 0 || 087 c.getIm().compareTo(ur.getIm()) > 0; 088 } 089 */ 090 091 092 /* 093 * Random point of recatangle. 094 * @return a random point contained in this rectangle. 095 public Complex<C> randomPoint() { 096 Complex<C> sw = getSW(); 097 Complex<C> se = getSE(); 098 Complex<C> nw = getNW(); 099 Complex<C> r = sw.factory().random(13); 100 C dr = se.getRe().subtract(sw.getRe()); // >= 0 101 C di = nw.getIm().subtract(sw.getIm()); // >= 0 102 C rr = r.getRe().abs(); 103 C ri = r.getIm().abs(); 104 C one = ((RingFactory<C>)dr.factory()).getONE(); 105 if ( !rr.isZERO() ) { 106 if ( rr.compareTo(one) > 0 ) { 107 rr = rr.inverse(); 108 } 109 } 110 if ( !ri.isZERO() ) { 111 if ( ri.compareTo(one) > 0 ) { 112 ri = ri.inverse(); 113 } 114 } 115 // 0 <= rr, ri <= 1 116 rr = rr.multiply(dr); 117 ri = ri.multiply(di); 118 Complex<C> rp = new Complex<C>(sw.factory(),rr,ri); 119 //System.out.println("rp = " + rp); 120 rp = sw.sum(rp); 121 return rp; 122 } 123 */ 124 125 126 /** 127 * Copy this. 128 * @return a copy of this. 129 */ 130 public RealRootTuple<C> copy() { 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 RealRootTuple<C> a = null; 143 try { 144 a = (RealRootTuple<C>) b; 145 } catch (ClassCastException e) { 146 } 147 if (a == null) { 148 return false; 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}