douhan1992 2014-10-13 09:36 采纳率: 100%
浏览 26

将通道中的值传递给Golang中的可变参数函数

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())
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题