Skip to content

Commit

Permalink
allow some shape mismatches in memberof and indexof
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jan 9, 2025
1 parent 6d5a995 commit b961454
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/algorithm/dyadic/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ impl<T: ArrayValue> Array<T> {
}
Ordering::Less => {
if !of.shape.ends_with(&elems.shape) {
return Err(env.error(format!(
"Cannot look for array of shape {} in array of shape {}",
elems.shape, of.shape
)));
let shape = Shape::from(&of.shape[..of.shape.len() - elems.shape.len() - 1]);
let data = eco_vec![0; shape.elements()];
return Ok(Array::new(shape, data));
}
if of.rank() - elems.rank() == 1 {
of.rows().any(|r| *elems == r).into()
Expand Down Expand Up @@ -187,11 +186,12 @@ impl<T: ArrayValue> Array<T> {
}
Ordering::Less => {
if !haystack.shape.ends_with(&needle.shape) {
return Err(env.error(format!(
"Cannot get index of array of shape {} in array of shape {}",
needle.shape(),
haystack.shape()
)));
let shape = Shape::from(
&haystack.shape[..haystack.shape.len() - needle.shape.len() - 1],
);
let elem = haystack.shape.row_count() as f64;
let data = eco_vec![elem; shape.elements()];
return Ok(Array::new(shape, data));
}
if haystack.rank() - needle.rank() == 1 {
(haystack
Expand Down
6 changes: 6 additions & 0 deletions tests/dyadic.ua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
⍤⤙≍ ⟜(△∊5 °△)2_4
⍤⤙≍ ⟜(△∊5 °△)1_4
⍤⤙≍ ⟜(△∊5 °△)0_4
⍤⤙≍ 0 ∊[¤1 ¤2] ¤5

# Index of
⍤⤙≍ 1 ⊗ 5 [1 5 5]
⍤⤙≍ [1] ⊗ [5] [1 5 5]
Expand All @@ -157,6 +159,10 @@
⍤⤙≍ ⟜(△⊗⊙5 °△)2_4
⍤⤙≍ ⟜(△⊗⊙5 °△)1_4
⍤⤙≍ ⟜(△⊗⊙5 °△)0_4
⍤⤙≍ 1 ⊗ ¤5 [¤1 ¤5]
⍤⤙≍ 2 ⊗ ¤5 [[1] [2] [5] [3]]
⍤⤙≍ 4 ⊗ ¤5 [[1] [2] [0] [3]]
⍤⤙≍ 4 ⊗ ¤5 [1_0 2_0 5_0 3_0]

# Find
⍤⤙≍ [0 1 0 0] ⌕ 2 [1 2 3 4]
Expand Down
1 change: 1 addition & 0 deletions tests/optimized.ua
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ F ← ⬚10(/+◌1⊞(ׯ))
⍤⤙≍ ⊃(∊∘♭₂⇡|∊♭₂⇡) [5 ¯5] [[2_¯π 3_∞ 0_¯5][NaN_0 5_¯5 ¯2_1]]
⍤⤙≍ ⊃(∊∘♭₂⇡|∊♭₂⇡) 3 °△2_3
⍤⤙≍ ⊃(∊∘♭₂⇡|∊♭₂⇡) 3_6 5
⍤⤙≍ ⊃(∊∘♭₂⇡|∊♭₂⇡) 5_5 ¤5

# Random Row
⍤⤙≍ ⊃(⊢°⍆|⊢∘°⍆) °△1_4_5_6
Expand Down

0 comments on commit b961454

Please sign in to comment.