# Tag Info

## New answers tagged matrix

3

Use Symbolic Constants Rather Than Numeric Constants In most programming languages there is a way to define symbolic constants for numbers which makes the code more readable and easier to maintain. When raw numbers are used in code they are sometimes called Magic Numbers. Using Magic Numbers is generally considered a poor programming practice as discussed in ...

1

Your code is pretty much a straightforward textbook solution. Good job on handling the zero-size matrix case, so that a.length doesn't cause a crash. It's more conventional to put a space after flow-control keywords like if and for, so that they look less like function calls: for (int i = 0; i < a.length; i++) { … }

2

How well/bad is this code written? A good first timer implementation. Is the code abstract enough? Mostly. It does make unnecessary assumptions about range. It assumes int math does not overflow. To make more abstract, code could use typedef int TVS_int; to ease future type changes. How can you solve such challenges more quickly? Take advantage ...

0

Since the number of rows is to be equal to the number of columns, you can simply get the number of columns from the first input and stop taking input when the user enters the same number of rows: l = [] while not l or len(l) < len(l): l.append(list(map(int, input().split()))) so that given an input of: 2 3 4 1 2 3 5 6 7 l would become: [[2, 3, ...

0

The two main datatypes for storing matrices in Python (other that the nested list that you use here) are Numpy arrays and Pandas dataframes. Both Numpy and Pandas support reading files, for instance see these links for Numpy and Pandas. You can open a spreadsheet program such as Excel, write the values there, save it as a CSV, then read the CSV into Python. ...

0

An obvious way to improve the code is to use standard containers to manage memory instead of raw pointers. For this code, I would choose std::vector<double> for vector and result, and probably std::vector<std::vector<double>> for matrix (though note that this isn't the most cache-friendly choice for a 2-d matrix). Remember, we can refer ...

7

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 Matrix but that causes the entire input matrix to be duplicated. Better would be to make it const Matrix & because it is not modified and it doesn't need to be duplicated. This is very likely the crux of ...

4

It would be more efficient to use binary exponentiation. Suppose that you want to raise an n by n matrix to the $k^{th}$ power. The current method requires that you do a normal matrix multiplication $k-1$ times. This of course has complexity $k$ multiplied by $f(n)$, Where $f(n)$ is the complexity of a matrix multiplication ( This is usually $n^... 4 In the end, since this was only needed (for now) for$LU$decomposition, I ended up ditching the lower/upper subclasses and went for the classical approach instead (as suggested by @harold), which is storing the lower and upper matrices of the decomposition in a single matrix, by taking advantage of the fact that$L\$is unit triangular:$\$ \begin{...

Top 50 recent answers are included