scirs2-special 0.4.1

Special functions module for SciRS2 (scirs2-special)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Core arithmetic operations for double-double numbers.
//!
//! Implements the TwoSum (Knuth–Moller) and TwoProduct (Dekker) error-free
//! transformations, and the standard `Add`, `Sub`, `Mul`, `Div` operator
//! traits for [`DoubleDouble`].

use super::DoubleDouble;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

// ---------------------------------------------------------------------------
// Error-free transformations
// ---------------------------------------------------------------------------

/// TwoSum: compute `s + e = a + b` exactly, where `s` is the floating-point
/// sum and `e` is the rounding error.
///
/// Requires IEEE 754 arithmetic with round-to-nearest.
#[inline]
pub fn two_sum(a: f64, b: f64) -> (f64, f64) {
    let s = a + b;
    let v = s - a;
    let e = (a - (s - v)) + (b - v);
    (s, e)
}

/// QuickTwoSum: a fast variant of TwoSum valid when `|a| >= |b|`.
#[inline]
pub fn quick_two_sum(a: f64, b: f64) -> (f64, f64) {
    let s = a + b;
    let e = b - (s - a);
    (s, e)
}

/// TwoDiff: compute `s + e = a - b` exactly.
#[inline]
pub fn two_diff(a: f64, b: f64) -> (f64, f64) {
    let s = a - b;
    let v = s - a;
    let e = (a - (s - v)) - (b + v);
    (s, e)
}

/// Split a f64 into high and low parts for Dekker multiplication.
/// Uses the Veltkamp split with `s = 2^27 + 1`.
#[inline]
fn split(a: f64) -> (f64, f64) {
    const SPLITTER: f64 = 134_217_729.0; // 2^27 + 1
    let t = SPLITTER * a;
    let a_hi = t - (t - a);
    let a_lo = a - a_hi;
    (a_hi, a_lo)
}

/// TwoProduct: compute `p + e = a * b` exactly using Dekker splitting.
///
/// If the platform has FMA, that single instruction gives the error directly;
/// otherwise we fall back to Dekker's algorithm.
#[inline]
pub fn two_product(a: f64, b: f64) -> (f64, f64) {
    let p = a * b;
    // Try FMA path: e = fma(a, b, -p)
    let e = a.mul_add(b, -p);
    (p, e)
}

// ---------------------------------------------------------------------------
// Negation & absolute value
// ---------------------------------------------------------------------------

impl Neg for DoubleDouble {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self {
        Self::new(-self.hi, -self.lo)
    }
}

impl DoubleDouble {
    /// Absolute value.
    #[inline]
    pub fn abs(self) -> Self {
        if self.is_negative() {
            -self
        } else {
            self
        }
    }

    /// Optimised squaring (cheaper than general multiply).
    #[inline]
    pub fn sqr(self) -> Self {
        let (p1, p2) = two_product(self.hi, self.hi);
        let p2 = p2 + 2.0 * self.hi * self.lo;
        let (s, e) = quick_two_sum(p1, p2);
        Self::new(s, e)
    }

    /// Reciprocal `1 / self`, computed via Newton iteration.
    pub fn recip(self) -> Self {
        if self.is_zero() {
            return Self::new(f64::INFINITY, 0.0);
        }
        // Initial approximation
        let x0 = 1.0 / self.hi;
        // Newton: x_{n+1} = x_n + x_n * (1 - a * x_n)
        let dd_x = Self::from(x0);
        let r = Self::from(1.0) - self * dd_x;
        let dd_x = dd_x + dd_x * r;
        // Second iteration for full DD precision
        let r = Self::from(1.0) - self * dd_x;
        dd_x + dd_x * r
    }
}

// ---------------------------------------------------------------------------
// DD + DD
// ---------------------------------------------------------------------------

impl Add for DoubleDouble {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self {
        let (s1, e1) = two_sum(self.hi, rhs.hi);
        let (s2, e2) = two_sum(self.lo, rhs.lo);
        let e1 = e1 + s2;
        let (s1, e1) = quick_two_sum(s1, e1);
        let e1 = e1 + e2;
        let (s1, e1) = quick_two_sum(s1, e1);
        Self::new(s1, e1)
    }
}

