Skip to content

Commit 00df28b

Browse files
author
dotchris90
committed
Fixed error when multiply complex matrix
1 parent 2fe522c commit 00df28b

4 files changed

Lines changed: 60 additions & 12 deletions

File tree

src/NumSharp/Shared/Operation.MatrixMultiplication.Complex.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ internal static Complex[] MatrixMultiplyComplexMatrix(Complex[]np1, Complex[]np2
1717

1818
for (int idx = 0; idx < result.Length;idx++)
1919
{
20-
int line = idx % dimNp1[0];
21-
int column = idx / dimNp2[1];
20+
int line = idx / dimNp1[0];
21+
int column = idx % dimNp2[1];
2222

2323
for (int kdx = 0; kdx < iterator;kdx++)
2424
{
25-
result[idx] += np1[line + kdx * dimNp1[0]] * np2[column * dimNp2[0] + kdx];
25+
int index1 = line * dimNp1[1] + kdx;
26+
int index2 = dimNp2[1] * kdx + column;
27+
28+
result[idx] += np1[index1] * np2[index2];
2629
}
2730
}
2831

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace NumSharp.Shared
8+
{
9+
internal static partial class ScalarProduct1D
10+
{
11+
//start 1
12+
internal static Complex[] MuliplyScalarProd1DComplex(Complex[] np1, Complex[]np2)
13+
{
14+
Complex sum = new Complex(0,0);
15+
for (int idx = 0; idx < np1.Length;idx++)
16+
sum += np1[idx] * np2[idx];
17+
18+
return new Complex[]{sum};
19+
}
20+
//end 1
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace NumSharp.Shared
8+
{
9+
internal static partial class ScalarProduct1D
10+
{
11+
//start 1
12+
internal static Quaternion[] MuliplyScalarProd1DQuaternion(Quaternion[] np1, Quaternion[]np2)
13+
{
14+
Quaternion sum = new Quaternion(0,0,0,0);
15+
16+
for (int idx = 0; idx < np1.Length;idx++)
17+
sum += np1[idx] * np2[idx];
18+
19+
return new Quaternion[]{sum};
20+
}
21+
//end 1
22+
}
23+
}

test/NumSharp.UnitTest/Extensions/NdArray.Dot.Test.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public void MatrixMultiplyComplex()
8686
var matrix3 = matrix1.Dot(matrix2);
8787

8888
var matrix4 = new Complex[9];
89-
matrix4[0] = new Complex(39,-7);
90-
matrix4[1] = new Complex(54,-14);
91-
matrix4[2] = new Complex(69,0);
92-
matrix4[3] = new Complex(49,-49);
93-
matrix4[4] = new Complex(68,-68);
94-
matrix4[5] = new Complex(87,-60);
95-
matrix4[6] = new Complex(59,-59);
96-
matrix4[7] = new Complex(82,-82);
97-
matrix4[8] = new Complex(105,-72);
89+
matrix4[0] = new Complex(7,-47);
90+
matrix4[1] = new Complex(30,-30);
91+
matrix4[2] = new Complex(9,-57);
92+
matrix4[3] = new Complex(61,-40);
93+
matrix4[4] = new Complex(68,0);
94+
matrix4[5] = new Complex(75,-48);
95+
matrix4[6] = new Complex(95,-60);
96+
matrix4[7] = new Complex(106,0);
97+
matrix4[8] = new Complex(117,-72);
9898

9999
Assert.IsTrue(Enumerable.SequenceEqual(matrix4,matrix3.Data));
100100
}

0 commit comments

Comments
 (0)