Skip to content

Commit

Permalink
ivy: fix bug in up and down (known as grade internally)
Browse files Browse the repository at this point in the history
They were using binary <, which works elementwise. This is what
OrderedCompare is for.

One of the cases where allowing non-scalar elements required changing
existing code.

Fixes #189
  • Loading branch information
robpike committed Dec 24, 2024
1 parent 0737034 commit 2f07743
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions testdata/binary_vector.ivy
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ rho (6 2 rho "abcdefghijkl") [3 2 rho iota 6]
x = 1 rho 1; rho x[up x]
1

up 'ab' 'ac' 'aa'
3 1 2

3 sel 1
1 1 1

Expand Down
3 changes: 3 additions & 0 deletions testdata/unary_vector.ivy
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,6 @@ mix split 2 2 rho iota 4
1 2
3 4

up mix 'ab' 'ac' 'aa'
3 1 2

5 changes: 3 additions & 2 deletions value/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,10 +966,11 @@ func (m *Matrix) grade(c Context) *Vector {
i = x[i] * stride
j = x[j] * stride
for k := 0; k < stride; k++ {
if toBool(c.EvalBinary(v.At(i+k), "==", v.At(j+k))) {
cmp := OrderedCompare(c, v.At(i+k), v.At(j+k))
if cmp == 0 {
continue
}
return toBool(c.EvalBinary(v.At(i+k), "<", v.At(j+k)))
return cmp < 0
}
return false
})
Expand Down
2 changes: 1 addition & 1 deletion value/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (v *Vector) grade(c Context) *Vector {
x[i] = i
}
sort.SliceStable(x, func(i, j int) bool {
return toBool(c.EvalBinary(v.At(x[i]), "<", v.At(x[j])))
return OrderedCompare(c, v.At(x[i]), v.At(x[j])) < 0
})
origin := c.Config().Origin()
for i := range x {
Expand Down

0 comments on commit 2f07743

Please sign in to comment.