-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
147 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
hw00_worker = hw00(); | ||
|
||
hw_assert(hw00_worker.p2(5 * eye(3)) == 125) | ||
hw_assert(hw00_worker.p2(5 * eye(6)) == 5^6) | ||
hw_assert(hw00_worker.p2(magic(4)) == 0) | ||
hw_assert(hw00_worker.p2(magic(6)) == 0) | ||
hw_assert(hw00_worker.p2(magic(8)) == 0) | ||
|
||
function hw_assert(X) | ||
if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
hw00_worker = hw00(); | ||
|
||
hw_assert(hw00_worker.p2(1) == 1) | ||
hw_assert(hw00_worker.p2(-1) == -1) | ||
hw_assert(hw00_worker.p2([[1 0];[0 1]]) == 1) | ||
hw_assert(hw00_worker.p2([[1 1];[1 1]]) == 0) | ||
hw_assert(hw00_worker.p2([[1 3];[3 1]]) == -8) | ||
|
||
function hw_assert(X) | ||
if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
hw00_worker = hw00(); | ||
|
||
for i = 1:10 | ||
start_time = tic; | ||
hw00_worker.p3(); | ||
elapsed = toc(start_time); | ||
|
||
hw_assert(abs(elapsed - 1.0) < 0.05); | ||
end | ||
|
||
function hw_assert(X) | ||
if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from hw00 import p1 | ||
import numpy as np | ||
import time | ||
|
||
def test_p1(): | ||
hw_assert (p1(0) == 0) | ||
hw_assert (p1(1) == 1) | ||
hw_assert (p1(2) == 1) | ||
hw_assert (p1(3) == 2) | ||
hw_assert (p1(4) == 4) | ||
hw_assert (p1(5) == 7) | ||
hw_assert (p1(6) == 13) | ||
hw_assert (p1(7) == 24) | ||
hw_assert (p1(8) == 44) | ||
hw_assert (p1(9) == 81) | ||
|
||
def hw_assert(X): | ||
if X: | ||
print('\t PASS') | ||
else: | ||
print('\t FAIL') | ||
|
||
test_p1() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from hw00 import p2 | ||
import numpy as np | ||
import time | ||
|
||
def magic(n): | ||
n = int(n) | ||
if n < 3: | ||
raise ValueError("Size must be at least 3") | ||
if n % 2 == 1: | ||
p = np.arange(1, n+1) | ||
return n*np.mod(p[:, None] + p - (n+3)//2, n) + np.mod(p[:, None] + 2*p-2, n) + 1 | ||
elif n % 4 == 0: | ||
J = np.mod(np.arange(1, n+1), 4) // 2 | ||
K = J[:, None] == J | ||
M = np.arange(1, n*n+1, n)[:, None] + np.arange(n) | ||
M[K] = n*n + 1 - M[K] | ||
else: | ||
p = n//2 | ||
M = magic(p) | ||
M = np.block([[M, M+2*p*p], [M+3*p*p, M+p*p]]) | ||
i = np.arange(p) | ||
k = (n-2)//4 | ||
j = np.concatenate((np.arange(k), np.arange(n-k+1, n))) | ||
M[np.ix_(np.concatenate((i, i+p)), j)] = M[np.ix_(np.concatenate((i+p, i)), j)] | ||
M[np.ix_([k, k+p], [0, k])] = M[np.ix_([k+p, k], [0, k])] | ||
return M | ||
|
||
def test_p2(): | ||
A = np.array([[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]]) | ||
hw_assert (p2(A) == 5**4) | ||
A = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) | ||
hw_assert (p2(A) == 1) | ||
A = np.array(magic(4)) | ||
hw_assert (p2(A) == 0) | ||
A = np.array(magic(6)) | ||
hw_assert (p2(A) == 0) | ||
A = np.array(magic(8)) | ||
hw_assert (p2(A) == 0) | ||
|
||
def hw_assert(X): | ||
if X: | ||
print('\t PASS') | ||
else: | ||
print('\t FAIL') | ||
|
||
test_p2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from hw00 import p2 | ||
import numpy as np | ||
import time | ||
|
||
def test_p2(): | ||
A = np.array([[1]]) | ||
hw_assert (p2(A) == 1) | ||
A = np.array([[1, 2], [3, 4]]) | ||
hw_assert (p2(A) == -2) | ||
A = np.array([[-1]]) | ||
hw_assert (p2(A) == -1) | ||
A = np.array([[1, 3], [3, 1]]) | ||
hw_assert (p2(A) == -8) | ||
A = np.array([[1, 0], [0, 1]]) | ||
hw_assert (p2(A) == 1) | ||
|
||
def hw_assert(X): | ||
if X: | ||
print('\t PASS') | ||
else: | ||
print('\t FAIL') | ||
|
||
test_p2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from hw00 import p3 | ||
import numpy as np | ||
import time | ||
|
||
def test_p3(): | ||
for i in range(10): | ||
start = time.time() | ||
p3() | ||
end = time.time() | ||
hw_assert (abs(end - start - 1.0) < 0.05) | ||
|
||
def hw_assert(X): | ||
if X: | ||
print('\t PASS') | ||
else: | ||
print('\t FAIL') | ||
|
||
test_p3() |