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():欲求行列式之方阵。
范例:
1.由键盘输入矩阵元素:
Private Sub Command1_Click()
Dim Keyin As String, m() As Single
Keyin = InputBox("请输入方阵(square matrix)," _
& "以分号"";""来分隔列元素,以空白来分隔行元素。" _
& vbNewLine & "例如有一3×3的矩阵:" & vbNewLine & "1 2 3" & vbNewLine & _
"4 5 6 " & vbNewLine & "7 8 9" & vbNewLine & "则输入: 1 2 3;4 5 6;7 8 9 ")
If SepStrToMatrix(Keyin, ";", " ", m) Then
Debug.Print Determinant(m)
Else
MsgBox "矩阵输入有误,请重新输入。"
End If
End Sub
其中 SepStrToMatrix 这个副程式,请参阅本站的 将字串拆解成阵列。
Private Sub Command2_Click()
Dim FileNo As Long, Keyin As String
Dim temp As String, m() As Single
FileNo = FreeFile
Open "c:\users\det.txt" For Input As #FileNo
Do While Not EOF(FileNo)
Line Input #FileNo, temp
If Trim(temp) <> "" Then Keyin = Keyin & temp & ";"
Loop
Close #FileNo
Keyin = Mid(Keyin, 1, Len(Keyin) - 1)
If SepStrToMatrix(Keyin, ";", " ", m) Then
Debug.Print Determinant(m)
Else
MsgBox "方阵输入有误,请重新输入。"
End If
End Sub
其中 SepStrToMatrix 这个副程式,请参阅本站的 将字串拆解成阵列。
'******本过程产生一个n元一次方程,系数随机*****
dim n
'n必须是数字
if not isnumeric(member.value) then exit sub
'必须大于1
if not cint(member.value)>1 then exit sub
'定义n元一次方程
n = cint(cint(member.value))'元数
dim i,j,str
redim matrix(n,n)
'系数
dim quotiety
str = ""
for i=0 to n-1
for j=0 to n-1
quotiety = RRR()
matrix(i,j) = quotiety
str = str & "<input value=' " & quotiety & " '/> "
str = str & "x" & j+1
if j<>n-1 then
str = str & " + "
else
quotiety = RRR()
matrix(i,j+1) = quotiety
str = str & " = "
str = str & "<input value=' " & quotiety & " '/><br/>" & vbcrlf
end if
next
next
ppp.innerHTML = str
'test
end sub
function RRR()
'用来产生-9到9随即整数的函数
randomize
RRR =cint(rnd * 18)-9
end function
sub test()
'测试用函数
dim str:str=""
for i=0 to ubound(matrix,1)
for j=0 to ubound(matrix,2)
str = str & matrix(i,j)
next
str = str & vbcrlf
next
alert(str)
end sub
</SCRIPT>
</BODY>
</HTML>
if ( CInt(A0)<>0 ) then
response.write "X1 = "& A1/A0 & "<br>"
response.write "X2 = "& A2/A0 & "<br>"
response.write "X3 = "& A3/A0 & "<br>"
else
response.write "该方程组的系数行列式的值为0,本程序无法求解!"
end if
%>
</body>
</html>
<%
function getValue(a)
getValue = 0
if ( ubound(a)<>ubound(a(0))) then
exit function
end if
if ( ubound(a)=1 ) then
getValue = a(0)(0)*a(1)(1) - a(0)(1)*a(1)(0)
else
'按第一行展开
for i=0 to ubound( a(0) )
if ( CInt( a(0)(i) )<>0 ) then
tmp = Array()
redim tmp( ubound(a)-1 )
for j=0 to UBound(tmp)
'构造列向量
tmp1 = Array()
redim tmp1( ubound(a(0))-1 )
indexK = 0
for k=0 to ubound( a(0) )
if ( k<>i ) then
tmp1(indexK) = a(j+1)(k)
indexK = indexK + 1
end if
next
tmp(j) = tmp1
next
if ( i mod 2=0 ) then
getValue = getValue + a(0)(i)*getValue(tmp)
else
getValue = getValue - a(0)(i)*getValue(tmp)
end if
end if
next
end if
end function
%>