gonum.org/v1/gonum@v0.14.0/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  //
    36  //	K(m) = B(m) + D(m),
    37  //	E(m) = B(m) + (1-m)D(m).
    38  func TestCompleteBD(t *testing.T) {
    39  	t.Parallel()
    40  	const tol = 1.0e-14
    41  
    42  	for m := 1; m <= 9999; m++ {
    43  		mf := float64(m) / 10000
    44  		B, D := CompleteB(mf), CompleteD(mf)
    45  		K, E := CompleteK(mf), CompleteE(mf)
    46  		difference1 := math.Abs(K - (B + D))
    47  		difference2 := math.Abs(E - (B + (1-mf)*D))
    48  		if difference1 > tol {
    49  			t.Fatalf("difference1 > tol: m=%v, difference1=%v, tol=%v", mf, difference1, tol)
    50  		}
    51  		if difference2 > tol {
    52  			t.Fatalf("difference2 > tol: m=%v, difference2=%v, tol=%v", mf, difference2, tol)
    53  		}
    54  	}
    55  }