5 kyu
Square Matrix Multiplication
1,507 of 7,772xDranik
Loading description...
Matrix
Linear Algebra
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This comment has been hidden.
Fork approved by someone, closing
fundamentals ;-)
Will the matrices go beyond 3x3 matrices??? or under 3x3 matrices please help???
Yes, for example in C++ matrices go from 2x2 to 11x11.
C++ fork with some minor improvements:
Please, review and approve/reject
approved
C:
const
-qualifiedfixed here
thanks !
This is JavaScript version Got this error:
STDERR assertSimilar is deprecated, use assert.deepEqual
Then I changed assertSimilar in the Sample Tests and got this:
TypeError: Cannot read properties of undefined (reading 'deepEqual') at Context. (test.js:28:13) at process.processImmediate (node:internal/timers:471:21)
Any advice? Is it something that I can fix on my end?
That's a warning, not an error, the error message is the one above:
And that's because your function isn't returning anything.
C++ Error:
Test Results: the_matrix_multiplication_function -> should_work_for_100_pairs_of_randomly_generated_matrices (FAILED)
Why is this a kata issue?
Your solution has a bug and it crashes. It's not a kata issue.
Note that in tests, matrices can be larger than 2x2. The 2x2 size is given only as an example.
Shoot, I forgot about that and initialized the result matrix with 2x2 size.
I'm so stupid. Thank you.
I think that this kata is a bit too easy for 5 kyu.
Traceback (most recent call last): File "/workspace/default/tests.py", line 3, in test.assert_equals(matrix_mult( File "/workspace/default/.venv/lib/python3.11/site-packages/codewars_test/test_framework.py", line 35, in assert_equals expect(actual == expected, message, allow_raise) File "/workspace/default/.venv/lib/python3.11/site-packages/codewars_test/test_framework.py", line 19, in expect if passed: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
nice kata
My solution passed the test but in the attempt it gives me this message -> TypeError: Cannot set property '0' of undefined.WHAT is the problem??
Your solution crashes when input martices are larger than 2x2. For example, it does not work for this test case:
matrixMultiplication([[1, 2, 3], [3, 2, 1], [2, 1, 3]], [[4, 5, 6], [6, 5, 4], [4, 6, 5]])
Not a kata issue.
That was fun! :D
I agree with Caydenz. My program goes through 300-2000 tests and one constantly returns an error. It would be nice if I could at least see the received and expected value so I know where to start. I am using dynamic memory allocation and do not modify the input matrices. (Decision in C. I'm sorry for my english).
PS. The problem has been resolved. I advise you to use calloc instead of malloc.
the OP solved the kata, I opened another issue about the poor assertion messages
Something is wrong with C tests:
Test Results: the_matrix_multiplication_function should_work_for_100_pairs_of_randomly_generated_matrices (449 of 450 Assertions) should_work_for_some_fixed_assertions (13 of 13 Assertions) should_work_for_the_example_provided_in_the_description (4 of 4 Assertions) Completed in 143.9431ms
Last test in should_work_for_100_pairs_of_randomly_generated_matrices always fails.
I am going to mark this issue as resolved, because the C version works for me and tests seem to be OK.
Memory management for this is not very tricky either, it's quite simple here: make sure you return a dynamically allocated array of pointers to dynamically allocated rows. Also make sure that you do not mutate input matrices.
If you are sure that something is wrong with the kata, reraise the issue and post your solution here, so we could better see what's wrong.
Hello. I've some questions. In C# we have two arrays like: int[,] a = { { 9, 7 }, { 0, 1 } }; int[,] b = { { 1, 1 }, { 4, 12 } };
This is expected value: int[,] expected = { { 37, 93 }, { 4, 12 } };
But how we reach this? My logic is: 9 * 0 + 9 * 4 = 28 9 * 1 + 7 * 12 = 93 1 * 0 + 1 * 4 = 4 1 * 1 + 1 * 12 = 13
Where I got wrong?
This comment has been hidden.
I just completed the kata using the same technique and it worked for me. If your solution crashes, it's probably caused by some other issue, and not by the way how your result matrix is allocated.
your algorithm does not perform matrices mutliplication: you always use the row and column
0
and1
:your code crashes because this performs out-of-bounds accesses for a
1 x 1
matrixThis is WAY too hard for 5 kyu. The numpy approach didn't work for me.
You might have not transformed the numpy array back to list format. The tests compare the expected solution with the actual one by doing "expected == actual" and this "==" type of comparison only works with lists, not numpy arrays. It's easy using numpy
In fact I would say this would be more of a 7 kyu in python because of numpy. All you have to do is know a bit of API and BOOM you got it. That is exactly why I loooove pthon
C# errors are the name of the collection rather than the content.
While in there, it would be really nice if the tabs were standardized to 4 spaces.
this seems to have been fixed by the upgrade to C# 12:
This problem is way to easy for a 5 kyu rating in C#.
How is this an issue? The challenge is the same in other languages, so if the ranking is an issue for C# then it's an issue for the other translations as well.
Also note that this kata was published in 2013, when rankings were a little more "inflated" than they are now.
C# Translation added.Please review and approve~
This comment has been hidden.
you always allocate a 2x2 matrix instead of taking the input size into account. your inizialization of
c
only works for 2x2 matrices also.This comment has been hidden.
Because you're getting a segfault.
Thanks!
For python this is hardly a kyu 7, let be kyu 5, unless imports are forbidden.
Definitely true.
old kata == easy kata...
This comment has been hidden.
There are
n^2
items...but this kata can solve without any loops.
This comment has been hidden.
Java translation - please review.
Great kata!
The kata should not be restricted to square matrices.
there are now 10 languages so it's too late to completely overhaul the kata. there is another kata about the more general case
Python translation kumited -- including random tests and a cleaner description. Please check and approve.
Approved! :)
No random tests.
Added random tests to Ruby. Python and Java already has them. Other languages still not
Could you note the languages which have the issues, otherwise each time anyone wants to address this they have to look at each language test case.
The JavaScript translation needs a major overhaul; the full test suite consists of only 3 fixed tests.
added to JavaScript and CoffeeScript, + upgrade to chai assertions. random tests only missing in Swift
C++ Translation Kumited - Based on C version, tests on larger matrices added.
Approved
I have a error when testing or attempt
This looks to be fixed.
C Translation Kumited - please carefully review and approve :D
The lack of assertions in the Submit Tests of the JavaScript version of this Kata is appalling.
It seems like the test/setup scripts are broken.. I'm seeing these errors (unrelated to my code):
same here.
This looked to be fixed.
i like it
Here's a link to help people easily understand how matrix multiplication works:
http://matrixmultiplication.xyz/
I found this really difficult but very enjoyable! upvoted
I think this kata needs a clarifications if matrices can be of different sizes.
Ruby has a built in Matrix class. This problem is fun, but it also feels a bit pointless given that http://www.ruby-doc.org/stdlib-2.1.1/libdoc/matrix/rdoc/Matrix.html is in the standard library.
yes, I agree it. if used it Matrix class. we only need implement like this Matrix.rows(a) * Matrix.rows(b). very sample
Hey guys! You're absolutely right. However, Javascript doesn't have a built in Matrix class. I wanted this to be available for all three languages, so I avoided making it specific to just one.
uh... I literally built this in JavaScript by myself due to this kata. And I did it without any libraries except figuring out how to multiply matrices--which I haven't done in quite a while. And, you know what? The algorithm that I wrote works for matrices of any size, I think, so I literally got to create something from this.
thanks, xDranik!
4 minor issues, but no comments saying what they are :/
You left matrixMultiplication in the default test and the incomplete solution in Ruby.
Woops, i changed my solution's function name, but not the initial function. it's fixed now
I'd suggest renaming the function matrix_mult (matrixMultiplication is not a conventional ruby function name)
Also, it might be better to link the Wikipedia page on matrix multiplication than describe it in the markdown for the kata.
Thanks for the feedback. I've added the Wiki page explaining how it should work. Also, I'm kinda new to Ruby, so I wasn't aware of the function naming convention. That's fixed too :)
you need to update the default method name in the example. You're still using 'matrixMultiplication' instead of 'matrix_mult'
Sorry about that. Changed my solution instead of the initial function. It's updated now
Seems matrixMultiplication is still used in default examples that are used when clicking "test" button
The pre-filled test case contains a
compareMatrices
function that's not defined in the test harness, so the pre-defined test won't run.Thanks for the heads up! I forgot to change the example Tests when i switched from .assertEquals() to .assertSimilar(). It has been fixed.
I would suggest to use
Test.assertSimilar(expected, actual)
in order to have a better feedback in case of wrong results.Hey BattleRattle, thanks for the feedback! I was planning on using Test.assertEquals, but you can't compare two arrays with ==. I had to use a preloaded function to compare the two :/
That's why you can use
Test.assertSimilar()
(notassertEquals
!), which is able to compare arrays :)No way! Thank you for telling me!! :) I've updated the tests to use Test.assertSimilar(). I should go update my Matrix Addition Kata now xD