pymath 0.2.0

A binary representation compatible Rust implementation of Python's math library.
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
//! Exponential, logarithmic, and power functions.

use crate::Result;

// Exponential functions

/// Return e raised to the power of x.
#[inline]
pub fn exp(x: f64) -> Result<f64> {
    super::math_1(x, crate::m::exp, true)
}

/// Return 2 raised to the power of x.
#[inline]
pub fn exp2(x: f64) -> Result<f64> {
    super::math_1(x, crate::m::exp2, true)
}

/// Return exp(x) - 1.
#[inline]
pub fn expm1(x: f64) -> Result<f64> {
    super::math_1(x, crate::m::expm1, true)
}

// Logarithmic functions

/// m_log: log implementation
#[inline]
fn m_log(x: f64) -> f64 {
    if x.is_finite() {
        if x > 0.0 {
            crate::m::log(x)
        } else if x == 0.0 {
            f64::NEG_INFINITY // log(0) = -inf
        } else {
            f64::NAN // log(-ve) = nan
        }
    } else if x.is_nan() || x > 0.0 {
        x // log(nan) = nan, log(inf) = inf
    } else {
        f64::NAN // log(-inf) = nan
    }
}

/// m_log10: log10 implementation
#[inline]
fn m_log10(x: f64) -> f64 {
    if x.is_finite() {
        if x > 0.0 {
            crate::m::log10(x)
        } else if x == 0.0 {
            f64::NEG_INFINITY // log10(0) = -inf
        } else {
            f64::NAN // log10(-ve) = nan
        }
    } else if x.is_nan() || x > 0.0 {
        x // log10(nan) = nan, log10(inf) = inf
    } else {
        f64::NAN // log10(-inf) = nan
    }
}

/// m_log2: log2 implementation
#[inline]
fn m_log2(x: f64) -> f64 {
    if !x.is_finite() {
        if x.is_nan() || x > 0.0 {
            x // log2(nan) = nan, log2(+inf) = +inf
        } else {
            f64::NAN // log2(-inf) = nan
        }
    } else if x > 0.0 {
        crate::m::log2(x)
    } else if x == 0.0 {
        f64::NEG_INFINITY // log2(0) = -inf
    } else {
        f64::NAN // log2(-ve) = nan
    }
}

/// m_log1p: CPython's m_log1p implementation
#[inline]
fn m_log1p(x: f64) -> f64 {
    // For x > -1, log1p is well-defined
    // For x == -1, result is -inf
    // For x < -1, result is nan
    if x.is_nan() {
        return x;
    }
    crate::m::log1p(x)
}

/// Return the logarithm of x to the given base.
#[inline]
pub fn log(x: f64, base: Option<f64>) -> Result<f64> {
    let num = m_log(x);

    // math_1 logic: check for domain errors
    if num.is_nan() && !x.is_nan() {
        return Err(crate::Error::EDOM);
    }
    if num.is_infinite() && x.is_finite() {
        return Err(crate::Error::EDOM);
    }

    match base {
        None => Ok(num),
        Some(b) => {
            let den = m_log(b);
            if den.is_nan() && !b.is_nan() {
                return Err(crate::Error::EDOM);
            }
            if den.is_infinite() && b.is_finite() {
                return Err(crate::Error::EDOM);
            }
            // log(x, 1) -> division by zero.
            // CPython raises ZeroDivisionError here (via PyNumber_TrueDivide),
            // but we return EDOM since our error type has no ZeroDivisionError
            // variant. The caller (e.g. RustPython) may remap this if needed.
            if den == 0.0 {
                return Err(crate::Error::EDOM);
            }
            Ok(num / den)
        }
    }
}

/// math_1 for Rust functions (m_log10, m_log2, m_log1p)
#[inline]
fn math_1_fn(x: f64, func: fn(f64) -> f64, can_overflow: bool) -> Result<f64> {
    let r = func(x);
    if r.is_nan() && !x.is_nan() {
        return Err(crate::Error::EDOM);
    }
    if r.is_infinite() && x.is_finite() {
        return Err(if can_overflow {
            crate::Error::ERANGE
        } else {
            crate::Error::EDOM
        });
    }
    Ok(r)
}

/// Return the base-10 logarithm of x.
#[inline]
pub fn log10(x: f64) -> Result<f64> {
    math_1_fn(x, m_log10, false)
}

/// Return the base-2 logarithm of x.
#[inline]
pub fn log2(x: f64) -> Result<f64> {
    math_1_fn(x, m_log2, false)
}

/// Return the natural logarithm of 1+x (base e).
#[inline]
pub fn log1p(x: f64) -> Result<f64> {
    math_1_fn(x, m_log1p, false)
}

// Power functions

/// Return the square root of x.
#[inline]
pub fn sqrt(x: f64) -> Result<f64> {
    super::math_1(x, crate::m::sqrt, false)
}

/// Return the cube root of x.
#[inline]
pub fn cbrt(x: f64) -> Result<f64> {
    super::math_1(x, crate::m::cbrt, false)
}

