⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complexmodule.bas

📁 科学与工程数值计算算法(Visual Basic版) 附赠的光盘包含了本书中全部的源代码
💻 BAS
📖 第 1 页 / 共 2 页
字号:
Attribute VB_Name = "ComplexModule"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  功能:  复数的运算
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 定义复数类型
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type Complex
    x As Double     ' 复数实部
    y As Double     ' 复数虚部
End Type

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 定义常量 PI
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const PI = 3.14159265358979

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  函数名:CMul
'  功能:  计算复数的乘法
'  参数:  cpxZ1   - Complex型变量
'          cpxZ2   - Complex型变量
'  返回值:Complex型,乘积
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CMul(cpxZ1 As Complex, cpxZ2 As Complex) As Complex
    CMul.x = cpxZ1.x * cpxZ2.x - cpxZ1.y * cpxZ2.y
    CMul.y = cpxZ1.x * cpxZ2.y + cpxZ1.y * cpxZ2.x

End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  函数名:CDiv
'  功能:  计算复数的除法
'  参数:  cpxZ1   - Complex型变量,被除复数
'          cpxZ2   - Complex型变量,除数
'  返回值:Complex型,商
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CDiv(cpxZ1 As Complex, cpxZ2 As Complex) As Complex
    ' 局部变量
    Dim e As Double, f As Double
    
    If Abs(cpxZ2.x) >= Abs(cpxZ2.y) Then
        e = cpxZ2.y / cpxZ2.x
        f = cpxZ2.x + e * cpxZ2.y
        
        CDiv.x = (cpxZ1.x + cpxZ1.y * e) / f
        CDiv.y = (cpxZ1.y - cpxZ1.x * e) / f
    Else
        e = cpxZ2.x / cpxZ2.y
        f = cpxZ2.y + e * cpxZ2.x
        
        CDiv.x = (cpxZ1.x * e + cpxZ1.y) / f
        CDiv.y = (cpxZ1.y * e - cpxZ1.x) / f
    End If
    
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  函数名:CAbs
'  功能:  计算复数的模
'  参数:  cpxZ   - Complex型变量,用于求模的复数
'  返回值:Double型,指定复数的模
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CAbs(cpxZ As Complex) As Double
    '求取实部和虚部的绝对值
    cpxZ.x = Abs(cpxZ.x)
    cpxZ.y = Abs(cpxZ.y)
    
    ' 特殊值处理
    If cpxZ.x = 0 Then
        CAbs = cpxZ.y
        Exit Function
    End If
    If cpxZ.y = 0 Then
        CAbs = cpxZ.x
        Exit Function
    End If
    
    ' 计算模
    If cpxZ.x > cpxZ.y Then
        CAbs = cpxZ.x * Sqr(1 + (cpxZ.y / cpxZ.x) * (cpxZ.y / cpxZ.x))
        Exit Function
    End If
    
    CAbs = cpxZ.y * Sqr(1 + (cpxZ.x / cpxZ.y) * (cpxZ.x / cpxZ.y))
   
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  函数名:CRoot
'  功能:  计算复数的根
'  参数:  cpxZ   - Complex型变量,用于求根的复数
'          n      - Integer型变量,复数的根次
'          cpxZR  - Complex型一维数组,用于存放求得的复数的根
'  返回值:Integer型,复数的根的个数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CRoot(cpxZ As Complex, n As Integer, cpxZR() As Complex) As Integer
    ' 局部变量
    Dim i As Integer
    Dim r As Double
    Dim c As Double, c1 As Double, ck As Double, cs As Double
    Dim s As Double, s1 As Double, sk As Double, sc As Double
    
     ' 特殊值处理
    If cpxZ.y = 0 Then
        r = Exp(Log(Abs(cpxZ.x)) / n)
        cs = 0
    Else
        If cpxZ.x = 0 Then
            r = Exp(Log(Abs(cpxZ.y)) / n)
            If cpxZ.y > 0 Then
                cs = 1.5707963268
            Else
                cs = -1.5707963268
            End If
        Else
            r = Exp(Log(cpxZ.x * cpxZ.x + cpxZ.y * cpxZ.y) / n / 2)
            cs = Atn(cpxZ.y / cpxZ.x)
        End If
    End If
    
    If cpxZ.x < 0 Then
        cs = cs + PI
    End If
    
    ' 中间值,用于提高运算速度
    cs = cs / n
    c = Cos(cs)
    s = Sin(cs)
    
    sc = 2 * PI / n
    c1 = Cos(sc)
    s1 = Sin(sc)
    
    ck = 1
    sk = 0
    
    ' 第一个根
    cpxZR(1).x = c * r
    cpxZR(1).y = s * r
    
    ' 其余n-1个根
    For i = 2 To n
        cs = ck * c1 - sk * s1
        sc = sk * c1 + ck * s1
        
        ' 递推公式
        cpxZR(i).x = (c * cs - s * sc) * r
        cpxZR(i).y = (s * cs - c * sc) * r
        
        ck = cs
        sk = sc
    Next i
    
    ' 共有n个根
    CRoot = n
    
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  函数名:CPow
'  功能:  计算复数的实幂指数
'  参数:  cpxZ   - Complex型变量,用于求实幂指数的复数
'          w      - Double型变量,复数的实幂指数幂次
'  返回值:Complex型变量,为求得的复数的实幂指数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CPow(cpxZ As Complex, w As Double) As Complex
    ' 局部变量
    Dim r As Double, t As Double
    
     ' 特殊值处理
    If cpxZ.x = 0 And cpxZ.y = 0 Then
        CPow.x = 0
        CPow.y = 0
        Exit Function
    End If
    
    ' 幂运算公式中的三角函数运算
    If cpxZ.x = 0 Then
        If cpxZ.y > 0 Then
            t = 1.5707963268
        Else
            t = -1.5707963268
        End If
            
    Else
        If cpxZ.x > 0 Then
            t = Atn(cpxZ.y / cpxZ.x)
        Else
            If cpxZ.y >= 0 Then
                t = Atn(cpxZ.y / cpxZ.x) + PI
            Else
                t = Atn(cpxZ.y / cpxZ.x) - PI
            End If
        End If
    End If
    
    ' 模的幂
    r = Exp(w * Log(Sqr(cpxZ.x * cpxZ.x + cpxZ.y * cpxZ.y)))
    
    ' 复数的实幂指数
    CPow.x = r * Cos(w * t)
    CPow.y = r * Sin(w * t)
    
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  模块名:ComplexModule.bas
'  函数名:CCPow
'  功能:  计算复数的复幂指数
'  参数:  cpxZ   - Complex型变量,用于求复幂指数的复数
'          cpxN   - Complex型变量,复数的复幂指数幂次
'          n      - Integer型变量,2*PI的整数倍数
'  返回值:Complex型变量,为求得的复数的复幂指数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CCPow(cpxZ As Complex, cpxN As Complex, n As Integer) As Complex

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -