I am writing a Robotic Manipulation library in Golang, and I use a function of the form matrix.Product(a *matrix.DenseMatrix, bs ...*matrix.DenseMatrix) from the skelterjohn/go.matrix library that I am using for multiplying matrices. As can be seen, the first argument is a static argument, in the sense that only 1 argument goes there, the second one is a variadic argument, in the sense multiple arguments can go there. So lets say I have 6 joints and each joint corresponds to a structure in my code of the form struct{matrix, angle}. I store these 6 structs in an array and pass them to a function that extracts each matrix from this array of structs and puts them in a buffered channel. Now, here is where I have a logic breakdown....O_O!. How do I write a code such that, the 6 matrices in this channel are extracted and placed as arguments to the Product function above ? My code that's trying to do the above is as follows:
// Struct with a matrix and angle
type TwistnTheta struct {
Tw *gm.DenseMatrix
Th float64
}
// Param : arr - Array of structs
// Param : defConf - Default Orientation struct
func ReqFinalConfig(arr []TwistnTheta, defConf DefaultOrientation) *gm.DenseMatrix {
ch1 := make(chan *gm.DenseMatrix, len(arr))
for i := 0; i < len(arr); i++ {
ch1 <- HomConfig(arr[i].Tw.Array(), &arr[i].Th) //Pushes matrices into channel
//fmt.Println(<-ch1, "
")
}
// I extract values from the channels manually and pass them into the function....
// I want to automate this argument passing....
return gm.Product(<-ch1 , <-ch1, <-ch1, <-ch1, defConf.ReqDefConfig())
}
EDIT: Figured out the logic, here is what I did,
func ReqFinalConfig(arr []TwistnTheta, defConf DefaultOrientation) *gm.DenseMatrix {
ch1 := make(chan *gm.DenseMatrix, len(arr))
//var m1 *gm.DenseMatrix
for i := 0; i < len(arr); i++ {
ch1 <- HomConfig(arr[i].Tw.Array(), &arr[i].Th)
//fmt.Println(HomConfig(arr[i].Tw.Array(), &arr[i].Th), "
")
}
close(ch1)
return extractChan(ch1,defConf)
}
func extractChan(ch chan *gm.DenseMatrix, defConf DefaultOrientation) *gm.DenseMatrix {
var Ms []*gm.DenseMatrix
ms1 := <-ch
for val := range ch {
Ms = append(Ms,val)
}
for i := 0; i < len(Ms); i++ {
ms1 = gm.Product(ms1, Ms[i])
}
return gm.Product(ms1, defConf.ReqDefConfig())
}