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
  • 50.1k
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 - ...
Divakar's user avatar
  • 926
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 ...
Gareth Rees's user avatar
  • 50.1k
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
  • 12.6k
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.5k
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,363
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
  • 12.6k
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
  • 12.6k
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,463
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.9k
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,106
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 &...
toolic's user avatar
  • 16.4k
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
  • 12.6k
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
  • 41.7k
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
  • 97.7k
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 ...
user555045's user avatar
  • 12.6k
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 ...
G. Sliepen's user avatar
  • 69.5k
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
  • 67.2k
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
  • 71.2k
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 ...
Toby Speight's user avatar
  • 88.7k
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
  • 12.6k
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. ...
STerliakov's user avatar
  • 2,142
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

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 (...
Amir Dib's user avatar
  • 106
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() ...
Mateusz Grzejek's user avatar
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 ...
Kruga's user avatar
  • 1,169
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 ...
vnp's user avatar
  • 58.7k
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 ...
Edward's user avatar
  • 67.2k

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