/// Return x raised to the power y.
#[inline]
pub fn pow(x: f64, y: f64) -> Result<f64> {
    // Deal directly with IEEE specials, to cope with problems on various
    // platforms whose semantics don't exactly match C99
    if !x.is_finite() || !y.is_finite() {
        if x.is_nan() {
            // NaN**0 = 1
            return Ok(if y == 0.0 { 1.0 } else { x });
        } else if y.is_nan() {
            // 1**NaN = 1
            return Ok(if x == 1.0 { 1.0 } else { y });
        } else if x.is_infinite() {
            let odd_y = y.is_finite() && crate::m::fmod(y.abs(), 2.0) == 1.0;
            if y > 0.0 {
                return Ok(if odd_y { x } else { x.abs() });
            } else if y == 0.0 {
                return Ok(1.0);
            } else {
                // y < 0
                return Ok(if odd_y {
                    crate::m::copysign(0.0, x)
                } else {
                    0.0
                });
            }
        } else {
            // y is infinite
            debug_assert!(y.is_infinite());
            if x.abs() == 1.0 {
                return Ok(1.0);
            } else if y > 0.0 && x.abs() > 1.0 {
                return Ok(y);
            } else if y < 0.0 && x.abs() < 1.0 {
                return Ok(-y); // result is +inf
            } else {
                return Ok(0.0);
            }
        }
    }

    // Let libm handle finite**finite
    let r = crate::m::pow(x, y);

    // A NaN result should arise only from (-ve)**(finite non-integer);
    // in this case we want to raise ValueError.
    if !r.is_finite() {
        if r.is_nan() {
            return Err(crate::Error::EDOM);
        } else if r.is_infinite() {
            // An infinite result here arises either from:
            // (A) (+/-0.)**negative (-> divide-by-zero)
            // (B) overflow of x**y with x and y finite
            if x == 0.0 {
                return Err(crate::Error::EDOM);
            } else {
                return Err(crate::Error::ERANGE);
            }
        }
    }

    Ok(r)
}

// Tests

#[cfg(test)]
mod tests {
    use super::*;

    fn test_exp(x: f64) {
        crate::test::test_math_1(x, "exp", exp);
    }
    fn test_exp2(x: f64) {
        crate::test::test_math_1(x, "exp2", exp2);
    }
    fn test_expm1(x: f64) {
        crate::test::test_math_1(x, "expm1", expm1);
    }
    fn test_log_n(x: f64) {
        crate::test::test_math_1(x, "log", |x| log(x, None));
    }
    fn test_log(x: f64, base: f64) {
        crate::test::test_math_2(x, base, "log", |x, b| log(x, Some(b)));
    }
    fn test_log10(x: f64) {
        crate::test::test_math_1(x, "log10", log10);
    }
    fn test_log2(x: f64) {
        crate::test::test_math_1(x, "log2", log2);
    }
    fn test_log1p(x: f64) {
        crate::test::test_math_1(x, "log1p", log1p);
    }
    fn test_sqrt(x: f64) {
        crate::test::test_math_1(x, "sqrt", sqrt);
    }
    fn test_cbrt(x: f64) {
        crate::test::test_math_1(x, "cbrt", cbrt);
    }
    fn test_pow(x: f64, y: f64) {
        crate::test::test_math_2(x, y, "pow", pow);
    }

    proptest::proptest! {
        #[test]
        fn proptest_exp(x: f64) {
            test_exp(x);
        }

        #[test]
        fn proptest_exp2(x: f64) {
            test_exp2(x);
        }

        #[test]
        fn proptest_sqrt(x: f64) {
            test_sqrt(x);
        }

        #[test]
        fn proptest_cbrt(x: f64) {
            test_cbrt(x);
        }

        #[test]
        fn proptest_expm1(x: f64) {
            test_expm1(x);
        }

        #[test]
        fn proptest_log_n(x: f64) {
            test_log_n(x);
        }

        #[test]
        fn proptest_log(x: f64, base: f64) {
            test_log(x, base);
        }

        #[test]
        fn proptest_log10(x: f64) {
            test_log10(x);
        }

        #[test]
        fn proptest_log2(x: f64) {
            test_log2(x);
        }

        #[test]
        fn proptest_log1p(x: f64) {
            test_log1p(x);
        }

        #[test]
        fn proptest_pow(x: f64, y: f64) {
            test_pow(x, y);
        }
    }

    #[test]
    fn edgetest_exp() {
        for &x in crate::test::EDGE_VALUES {
            test_exp(x);
        }
    }

    #[test]
    fn edgetest_exp2() {
        for &x in crate::test::EDGE_VALUES {
            test_exp2(x);
        }
    }

    #[test]
    fn edgetest_expm1() {
        for &x in crate::test::EDGE_VALUES {
            test_expm1(x);
        }
    }

    #[test]
    fn edgetest_sqrt() {
        for &x in crate::test::EDGE_VALUES {
            test_sqrt(x);
        }
    }

    #[test]
    fn edgetest_cbrt() {
        for &x in crate::test::EDGE_VALUES {
            test_cbrt(x);
        }
    }

    #[test]
    fn edgetest_log_n() {
        for &x in crate::test::EDGE_VALUES {
            test_log_n(x);
        }
    }

    #[test]
    fn edgetest_log10() {
        for &x in crate::test::EDGE_VALUES {
            test_log10(x);
        }
    }

    #[test]
    fn edgetest_log2() {
        for &x in crate::test::EDGE_VALUES {
            test_log2(x);
        }
    }

    #[test]
    fn edgetest_log1p() {
        for &x in crate::test::EDGE_VALUES {
            test_log1p(x);
        }
    }

    #[test]
    fn edgetest_pow() {
        for &x in crate::test::EDGE_VALUES {
            for &y in crate::test::EDGE_VALUES {
                test_pow(x, y);
            }
        }
    }
}