Golang 找到复数共轭

Golang 找到复数共轭

Go语言提供了内置的complex数学函数和基本常数,借助于cmplx包。你可以使用math/cmplx包中提供的Conj()方法来找到指定复数的共轭。因此,你需要在你的程序中添加math/cmplx包,并使用import关键字来访问Conj()方法。

语法:

func Conj(y complex128) complex128

我们通过以下例子来讨论此概念:

例1:

// Golang程序演示如何找到指定复数的共轭
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// 主方法
func main() {
  
    // 找到指定复数的共轭
    // 使用Conj()方法
    res_1 := cmplx.Conj(5i)
    res_2 := cmplx.Conj(-1 + 12i)
    res_3 := cmplx.Conj(-7 - 9i)
  
    // 显示结果
    fmt.Printf("Result 1: %.1f", res_1)
    fmt.Printf("\nResult 2:  %.1f", res_2)
    fmt.Printf("\nResult 3:  %.1f", res_3)
} 

输出:

Result 1: (0.0-5.0i)
Result 2: (-1.0-12.0i)
Result 3: (-7.0+9.0i)

例2:

// Golang程序演示如何找到指定复数的共轭
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// 主方法
func main() {
  
    cnumber_1 := complex(0, 2)
    cnumber_2 := complex(1, 6)
  
    // 找到所给出复数的共轭
    cvalue_1 := cmplx.Conj(cnumber_1)
    cvalue_2 := cmplx.Conj(cnumber_2)
  
    // 所给值的总和
    res := cvalue_1 + cvalue_2
  
    // 显示结果
    fmt.Println("Complex Number 1: ", cnumber_1)
    fmt.Printf("Conjugate 1: %.1f", cvalue_1)
  
    fmt.Println("\nComplex Number 2: ", cnumber_2)
    fmt.Printf("Conjugate 2: %.1f ", cvalue_2)
    fmt.Printf("\nSum : %.1f", res)
  
} 

输出:

Complex Number 1:  (0+2i)
Conjugate 1: (0.0-2.0i)
Complex Number 2:  (1+6i)
Conjugate 2: (1.0-6.0i) 
Sum : (1.0-8.0i)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程