aboutsummaryrefslogtreecommitdiff
path: root/std/math/complex/sqrt.zig
blob: afda69f7c9e4b6cc03f0373c78ff42ff575c31b4 (plain)
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
const std = @import("../../index.zig");
const debug = std.debug;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;

// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))
pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
    const T = @typeOf(z.re);

    return switch (T) {
        f32 => sqrt32(z),
        f64 => sqrt64(z),
        else => @compileError("sqrt not implemented for " ++ @typeName(z)),
    };
}

fn sqrt32(z: &const Complex(f32)) Complex(f32) {
    const x = z.re;
    const y = z.im;

    if (x == 0 and y == 0) {
        return Complex(f32).new(0, y);
    }
    if (math.isInf(y)) {
        return Complex(f32).new(math.inf(f32), y);
    }
    if (math.isNan(x)) {
        // raise invalid if y is not nan
        const t = (y - y) / (y - y);
        return Complex(f32).new(x, t);
    }
    if (math.isInf(x)) {
        // sqrt(inf + i nan)    = inf + nan i
        // sqrt(inf + iy)       = inf + i0
        // sqrt(-inf + i nan)   = nan +- inf i
        // sqrt(-inf + iy)      = 0 + inf i
        if (math.signbit(x)) {
            return Complex(f32).new(math.fabs(x - y), math.copysign(f32, x, y));
        } else {
            return Complex(f32).new(x, math.copysign(f32, y - y, y));
        }
    }

    // y = nan special case is handled fine below

    // double-precision avoids overflow with correct rounding.
    const dx = f64(x);
    const dy = f64(y);

    if (dx >= 0) {
        const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
        return Complex(f32).new(f32(t), f32(dy / (2.0 * t)));
    } else {
        const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
        return Complex(f32).new(f32(math.fabs(y) / (2.0 * t)), f32(math.copysign(f64, t, y)));
    }
}

fn sqrt64(z: &const Complex(f64)) Complex(f64) {
    // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
    const threshold = 0x1.a827999fcef32p+1022;

    var x = z.re;
    var y = z.im;

    if (x == 0 and y == 0) {
        return Complex(f64).new(0, y);
    }
    if (math.isInf(y)) {
        return Complex(f64).new(math.inf(f64), y);
    }
    if (math.isNan(x)) {
        // raise invalid if y is not nan
        const t = (y - y) / (y - y);
        return Complex(f64).new(x, t);
    }
    if (math.isInf(x)) {
        // sqrt(inf + i nan)    = inf + nan i
        // sqrt(inf + iy)       = inf + i0
        // sqrt(-inf + i nan)   = nan +- inf i
        // sqrt(-inf + iy)      = 0 + inf i
        if (math.signbit(x)) {
            return Complex(f64).new(math.fabs(x - y), math.copysign(f64, x, y));
        } else {
            return Complex(f64).new(x, math.copysign(f64, y - y, y));
        }
    }

    // y = nan special case is handled fine below

    // scale to avoid overflow
    var scale = false;
    if (math.fabs(x) >= threshold or math.fabs(y) >= threshold) {
        x *= 0.25;
        y *= 0.25;
        scale = true;
    }

    var result: Complex(f64) = undefined;
    if (x >= 0) {
        const t = math.sqrt((x + math.hypot(f64, x, y)) * 0.5);
        result = Complex(f64).new(t, y / (2.0 * t));
    } else {
        const t = math.sqrt((-x + math.hypot(f64, x, y)) * 0.5);
        result = Complex(f64).new(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y));
    }

    if (scale) {
        result.re *= 2;
        result.im *= 2;
    }

    return result;
}

const epsilon = 0.0001;

test "complex.csqrt32" {
    const a = Complex(f32).new(5, 3);
    const c = sqrt(a);

    debug.assert(math.approxEq(f32, c.re, 2.327117, epsilon));
    debug.assert(math.approxEq(f32, c.im, 0.644574, epsilon));
}

test "complex.csqrt64" {
    const a = Complex(f64).new(5, 3);
    const c = sqrt(a);

    debug.assert(math.approxEq(f64, c.re, 2.3271175190399496, epsilon));
    debug.assert(math.approxEq(f64, c.im, 0.6445742373246469, epsilon));
}