diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | src/algorithm/monadic.rs | 20 | ||||
-rw-r--r-- | src/primitive/defs.rs | 5 | ||||
-rw-r--r-- | tests/units.ua | 4 |
4 files changed, 28 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md index 75c92fae..ba60e470 100644 --- a/changelog.md +++ b/changelog.md @@ -23,6 +23,8 @@ This version is not yet released. If you are reading this on the website, then t - Deprecate existing stack-based macro placeholders (`^!`, `^.`, etc.) - [`fold ∧`](https://uiua.org/docs/fold) now works with any signature - Excess values are collected into arrays +- [`parse ⋕`](https://uiua.org/docs/parse) now parses complex values from both `arbi`and `a+bi` formats + - [`un °`](https://uiua.org/docs/un)[`parse ⋕`](https://uiua.org/docs/parse)'s return value uses the `arbi` format - [`un °`](https://uiua.org/docs/un) [`orient ⤸`](https://uiua.org/docs/orient) is now equivalent to [`range ⇡`](https://uiua.org/docs/range) [`length ⧻`](https://uiua.org/docs/length) [`shape △`](https://uiua.org/docs/shape) [`duplicate .`](https://uiua.org/docs/duplicate) - This is analogous to the behavior of [`un °`](https://uiua.org/docs/un) [`select ⊏`](https://uiua.org/docs/select) - Add a feature to some [`under ⍜`](https://uiua.org/docs/under) functions that makes them more permissive of changes in rank diff --git a/src/algorithm/monadic.rs b/src/algorithm/monadic.rs index 8382fc32..c20c7312 100644 --- a/src/algorithm/monadic.rs +++ b/src/algorithm/monadic.rs @@ -9,6 +9,7 @@ use std::{ io::Write, iter, mem::size_of, + ops::Neg, ptr, slice, time::Duration, }; @@ -88,8 +89,12 @@ impl Value { Ok(match (self, self.shape().dims()) { (Value::Char(arr), [] | [_]) => { let mut s: String = arr.data.iter().copied().collect(); - match s.strip_suffix("i").and_then(|s| s.split_once("r")) { - Some((re, im)) => { + match ( + s.strip_suffix("i").and_then(|s| s.split_once("r")), + s.strip_suffix("i").and_then(|s| s.split_once("+")), + s.strip_suffix("i").and_then(|s| s.split_once("-")), + ) { + (Some((re, im)), None, _) | (None, Some((re, im)), _) => { let re_parsed = Self::from(re).parse_num(env)?; let im_parsed = Self::from(im).parse_num(env)?; Complex { @@ -98,7 +103,16 @@ impl Value { } .into() } - None => { + (_, _, Some((re, im))) => { + let re_parsed = Self::from(re).parse_num(env)?; + let im_parsed = Self::from(im).parse_num(env)?; + Complex { + re: re_parsed.as_num(env, "")?, + im: im_parsed.as_num(env, "")?.neg(), + } + .into() + } + _ => { let mut mul = 1.0; if s.contains('¯') { s = s.replace('¯', "-"); diff --git a/src/primitive/defs.rs b/src/primitive/defs.rs index 859bca71..d7a5216e 100644 --- a/src/primitive/defs.rs +++ b/src/primitive/defs.rs @@ -1128,6 +1128,11 @@ primitive!( /// ex: °⋕ 1_2_3 /// ex: °⋕ ↯3_4⇡12 /// + /// [parse] accepts both `a+bi` and `arbi` formats, [un][parse] returns a string using the latter. + /// ex: ∩⋕ "8+3i" "8-3i" + /// ex: ∩⋕ "8r3i" "8r-3i" + /// ex: °⋕ ℂ3 8 + /// /// [fill][parse] sets a default value for failed parses. /// ex: ⬚5⋕ {"13" "124" "not a number"} /// [fill][un][parse] pads the strings to make a character array instead of a box array. diff --git a/tests/units.ua b/tests/units.ua index 6846658b..0b2702fb 100644 --- a/tests/units.ua +++ b/tests/units.ua @@ -213,6 +213,10 @@ ⍤⤙≍ [2 10 5] ⬚5⋕{"2" "10" "dog"} ⍤⤙≍ [ℂ 3.4e6 1.2 ℂ π ∞] ⋕ {"1.2r3.4e6i" "infrpii"} ⍤⟜≍ ⋕°⋕. [ℂ 3.4e6 1.2 ℂ π ∞] +⍤⤙≍ [ℂ4.5 `12.3 ℂ4.5 12.3 ℂ`4.5 12.3 ℂ`4.5 12.3 ℂ4.5 12.3] ⋕{"-12.3r4.5i" "12.3r4.5i" "12.3-4.5i" "12.3r-4.5i" "12.3r4.5i"} +⍤⤙≍ [ℂ4 ¯12 ℂ4 12 ℂ¯4 12 ℂ¯4 12 ℂ4 12] ⋕{"-12r4i" "12r4i" "12-4i" "12r-4i" "12r4i"} +⍤⤙≍ [ℂ`∞ π ℂ∞ π ℂτ π ℂ`τ π ℂτ `π] ⋕{"πr-∞i" "πr∞i" "πrτi" "πr-τi" "-πrτi"} +⍤⤙≍ [ℂ`∞ π ℂ∞ π ℂτ π ℂ`τ π ℂτ `π] ⋕{"π-∞i" "π+∞i" "π+τi" "π-τi" "-π+τi"} ⍤⟜≍: ["12.1 " " 3 " " 6.25"] ⬚@ °⋕ [12.1 3 6.25] ⍤⟜≍: ["12.10" "03.00" "06.25"] ⬚@0°⋕ [12.1 3 6.25] |