Skip to content

Commit

Permalink
give an explicit implementation for un shape
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 16, 2024
1 parent fc9d623 commit ee50e18
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
22 changes: 2 additions & 20 deletions src/algorithm/invert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn prim_inverse(prim: Primitive, span: usize) -> Option<Instr> {
Utf => Instr::ImplPrim(UnUtf, span),
Parse => Instr::ImplPrim(UnParse, span),
Fix => Instr::ImplPrim(UnFix, span),
Shape => Instr::ImplPrim(UnShape, span),
Map => Instr::ImplPrim(UnMap, span),
Trace => Instr::ImplPrim(TraceN(1, true), span),
Stack => Instr::ImplPrim(UnStack, span),
Expand Down Expand Up @@ -130,6 +131,7 @@ fn impl_prim_inverse(prim: ImplPrimitive, span: usize) -> Option<Instr> {
UnCouple => Instr::Prim(Couple, span),
UnParse => Instr::Prim(Parse, span),
UnFix => Instr::Prim(Fix, span),
UnShape => Instr::Prim(Shape, span),
UnMap => Instr::Prim(Map, span),
UnStack => Instr::Prim(Stack, span),
UnJoin => Instr::Prim(Join, span),
Expand Down Expand Up @@ -190,7 +192,6 @@ static INVERT_PATTERNS: &[&dyn InvertPattern] = {
&InvertPatternFn(invert_dup_pattern, "dup"),
&InvertPatternFn(invert_stack_swizzle_pattern, "stack swizzle"),
&InvertPatternFn(invert_select_pattern, "select"),
&InvertPatternFn(invert_shape_pattern, "shape"),
&pat!(Sqrt, (Dup, Mul)),
&pat!((Dup, Add), (2, Div)),
&([Dup, Mul], [Sqrt]),
Expand Down Expand Up @@ -918,25 +919,6 @@ fn invert_stack_swizzle_pattern<'a>(
Some((input, instrs))
}

fn invert_shape_pattern<'a>(
input: &'a [Instr],
comp: &mut Compiler,
) -> Option<(&'a [Instr], EcoVec<Instr>)> {
let [Instr::Prim(Primitive::Shape, span), input @ ..] = input else {
return None;
};
let mul = make_fn(eco_vec![Instr::Prim(Primitive::Mul, *span)], *span, comp)?;
let instrs = eco_vec![
Instr::Prim(Primitive::Dup, *span),
Instr::PushFunc(mul),
Instr::Prim(Primitive::Reduce, *span),
Instr::Prim(Primitive::Range, *span),
Instr::Prim(Primitive::Flip, *span),
Instr::Prim(Primitive::Reshape, *span),
];
Some((input, instrs))
}

fn invert_select_pattern<'a>(
input: &'a [Instr],
_: &mut Compiler,
Expand Down
16 changes: 16 additions & 0 deletions src/algorithm/monadic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ impl Value {
Err(data) => Array::new(shape, data).into(),
})
}
pub(crate) fn unshape(&self, env: &Uiua) -> UiuaResult<Self> {
let ishape = self.as_ints(
env,
"Shape should be a single integer or a list of integers",
)?;
let shape = Shape::from_iter(ishape.iter().map(|n| n.unsigned_abs()));
let elems: usize = validate_size::<f64>(shape.iter().copied(), env)?;
let data = EcoVec::from_iter((0..elems).map(|i| i as f64));
let mut arr = Array::new(shape, data);
for (i, s) in ishape.into_iter().enumerate() {
if s < 0 {
arr.reverse_depth(i);
}
}
Ok(arr.into())
}
}

fn range(shape: &[isize], env: &Uiua) -> UiuaResult<Result<CowSlice<f64>, CowSlice<u8>>> {
Expand Down
1 change: 1 addition & 0 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,7 @@ impl_primitive!(
(1(2), UnComplex),
(1, UnParse),
(1, UnFix),
(1, UnShape),
(1[1], UnScan),
(1(2), UnMap),
(0(0), UnStack, Impure),
Expand Down
2 changes: 2 additions & 0 deletions src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl fmt::Display for ImplPrimitive {
UnUtf => write!(f, "{Un}{Utf}"),
UnParse => write!(f, "{Un}{Parse}"),
UnFix => write!(f, "{Un}{Fix}"),
UnShape => write!(f, "{Un}{Shape}"),
UnJoin | UnJoinPattern => write!(f, "{Un}{Join}"),
UnKeep => write!(f, "{Un}{Keep}"),
UnScan => write!(f, "{Un}{Scan}"),
Expand Down Expand Up @@ -989,6 +990,7 @@ impl ImplPrimitive {
}
ImplPrimitive::UnParse => env.monadic_ref_env(Value::unparse)?,
ImplPrimitive::UnFix => env.monadic_mut_env(Value::unfix)?,
ImplPrimitive::UnShape => env.monadic_ref_env(Value::unshape)?,
ImplPrimitive::UndoFix => env.monadic_mut(Value::undo_fix)?,
ImplPrimitive::UnScan => reduce::unscan(env)?,
ImplPrimitive::TraceN(n, inverse) => trace_n(env, *n, *inverse)?,
Expand Down

0 comments on commit ee50e18

Please sign in to comment.