0

I am working on a 2D matrix and finding sum of elements, below is my logic:

def calculateSum(a, x, y):
    s = 0;
    for i in range(0,x+1):
       for j in range(0,y+1):
           s = s + a[i][j];
    print(s)
    return s

def check(a):
    arr = []

    x = 0
    y = 0
    for i in range(len(a)):
        row = []
        y = 0
        for j in range(len(a[i])):
            row.append(calculateSum(a, x, y))
            y = y + 1
        x = x + 1
        print(row)

check([[1, 2], [3, 4]])

calculateSum is the function that calculates sum of elements.

Now my question is, if the matrix size is huge then is there is a way to improve performance of the above program?

Update:

import numpy as np
def calculateSum(a, x, y):
   return np.sum(a[x:,y:])

After using numpy I am getting error as TypeError: list indices must be integers or slices, not tuple if I use numpy

learner
  • 6,062
  • 14
  • 79
  • 139
  • 1
    If the matrix is huge and efficiency is an issue, you should consider Numpy. It has a convenient `sum()` function too. – Mark Jan 25 '19 at 06:01
  • https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python – tkausl Jan 25 '19 at 06:01
  • @MarkMeyer, How can I specify what elements I want to sum using Numpy, because in my above program I am using selected items from input array to calculate sum? – learner Jan 25 '19 at 06:05
  • @learner, you can sum over a slice of the matrix, i.e `np.sum(a[1:,1:])` to sum everthing from the second row and column. – Mark Jan 25 '19 at 06:08
  • @MarkMeyer, I am getting `TypeError: list indices must be integers or slices, not tuple` error when I used numpy, can you please tell me what is the issue here? I updated my post with latest code. – learner Jan 25 '19 at 06:15
  • @learner Can you please give the desired output? – iz_ Jan 25 '19 at 06:18
  • @Tomothy32, to get sum of elements in array for given indices – learner Jan 25 '19 at 06:22
  • @learner When I run your original code I get `[1, 3]` and `[4, 10]`. Can you enlighten me on how this relates to `[[1, 2], [3, 4]`? – iz_ Jan 25 '19 at 06:24
  • @learner, if you want to use Numpy, you need to pass it a Numpy array: `calculateSum(np.array([[1, 2], [3, 4]]), 1, 1)` – Mark Jan 25 '19 at 06:27
  • @learner. You've completely changed the question since you first asked it, invalidating all the existing answers. Please don't do that. Post another question instead regarding your updated code. – Mad Physicist Jan 25 '19 at 06:31

1 Answers1

0

As the matrix dimensions increases, Efficiency will fall, the efficient way to deal with this is to parallelize the task of summing the values, this is possible because addition follows Associative property.

Luckily for you this parallelization is already implemented in a library known as numpy. To get started with numpy, use pip install numpy To get an overview of the library visit: https://www.geeksforgeeks.org/numpy-in-python-set-1-introduction/

And for your question you will need to use function numpy.sum()

Edit:

Also as @Mad Physicist pointed out Numpy also has packed memory layout and the routines are implemented in C which boost its speed even further.

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35
  • Numpy's efficiency is not only in parallelization. It's also the packed memory layout and the routines implemented in C. – Mad Physicist Jan 25 '19 at 06:16