thearn commited on
Commit
85e7abc
·
1 Parent(s): 17f8480

fixed edge case

Browse files
Files changed (2) hide show
  1. algorithms.py +4 -3
  2. tests/test_algorithms.py +20 -0
algorithms.py CHANGED
@@ -74,8 +74,9 @@ def brute_submatrix_max(A: np.ndarray) -> Tuple[Tuple[slice, slice], float, floa
74
  """
75
  M, N = A.shape
76
  t0 = time.time()
77
- this_location, max_value = ((0, 0), (0, 0)), 0
78
- for m, n in itertools.product(range(M), range(N)):
 
79
  for i, k in itertools.product(range(M - m + 1), range(N - n + 1)):
80
  this_location = (slice(i, i + m), slice(k, k + n))
81
  value = A[this_location].sum()
@@ -129,7 +130,7 @@ def fft_submatrix_max(A: np.ndarray) -> Tuple[Tuple[slice, slice], float, float]
129
  t0 = time.time()
130
  location = None
131
  max_value = -np.inf
132
- for m, n in itertools.product(range(2, M+1), range(2, N+1)):
133
  convolved = conv(A, np.ones((m, n)), mode='same')
134
  row, col = np.unravel_index(convolved.argmax(), convolved.shape)
135
  m_off = 1 if m % 2 == 1 else 0
 
74
  """
75
  M, N = A.shape
76
  t0 = time.time()
77
+ location = (slice(0, 1), slice(0, 1))
78
+ max_value = A[0, 0]
79
+ for m, n in itertools.product(range(1, M+1), range(1, N+1)):
80
  for i, k in itertools.product(range(M - m + 1), range(N - n + 1)):
81
  this_location = (slice(i, i + m), slice(k, k + n))
82
  value = A[this_location].sum()
 
130
  t0 = time.time()
131
  location = None
132
  max_value = -np.inf
133
+ for m, n in itertools.product(range(1, M+1), range(1, N+1)):
134
  convolved = conv(A, np.ones((m, n)), mode='same')
135
  row, col = np.unravel_index(convolved.argmax(), convolved.shape)
136
  m_off = 1 if m % 2 == 1 else 0
tests/test_algorithms.py CHANGED
@@ -33,3 +33,23 @@ def test_kidane():
33
  # The best submatrix is expected to have a sum of 9.
34
  _, max_value, _ = algorithms.kidane_max_submatrix(matrix)
35
  assert max_value == 9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # The best submatrix is expected to have a sum of 9.
34
  _, max_value, _ = algorithms.kidane_max_submatrix(matrix)
35
  assert max_value == 9
36
+
37
+ def test_max_submatrix_sum():
38
+ matrix = np.array([
39
+ [ 4.6, -4.8, 1.2, -8.4, 9.7, -0.4, -8.3, -5.7, -3.2, -3.7],
40
+ [-4.0, 5.4, 5.6, 5.5, 3.4, -9.5, 5.0, -4.4, 2.9, -2.6],
41
+ [ 4.8, 7.7, 0.6, -6.4, -5.1, -2.4, 5.2, 6.0, -9.8, -5.8],
42
+ [-4.4, -5.2, 8.6, -1.9, -0.4, -7.2, 4.6, 5.5, -5.2, 7.2],
43
+ [ 6.0, -8.9, 2.7, 4.6, -9.8, -9.7, -3.8, -0.6, 3.2, 2.1],
44
+ [-6.5, -5.7, 0.9, -4.5, -5.7, -7.7, 9.6, 4.9, -4.3, -7.4],
45
+ [-4.5, 7.7, 4.5, -4.3, 0.5, -9.9, -0.5, -4.8, -1.8, 8.3],
46
+ [-5.9, -4.4, 8.3, 0.5, 9.4, -2.4, -4.3, -8.8, -0.5, -9.7],
47
+ [ 2.0, -3.9, 0.0, -7.0, -6.9, 3.8, -8.5, -5.0, -5.4, -2.9],
48
+ [ 0.5, 4.6, -7.7, 3.3, 5.3, 0.1, 0.0, 1.5, 8.4, 8.7]
49
+ ])
50
+ ret, max_value, _ = algorithms.brute_submatrix_max(matrix)
51
+ assert abs(max_value - 32.4) < 1e-6
52
+ ret, max_value, _ = algorithms.fft_submatrix_max(matrix)
53
+ assert abs(max_value - 32.4) < 1e-6
54
+ ret, max_value, _ = algorithms.kidane_max_submatrix(matrix)
55
+ assert abs(max_value - 32.4) < 1e-6