Description
For various mathematical applications it would be useful to have a reflection method that mirrors the array around a given axis.
For a 2D example:
1 2 3
4 5 6
7 8 9
reflected around the Y-axis, this should result in:
3 2 1
6 5 4
9 8 7
I'm trying to implement this for n-dimensional objects. The operation is simple: leave all indices as they are, except the ones of the given axis which equal (size at that axis)-(original position)
For a 2D matrix that could be implemented as:
func (a *Array64) ReflectX() {
tmp := a.C()
for x := 0; x < int(a.shape[0]; x++ {
for y := 0; y < int(a.shape[1]; y++ {
val := a.Get( x,y)
tmp.Set( a.shape[0] - x ,y )
}
}
a = tmp
}
func (a *Array64) ReflectY() {
tmp := a.C()
for x := 0; x < int(a.shape[0]; x++ {
for y := 0; y < int(a.shape[1]; y++ {
val := a.Get( x,y)
tmp.Set(x, a.shape[1] - y )
}
}
a = tmp
}
Since I can't make a method for every axis (unknown number of axis), I need a general method that takes the axis to flip around as parameter. Also I can't add a nested for loop for every axis, since I don't know how many there are.
// Reflect mirrors the array at around a given axis
func (a *Array64) Reflect(axis int) {
if len(a.shape) < axis {
/*handle error, invalid axis */
}
tmp := a.C()
for i := 0; i < int(a.shape[axis]); i++ {
val := a.Get(/*other axis before*/, i, /*other axis following*/)
tmp.Set(val, /*other axis before*/, a.shape[axis] - i /*other axis following*/)
}
a = tmp
}
I have trouble filling in the blanks (see comments in code above). How do I go about implementing this?
More generally: How do I loop through all axis with indexes?