impl AddAssign for DoubleDouble {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

// ---------------------------------------------------------------------------
// DD - DD
// ---------------------------------------------------------------------------

impl Sub for DoubleDouble {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self {
        self + (-rhs)
    }
}

impl SubAssign for DoubleDouble {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

// ---------------------------------------------------------------------------
// DD * DD
// ---------------------------------------------------------------------------

impl Mul for DoubleDouble {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: Self) -> Self {
        // Hida / Li / Bailey QD-style multiplication:
        // (a.hi + a.lo) * (b.hi + b.lo)
        //  = a.hi*b.hi + a.hi*b.lo + a.lo*b.hi + a.lo*b.lo
        //
        // TwoProduct gives exact a.hi*b.hi = p1 + p2
        let (p1, p2) = two_product(self.hi, rhs.hi);
        // Cross terms (only need the leading f64 of each)
        let p2 = p2 + (self.hi * rhs.lo + self.lo * rhs.hi);
        // Renormalize
        let (s, e) = quick_two_sum(p1, p2);
        Self::new(s, e)
    }
}

impl MulAssign for DoubleDouble {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

// ---------------------------------------------------------------------------
// DD / DD
// ---------------------------------------------------------------------------

impl Div for DoubleDouble {
    type Output = Self;

    fn div(self, rhs: Self) -> Self {
        if rhs.is_zero() {
            if self.is_zero() {
                return Self::new(f64::NAN, 0.0);
            }
            let sign = if self.is_negative() != rhs.is_negative() {
                -1.0
            } else {
                1.0
            };
            return Self::new(sign * f64::INFINITY, 0.0);
        }
        // Compute quotient via long-division style:
        //   q1 = a.hi / b.hi  (approx quotient)
        //   r  = a - b * q1   (remainder in DD)
        //   q2 = r.hi / b.hi  (correction)
        let q1 = self.hi / rhs.hi;
        let r = self - rhs * Self::from(q1);
        let q2 = r.hi / rhs.hi;
        let r = r - rhs * Self::from(q2);
        let q3 = r.hi / rhs.hi;
        let (s, e) = quick_two_sum(q1, q2);
        Self::new(s, e) + Self::from(q3)
    }
}

impl DivAssign for DoubleDouble {
    #[inline]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

// ---------------------------------------------------------------------------
// Mixed operations: DD op f64
// ---------------------------------------------------------------------------

impl Add<f64> for DoubleDouble {
    type Output = Self;

    #[inline]
    fn add(self, rhs: f64) -> Self {
        self + Self::from(rhs)
    }
}

impl Sub<f64> for DoubleDouble {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: f64) -> Self {
        self - Self::from(rhs)
    }
}

impl Mul<f64> for DoubleDouble {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: f64) -> Self {
        let (p1, p2) = two_product(self.hi, rhs);
        let p2 = p2 + self.lo * rhs;
        let (s, e) = quick_two_sum(p1, p2);
        Self::new(s, e)
    }
}

impl Div<f64> for DoubleDouble {
    type Output = Self;

    #[inline]
    fn div(self, rhs: f64) -> Self {
        self / Self::from(rhs)
    }
}

// ---------------------------------------------------------------------------
// f64 op DD (commutative convenience)
// ---------------------------------------------------------------------------

impl Add<DoubleDouble> for f64 {
    type Output = DoubleDouble;

    #[inline]
    fn add(self, rhs: DoubleDouble) -> DoubleDouble {
        rhs + self
    }
}

impl Sub<DoubleDouble> for f64 {
    type Output = DoubleDouble;

    #[inline]
    fn sub(self, rhs: DoubleDouble) -> DoubleDouble {
        DoubleDouble::from(self) - rhs
    }
}

impl Mul<DoubleDouble> for f64 {
    type Output = DoubleDouble;

    #[inline]
    fn mul(self, rhs: DoubleDouble) -> DoubleDouble {
        rhs * self
    }
}

impl Div<DoubleDouble> for f64 {
    type Output = DoubleDouble;

