Golang complex64 类型
最后修改时间 2025 年 5 月 8 日
本教程将解释如何在 Go 中使用 complex64
内置类型。我们将通过实际的复数运算示例涵盖复数基础知识。
complex64
类型表示实部和虚部为 float32 的复数。它是 Go 中用于复数运算的内置数值类型之一。
在 Go 中,complex64
为复数提供了高效的存储。complex64 值的运算对两个分量都使用 32 位浮点精度。
基本 complex64 创建
创建 complex64 值最简单的方法是使用 complex
函数。此示例演示了基本的复数创建。
注意:虚部用 'i' 后缀表示。
basic_complex.go
package main import "fmt" func main() { // Create complex numbers a := complex(3.0, 4.0) // 3 + 4i b := complex(1.5, -2.5) // 1.5 - 2.5i fmt.Printf("a = %v (type %T)\n", a, a) fmt.Printf("b = %v (type %T)\n", b, b) // Access real and imaginary parts fmt.Println("Real part of a:", real(a)) fmt.Println("Imaginary part of a:", imag(a)) }
complex
函数在给出 float32 参数时创建 complex64 值。real
和 imag
函数用于提取实部和虚部。
复数算术运算
Complex64 值支持标准的算术运算。此示例展示了复数的で基本算术运算。
complex_arithmetic.go
package main import "fmt" func main() { x := complex(2.0, 3.0) y := complex(1.0, -1.0) // Addition sum := x + y fmt.Println("Sum:", sum) // (3+2i) // Subtraction diff := x - y fmt.Println("Difference:", diff) // (1+4i) // Multiplication product := x * y fmt.Println("Product:", product) // (5+1i) // Division quotient := x / y fmt.Println("Quotient:", quotient) // (-0.5+2.5i) }
复数运算遵循标准的数学规则。运算在各个分量上进行,同时遵循虚数单位的性质。
复共轭和模
常见的复数运算包括共轭和模。此示例展示了如何计算这些属性。
complex_properties.go
package main import ( "fmt" "math" ) func magnitude(c complex64) float32 { r := real(c) i := imag(c) return float32(math.Sqrt(float64(r*r + i*i))) } func main() { z := complex(4.0, 3.0) // Complex conjugate conjugate := complex(real(z), -imag(z)) fmt.Println("Conjugate:", conjugate) // (4-3i) // Magnitude (absolute value) fmt.Println("Magnitude:", magnitude(z)) // 5 // Built-in complex64 operations fmt.Println("Built-in magnitude:", cmplx.Abs(z)) }
共轭会改变虚部的符号。模是使用勾股定理计算的。Go 的 math/cmplx 包提供了这些运算。
在函数中使用 complex64
Complex64 值可以作为参数传递给函数并从函数返回。此示例演示了复数函数的使用。
complex_functions.go
package main import ( "fmt" "math/cmplx" ) func rotate(c complex64, angle float32) complex64 { // Convert angle to radians rad := complex(0, float32(cmplx.Pi)*angle/180) return c * cmplx.Exp(rad) } func main() { point := complex(1.0, 0.0) // Rotate 90 degrees rotated := rotate(point, 90) fmt.Println("After 90° rotation:", rotated) // ~(0+1i) // Rotate another 90 degrees rotated = rotate(rotated, 90) fmt.Println("After 180° rotation:", rotated) // ~(-1+0i) }
rotate
函数按给定角度旋转复数。复数自然地表示 2D 点和旋转。
信号处理中的 Complex64
复数是信号处理中的基本概念。此示例显示了一个基本的傅立叶变换模拟。
signal_processing.go
package main import ( "fmt" "math" ) func dft(signal []complex64) []complex64 { N := len(signal) spectrum := make([]complex64, N) for k := 0; k < N; k++ { var sum complex64 for n := 0; n < N; n++ { angle := -2 * math.Pi * float64(k*n) / float64(N) c := complex(float32(math.Cos(angle)), float32(math.Sin(angle))) sum += signal[n] * c } spectrum[k] = sum } return spectrum } func main() { // Create a simple signal (sine wave) signal := make([]complex64, 8) for i := range signal { signal[i] = complex(float32(math.Sin(2*math.Pi*float64(i)/8)), 0) } // Compute DFT spectrum := dft(signal) fmt.Println("Signal:", signal) fmt.Println("Spectrum:", spectrum) }
此离散傅立叶变换实现使用 complex64 进行高效存储。每个频率分量都表示为复数。
来源
本教程通过复数运算和应用的实际示例,涵盖了 Go 中的 complex64
类型。