uiua_parser 0.17.0-rc.2

Uiua parser implementation
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
//! The [`Complex`] type

use std::{f64::consts::E, fmt, ops::*};

use bytemuck::{Pod, Zeroable};
use serde::*;

/// Uiua's complex number type
#[derive(Debug, Clone, Copy, PartialOrd, Default, Serialize, Deserialize, Pod, Zeroable)]
#[serde(from = "(f64, f64)", into = "(f64, f64)")]
#[repr(C)]
pub struct Complex {
    /// The real part
    pub re: f64,
    /// The imaginary part
    pub im: f64,
}

impl From<(f64, f64)> for Complex {
    fn from((re, im): (f64, f64)) -> Self {
        Self { re, im }
    }
}

impl From<Complex> for (f64, f64) {
    fn from(c: Complex) -> Self {
        (c.re, c.im)
    }
}

impl PartialEq for Complex {
    fn eq(&self, other: &Self) -> bool {
        self.re == other.re && self.im == other.im
    }
}

impl Eq for Complex {}

impl Complex {
    /// The complex number 0 + 0i
    pub const ZERO: Self = Self { re: 0.0, im: 0.0 };
    /// The complex number 1 + 0i
    pub const ONE: Self = Self { re: 1.0, im: 0.0 };
    /// The complex number 0 + 1i
    pub const I: Self = Self { re: 0.0, im: 1.0 };
    /// Create a new complex number
    pub fn new(re: f64, im: f64) -> Self {
        Self { re, im }
    }
    /// Get the minimum of the real and imaginary parts of two complex numbers, ignoring NaN
    pub fn min(self, rhs: impl Into<Self>) -> Self {
        let rhs = rhs.into();
        Self {
            re: self.re.min(rhs.re),
            im: self.im.min(rhs.im),
        }
    }
    /// Get the maximum of the real and imaginary parts of two complex numbers, ignoring NaN
    pub fn max(self, rhs: impl Into<Self>) -> Self {
        let rhs = rhs.into();
        Self {
            re: self.re.max(rhs.re),
            im: self.im.max(rhs.im),
        }
    }
    /// Get the floor of the real and imaginary parts of a complex number
    pub fn floor(self) -> Self {
        Self {
            re: self.re.floor(),
            im: self.im.floor(),
        }
    }
    /// Get the ceiling of the real and imaginary parts of a complex number
    pub fn ceil(self) -> Self {
        Self {
            re: self.re.ceil(),
            im: self.im.ceil(),
        }
    }
    /// Round the real and imaginary parts of a complex number
    pub fn round(self) -> Self {
        Self {
            re: self.re.round(),
            im: self.im.round(),
        }
    }
    /// Get the absolute value of a complex number
    pub fn abs(self) -> f64 {
        // Do not use `self.re.hypot(self.im)` because it is slower, especially on WASM
        (self.re * self.re + self.im * self.im).sqrt()
    }
    /// Get the arctangent of a complex number
    pub fn atan2(self, x: impl Into<Self>) -> Complex {
        let y = self;
        let x = x.into();
        -Complex::I * ((x + Complex::I * y) / (y * y + x * x).sqrt()).ln()
    }
    /// Normalize a complex number
    pub fn normalize(self) -> Self {
        let len = self.abs();
        if len == 0.0 {
            Self::ZERO
        } else {
            self / len
        }
    }
    /// Calculate the principal value of the complex number
    pub fn arg(self) -> f64 {
        self.im.atan2(self.re)
    }
    /// Convert a complex number to polar coordinates
    pub fn to_polar(self) -> (f64, f64) {
        (self.abs(), self.arg())
    }
    /// Convert polar coordinates to a complex number
    pub fn from_polar(r: f64, theta: f64) -> Self {
        r * Self::new(theta.cos(), theta.sin())
    }
    /// Raise a complex number to a complex power
    pub fn powc(self, power: impl Into<Self>) -> Self {
        let power = power.into();
        if power.im == 0.0 {
            return self.powf(power.re);
        }
        let (r, theta) = self.to_polar();
        ((r.ln() + Self::I * theta) * power).exp()
    }
    /// Raise a complex number to a real power
    pub fn powf(self, power: f64) -> Self {
        if power == 0.0 {
            return Self::ONE;
        }
        if power.fract() == 0.0 && self.im == 0.0 {
            return self.re.powf(power).into();
        }
        let (r, theta) = self.to_polar();
        Self::from_polar(r.powf(power), theta * power)
    }
    /// Calculate the exponential of a complex number
    pub fn exp(self) -> Self {
        Self::from_polar(E.powf(self.re), self.im)
    }
    /// Calculate the natural logarithm of a complex number
    pub fn ln(self) -> Self {
        let (r, theta) = self.to_polar();
        Self::new(r.ln(), theta)
    }
    /// Calculate the logarithm of a complex number
    pub fn log(self, base: impl Into<Self>) -> Self {
        let base = base.into();
        Self::new(self.abs().ln(), self.arg()) / (Self::new(base.abs().ln(), base.arg()))
    }
    /// Calculate the square root of a complex number
    pub fn sqrt(self) -> Self {
        if self.im == 0.0 {
            return if self.re >= 0.0 {
                Self::new(self.re.sqrt(), 0.0)
            } else {
                Self::new(0.0, self.re.abs().sqrt())
            };
        }
        let (r, theta) = self.to_polar();
        Self::from_polar(r.sqrt(), theta / 2.0)
    }
    /// Calculate the sine of a complex number
    pub fn sin(self) -> Self {
        Self::new(
            self.re.sin() * self.im.cosh(),
            self.re.cos() * self.im.sinh(),
        )
    }
    /// Calculate the cosine of a complex number
    pub fn cos(self) -> Self {
        Self::new(
            self.re.cos() * self.im.cosh(),
            -self.re.sin() * self.im.sinh(),
        )
    }
    /// Calculate the arc sine of a complex number
    pub fn asin(self) -> Self {
        -Self::I * ((Self::ONE - self * self).sqrt() + Self::I * self).ln()
    }
    /// Calculate the arc cosine of a complex number
    pub fn acos(self) -> Self {
        -Self::I * (Self::I * (Self::ONE - self * self).sqrt() + self).ln()
    }
    /// Check if either real or imaginary part is NaN
    pub fn is_nan(&self) -> bool {
        self.re.is_nan() || self.im.is_nan()
    }
    /// Get the complex number as a real number
    pub fn into_real(self) -> Option<f64> {
        if self.im.abs() < f64::EPSILON {
            Some(self.re)
        } else {
            None
        }
    }
    /// Multiply by another complex number, with 0 × ∞ = 0
    pub fn safe_mul(self, rhs: impl Into<Self>) -> Self {
        let rhs = rhs.into();
        Self {
            re: safe_mul(self.re, rhs.re) - safe_mul(self.im, rhs.im),
            im: safe_mul(self.re, rhs.im) + safe_mul(self.im, rhs.re),
        }
    }
    pub fn recip(self) -> Self {
        Self::ONE / self
    }
}

