Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix matrix multiplication #141

Merged
merged 8 commits into from
Jun 23, 2021
Merged

Fix matrix multiplication #141

merged 8 commits into from
Jun 23, 2021

Conversation

toor1245
Copy link
Owner

No description provided.

@toor1245 toor1245 added the bug Something isn't working label Jun 22, 2021
@toor1245 toor1245 requested a review from Dilorfin June 22, 2021 22:01
@toor1245 toor1245 self-assigned this Jun 22, 2021
Comment on lines 120 to 148
private static unsafe double[,] MulMatrixTest(double[,] left, double[,] right)
{
var m = left.GetLength(0);
var n = right.GetLength(1);
var K = left.GetLength(1);
var len1 = left.Length;
var matrix = new double[m, n];

fixed (double* pointer1 = left)
fixed (double* pointer2 = right)
fixed (double* pointer3 = matrix)
{
var span1 = new Span<double>(pointer1, len1);

for (var i = 0; i < m; i++)
{
var c = pointer3 + i * n;

for (var k = 0; k < K; k++)
{
var b = pointer2 + k * n;
var a = span1[i * K + k];
for (var j = 0; j < n; j++) c[j] += a * b[j];
}
}

return matrix;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a f is here we have?)
Are you sure that we should use "unsafe code" is Unit Tests?
Obviously, I don't think so! As for me code in unit tests must be as simple&stupid as it only can be

Copy link
Owner Author

@toor1245 toor1245 Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is ctrl C + Ctrl V for testing:)

Copy link
Collaborator

@Dilorfin Dilorfin Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but in the end it doesn't even matter, copying code for unit tests from tested code...

Comment on lines 102 to 111
var matrixB = new double[size, size];
for (int i = 0; i < matrixB.GetLength(0); i++)
{
for (int j = 0; j < matrixB.GetLength(1); j++)
{
matrixB[i, j] = 1;
}
}

var expected = MulMatrixTest(matrixB, matrixB);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiplication result for square matrixes that consist only of 1 will be just matrix sizexsize that consist of size

@toor1245 toor1245 merged commit 151ac23 into master Jun 23, 2021
@toor1245 toor1245 linked an issue Jun 23, 2021 that may be closed by this pull request
@Dilorfin Dilorfin deleted the matrix-multiplication-fix branch July 26, 2022 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug in multiplication of matrix
2 participants