    #[inline]
    fn div(self, rhs: DoubleDouble) -> DoubleDouble {
        DoubleDouble::from(self) / rhs
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::double_double::{DD_E, DD_ONE, DD_PI, DD_TWO, DD_ZERO};

    #[test]
    fn test_two_sum_exactness() {
        // a + b = s + e  exactly
        let a = 1.0;
        let b = 1e-20;
        let (s, e) = two_sum(a, b);
        // Reconstruct
        let reconstructed = s + e;
        assert!(
            (reconstructed - (a + b)).abs() < 1e-35,
            "TwoSum not exact: diff = {:e}",
            reconstructed - (a + b)
        );
    }

    #[test]
    fn test_two_product_exactness() {
        let a = 1.0 + 1e-10;
        let b = 1.0 - 1e-10;
        let (p, e) = two_product(a, b);
        // p + e should equal a*b exactly (within representation)
        let reconstructed = p + e;
        let exact = a * b;
        assert!(
            (reconstructed - exact).abs() < 1e-30,
            "TwoProduct error: {:e}",
            reconstructed - exact
        );
    }

    #[test]
    fn test_add_sub_roundtrip() {
        let a = DD_PI;
        let b = DD_E;
        let c = a + b;
        let d = c - b;
        // d should equal a to ~31 digits
        let diff = (d - a).abs();
        assert!(
            diff.to_f64() < 1e-30,
            "add/sub roundtrip error: {:e}",
            diff.to_f64()
        );
    }

    #[test]
    fn test_mul_div_roundtrip() {
        let a = DD_PI;
        let b = DD_E;
        let c = a * b;
        let d = c / b;
        let diff = (d - a).abs();
        assert!(
            diff.to_f64() < 1e-29,
            "mul/div roundtrip error: {:e}",
            diff.to_f64()
        );
    }

    #[test]
    fn test_commutativity_add() {
        let a = DD_PI;
        let b = DD_E;
        let ab = a + b;
        let ba = b + a;
        assert_eq!(ab, ba);
    }

    #[test]
    fn test_commutativity_mul() {
        let a = DD_PI;
        let b = DD_E;
        let ab = a * b;
        let ba = b * a;
        // May differ by a few ulps due to ordering; check closeness
        let diff = (ab - ba).abs();
        assert!(
            diff.to_f64() < 1e-30,
            "mul commutativity error: {:e}",
            diff.to_f64()
        );
    }

    #[test]
    fn test_negation() {
        let a = DD_PI;
        let neg_a = -a;
        let sum = a + neg_a;
        assert!(sum.abs().to_f64() < 1e-31);
    }

    #[test]
    fn test_sqr() {
        let a = DD_TWO;
        let sq = a.sqr();
        let diff = (sq - DoubleDouble::from(4.0)).abs();
        assert!(diff.to_f64() < 1e-30);
    }

    #[test]
    fn test_recip() {
        let a = DD_TWO;
        let r = a.recip();
        let diff = (r - DoubleDouble::from(0.5)).abs();
        assert!(diff.to_f64() < 1e-30);
    }

    #[test]
    fn test_div_by_zero() {
        let r = DD_ONE / DD_ZERO;
        assert!(r.hi.is_infinite());
        assert!(r.hi > 0.0);

        let r2 = DD_ZERO / DD_ZERO;
        assert!(r2.hi.is_nan());
    }

    #[test]
    fn test_mixed_dd_f64() {
        let a = DD_PI;
        let b = a + 1.0;
        let c = b - 1.0;
        let diff = (c - a).abs();
        assert!(diff.to_f64() < 1e-30);

        let d = a * 2.0;
        let e = d / 2.0;
        let diff2 = (e - a).abs();
        assert!(diff2.to_f64() < 1e-30);
    }

    #[test]
    fn test_f64_op_dd() {
        let a = 2.0 + DD_PI;
        let b = DD_PI + 2.0;
        assert_eq!(a, b);

        let c = 10.0 - DD_PI;
        assert!(c.to_f64() > 6.0);

        let d = 2.0 * DD_PI;
        let e = DD_PI * 2.0;
        let diff = (d - e).abs();
        assert!(diff.to_f64() < 1e-30);
    }
}