github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mathext/ell_complete_test.go (about)

     1  // Copyright ©2017 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mathext
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  )
    11  
    12  // TestCompleteKE checks if the Legendre's relation for m=0.0001(0.0001)0.9999
    13  // is satisfied with accuracy 1e-14.
    14  func TestCompleteKE(t *testing.T) {
    15  	t.Parallel()
    16  	const tol = 1.0e-14
    17  
    18  	for m := 1; m <= 9999; m++ {
    19  		mf := float64(m) / 10000
    20  		mp := 1 - mf
    21  		K, Kp := CompleteK(mf), CompleteK(mp)
    22  		E, Ep := CompleteE(mf), CompleteE(mp)
    23  		legendre := math.Abs(E*Kp + Ep*K - K*Kp - math.Pi/2)
    24  		if legendre > tol {
    25  			t.Fatalf("legendre > tol: m=%v, legendre=%v, tol=%v", mf, legendre, tol)
    26  		}
    27  	}
    28  }
    29  
    30  // TestCompleteBD checks if the relations between two associate elliptic integrals B(m), D(m)
    31  // and more common Legendre's elliptic integrals K(m), E(m) are satisfied with accuracy 1e-14
    32  // for m=0.0001(0.0001)0.9999.
    33  //
    34  // K(m) and E(m) can be computed without cancellation problems as following:
    35  //	K(m) = B(m) + D(m),
    36  //	E(m) = B(m) + (1-m)D(m).
    37  func TestCompleteBD(t *testing.T) {
    38  	t.Parallel()
    39  	const tol = 1.0e-14
    40  
    41  	for m := 1; m <= 9999; m++ {
    42  		mf := float64(m) / 10000
    43  		B, D := CompleteB(mf), CompleteD(mf)
    44  		K, E := CompleteK(mf), CompleteE(mf)
    45  		difference1 := math.Abs(K - (B + D))
    46  		difference2 := math.Abs(E - (B + (1-mf)*D))
    47  		if difference1 > tol {
    48  			t.Fatalf("difference1 > tol: m=%v, difference1=%v, tol=%v", mf, difference1, tol)
    49  		}
    50  		if difference2 > tol {
    51  			t.Fatalf("difference2 > tol: m=%v, difference2=%v, tol=%v", mf, difference2, tol)
    52  		}
    53  	}
    54  }