# Source: content/notes/libraries/numpy.md
# Independent CPU example; use the curriculum environment.
# See /notes/ml/#example-environment or /notes/deep-learning/#example-environment.

import numpy as np
a = np.arange(12)
strided = a[::2].reshape(2, 3)
assert np.shares_memory(a, strided)
assert not np.shares_memory(a, a.reshape(3, 4).T.reshape(-1))
assert np.isscalar(a[3]) and np.shares_memory(a, a[3:4])
counts = np.zeros(3, dtype=int)
np.add.at(counts, [1, 1, 2], 1)
assert counts.tolist() == [0, 2, 1]
assert (np.array([2**62], dtype=np.int64) * 4).item() == 0
assert 50_000 * 50_000 * 3 * 8 == 60_000_000_000
X = np.array([[1., 2.], [1., 4.]])
std = X.std(0)
normalized = np.divide(X-X.mean(0), std, out=np.zeros_like(X), where=std!=0)
assert np.allclose(normalized, [[0, -1], [0, 1]])
logits = np.array([[1000., 1001., 999.]])
exp = np.exp(logits-logits.max(-1, keepdims=True))
prob = exp/exp.sum(-1, keepdims=True)
assert np.isfinite(prob).all() and np.allclose(prob.sum(-1), 1)
print("aliasing, overflow, allocation, normalization, softmax checks passed")