fn safe_mul(a: f64, b: f64) -> f64 {
    if a.is_infinite() && b == 0.0 || a == 0.0 && b.is_infinite() {
        0.0
    } else {
        a * b
    }
}

impl From<f64> for Complex {
    fn from(re: f64) -> Self {
        Self { re, im: 0.0 }
    }
}

impl From<u8> for Complex {
    fn from(value: u8) -> Self {
        f64::from(value).into()
    }
}

impl fmt::Display for Complex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.im == 0.0 {
            self.re.fmt(f)
        } else {
            write!(f, "{}r{}i", self.re, self.im)
        }
    }
}

impl Add for Complex {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re + rhs.re,
            im: self.im + rhs.im,
        }
    }
}

impl Add<f64> for Complex {
    type Output = Self;
    fn add(self, rhs: f64) -> Self::Output {
        Self {
            re: self.re + rhs,
            im: self.im,
        }
    }
}

impl Add<Complex> for f64 {
    type Output = Complex;
    fn add(self, rhs: Complex) -> Self::Output {
        Complex {
            re: self + rhs.re,
            im: rhs.im,
        }
    }
}

impl Sub for Complex {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re - rhs.re,
            im: self.im - rhs.im,
        }
    }
}

impl Sub<f64> for Complex {
    type Output = Self;
    fn sub(self, rhs: f64) -> Self::Output {
        Self {
            re: self.re - rhs,
            im: self.im,
        }
    }
}

