Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions maths/softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@
import numpy as np


def softmax(vector):
def softmax(vector: np.ndarray | list | tuple) -> np.ndarray:
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new annotation vector: np.ndarray | list | tuple is very broad and loses element-type information. Consider using a typed protocol/ABC (e.g., Sequence[float] from collections.abc) for list/tuple inputs and/or specifying element types (e.g., list[float] | tuple[float, ...]) to make the type hint more informative and consistent with other modules (e.g., maths/polynomial_evaluation.py).

Suggested change
def softmax(vector: np.ndarray | list | tuple) -> np.ndarray:
def softmax(vector: np.ndarray | list[float] | tuple[float, ...]) -> np.ndarray:

Copilot uses AI. Check for mistakes.
"""
Implements the softmax function
Parameters:
vector (np.array,list,tuple): A numpy array of shape (1,n)
consisting of real values or a similar list,tuple
vector (np.array | list | tuple): A numpy array of shape (1,n)
consisting of real values or a similar list, tuple
Returns:
softmax_vec (np.array): The input numpy array after applying
softmax.
np.array: The input numpy array after applying softmax.
Comment on lines +21 to +25
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring uses np.array as a type name in Parameters/Returns, but the function returns an np.ndarray and np.array is the constructor function. Updating the docstring to np.ndarray (and adjusting the described shape to match the 1-D examples) would avoid confusion and keep documentation consistent with the new type hints.

Copilot uses AI. Check for mistakes.
The softmax vector adds up to one. We need to ceil to mitigate for
precision
>>> float(np.ceil(np.sum(softmax([1,2,3,4]))))
The softmax vector adds up to one. We need to ceil to mitigate for precision
>>> float(np.ceil(np.sum(softmax([1, 2, 3, 4]))))
1.0
>>> vec = np.array([5,5])
>>> vec = np.array([5, 5])
>>> softmax(vec)
array([0.5, 0.5])
Expand Down
Loading