SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
elliptic.c File Reference

Detailed Description

Functions for evaluating complete elliptic integrals of the first and second kind (K and E), as well as their total derivatives with respect to the modulus k.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file elliptic.c.

#include "mdb.h"
#include "mdb_thread.h"

Go to the source code of this file.

Functions

static double getCeiAccuracy (void)
 
void setCeiAccuracy (double newAccuracy)
 
double K_cei (double k)
 Compute the complete elliptic integral of the first kind, K(k).
 
double E_cei (double k)
 Compute the complete elliptic integral of the second kind, E(k).
 
double * KE_cei (double k, double *buffer)
 
double dK_cei (double k)
 Compute the total derivative dK/dk of the complete elliptic integral of the first kind.
 
double dE_cei (double k)
 Compute the total derivative dE/dk of the complete elliptic integral of the second kind.
 

Function Documentation

◆ dE_cei()

double dE_cei ( double k)

Compute the total derivative dE/dk of the complete elliptic integral of the second kind.

Uses K(k) and E(k) to determine the derivative with respect to k.

Parameters
[in]kThe modulus of the elliptic integral.
Returns
The value of dE/dk.

Definition at line 158 of file elliptic.c.

159{
160 double buffer[2];
161 KE_cei(k, buffer);
162 return (buffer[1] - buffer[0]) / k;
163}

◆ dK_cei()

double dK_cei ( double k)

Compute the total derivative dK/dk of the complete elliptic integral of the first kind.

Uses K(k) and E(k) to determine the derivative with respect to k.

Parameters
[in]kThe modulus of the elliptic integral.
Returns
The value of dK/dk.

Definition at line 144 of file elliptic.c.

144 {
145 double buffer[2];
146 KE_cei(k, buffer);
147 return (buffer[1] / (1 - k * k) - buffer[0]) / k;
148}

◆ E_cei()

double E_cei ( double k)

Compute the complete elliptic integral of the second kind, E(k).

E(k) = ∫_0^(π/2) √(1 - k² sin² θ) dθ

Parameters
[in]kThe modulus of the elliptic integral.
Returns
The value of E(k).

Definition at line 72 of file elliptic.c.

72 {
73 double a0, b0, c0, a1, b1, c1, K, sum, powerOf2;
74 double accuracy = getCeiAccuracy();
75
76 a0 = 1;
77 b0 = sqrt(1 - sqr(k));
78 c0 = k;
79 sum = sqr(c0);
80 powerOf2 = 1;
81
82 do {
83 /* do two steps of recurrence per pass in the loop */
84 a1 = (a0 + b0) / 2;
85 b1 = sqrt(a0 * b0);
86 c1 = (a0 - b0) / 2;
87 sum += sqr(c1) * (powerOf2 *= 2);
88 ;
89
90 a0 = (a1 + b1) / 2;
91 b0 = sqrt(a1 * b1);
92 c0 = (a1 - b1) / 2;
93 sum += sqr(c0) * (powerOf2 *= 2);
94 } while (fabs(c0) > accuracy);
95
96 K = PI / (2 * a0);
97 return K * (1 - sum / 2);
98}

◆ getCeiAccuracy()

static double getCeiAccuracy ( void )
static

Definition at line 23 of file elliptic.c.

23 {
24 double accuracy;
25 mdb_thread_lock(&cei_accuracy_lock);
26 accuracy = ceiAccuracy;
27 mdb_thread_unlock(&cei_accuracy_lock);
28 return accuracy;
29}

◆ K_cei()

double K_cei ( double k)

Compute the complete elliptic integral of the first kind, K(k).

K(k) = ∫_0^(π/2) dθ / √(1 - k² sin² θ)

Parameters
[in]kThe modulus of the elliptic integral.
Returns
The value of K(k).

Definition at line 45 of file elliptic.c.

45 {
46 double a0, b0, c0, a1, b1;
47 double accuracy = getCeiAccuracy();
48
49 a0 = 1;
50 b0 = sqrt(1 - sqr(k));
51 c0 = k;
52
53 do {
54 /* do two steps of recurrence per pass in the loop */
55 a1 = (a0 + b0) / 2;
56 b1 = sqrt(a0 * b0);
57 a0 = (a1 + b1) / 2;
58 b0 = sqrt(a1 * b1);
59 c0 = (a1 - b1) / 2;
60 } while (fabs(c0) > accuracy);
61 return PI / (2 * a0);
62}

◆ KE_cei()

double * KE_cei ( double k,
double * buffer )

Definition at line 100 of file elliptic.c.

100 {
101 double a0, b0, c0, a1, b1, c1, K, sum, powerOf2;
102 double accuracy = getCeiAccuracy();
103
104 if (!buffer)
105 buffer = tmalloc(sizeof(*buffer) * 2);
106
107 a0 = 1;
108 b0 = sqrt(1 - sqr(k));
109 c0 = k;
110 sum = sqr(c0);
111 powerOf2 = 1;
112
113 do {
114 /* do two steps of recurrence per pass in the loop */
115 a1 = (a0 + b0) / 2;
116 b1 = sqrt(a0 * b0);
117 c1 = (a0 - b0) / 2;
118 sum += sqr(c1) * (powerOf2 *= 2);
119 ;
120
121 a0 = (a1 + b1) / 2;
122 b0 = sqrt(a1 * b1);
123 c0 = (a1 - b1) / 2;
124 sum += sqr(c0) * (powerOf2 *= 2);
125 } while (fabs(c0) > accuracy);
126
127 buffer[0] = K = PI / (2 * a0);
128 buffer[1] = K * (1 - sum / 2);
129 return buffer;
130}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:65

◆ setCeiAccuracy()

void setCeiAccuracy ( double newAccuracy)

Definition at line 31 of file elliptic.c.

31 {
32 mdb_thread_lock(&cei_accuracy_lock);
33 ceiAccuracy = newAccuracy;
34 mdb_thread_unlock(&cei_accuracy_lock);
35}