'Make a MMULT that can handle complex number
I tried to make a function that can do complex matrix multiplication (let's call it IMMMULT), it somehow work but it can't do like
=IMMMULT(IMMMULT(A,B),C)
or
=IMMMULT(TRANSPOSE(D),E)
Obviously my input setting has some problems but I don't know how to fix it. :(
Here is my file and code, please help me. Orz
https://drive.google.com/drive/folders/1XMLdngg_wLxz5s2JE1weCZd_RMG_-cjP?usp=sharing
Function IMMMULT(Leftmatrix, Rightmatrix)
'(a+bi)(c+di)=(ac-bd)+(ad+bc)i
Dim rowL As Integer, colL As Integer, rowR As Integer, colR As Integer
Dim result_R As Variant, result_I As Variant, result As Variant
rowL = Leftmatrix.Rows.Count
colL = Leftmatrix.Columns.Count
rowR = Rightmatrix.Rows.Count
colR = Rightmatrix.Columns.Count
'column of A and row of B need to be same
If (colL <> rowR) Then
MsgBox "Dimension Error"
Exit Function
End If
'Real part
ReDim result_R(1 To rowL, 1 To colR)
For i = 1 To rowL
For j = 1 To colR
For k = 1 To colL
result_R(i, j) = result_R(i, j) + Application.WorksheetFunction.ImReal(Leftmatrix(i, k).Value) _
* Application.WorksheetFunction.ImReal(Rightmatrix(k, j).Value) _
- Application.WorksheetFunction.Imaginary(Leftmatrix(i, k).Value) _
* Application.WorksheetFunction.Imaginary(Rightmatrix(k, j).Value)
Next k
Next j
Next i
'Imaginary part
ReDim result_I(1 To rowL, 1 To colR)
For i = 1 To rowL
For j = 1 To colR
For k = 1 To colL
result_I(i, j) = result_I(i, j) + Application.WorksheetFunction.ImReal(Leftmatrix(i, k).Value) _
* Application.WorksheetFunction.Imaginary(Rightmatrix(k, j).Value) _
+ Application.WorksheetFunction.Imaginary(Leftmatrix(i, k).Value) _
* Application.WorksheetFunction.ImReal(Rightmatrix(k, j).Value)
Next k
Next j
Next i
'put together
ReDim result(1 To rowL, 1 To colR)
For i = 1 To rowL
For j = 1 To colR
result(i, j) = Application.WorksheetFunction.Complex(result_R(i, j), result_I(i, j))
Next j
Next i
IMMMULT = result
End Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
