Skip to content

Commit 97c6b04

Browse files
authored
Optimize pow3 and pow4 (#963)
1 parent db42164 commit 97c6b04

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/compile/optimize.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ static UNSORTED_OPTS: &[&dyn Optimization] = &[
123123
&((Sort, Last), LastSort),
124124
&((Pop, Rand), ReplaceRand),
125125
&((Pop, Pop, Rand), ReplaceRand2),
126-
&((-1, Pow), (1, Flip, Div)),
126+
&((-1, Pow), Reciprocal),
127127
&((2, Pow), (Dup, Mul)),
128+
&((3, Pow), (Dup, Dup, Mul, Mul)),
129+
&((4, Pow), (Dup, Mul, Dup, Mul)),
128130
&((Complex, Abs), AbsComplex),
129131
&((Abs, Dup, Mul), (SquareAbs)),
130132
&ByToDup,

src/value.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,9 +1880,21 @@ impl Value {
18801880
pub fn pow(self, base: Self, env: &Uiua) -> UiuaResult<Self> {
18811881
if let Ok(pow) = self.as_int(env, None) {
18821882
match pow {
1883+
-1 => return base.div(Value::from(1), env),
18831884
1 => return Ok(base),
18841885
2 => return base.clone().mul(base, env),
1885-
-1 => return base.div(Value::from(1), env),
1886+
3 => {
1887+
return base
1888+
.clone()
1889+
.mul(base.clone(), env)
1890+
.and_then(|square| square.mul(base, env));
1891+
}
1892+
4 => {
1893+
return base
1894+
.clone()
1895+
.mul(base, env)
1896+
.and_then(|square| square.clone().mul(square, env));
1897+
}
18861898
_ => {}
18871899
}
18881900
}

0 commit comments

Comments
 (0)