forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmath.rs
More file actions
207 lines (175 loc) · 5.26 KB
/
cmath.rs
File metadata and controls
207 lines (175 loc) · 5.26 KB
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
// TODO: Keep track of rust-num/num-complex/issues/2. A common trait could help with duplication
// that exists between cmath and math.
pub(crate) use cmath::make_module;
#[pymodule]
mod cmath {
use crate::vm::{
function::{ArgIntoComplex, ArgIntoFloat, OptionalArg},
PyResult, VirtualMachine,
};
use num_complex::Complex64;
// Constants
#[pyattr]
use std::f64::consts::{E as e, PI as pi, TAU as tau};
#[pyattr]
use std::f64::{INFINITY as inf, NAN as nan};
#[pyattr(name = "infj")]
const INFJ: Complex64 = Complex64::new(0., std::f64::INFINITY);
#[pyattr(name = "nanj")]
const NANJ: Complex64 = Complex64::new(0., std::f64::NAN);
#[pyfunction]
fn phase(z: ArgIntoComplex) -> f64 {
z.arg()
}
#[pyfunction]
fn polar(x: ArgIntoComplex) -> (f64, f64) {
x.to_polar()
}
#[pyfunction]
fn rect(r: ArgIntoFloat, phi: ArgIntoFloat) -> Complex64 {
Complex64::from_polar(*r, *phi)
}
#[pyfunction]
fn isinf(z: ArgIntoComplex) -> bool {
let Complex64 { re, im } = *z;
re.is_infinite() || im.is_infinite()
}
#[pyfunction]
fn isfinite(z: ArgIntoComplex) -> bool {
z.is_finite()
}
#[pyfunction]
fn isnan(z: ArgIntoComplex) -> bool {
z.is_nan()
}
#[pyfunction]
fn exp(z: ArgIntoComplex, vm: &VirtualMachine) -> PyResult<Complex64> {
let z = *z;
result_or_overflow(z, z.exp(), vm)
}
#[pyfunction]
fn sqrt(z: ArgIntoComplex) -> Complex64 {
z.sqrt()
}
#[pyfunction]
fn sin(z: ArgIntoComplex) -> Complex64 {
z.sin()
}
#[pyfunction]
fn asin(z: ArgIntoComplex) -> Complex64 {
z.asin()
}
#[pyfunction]
fn cos(z: ArgIntoComplex) -> Complex64 {
z.cos()
}
#[pyfunction]
fn acos(z: ArgIntoComplex) -> Complex64 {
z.acos()
}
#[pyfunction]
fn log(z: ArgIntoComplex, base: OptionalArg<ArgIntoComplex>) -> Complex64 {
// TODO: Complex64.log with a negative base yields wrong results.
// Issue is with num_complex::Complex64 implementation of log
// which returns NaN when base is negative.
// log10(z) / log10(base) yields correct results but division
// doesn't handle pos/neg zero nicely. (i.e log(1, 0.5))
z.log(
base.into_option()
.map(|base| base.re)
.unwrap_or(std::f64::consts::E),
)
}
#[pyfunction]
fn log10(z: ArgIntoComplex) -> Complex64 {
z.log(10.0)
}
#[pyfunction]
fn acosh(z: ArgIntoComplex) -> Complex64 {
z.acosh()
}
#[pyfunction]
fn atan(z: ArgIntoComplex) -> Complex64 {
z.atan()
}
#[pyfunction]
fn atanh(z: ArgIntoComplex) -> Complex64 {
z.atanh()
}
#[pyfunction]
fn tan(z: ArgIntoComplex) -> Complex64 {
z.tan()
}
#[pyfunction]
fn tanh(z: ArgIntoComplex) -> Complex64 {
z.tanh()
}
#[pyfunction]
fn sinh(z: ArgIntoComplex) -> Complex64 {
z.sinh()
}
#[pyfunction]
fn cosh(z: ArgIntoComplex) -> Complex64 {
z.cosh()
}
#[pyfunction]
fn asinh(z: ArgIntoComplex) -> Complex64 {
z.asinh()
}
#[derive(FromArgs)]
struct IsCloseArgs {
#[pyarg(positional)]
a: ArgIntoComplex,
#[pyarg(positional)]
b: ArgIntoComplex,
#[pyarg(named, optional)]
rel_tol: OptionalArg<ArgIntoFloat>,
#[pyarg(named, optional)]
abs_tol: OptionalArg<ArgIntoFloat>,
}
#[pyfunction]
fn isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
let a = *args.a;
let b = *args.b;
let rel_tol = args.rel_tol.map_or(1e-09, Into::into);
let abs_tol = args.abs_tol.map_or(0.0, Into::into);
if rel_tol < 0.0 || abs_tol < 0.0 {
return Err(vm.new_value_error("tolerances must be non-negative".to_owned()));
}
if a == b {
/* short circuit exact equality -- needed to catch two infinities of
the same sign. And perhaps speeds things up a bit sometimes.
*/
return Ok(true);
}
/* This catches the case of two infinities of opposite sign, or
one infinity and one finite number. Two infinities of opposite
sign would otherwise have an infinite relative tolerance.
Two infinities of the same sign are caught by the equality check
above.
*/
if a.is_infinite() || b.is_infinite() {
return Ok(false);
}
let diff = c_abs(b - a);
Ok(diff <= (rel_tol * c_abs(b)) || (diff <= (rel_tol * c_abs(a))) || diff <= abs_tol)
}
#[inline]
fn c_abs(Complex64 { re, im }: Complex64) -> f64 {
re.hypot(im)
}
#[inline]
fn result_or_overflow(
value: Complex64,
result: Complex64,
vm: &VirtualMachine,
) -> PyResult<Complex64> {
if !result.is_finite() && value.is_finite() {
// CPython doesn't return `inf` when called with finite
// values, it raises OverflowError instead.
Err(vm.new_overflow_error("math range error".to_owned()))
} else {
Ok(result)
}
}
}