Skip to content

Commit

Permalink
ivy: repl is just sel
Browse files Browse the repository at this point in the history
I should pay more attention. The repl operator is just redundant.
However, as one grows through life, one improves. The implementation
of repl is far cleaner, so I kept it and made sel call, then deleted the
repl entry.

All the tests still pass. In fact the repl tests for matrix were better too.

Update #195
  • Loading branch information
robpike committed Jan 9, 2025
1 parent ce185fc commit 1813d15
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 172 deletions.
1 change: 0 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ Binary operators
Partition A⊆B part Vector of subvectors of B grouped by elements of A:
If 0, ignore; otherwise start new group at boundaries
where elements of A increase
Replicate A/B repl Replicate elements of B A times
Index of A⍳B iota The location (index) of B in A; 1+⌈/⍳⍴A if not found
In ivy: origin-1 if not found (that is, 0 if one-indexed)
Matrix divide A⌹B mdiv Solution to system of linear equations Bx = A
Expand Down
1 change: 0 additions & 1 deletion mobile/help.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 38 additions & 40 deletions parse/help.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 28 additions & 33 deletions testdata/binary_matrix.ivy
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,34 @@ rho transp 1 0 2 rho 0
transp 0 1 sel transp (2 3 rho iota 6)
4 5 6

3 sel 2 2 rho iota 4
1 1 1 2 2 2
3 3 3 4 4 4

2 3 4 sel 4 3 rho iota 12
1 1 2 2 2 3 3 3 3
4 4 5 5 5 6 6 6 6
7 7 8 8 8 9 9 9 9
10 10 11 11 11 12 12 12 12

2 -3 4 sel 4 3 rho iota 12
1 1 0 0 0 3 3 3 3
4 4 0 0 0 6 6 6 6
7 7 0 0 0 9 9 9 9
10 10 0 0 0 12 12 12 12

2 sel box 4 3 rho iota 12
( 1 2 3| ( 1 2 3|
| 4 5 6| | 4 5 6|
| 7 8 9| | 7 8 9|
|10 11 12) |10 11 12)

-2 sel box 4 3 rho iota 12
(0 0 0| (0 0 0|
|0 0 0| |0 0 0|
|0 0 0| |0 0 0|
|0 0 0) |0 0 0)

x = 5 4 rho 0 0 1
x[up x]
0 0 1 0
Expand Down Expand Up @@ -1025,36 +1053,3 @@ rho 5 drop 4 4 rho iota 12
0 0 1 0
0 1 0 0

# Odd APL semantics: fill with zeros reshaping empty vector.
3 4 rho iota 0
0 0 0 0
0 0 0 0
0 0 0 0

3 repl 2 2 rho iota 4
1 1 1 2 2 2
3 3 3 4 4 4

2 3 4 repl 4 3 rho iota 12
1 1 2 2 2 3 3 3 3
4 4 5 5 5 6 6 6 6
7 7 8 8 8 9 9 9 9
10 10 11 11 11 12 12 12 12

2 -3 4 repl 4 3 rho iota 12
1 1 0 0 0 3 3 3 3
4 4 0 0 0 6 6 6 6
7 7 0 0 0 9 9 9 9
10 10 0 0 0 12 12 12 12

2 repl box 4 3 rho iota 12
( 1 2 3| ( 1 2 3|
| 4 5 6| | 4 5 6|
| 7 8 9| | 7 8 9|
|10 11 12) |10 11 12)

-2 repl box 4 3 rho iota 12
(0 0 0| (0 0 0|
|0 0 0| |0 0 0|
|0 0 0| |0 0 0|
|0 0 0) |0 0 0)
24 changes: 0 additions & 24 deletions testdata/binary_vector.ivy
Original file line number Diff line number Diff line change
Expand Up @@ -525,30 +525,6 @@ x='hello world '; (x != ' ') part x
(iota 10) part (iota 10)
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

0 repl 5
#

3 repl 5
5 5 5

-3 repl 5
0 0 0

3 repl iota 3
1 1 1 2 2 2 3 3 3

3 4 5 repl iota 3
1 1 1 2 2 2 2 3 3 3 3 3