impl Sub<Complex> for f64 {
    type Output = Complex;
    fn sub(self, rhs: Complex) -> Self::Output {
        Complex {
            re: self - rhs.re,
            im: -rhs.im,
        }
    }
}

impl Mul for Complex {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re * rhs.re - self.im * rhs.im,
            im: self.re * rhs.im + self.im * rhs.re,
        }
    }
}

impl Mul<f64> for Complex {
    type Output = Self;
    fn mul(self, rhs: f64) -> Self::Output {
        Self {
            re: self.re * rhs,
            im: self.im * rhs,
        }
    }
}

impl Mul<Complex> for f64 {
    type Output = Complex;
    fn mul(self, rhs: Complex) -> Self::Output {
        Complex {
            re: self * rhs.re,
            im: self * rhs.im,
        }
    }
}

impl Div for Complex {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        let denom = rhs.re * rhs.re + rhs.im * rhs.im;
        Self {
            re: (self.re * rhs.re + self.im * rhs.im) / denom,
            im: (self.im * rhs.re - self.re * rhs.im) / denom,
        }
    }
}

impl Div<f64> for Complex {
    type Output = Self;
    fn div(self, rhs: f64) -> Self::Output {
        Self {
            re: self.re / rhs,
            im: self.im / rhs,
        }
    }
}

impl Div<Complex> for f64 {
    type Output = Complex;
    fn div(self, rhs: Complex) -> Self::Output {
        let denom = rhs.re * rhs.re + rhs.im * rhs.im;
        Complex {
            re: self * rhs.re / denom,
            im: -self * rhs.im / denom,
        }
    }
}

impl Rem for Complex {
    type Output = Self;
    fn rem(self, rhs: Self) -> Self::Output {
        self - (self / rhs).floor() * rhs
    }
}

impl Rem<f64> for Complex {
    type Output = Self;
    fn rem(self, rhs: f64) -> Self::Output {
        Self {
            re: self.re.rem_euclid(rhs),
            im: self.im.rem_euclid(rhs),
        }
    }
}

impl Rem<Complex> for f64 {
    type Output = Complex;
    fn rem(self, rhs: Complex) -> Self::Output {
        Complex {
            re: self % rhs.re,
            im: self % rhs.im,
        }
    }
}

impl Neg for Complex {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self {
            re: -self.re,
            im: -self.im,
        }
    }
}

impl<T> AddAssign<T> for Complex
where
    Complex: Add<T, Output = Complex>,
{
    fn add_assign(&mut self, rhs: T) {
        *self = *self + rhs;
    }
}

impl<T> SubAssign<T> for Complex
where
    Complex: Sub<T, Output = Complex>,
{
    fn sub_assign(&mut self, rhs: T) {
        *self = *self - rhs;
    }
}

impl<T> MulAssign<T> for Complex
where
    Complex: Mul<T, Output = Complex>,
{
    fn mul_assign(&mut self, rhs: T) {
        *self = *self * rhs;
    }
}

impl<T> DivAssign<T> for Complex
where
    Complex: Div<T, Output = Complex>,
{
    fn div_assign(&mut self, rhs: T) {
        *self = *self / rhs;
    }
}