gpt4 book ai didi

go - 迭代复数

转载 作者:IT王子 更新时间:2023-10-29 02:20:36 25 4
gpt4 key购买 nike

我需要迭代 complex refractive index = n + ik

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

我需要这样的东西:

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

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

最佳答案

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)

关于go - 迭代复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51441154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com