Skip to main content
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 ...
Gareth Rees's user avatar
  • 49.6k
23 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 - ...
Divakar's user avatar
  • 896
22 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 ...
Gareth Rees's user avatar
  • 49.6k
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 ...
Mathieu Guindon's user avatar
17 votes
Accepted

AVX SIMD in matrix multiplication

The biggest performance problem (for small matrices), and a common mistake, is doing this: ...
user555045's user avatar
  • 10.1k
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 ...
alecxe's user avatar
  • 17.3k
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 ...
AlexV's user avatar
  • 7,270
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, ...
user555045's user avatar
  • 10.1k
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: ...
user555045's user avatar
  • 10.1k
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 ...
Null's user avatar
  • 1,433
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 ...
Blindman67's user avatar
  • 22.2k
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 ...
Josiah's user avatar
  • 6,006
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 ...
user555045's user avatar
  • 10.1k
9 votes
Accepted

Read matrix from file, the C++11/14 way

I see some things that may help you improve your code. Use the required #includes The code uses std::vector which means that ...
Edward's user avatar
  • 66.5k
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. ...
Graipher's user avatar
  • 41k
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 ...
Loki Astari's user avatar
  • 95.4k
9 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 &...
toolic's user avatar
  • 3,942
8 votes
Accepted

Matrix template with basic operations and utilities

A bug Your multiplication algorithm has a bug: temp[i][j] += matrix[i][k] * rhs[j][k]; Your version will produce $$ \begin{bmatrix} 1 & 2 \\ 3 & 4 \...
coderodde's user avatar
  • 28.9k
8 votes
Accepted

Implementing a tensor product formula

Readability & Maintainability Right now your code is a bit hard to read. A way to greatly help this would be to use better variable names. Put the variable declarations to separate lines and ...
syb0rg's user avatar
  • 21.8k
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, ...
Edward's user avatar
  • 66.5k
8 votes

Transpose function

Reconsider ansi -ansi is equivalent to C89, which is many versions behind the current standard (C99 -> C11 -> C17). C99 is ...
Reinderien's user avatar
  • 62.8k
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 ...
Toby Speight's user avatar
  • 77.3k
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 ...
user555045's user avatar
  • 10.1k
8 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 ...
user555045's user avatar
  • 10.1k
8 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 ...
G. Sliepen's user avatar
  • 58.7k
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 ...
Alexander Ivanchenko's user avatar
7 votes
Accepted

Rotate Matrix using Python

You can use much simpler algorithm in python: Transpose matrix: zip(*matrix) Inverse rows in transposed matrix (equals rotation right): ...
Ariksu's user avatar
  • 86
7 votes
Accepted

Generic matrix type in C++

Use one vector instead of two vectors You can improve performance by using one vector instead of two vectors; you can calculate the 2nd dimension using a little math. This is actually how primitive ...
user2296177's user avatar
  • 3,543

Only top scored, non community-wiki answers of a minimum length are eligible