Skip to content

Commit

Permalink
ivy: fix matrix index of matrix (#112)
Browse files Browse the repository at this point in the history
The right-to-left evaluation order for indexes was causing the
accumulation of the shape to end up reversed. Fix that by doing
an additional reverse after the loop.

Fixes #111.
  • Loading branch information
rsc authored May 10, 2022
1 parent be03b14 commit df6b0d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions testdata/binary_matrix.ivy
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,14 @@ x[down x]
7 8 9
4 5 6
1 2 3

(5 5 rho iota 25)[3]
11 12 13 14 15

(5 5 rho iota 25)[3 2]
11 12 13 14 15
6 7 8 9 10

(5 5 rho iota 25)[3 2; 1 2 3]
11 12 13
6 7 8
11 changes: 10 additions & 1 deletion value/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func (ix *indexState) init(context Context, top, left Expr, index []Expr) {
ix.xshape = append(ix.xshape, len(x))
case *Matrix:
ix.indexes[i] = x.Data()
ix.xshape = append(ix.xshape, x.Shape()...)
// Append shape in reverse, because ix.shape will be reversed below.
shape := x.Shape()
for j := len(shape) - 1; j >= 0; j-- {
ix.xshape = append(ix.xshape, shape[j])
}
}
for _, v := range ix.indexes[i] {
if _, ok := v.(Int); !ok {
Expand All @@ -49,6 +53,11 @@ func (ix *indexState) init(context Context, top, left Expr, index []Expr) {
}
}

// Walked indexes right-to-left, so reverse shape.
for i, j := 0, len(ix.xshape)-1; i < j; i, j = i+1, j-1 {
ix.xshape[i], ix.xshape[j] = ix.xshape[j], ix.xshape[i]
}

// Can now safely evaluate left side
// (must wait until indexes have been evaluated, R-to-L).
ix.lhs = left.Eval(context)
Expand Down

0 comments on commit df6b0d3

Please sign in to comment.