/** * Test if the value is a perfect square (i.e. its square root is another * integer). See <a href= * "http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer"> * Stackoverflow.com - Fastest way to determine if an integer's square root * is an integer</a> * * @param bi * @return <code>true</code> if the number is a perfect square. */ public final static boolean isPerfectSquare(BigInteger bi) { try { return isPerfectSquare(toLong(bi)); } catch (ArithmeticException ae) { } return false; // number out of range exception }
/** * Test if the fraction value is a perfect square (i.e. its numerator's and * denominator's square root are integers). See <a href= * "http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer"> * Stackoverflow.com - Fastest way to determine if an integer's square root * is an integer</a> * * @param bf * @return <code>true</code> if the number is a perfect square. */ public final static boolean isPerfectSquare(BigFraction bf) { try { long num = toLong(bf.getNumerator()); long den = toLong(bf.getDenominator()); return isPerfectSquare(den) && isPerfectSquare(num); } catch (ArithmeticException ae) { } return false; // number out of range exception }
/** * Test if the fraction value is a perfect square (i.e. its numerator's and * denominator's square root are integers). See <a href= * "http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer"> * Stackoverflow.com - Fastest way to determine if an integer's square root * is an integer</a> * * @param bf * @return <code>true</code> if the number is a perfect square. */ public final static boolean isPerfectSquare(IRational bf) { try { long num = toLong(bf.toBigNumerator()); long den = toLong(bf.toBigDenominator()); return isPerfectSquare(den) && isPerfectSquare(num); } catch (ArithmeticException ae) { } return false; // number out of range exception }
@Override public IExpr eComFraArg(final IComplex base, final IFraction exponent) { if (exponent.isNumEqualRational(F.C1D2) && base.getRealPart().isZero()) { // square root of pure imaginary number IRational im = base.getImaginaryPart(); boolean negative = false; im = im.divideBy(F.C2); if (im.isNegative()) { im = im.negate(); negative = true; } if (NumberUtil.isPerfectSquare(im)) { IExpr temp = F.Sqrt(im); if (negative) { // Sqrt(im.negate()) - I * Sqrt(im); return F.Plus(temp, F.Times(F.CNI, temp)); } // Sqrt(im.negate()) + I * Sqrt(im); return F.Plus(temp, F.Times(F.CI, temp)); } } return F.NIL; }