迭代复数

Iterate over complex numbers

我需要遍历 complex refractive index = n + ik

我制作了两个 floats.Span() 填充均匀间隔的数字,包含我需要迭代的每个 n 和 k。我现在如何 "mix" 这两个值,以便我可以对每个可能的组合进行 for 循环?

我需要这样的东西:

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

如果它不是切片,我该如何迭代它?

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) { ... }

floats.Span 文档建议使用带有 range 子句的 for


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

Go 编程语言文档解释了 Go 中的复数。


例如,

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)
        }
    }
}

输出:

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)