行列式的符号表示法类似矩阵的型式,差别在于矩阵是以两个中括号将元素包围起来,而行列式是以两条直线将元素包围起来。行列式值(determinant)在解线性联立方程式系统时占有举足轻重的地位,若且为若一个线性联立方程式系统的系数矩阵之行列式值不等于 0,则此系统具有唯一解。行列式可以是 m×n 矩阵,但是一般我们常碰到的问题都是解决 n×n 矩阵的行列式问题,也就是方阵(square matrix)的行列式问题,因此我们可以利用行列式的性质,以高斯消去法将方阵化简成上三角矩阵之后,则该方阵的行列式值即为对角线元素相乘之值。以下的副程式使用了高斯消去法配合 pivoting strategy,如此可将舍入误差(roundoff error)降至最小,并避免除以零的问题。关于行列式更进一步的介绍,请参阅市面上的数值分析书籍。 副程式:Public Function Determinant(m() As Single) As Single Dim i As Long, j As Long, k As Long, row As Long, order As Long Dim r As Long, c As Long, Pivot As Single, Pivot2 As Single, temp() As Single Determinant = 1 row = UBound(m, 1) If UBound(m, 2) <> row Then MsgBox "这不是方阵": Exit Function ReDim temp(1 To row) For i = 1 To row Pivot = 0 For j = i To row For k = i To row If Abs(m(k, j)) > Pivot Then Pivot = Abs(m(k, j)) r = k: c = j End If Next k Next j If Pivot = 0 Then Determinant = 0: Exit Function If r <> i Then order = order + 1 For j = 1 To row temp(j) = m(i, j) m(i, j) = m(r, j) m(r, j) = temp(j) Next j End If If c <> i Then order = order + 1 For j = 1 To row temp(j) = m(j, i) m(j, i) = m(j, c) m(j, c) = temp(j) Next j End If Pivot = m(i, i) Determinant = Determinant * Pivot For j = i + 1 To row Pivot2 = m(j, i) If Pivot2 <> 0 Then For k = 1 To row m(j, k) = m(j, k) - m(i, k) * Pivot2 / Pivot Next End If Next Next Determinant = Determinant * (-1) ^ order End Function 参数说明:m():欲求行列式之方阵。可从以下地址下载源文件: http://www.zjonline.com.cn/vbbible/software/program/code/number/vbNumerical17.zip
Dim i As Long, j As Long, k As Long, row As Long, order As Long
Dim r As Long, c As Long, Pivot As Single, Pivot2 As Single, temp() As Single
Determinant = 1
row = UBound(m, 1)
If UBound(m, 2) <> row Then MsgBox "这不是方阵": Exit Function
ReDim temp(1 To row)
For i = 1 To row
Pivot = 0
For j = i To row
For k = i To row
If Abs(m(k, j)) > Pivot Then
Pivot = Abs(m(k, j))
r = k: c = j
End If
Next k
Next j
If Pivot = 0 Then Determinant = 0: Exit Function
If r <> i Then
order = order + 1
For j = 1 To row
temp(j) = m(i, j)
m(i, j) = m(r, j)
m(r, j) = temp(j)
Next j
End If
If c <> i Then
order = order + 1
For j = 1 To row
temp(j) = m(j, i)
m(j, i) = m(j, c)
m(j, c) = temp(j)
Next j
End If
Pivot = m(i, i)
Determinant = Determinant * Pivot
For j = i + 1 To row
Pivot2 = m(j, i)
If Pivot2 <> 0 Then
For k = 1 To row
m(j, k) = m(j, k) - m(i, k) * Pivot2 / Pivot
Next
End If
Next
Next
Determinant = Determinant * (-1) ^ order
End Function
参数说明:m():欲求行列式之方阵。可从以下地址下载源文件:
http://www.zjonline.com.cn/vbbible/software/program/code/number/vbNumerical17.zip