BigDecimal
カスタムメイドの100行アルゴリズムではなく、Java APIのみを使用してJavaの平方根を計算できますか?
12 に答える
私はこれを使用しましたが、非常にうまく機能します。 アルゴリズムが高レベルでどのように機能するかの例を次に示します。
編集:以下に定義されているように、これがどれほど正確であるかを知りたいと思っていました。公式ソースからの sqrt(2) は次のとおりです。
(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147
ここでは、以下で概説するSQRT_DIG
150 に等しいアプローチを使用しています。
(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206086685
最初の偏差は、195 桁の精度の後に発生します。このような高レベルの精度が必要な場合は、自己責任で使用してください。
SQRT_DIG
1000 に変更すると、 1570 桁の精度が得られました。
private static final BigDecimal SQRT_DIG = new BigDecimal(150);
private static final BigDecimal SQRT_PRE = new BigDecimal(10).pow(SQRT_DIG.intValue());
/**
* Private utility method used to compute the square root of a BigDecimal.
*
* @author Luciano Culacciatti
* @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal
*/
private static BigDecimal sqrtNewtonRaphson (BigDecimal c, BigDecimal xn, BigDecimal precision){
BigDecimal fx = xn.pow(2).add(c.negate());
BigDecimal fpx = xn.multiply(new BigDecimal(2));
BigDecimal xn1 = fx.divide(fpx,2*SQRT_DIG.intValue(),RoundingMode.HALF_DOWN);
xn1 = xn.add(xn1.negate());
BigDecimal currentSquare = xn1.pow(2);
BigDecimal currentPrecision = currentSquare.subtract(c);
currentPrecision = currentPrecision.abs();
if (currentPrecision.compareTo(precision) <= -1){
return xn1;
}
return sqrtNewtonRaphson(c, xn1, precision);
}
/**
* Uses Newton Raphson to compute the square root of a BigDecimal.
*
* @author Luciano Culacciatti
* @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal
*/
public static BigDecimal bigSqrt(BigDecimal c){
return sqrtNewtonRaphson(c,new BigDecimal(1),new BigDecimal(1).divide(SQRT_PRE));
}
barwnikkの答えを必ずチェックしてください。それはより簡潔で、一見良いかそれ以上の精度を提供します。
整数の平方根のみを求める必要がある場合は、これら 2 つの方法を使用できます。
Newton 法- 1000 桁でも非常に高速 BigInteger:
public static BigInteger sqrtN(BigInteger in) {
final BigInteger TWO = BigInteger.valueOf(2);
int c;
// Significantly speed-up algorithm by proper select of initial approximation
// As square root has 2 times less digits as original value
// we can start with 2^(length of N1 / 2)
BigInteger n0 = TWO.pow(in.bitLength() / 2);
// Value of approximate value on previous step
BigInteger np = in;
do {
// next approximation step: n0 = (n0 + in/n0) / 2
n0 = n0.add(in.divide(n0)).divide(TWO);
// compare current approximation with previous step
c = np.compareTo(n0);
// save value as previous approximation
np = n0;
// finish when previous step is equal to current
} while (c != 0);
return n0;
}
二分法- ニュートン法より最大 50 倍遅い - 教育目的でのみ使用:
public static BigInteger sqrtD(final BigInteger in) {
final BigInteger TWO = BigInteger.valueOf(2);
BigInteger n0, n1, m, m2, l;
int c;
// Init segment
n0 = BigInteger.ZERO;
n1 = in;
do {
// length of segment
l = n1.subtract(n0);
// middle of segment
m = l.divide(TWO).add(n0);
// compare m^2 with in
c = m.pow(2).compareTo(in);
if (c == 0) {
// exact value is found
break;
} else if (c > 0) {
// m^2 is bigger than in - choose left half of segment
n1 = m;
} else {
// m^2 is smaller than in - choose right half of segment
n0 = m;
}
// finish if length of segment is 1, i.e. approximate value is found
} while (l.compareTo(BigInteger.ONE) > 0);
return m;
}
これは非常に正確で高速なソリューションです。これは、私のBigIntSqRoot ソリューションと次の観察に基づいています: A^2B の平方根 - A に B の根を掛けたものです。この方法を使用すると、2 の平方根の最初の 1000 桁を簡単に計算できます。
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372352885092648612494977154218334204285686060146824720771435854874155657069677653720226485447015858801620758474922657226002085584466521458398893944370926591800311388246468157082630100594858704003186480342194897278290641045072636881313739855256117322040245091227700226941127573627280495738108967504018369868368450725799364729060762996941380475654823728997180326802474420629269124859052181004459842150591120249441341728531478105803603371077309182869314710171111683916581726889419758716582152128229518488472
というわけでソースコードはこちら
public class BigIntSqRoot {
private static final int PRECISION = 10000;
private static BigInteger multiplier = BigInteger.valueOf(10).pow(PRECISION * 2);
private static BigDecimal root = BigDecimal.valueOf(10).pow(PRECISION);
private static BigInteger two = BigInteger.valueOf(2L);
public static BigDecimal bigDecimalSqRootFloor(BigInteger x)
throws IllegalArgumentException {
BigInteger result = bigIntSqRootFloor(x.multiply(multiplier));
//noinspection BigDecimalMethodWithoutRoundingCalled
return new BigDecimal(result).divide(root);
}
public static BigInteger bigIntSqRootFloor(BigInteger x)
throws IllegalArgumentException {
if (checkTrivial(x)) {
return x;
}
if (x.bitLength() < 64) { // Can be cast to long
double sqrt = Math.sqrt(x.longValue());
return BigInteger.valueOf(Math.round(sqrt));
}
// starting with y = x / 2 avoids magnitude issues with x squared
BigInteger y = x.divide(two);
BigInteger value = x.divide(y);
while (y.compareTo(value) > 0) {
y = value.add(y).divide(two);
value = x.divide(y);
}
return y;
}
public static BigInteger bigIntSqRootCeil(BigInteger x)
throws IllegalArgumentException {
BigInteger y = bigIntSqRootFloor(x);
if (x.compareTo(y.multiply(y)) == 0) {
return y;
}
return y.add(BigInteger.ONE);
}
private static boolean checkTrivial(BigInteger x) {
if (x == null) {
throw new NullPointerException("x can't be null");
}
if (x.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException("Negative argument.");
}
return x.equals(BigInteger.ZERO) || x.equals(BigInteger.ONE);
}
}
double (大きなスケールの BigDecimal) に収まらない桁数の数値の平方根を計算する場合:
ウィキペディアに平方根の計算に関する記事があります: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
これは私の実装です:
public static BigDecimal sqrt(BigDecimal in, int scale){
BigDecimal sqrt = new BigDecimal(1);
sqrt.setScale(scale + 3, RoundingMode.FLOOR);
BigDecimal store = new BigDecimal(in.toString());
boolean first = true;
do{
if (!first){
store = new BigDecimal(sqrt.toString());
}
else first = false;
store.setScale(scale + 3, RoundingMode.FLOOR);
sqrt = in.divide(store, scale + 3, RoundingMode.FLOOR).add(store).divide(
BigDecimal.valueOf(2), scale + 3, RoundingMode.FLOOR);
}while (!store.equals(sqrt));
return sqrt.setScale(scale, RoundingMode.FLOOR);
}
setScale(scale + 3, RoundingMode.Floor)
計算しすぎると精度が上がるからです。RoundingMode.Floor
数値を切り捨て、RoundingMode.HALF_UP
通常の丸めを行います。
前に述べたように: 答えの精度を気にせず、15 番目以降の有効な数字のみを乱数で生成したい場合、なぜ BigDecimal を使用するのでしょうか?
浮動小数点 BigDecimals でトリックを実行するメソッドのコードを次に示します。
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
public BigDecimal bigSqrt(BigDecimal d, MathContext mc) {
// 1. Make sure argument is non-negative and treat Argument 0
int sign = d.signum();
if(sign == -1)
throw new ArithmeticException("Invalid (negative) argument of sqrt: "+d);
else if(sign == 0)
return BigDecimal.ZERO;
// 2. Scaling:
// factorize d = scaledD * scaleFactorD
// = scaledD * (sqrtApproxD * sqrtApproxD)
// such that scalefactorD is easy to take the square root
// you use scale and bitlength for this, and if odd add or subtract a one
BigInteger bigI=d.unscaledValue();
int bigS=d.scale();
int bigL = bigI.bitLength();
BigInteger scaleFactorI;
BigInteger sqrtApproxI;
if ((bigL%2==0)){
scaleFactorI=BigInteger.ONE.shiftLeft(bigL);
sqrtApproxI=BigInteger.ONE.shiftLeft(bigL/2);
}else{
scaleFactorI=BigInteger.ONE.shiftLeft(bigL-1);
sqrtApproxI=BigInteger.ONE.shiftLeft((bigL-1)/2 );
}
BigDecimal scaleFactorD;
BigDecimal sqrtApproxD;
if ((bigS%2==0)){
scaleFactorD=new BigDecimal(scaleFactorI,bigS);
sqrtApproxD=new BigDecimal(sqrtApproxI,bigS/2);
}else{
scaleFactorD=new BigDecimal(scaleFactorI,bigS+1);
sqrtApproxD=new BigDecimal(sqrtApproxI,(bigS+1)/2);
}
BigDecimal scaledD=d.divide(scaleFactorD);
// 3. This is the core algorithm:
// Newton-Ralpson for scaledD : In case of f(x)=sqrt(x),
// Heron's Method or Babylonian Method are other names for the same thing.
// Since this is scaled we can be sure that scaledD.doubleValue() works
// for the start value of the iteration without overflow or underflow
System.out.println("ScaledD="+scaledD);
double dbl = scaledD.doubleValue();
double sqrtDbl = Math.sqrt(dbl);
BigDecimal a = new BigDecimal(sqrtDbl, mc);
BigDecimal HALF=BigDecimal.ONE.divide(BigDecimal.ONE.add(BigDecimal.ONE));
BigDecimal h = new BigDecimal("0", mc);
// when to stop iterating? You start with ~15 digits of precision, and Newton-Ralphson is quadratic
// in approximation speed, so in roundabout doubles the number of valid digits with each step.
// This fmay be safer than testing a BigDecifmal against zero.
int prec = mc.getPrecision();
int start = 15;
do {
h = scaledD.divide(a, mc);
a = a.add(h).multiply(HALF);
start *= 2;
} while (start <= prec);
// 3. Return rescaled answer. sqrt(d)= sqrt(scaledD)*sqrtApproxD :
return (a.multiply(sqrtApproxD));
}
テストとして、平方根を繰り返すよりも数回平方を繰り返してみて、開始した場所からどれだけ近いかを確認してください。
平方根を取るだけでなく、すべての BigDecimal の整数以下のすべての根を計算するアルゴリズムを思いつきました。検索アルゴリズムを実行しないという大きな利点があるため、実行時間は 0.1ms ~ 1ms と非常に高速です。
しかし、速度と汎用性が得られますが、精度に欠けており、平均して 5 桁の正しい数字で、5 桁目の偏差は 3 です。(100 万個の乱数とルートでテスト済み)、テストは非常に高いルートで実行されたため、ルートを 10 未満に保つと、もう少し精度が期待できます。
結果は 64 ビットの精度しか保持せず、残りの数値はゼロであるため、非常に高いレベルの精度が必要な場合は、この関数を使用しないでください。
非常に大きな数と非常に大きな根を処理するように作られていますが、非常に小さな数ではありません。
public static BigDecimal nrt(BigDecimal bd,int root) {
//if number is smaller then double_max_value it's faster to use the usual math
//library
if(bd.compareTo(BigDecimal.valueOf(Double.MAX_VALUE)) < 0)
return new BigDecimal( Math.pow(bd.doubleValue(), 1D / (double)root ));
BigDecimal in = bd;
int digits = bd.precision() - bd.scale() -1; //take digits to get the numbers power of ten
in = in.scaleByPowerOfTen (- (digits - digits%root) ); //scale down to the lowest number with it's power of ten mod root is the same as initial number
if(in.compareTo(BigDecimal.valueOf( Double.MAX_VALUE) ) > 0) { //if down scaled value is bigger then double_max_value, we find the answer by splitting the roots into factors and calculate them seperately and find the final result by multiplying the subresults
int highestDenominator = highestDenominator(root);
if(highestDenominator != 1) {
return nrt( nrt(bd, root / highestDenominator),highestDenominator); // for example turns 1^(1/25) 1^(1/5)^1(1/5)
}
//hitting this point makes the runtime about 5-10 times higher,
//but the alternative is crashing
else return nrt(bd,root+1) //+1 to make the root even so it can be broken further down into factors
.add(nrt(bd,root-1),MathContext.DECIMAL128) //add the -1 root and take the average to deal with the inaccuracy created by this
.divide(BigDecimal.valueOf(2),MathContext.DECIMAL128);
}
double downScaledResult = Math.pow(in.doubleValue(), 1D /root); //do the calculation on the downscaled value
BigDecimal BDResult =new BigDecimal(downScaledResult) // scale back up by the downscaled value divided by root
.scaleByPowerOfTen( (digits - digits % root) / root );
return BDResult;
}
private static int highestDenominator(int n) {
for(int i = n-1; i>1;i--) {
if(n % i == 0) {
return i;
}
}
return 1;
}
基本的に、平方根を実行しているときに x^0.5 を (x/100)^0,5 * 10 に変更できるという数学的特性を使用して機能するため、基数を 100 で割り、累乗して 10 を掛けます。
一般化すると x^(1/n) = (x / 10^n) ^ (1/n) * 10 となります。
したがって、立方根の場合は底を 10^3 で割る必要があり、4 乗根の場合は 10^4 で割る必要があります。
アルゴリズムは、その関数を使用して、入力を数学ライブラリが処理できるものに縮小し、入力がどれだけ縮小されたかに基づいて再び拡大します。
また、入力を十分に縮小できないいくつかのエッジ ケースも処理します。これらのエッジ ケースは、多くの精度の問題を追加します。
Java API には何もないので、double が十分に正確でない場合 (そうでない場合、BigDecimal を使用する必要はありません)、以下のコードのようなものが必要です)。
import java.math.BigDecimal;
public class BigDSqrt {
public static BigDecimal sqrt(BigDecimal n, int s) {
BigDecimal TWO = BigDecimal.valueOf(2);
// Obtain the first approximation
BigDecimal x = n
.divide(BigDecimal.valueOf(3), s, BigDecimal.ROUND_DOWN);
BigDecimal lastX = BigDecimal.valueOf(0);
// Proceed through 50 iterations
for (int i = 0; i < 50; i++) {
x = n.add(x.multiply(x)).divide(x.multiply(TWO), s,
BigDecimal.ROUND_DOWN);
if (x.compareTo(lastX) == 0)
break;
lastX = x;
}
return x;
}
}
BigDecimal.valueOf(Math.sqrt(myBigDecimal.doubleValue()));