| name | python-scientific-computing-1-use-vectorization |
| description | Sub-skill of python-scientific-computing: 1. Use Vectorization (+4). |
| version | 1.0.0 |
| category | data |
| type | reference |
| scripts_exempt | true |
1. Use Vectorization (+4)
1. Use Vectorization
result = []
for x in x_array:
result.append(np.sin(x) * np.exp(-x))
result = np.sin(x_array) * np.exp(-x_array)
2. Choose Right Data Type
float32_array = np.array([1, 2, 3], dtype=np.float32)
float64_array = np.array([1, 2, 3], dtype=np.float64)
int_array = np.array([1, 2, 3], dtype=np.int32)
3. Avoid Matrix Inverse When Possible
x = np.linalg.inv(A) @ b
x = np.linalg.solve(A, b)
4. Use Broadcasting
A = np.array([[1, 2, 3],
[4, 5, 6]])
b = np.array([10, 20, 30])
C = A + b
5. Check Numerical Stability
cond = np.linalg.cond(A)
if cond > 1e10:
print("Warning: Matrix is ill-conditioned")
if np.allclose(A, A.T) and np.all(np.linalg.eigvals(A) > 0):
x = np.linalg.solve(A, b)