vb.net的矩阵乘法类 [英] matrix multiplication class for vb.net

查看:131
本文介绍了vb.net的矩阵乘法类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我班上矩阵乘法的代码.
但我没有得到所需的乘法结果
由于索引数超过索引数组的维数"而出现错误
请帮我解决这个问题...
解冻你.....



this is the code for my class for matrix multiplication.
but i am not getting the required result of multiplication
getting error as "Number of indices exceeds the number of dimensions of the indexed array"
please help me to solve this problem...
thaking you.....


Public Class complexnum

    Private x As Double
    Private y As Double
       Private matrixA As Double
    Private matrixB As Double
    Private e, f, g As Integer
    Public Property Real() As Double
        Get
            Return x
        End Get
        Set(ByVal Value As Double)
            x = Value
        End Set
    End Property
    Public Property Imaginary() As Double
        Get
            Return y
        End Get
        Set(ByVal Value As Double)
            y = Value
        End Set
    End Property
   

    Public Sub New(Optional ByVal real As Double = 0.0, Optional ByVal imaginary As Double = 0.0)
        x = real
        y = imaginary
    End Sub
         Public Function multiplication() As complexnum
        Dim row1() As Double
        Dim row2() As Double
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim tmprow1 As Double
        Dim tmprow2 As Double

        For i = 0 To e - 1
            For j = 0 To f - 1
                For k = 0 To g - 1

                    Dim result(i, j) As Double
                    result(i, j) = result(i, j) + (row1(i, k) * row2(k, j))
                Next
            Next
        Next

    End Function
    Public Overrides Function ToString() As String
        Dim result As String = x.ToString()
        If y >= 0 Then
            result &= " + " & _
                y.ToString() & _
               "i"
        Else
            result &= " - " & _
                (-y).ToString() & _
                "i"
        End If

        Return result
    End Function
End Class



这是输入值并获得输出的形式



here is the form in which the values are inputed and getting output

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       ''Matrix1 values are inputed
        ar1.Real = Val(ar1txt.Text)
        ai1.Imaginary = Val(ai1txt.Text)
        ar2.Real = Val(ar2txt.Text)
        ai2.Imaginary = Val(ai2txt.Text)
        ar3.Real = Val(ar3txt.Text)
        ai3.Imaginary = Val(ai3txt.Text)
        ar4.Real = Val(ar4txt.Text)
        ai4.Imaginary = Val(ai4txt.Text)

      ''Matrix2 values are inputed
        br1.Real = Val(br1txt.Text)
        bi1.Imaginary = Val(bi1txt.Text)
        br2.Real = Val(br2txt.Text)
        bi2.Imaginary = Val(bi2txt.Text)
        br3.Real = Val(br3txt.Text)
        bi3.Imaginary = Val(bi3txt.Text)
        br4.Real = Val(br4txt.Text)
        bi4.Imaginary = Val(bi4txt.Text)

     ''output of complex matrix

        cr1 = cr1.multiplication(ar1, bi1)
        ans1txt.Text = cr1.Real & "+i" & cr1.Imaginary

        cr2 = cr2.multiplication(ar2, bi2)
        ans2txt.Text = cr2.Real & "+i" & cr2.Imaginary

        cr3 = cr3.multiplication(ar3, bi3)
        ans3txt.Text = cr3.Real & "+i" & cr3.Imaginary

        cr4 = cr4.multiplication(ar4, bi4)
        ans4txt.Text = cr4.Real & "+i" & cr4.Imaginary
    End Sub
End Class

推荐答案

您的代码中有很多错误,例如,您从未为row1row2数组分配内存.
There are many errors in your code, for instance, you never allocate memory for the row1, row2 arrays.


您尝试访问数组变量row1()row2()而不定义大小和值.

正如CPallini所说,您需要为这些数组分配数据,以便可以在计算中使用它们的值.
you trying to access the array variables row1() and row2() without defining there size and values.

as CPallini has stated you need to assign data to these arrays so that you can use their values in your calculation.


这篇关于vb.net的矩阵乘法类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