001/* 002 * $Id: DecimalRoots.java 5575 2016-08-08 19:03:21Z kredel $ 003 */ 004 005package edu.jas.root; 006 007 008import java.io.Serializable; 009import java.util.List; 010 011import edu.jas.arith.Rational; 012import edu.jas.arith.BigDecimal; 013import edu.jas.poly.GenPolynomial; 014import edu.jas.poly.Complex; 015import edu.jas.structure.GcdRingElem; 016 017 018/** 019 * Container for the real and complex algebraic roots of a univariate 020 * polynomial. 021 * @param <C> coefficient type. 022 * @author Heinz Kredel 023 */ 024public class DecimalRoots<C extends GcdRingElem<C> & Rational> implements Serializable { 025 026 027 /** 028 * univariate polynomial. 029 */ 030 public final GenPolynomial<C> p; 031 032 033 /** 034 * real decimal roots. 035 */ 036 public final List<BigDecimal> real; 037 038 039 /** 040 * univariate polynomial with complex coefficients. 041 */ 042 public final GenPolynomial<Complex<C>> cp; 043 044 045 /** 046 * complex decimal roots. 047 */ 048 public final List<Complex<BigDecimal>> complex; 049 050 051 /** 052 * Constructor. 053 * @param p univariate polynomial 054 * @param cp univariate complex polynomial 055 * @param r list of real decimal roots 056 * @param c list of complex decimal roots 057 */ 058 public DecimalRoots(GenPolynomial<C> p, GenPolynomial<Complex<C>> cp, List<BigDecimal> r, 059 List<Complex<BigDecimal>> c) { 060 this.p = p; 061 this.cp = cp; 062 this.real = r; 063 this.complex = c; 064 } 065 066 067 /** 068 * String representation of AlgebraicRoots. 069 * @see java.lang.Object#toString() 070 */ 071 @Override 072 public String toString() { 073 return "[" + p + ", real=" + real + ", complex=" + complex + "]"; 074 } 075 076 077 /** 078 * Get a scripting compatible string representation. 079 * @return script compatible representation for this Interval. 080 */ 081 public String toScript() { 082 // Python case 083 StringBuffer sb = new StringBuffer("["); 084 sb.append(p.toScript()); 085 if (!real.isEmpty()) { 086 sb.append(", real=["); 087 boolean first = true; 088 for (BigDecimal r : real) { 089 if (first) { 090 first = false; 091 } else { 092 sb.append(", "); 093 } 094 sb.append(r.toScript()); 095 } 096 sb.append("]"); 097 } 098 if (!complex.isEmpty()) { 099 sb.append(", complex=["); 100 boolean first = true; 101 for (Complex<BigDecimal> c : complex) { 102 if (first) { 103 first = false; 104 } else { 105 sb.append(", "); 106 } 107 sb.append(c.toScript()); 108 } 109 sb.append("]"); 110 } 111 sb.append("]"); 112 return sb.toString(); 113 } 114 115 116 /** 117 * Copy this. 118 * @return a copy of this. 119 */ 120 public DecimalRoots<C> copy() { 121 return new DecimalRoots<C>(p, cp, real, complex); 122 } 123 124 125 /** 126 * Comparison with any other object. 127 * @see java.lang.Object#equals(java.lang.Object) 128 */ 129 @Override 130 @SuppressWarnings("unchecked") 131 public boolean equals(Object b) { 132 if (!(b instanceof DecimalRoots)) { 133 return false; 134 } 135 DecimalRoots<C> a = null; 136 try { 137 a = (DecimalRoots<C>) b; 138 } catch (ClassCastException e) { 139 return false; 140 } 141 return p.equals(a.p) && real.equals(a.real) && complex.equals(a.complex); 142 } 143 144 145 /** 146 * Hash code for this AlgebraicRoots. 147 * @see java.lang.Object#hashCode() 148 */ 149 @Override 150 public int hashCode() { 151 return (161 * p.hashCode() + 37) * real.hashCode() + complex.hashCode(); 152 } 153 154}