thearn commited on
Commit
bc5bc31
·
1 Parent(s): bf77b60

updated for python 3

Browse files
__pycache__/algorithms.cpython-313.pyc ADDED
Binary file (4.12 kB). View file
 
algorithms.py CHANGED
@@ -4,7 +4,9 @@ import itertools
4
  import time
5
 
6
 
7
- def local_search(A, loc):
 
 
8
  """
9
  Utility function to verify local optimality of a
10
  subarray slice specification 'loc' of array 'A'
@@ -26,7 +28,7 @@ def local_search(A, loc):
26
  return loc2, mx
27
 
28
 
29
- def brute_submatrix_max(A):
30
  """
31
  Searches for the rectangular subarray of A with maximum sum
32
  Uses brute force searching
@@ -34,8 +36,8 @@ def brute_submatrix_max(A):
34
  M, N = A.shape
35
  t0 = time.time()
36
  this_location, max_value = ((0, 0), (0, 0)), 0
37
- for m, n in itertools.product(xrange(M), xrange(N)):
38
- for i, k in itertools.product(xrange(M - m + 1), xrange(N - n + 1)):
39
  this_location = (slice(i, i + m), slice(k, k + n))
40
  value = A[this_location].sum()
41
  if value >= max_value:
@@ -45,7 +47,7 @@ def brute_submatrix_max(A):
45
  return location, max_value, t
46
 
47
 
48
- def fft_submatrix_max(A):
49
  """
50
  Searches for the rectangular subarray of A with maximum sum
51
  Uses FFT-based convolution operations
@@ -53,7 +55,7 @@ def fft_submatrix_max(A):
53
  M, N = A.shape
54
  this_location, max_value = ((0, 0), (0, 0)), 0
55
  t0 = time.time()
56
- for m, n in itertools.product(xrange(2, M), xrange(2, N)):
57
  convolved = conv(A, np.ones((m, n)), mode='same')
58
  row, col = np.unravel_index(convolved.argmax(), convolved.shape)
59
  # index offsets for odd dimension length:
@@ -67,7 +69,7 @@ def fft_submatrix_max(A):
67
  n_off = 0
68
 
69
  this_location = (
70
- slice(row - m / 2, row + m / 2 + m_off), slice(col - n / 2, col + n / 2 + n_off))
71
  value = A[this_location].sum()
72
 
73
  if value >= max_value:
 
4
  import time
5
 
6
 
7
+ from typing import Tuple, Any
8
+
9
+ def local_search(A: np.ndarray, loc: Tuple[slice, slice]) -> Tuple[Tuple[slice, slice], float]:
10
  """
11
  Utility function to verify local optimality of a
12
  subarray slice specification 'loc' of array 'A'
 
28
  return loc2, mx
29
 
30
 
31
+ def brute_submatrix_max(A: np.ndarray) -> Tuple[Tuple[slice, slice], float, float]:
32
  """
33
  Searches for the rectangular subarray of A with maximum sum
34
  Uses brute force searching
 
36
  M, N = A.shape
37
  t0 = time.time()
38
  this_location, max_value = ((0, 0), (0, 0)), 0
39
+ for m, n in itertools.product(range(M), range(N)):
40
+ for i, k in itertools.product(range(M - m + 1), range(N - n + 1)):
41
  this_location = (slice(i, i + m), slice(k, k + n))
42
  value = A[this_location].sum()
43
  if value >= max_value:
 
47
  return location, max_value, t
48
 
49
 
50
+ def fft_submatrix_max(A: np.ndarray) -> Tuple[Tuple[slice, slice], float, float]:
51
  """
52
  Searches for the rectangular subarray of A with maximum sum
53
  Uses FFT-based convolution operations
 
55
  M, N = A.shape
56
  this_location, max_value = ((0, 0), (0, 0)), 0
57
  t0 = time.time()
58
+ for m, n in itertools.product(range(2, M), range(2, N)):
59
  convolved = conv(A, np.ones((m, n)), mode='same')
60
  row, col = np.unravel_index(convolved.argmax(), convolved.shape)
61
  # index offsets for odd dimension length:
 
69
  n_off = 0
70
 
71
  this_location = (
72
+ slice(row - m // 2, row + m // 2 + m_off), slice(col - n // 2, col + n // 2 + n_off))
73
  value = A[this_location].sum()
74
 
75
  if value >= max_value:
run.py CHANGED
@@ -10,8 +10,8 @@ A = np.random.randint(-100, 100, size=(M, N))
10
  # Test each algorithm
11
  # output format: maximizing subarray slice specification, maximum sum
12
  # value, running time
13
- print
14
- print "Running FFT algorithm:"
15
- print fft_submatrix_max(A)
16
- print "Running brute force algorithm:"
17
- print brute_submatrix_max(A)
 
10
  # Test each algorithm
11
  # output format: maximizing subarray slice specification, maximum sum
12
  # value, running time
13
+ print()
14
+ print("Running FFT algorithm:")
15
+ print(fft_submatrix_max(A))
16
+ print("Running brute force algorithm:")
17
+ print(brute_submatrix_max(A))