3 -4 5 repl iota 3
1 1 1 0 0 0 0 3 3 3 3 3

3 repl 'a'
aaa

3 4 repl 'ab'
aaabbbb

3 fill 1
1 1 1

Expand Down
81 changes: 12 additions & 69 deletions value/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,28 +1131,6 @@ func init() {
},
},

{
name: "repl",
whichType: atLeastVectorType,
fn: [numType]binaryFn{
vectorType: func(c Context, u, v Value) Value {
countV, data := u.(*Vector), v.(*Vector)
return data.repl(countV, data.Len())
},
matrixType: func(c Context, u, v Value) Value {
count, m := u.(*Matrix), v.(*Matrix)
if len(count.shape) != 1 {
Errorf("repl count cannot be matrix")
}
result := m.data.repl(count.data, m.shape[len(m.shape)-1])
newShape := make([]int, len(m.shape))
copy(newShape, m.shape)
newShape[len(m.shape)-1] = result.Len() / size(m.shape[:len(m.shape)-1])
return NewMatrix(newShape, result)
},
},
},

{
name: "iota",
whichType: atLeastVectorType,
Expand Down Expand Up @@ -1554,57 +1532,22 @@ func init() {

{
name: "sel",
whichType: vectorAndAtLeastVectorType,
whichType: atLeastVectorType,
fn: [numType]binaryFn{
vectorType: func(c Context, u, v Value) Value {
i := u.(*Vector)
j := v.(*Vector)
if i.Len() == 0 {
return empty
}
// All lhs values must be small integers.
var count int64
for _, x := range i.All() {
y, ok := x.(Int)
if !ok {
Errorf("sel: left operand must be small integers")
}
if y < 0 {
count -= int64(y)
} else {
count += int64(y)
}
}
if count > 1e8 {
Errorf("sel: result too large: %d elements", count)
}
result := make([]Value, 0, count)
add := func(howMany, what Value) {
hm := int(howMany.(Int))
if hm < 0 {
hm = -hm
what = zero
}
for ; hm > 0; hm-- {
result = append(result, what)
}
}
if i.Len() == 1 {
for _, y := range j.All() {
add(i.At(0), y)
}
} else {
if i.Len() != j.Len() {
Errorf("sel: unequal lengths %d != %d", i.Len(), j.Len())
}
for x, y := range j.All() {
add(i.At(x), y)
}
}
return NewVector(result)
countV, data := u.(*Vector), v.(*Vector)
return data.sel(countV, data.Len())
},
matrixType: func(c Context, u, v Value) Value {
return v.(*Matrix).sel(c, u.(*Vector))
count, m := u.(*Matrix), v.(*Matrix)
if len(count.shape) != 1 {
Errorf("sel count cannot be matrix")
}
result := m.data.sel(count.data, m.shape[len(m.shape)-1])
newShape := make([]int, len(m.shape))
copy(newShape, m.shape)
newShape[len(m.shape)-1] = result.Len() / size(m.shape[:len(m.shape)-1])
return NewMatrix(newShape, result)
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions value/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,19 +375,19 @@ func (v *Vector) rotate(n int) Value {
return NewVector(elems)
}

// repl returns a Vector with each element repeated n times. n must be either one
// sel returns a Vector with each element repeated n times. n must be either one
// integer or a vector of the same length as v. elemCount is the number of elements
// we are to duplicate; this will be number of columns for a matrix's data.
// If the count is negative, we replicate zeros of the appropriate shape.
func (v *Vector) repl(n *Vector, elemCount int) *Vector {
func (v *Vector) sel(n *Vector, elemCount int) *Vector {
if n.Len() != 1 && n.Len() != elemCount {
Errorf("repl length mismatch")
Errorf("sel length mismatch")
}
result := make([]Value, 0)
for i := range v.Len() {
count, ok := n.At(i % n.Len()).(Int)
if !ok {
Errorf("repl count must be small integer")
Errorf("sel count must be small integer")
}
val := v.At(i)
if count < 0 { // Thanks, APL.
Expand Down

0 comments on commit 1813d15

Please sign in to comment.