32
votes
Accepted
Looking for a way to create a particular matrix in python in a less amount of time
The NumPy Reference should be the first place you look when you have a problem like this. The operations you need are nearly always in there somewhere. And functions that you find while browsing the ...
24
votes
Python 3 function to rotate an image 90°
You can use NumPy module that's good with arrays and matrices. It has a built-in for exactly that purpose -
...
23
votes
Accepted
Python 3 function to rotate an image 90°
I believe the time complexity is \$O(n^2)\$, but I'd like to know for sure
There's a general method for figuring out the time complexity for a piece of code, which is to annotate each line with the ...
18
votes
Accepted
Matrix class in C#
I like that your braces and indentation is beautifully consistent. But, this is C#, and your Java is showing ;-) - Kudos for the mostly-consistent PascalCase type ...
17
votes
Accepted
AVX SIMD in matrix multiplication
The biggest performance problem (for small matrices), and a common mistake, is doing this:
...
15
votes
Accepted
Triangle area computation and linear transformations
Here are a few of the high- and low-level ideas:
too many comments. Remember, comments tend to age and outdate, they need to be maintained as the code changes. And, if they are over-used, they hurt ...
12
votes
Accepted
Python algorithm that converts array-like data into MathJax
This looks like a handy tool to have around, maybe we can make it even better.
Style
As per the official Python Style Guide (often known by his nickname PEP8), keyword-arguments of functions should ...
11
votes
Matrix class in C#
A bug: Transpose does data[i, j] = data[j, i];, but it returns a new matrix, it's not supposed to change the matrix in-place, ...
11
votes
Accepted
Calculate determinant of a matrix using cofactor expansion
It's much worse than cubic time.
At every "level" of the recursion, there are n recursive calls to a determinant of a matrix that is smaller by 1:
...
10
votes
Accepted
4×4 matrix multiplication
Avoid using namespace std
This can cause name collisions because it adds every name in the std namespace to the global ...
10
votes
Accepted
Rotate an N × N matrix 90 degrees clockwise
Are you up to date?
There is really not much to the problem and there are a variety of solutions.
It is also hard to know what the interviews are looking for. For some it is knowledge of latest ...
10
votes
Accepted
Code to multiply matrices together
Both of the other answers make some very good points, especially when it comes to replacing vector. Two dimensional vectors are particularly iffy because the whole lump of memory will not be ...
10
votes
Create a snail matrix
It is great that you provided example code for us to run. The code layout is
good and the implementation is compact.
Add a docstring to the top of the code to describe its purpose.
For example, the &...
10
votes
Accepted
Python matrix class
Transposes the current Matrix, modifying the underlying arrays object.
You can do it that way. However, it commonly happens in linear algebra code (especially when transcribing formulas from math ...
9
votes
Python algorithm that converts array-like data into MathJax
Python's string formatting has come a long way from the "%s" formatting days. Nowadays classes can even determine on their own how to handle format specifiers. ...
9
votes
Template Matrix Class: implemented some basic functionalities
Overview
Potentially not very efficient.
I have seen other Matrix libraries not do the actual computation until the value is needed. So what is passed around is a Matrix wrapper that contains ...
9
votes
Matrix Class Implementation
Missing all includes and the C++ version, so I had to make some guesses to get this to compile.
assert(rows >= 0);
assert(cols >= 0);
OK, but ...
9
votes
Matrix Class Implementation
Use std::mdspan
Since you tagged the post c++23, consider using std::mdspan in your code. Not only can you use that internally ...
8
votes
Transpose function
Here are some things that may help you improve your program.
Declare variables only where needed
Old-style C required all variables to be declared at the top of the function in which they were used, ...
8
votes
Transpose function
Reconsider ansi
-ansi is equivalent to C89, which is many versions behind the current standard (C99 -> C11 -> C17). C99 is ...
8
votes
Accepted
A Matrix Library in C++;
using namespace std;
Never do that; certainly not in a header - that inflicts the harm onto every source file that includes the header.
Prefer to include your own ...
8
votes
Accepted
Generic matrix library in Java
Dense matrix multiplication in 15572 ms.
Going by the demo code I assume this was the product between two 1000x1000 matrices. By my estimate based on old code the raw operations by themselves could ...
8
votes
Create a snail matrix
First of all, I wish there was more code in wild written like this. This implementation is self-contained and easy to read - I can quickly tell what it is doing just looking through the snippet.
...
8
votes
Accepted
Snail matrix in Java
Expose behavior / Don't manipulate the internal state directly
Object-orientation is about exposing behavior (not data), defining methods that are tailored for the problem at hand, that are meant to ...
7
votes
Accepted
numpy performance of norm calculation using variable dimension
Indeed, there is a better way. Exponentiation aside, we can see that this operation is equivalent to multiplication by an upper triangular matrix of 1. The former is about 100x faster. Here the code (...
7
votes
Accepted
Short square matrix class in C++ using an array
(1) The exception is a good way of handling such errors, but it may be a good idea to provide a non-throwing, less safe accessors. For example, std::vector::at() ...
7
votes
Rotate an N × N matrix 90 degrees clockwise
Cloning the matrix
.slice is a good way to copy the values of an array, instead of getting a reference to the same array. Unfortunately all your values are ...
7
votes
Code to multiply matrices together
std::vector doesn't seem to be the best choice. The real strength of std::vector comes from its ability to dynamically change ...
7
votes
Accepted
Search for row number of maximum value in matrix column
I see a number of things that may help you improve your code.
Pass by const reference where practical
The first argument to MaxLocColumnWise is a ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
matrix × 690performance × 179
c++ × 174
python × 163
algorithm × 101
java × 96
c × 72
beginner × 46
numpy × 45
python-3.x × 40
programming-challenge × 36
r × 35
matlab × 35
c# × 33
mathematics × 32
array × 30
object-oriented × 23
javascript × 21
interview-questions × 21
time-limit-exceeded × 17
image × 17
c++11 × 16
recursion × 16
graph × 14
dynamic-programming × 13