Skip to content

Commit b4767dc

Browse files
authored
Optimize negabs (#962)
* Optimize negabs * Keep this for another PR * cargo fmt * Fix brackets in value_mon_impl macro * Can't use [] for Complex, result is real
1 parent 97c6b04 commit b4767dc

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

src/algorithm/pervade.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,34 @@ pub mod square_abs {
12421242
}
12431243
}
12441244

1245+
pub mod neg_abs {
1246+
use super::*;
1247+
pub fn num(a: f64) -> f64 {
1248+
-a.abs()
1249+
}
1250+
pub fn byte(a: u8) -> f64 {
1251+
-f64::from(a)
1252+
}
1253+
pub fn char(a: char) -> char {
1254+
if a.is_uppercase() {
1255+
let mut upper = a.to_lowercase();
1256+
if upper.len() == 1 {
1257+
upper.next().unwrap()
1258+
} else {
1259+
a
1260+
}
1261+
} else {
1262+
a
1263+
}
1264+
}
1265+
pub fn com(a: Complex) -> f64 {
1266+
-a.abs()
1267+
}
1268+
pub fn error<T: Display>(a: T, env: &Uiua) -> UiuaError {
1269+
env.error(format!("Cannot take the absolute value of {a}"))
1270+
}
1271+
}
1272+
12451273
macro_rules! eq_impl {
12461274
($name:ident $eq:tt $ordering:expr) => {
12471275
pub mod $name {

src/compile/optimize.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ static UNSORTED_OPTS: &[&dyn Optimization] = &[
128128
&((3, Pow), (Dup, Dup, Mul, Mul)),
129129
&((4, Pow), (Dup, Mul, Dup, Mul)),
130130
&((Complex, Abs), AbsComplex),
131-
&((Abs, Dup, Mul), (SquareAbs)),
131+
&((Abs, Dup, Mul), SquareAbs),
132+
&((Abs, Neg), NegAbs),
132133
&ByToDup,
133134
&RowsFlipOpt,
134135
&InlineCustomInverse,

src/impl_prim.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ impl_primitive!(
242242
(2[1], SplitByKeepEmpty),
243243
(2, AbsComplex),
244244
(1, SquareAbs),
245+
(1, NegAbs),
245246
(2, MatrixDiv),
246247
(1, RangeSub(i32)),
247248
(1, Exp2),

src/run_prim.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ impl ImplPrimitive {
10111011
// Optimizations
10121012
ImplPrimitive::AbsComplex => env.dyadic_oo_env(Value::abs_complex)?,
10131013
ImplPrimitive::SquareAbs => env.monadic_env(Value::square_abs)?,
1014+
ImplPrimitive::NegAbs => env.monadic_env(Value::neg_abs)?,
10141015
ImplPrimitive::FirstMinIndex => env.monadic_ref_env(Value::first_min_index)?,
10151016
ImplPrimitive::FirstMaxIndex => env.monadic_ref_env(Value::first_max_index)?,
10161017
ImplPrimitive::LastMinIndex => env.monadic_ref_env(Value::last_min_index)?,

src/value.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,13 @@ value_mon_impl!(exp10, [Num, num], (Byte, byte), [Complex, com]);
18051805
value_mon_impl!(log2, [Num, num], (Byte, byte), [Complex, com]);
18061806
value_mon_impl!(log10, [Num, num], (Byte, byte), [Complex, com]);
18071807
value_mon_impl!(square_abs, [Num, num], (Byte, byte), (Complex, com));
1808+
value_mon_impl!(
1809+
neg_abs,
1810+
[Num, num],
1811+
(Byte, byte),
1812+
(Complex, com),
1813+
[Char, char]
1814+
);
18081815

18091816
impl Value {
18101817
/// Get the `absolute value` of a value

0 commit comments

Comments
 (0)