Since I couldn't find anything about this subject, I decided to ask this question here. I am completely beginner and this question might be ridiculous.
Suppose we have A(NxN) matrix and a column vector (B(Nx1)). We have also a function f(i,j) that returns the element of the matrix A at row i and column j.
If we want to do some matrix operation, say, matrix product of A and B we can use: the following (below, C is the result of the matrix product) :
- using the function
f(i,j):
N = 100000
def f(i, j):
return i + j
for i in range(N):
for j in range(N):
s = 0
for k in range(N):
s += f(i, k) * B[k]
C[i] = s
- Using Matrix A (
NxN) (suppose that A is already defined and contains the same elements returned by the functionf)
N = 100000
for i in range(N):
for j in range(N):
s = 0
for k in range(N):
s += A[i,k]*B[k]
C[i] = s
In my opinion, the advantage of the function is that it avoids storing all the values of the matrix thus saving the memory.
My questrions are:
In this case which is the most efficient way to do matrix multiplication (using the function or the matrix itself)?
Is there any performance difference between the two approaches?
EDIT: My question is not specific to Python or any other particular language.