dongxiequ3724 2018-07-20 11:07
浏览 74
已采纳

遍历复数

I need to iterate over the complex refractive index = n + ik

I made two floats.Span() filled with evenly spaced numbers, containing every n and k that I need to iterate over. How do I "mix" these two values now so I can make a for loop over every possible combination?

I need something like:

0.1+0.1i, 0.1+0.2i, 0.1+0.2i, (...) 0.2+0.1i, 0.2+0.2i, (...)

And if it is not a slice, how do I iterate over it?

  • 写回答

1条回答 默认 最新

  • dpqy77560 2018-07-20 11:20
    关注

    I need to iterate over the complex refractive index = n + ik. I made two floats.Span(). how do I iterate over them? How do I "mix" these two values?


    package floats

    import "gonum.org/v1/gonum/floats"

    func Span

    func Span(dst []float64, l, u float64) []float64
    

    Span returns a set of N equally spaced points between l and u, where N is equal to the length of the destination. The first element of the destination is l, the final element of the destination is u.

    Panics if len(dst) < 2.

    Span also returns the mutated slice dst, so that it can be used in range expressions, like:

    for i, x := range Span(dst, l, u) { ... }
    

    The floats.Span documentation suggests using for wiith a range clause.


    The Go Programming Language Specification

    Manipulating complex numbers

    Three functions assemble and disassemble complex numbers. The built-in function complex constructs a complex value from a floating-point real and imaginary part, while real and imag extract the real and imaginary parts of a complex value.

    complex(realPart, imaginaryPart floatT) complexT
    real(complexT) floatT
    imag(complexT) floatT
    

    The Go programming language documentation explains complex numbers in Go.


    For example,

    package main
    
    import (
        "fmt"
    
        "gonum.org/v1/gonum/floats"
    )
    
    func main() {
        loN, hiN := 1.0, 4.0
        dstN := make([]float64, int((hiN-loN)+1))
        loK, hiK := 5.0, 8.0
        dstK := make([]float64, int((hiK-loK)+1))
        for _, n := range floats.Span(dstN, loN, hiN) {
            for _, k := range floats.Span(dstK, loK, hiK) {
                c := complex(n, k)
                fmt.Println(n, k, c)
            }
        }
    }
    

    Output:

    1 5 (1+5i)
    1 6 (1+6i)
    1 7 (1+7i)
    1 8 (1+8i)
    2 5 (2+5i)
    2 6 (2+6i)
    2 7 (2+7i)
    2 8 (2+8i)
    3 5 (3+5i)
    3 6 (3+6i)
    3 7 (3+7i)
    3 8 (3+8i)
    4 5 (4+5i)
    4 6 (4+6i)
    4 7 (4+7i)
    4 8 (4+8i)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