Write a NumPy function that computes the determinant of a square matrix with np.linalg.det and returns it as a scalar float. It tests how NumPy evaluates determinants, why the result carries floating-point error, and how to handle singular or very large matrices.
Write `determinant(A)` that computes the determinant of a square 2-D array-like `A` (a list of lists or an `np.ndarray`) using `np.linalg.det`. Return a scalar Python or NumPy `float`, not a 0-d array.
For example:
```python
A = [[1, 2], [3, 4]]
determinant(A) # -2.0000000000000004
```
```hint Return type
Check what type `np.linalg.det` returns for a single 2-D matrix, and how to turn it into a plain Python float.
```
### Constraints & Assumptions
- `A` is 2-D and square (n x n).
- The entries are real numbers, given as ints or floats.
### What a Strong Answer Covers
- How the input is converted and which input types `np.linalg.det` accepts.
- What `np.linalg.det` returns and how the result becomes a scalar float.
- The algorithm NumPy uses to compute the determinant and its cost for an n x n matrix.
- What happens for non-square, singular or very large matrices.
### Follow-up Questions
- Why can `np.linalg.det` return a value like `-2.0000000000000004` instead of exactly `-2`?
- For a large matrix the determinant can overflow to `inf` or underflow to `0.0`. How would you still work with it?
- Is a determinant close to zero a reliable sign that a matrix is nearly singular?
Overview: Write a NumPy function that computes the determinant of a square matrix with np.linalg.det and returns it as a scalar float. It tests how NumPy evaluates determinants, why the result carries floating-point error, and how to handle singular or very large matrices.
Write determinant(A) that computes the determinant of a square 2-D array-like A (a list of lists or an np.ndarray) using np.linalg.det. Return a scalar Python or NumPy float, not a 0-d array.
For example:
A = [[1, 2], [3, 4]]
determinant(A) # -2.0000000000000004
Constraints & Assumptions
A
is 2-D and square (n x n).
The entries are real numbers, given as ints or floats.
What a Strong Answer Covers Guidance
How the input is converted and which input types
np.linalg.det
accepts.
What
np.linalg.det
returns and how the result becomes a scalar float.
The algorithm NumPy uses to compute the determinant and its cost for an n x n matrix.
What happens for non-square, singular or very large matrices.
Follow-up Questions Guidance
Why can
np.linalg.det
return a value like
-2.0000000000000004
instead of exactly
-2
?
For a large matrix the determinant can overflow to
inf
or underflow to
0.0
. How would you still work with it?
Is a determinant close to zero a reliable sign that a matrix is nearly singular?