Skip to content

Commit 0bd8cbf

Browse files
authored
feat: reciprocal operator (#769)
1 parent 5268082 commit 0bd8cbf

File tree

11 files changed

+42
-2
lines changed

11 files changed

+42
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This version is not yet released. If you are reading this on the website, then t
4343
- Add [`un °`](https://uiua.org/docs/un) and [`under ⍜`](https://uiua.org/docs/under) capability to [`&cd`](https://uiua.org/docs/&cd)
4444
- [`un °`](https://uiua.org/docs/un)[`&cd`](https://uiua.org/docs/&cd) will output the current working directory
4545
- [`under ⍜`](https://uiua.org/docs/under)[`&cd`](https://uiua.org/docs/&cd) will return to the original directory afterward
46+
- Add experimental [`reciprocal ⨪`](https://uiua.org/docs/reciprocal) function, which computes the multiplicative inverse AKA reciprocal of a number
4647
### Interpreter
4748
- Speed up the implementation of [`or ∨`](https://uiua.org/docs/or)
4849
- The fomatter no longer truncates trailing decimal `0`s from number literals

examples/photobooth.ua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Pixelate an image with the given ratio
55
# ? Ratio Image
6-
Pixelate ← ◌⍥(÷⊙1⟜▽⟜≡▽)2 ÷⊙1
6+
Pixelate ← ◌⍥(⟜▽⟜≡▽)2
77

88
# Run an edge-detection algorithm on an image.
99
# Uses a 5x5 kernel.

parser/src/complex.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ impl Complex {
204204
im: safe_mul(self.re, rhs.im) + safe_mul(self.im, rhs.re),
205205
}
206206
}
207+
pub fn recip(self) -> Self {
208+
Self::ONE / self
209+
}
207210
}
208211

209212
fn safe_mul(a: f64, b: f64) -> f64 {

parser/src/defs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ primitive!(
202202
///
203203
/// The glyph looks like the graph of `|x|`.
204204
(1, Abs, MonadicPervasive, ("absolute value", '⌵')),
205+
/// Get the reciprocal of a number
206+
///
207+
/// ex: # Experimental!
208+
/// : ⨪3
209+
/// ex: # Experimental!
210+
/// : ⨪[1 2 3 6]
211+
/// ex: # Experimental!
212+
/// : ⨪ℂ2 1
213+
(1, Reciprocal, MonadicPervasive, ("reciprocal", '⨪'), { experimental: true }),
205214
/// Take the square root of a number
206215
///
207216
/// ex: √4

site/primitives.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,14 @@
11931193
"description": "Call a function on the first and third values on the stack",
11941194
"experimental": true
11951195
},
1196+
"reciprocal": {
1197+
"glyph": "",
1198+
"args": 1,
1199+
"outputs": 1,
1200+
"class": "MonadicPervasive",
1201+
"description": "Get the reciprocal of a number",
1202+
"experimental": true
1203+
},
11961204
"recv": {
11971205
"args": 1,
11981206
"outputs": 1,

site/src/primitive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ fn all_uns() -> impl IntoView {
214214
{ inverse_row([Identity], No, "Self inverse", "°∘ 5") }
215215
{ inverse_row([Neg], No, "Self inverse", "°¯ 5") }
216216
{ inverse_row([Not], No, "Self inverse", "°¬ 5") }
217+
{ inverse_row([Reciprocal], No, "Self inverse", "# Experimental!\n°⨪ 5") }
217218
{ inverse_row([Sqrt], No, "", "°√ 5") }
218219
{ inverse_row([Sin], No, "Arcsine", "°∿ 1") }
219220
{ inverse_row([Add], Required, "", "°(+1) 5") }

site/src/tutorial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn TutorialBasic() -> impl IntoView {
270270
<p><Prim prim=Backward/>" swaps the arguments to a function that takes two values."</p>
271271
<blockquote>"Note that "<Prim prim=Backward/>" is not actually a function itself. It is something called a "<em>"modifier"</em>". We'll talk about modifiers in more depth in a "<A href="/tutorial/functions#modifiers">"later section"</A>"."</blockquote>
272272
<p><Prim prim=Backward/>" is useful when you want to call a function that takes two arguments, but the arguments are on the stack in the wrong order."</p>
273-
<p>"For example, if you wanted to get the reciprocal of a number, you would "<Prim prim=Div/>" "<code>"1"</code>" by it. But, if the number is already on the stack, you would need to use "<Prim prim=Backward/>"."</p>
273+
<p>"For example, if you wanted to get the reciprocal of a number without using "<Prim prim=Reciprocal/>", you would "<Prim prim=Div/>" "<code>"1"</code>" by it. But, if the number is already on the stack, you would need to use "<Prim prim=Backward/>"."</p>
274274
<Editor example=" ÷1 5\n˜÷1 5"/>
275275
<Editor example=" -2 5\nback-2 5"/>
276276
<br/>

src/algorithm/pervade.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,21 @@ pub mod sign {
941941
env.error(format!("Cannot get the sign of {a}"))
942942
}
943943
}
944+
pub mod recip {
945+
use super::*;
946+
pub fn num(a: f64) -> f64 {
947+
a.recip()
948+
}
949+
pub fn byte(a: u8) -> f64 {
950+
f64::from(a).recip()
951+
}
952+
pub fn com(a: Complex) -> Complex {
953+
a.recip()
954+
}
955+
pub fn error<T: Display>(a: T, env: &Uiua) -> UiuaError {
956+
env.error(format!("Cannot get the reciprocal of {a}"))
957+
}
958+
}
944959
pub mod sqrt {
945960
use super::*;
946961
pub fn num(a: f64) -> f64 {

src/compile/invert/un.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ inverse!(PrimPat, input, _, Prim(prim, span), {
12401240
Pop => ImplPrim(UnPop, span),
12411241
Neg => Prim(Neg, span),
12421242
Not => Prim(Not, span),
1243+
Reciprocal => Prim(Reciprocal, span),
12431244
Sin => ImplPrim(Asin, span),
12441245
Exp => ImplPrim(Ln, span),
12451246
Atan => ImplPrim(UnAtan, span),

src/run_prim.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn run_prim_func(prim: &Primitive, env: &mut Uiua) -> UiuaResult {
8282
Primitive::Abs => env.monadic_env(Value::abs)?,
8383
Primitive::Sign => env.monadic_env(Value::sign)?,
8484
Primitive::Sqrt => env.monadic_env(Value::sqrt)?,
85+
Primitive::Reciprocal => env.monadic_env(Value::recip)?,
8586
Primitive::Exp => env.monadic_env(Value::exp)?,
8687
Primitive::Sin => env.monadic_env(Value::sin)?,
8788
Primitive::Floor => env.monadic_env(Value::floor)?,

0 commit comments

Comments
 (0)