Write a NumPy function that inverts a square, invertible matrix with np.linalg.inv and verifies the result against the identity within floating-point tolerance. It tests how NumPy computes an inverse, when to solve a linear system instead, and how to handle singular or ill-conditioned matrices.
Write `inverse(A)` that returns the inverse of an invertible square NumPy array `A` using `np.linalg.inv`. The result must satisfy `A @ inverse(A)` equal to `np.eye(n)` within floating-point tolerance, as checked by `np.allclose(A @ inverse(A), np.eye(n))`.
For example:
```python
A = np.array([[4., 7.], [2., 6.]])
inverse(A)
# array([[ 0.6, -0.7],
# [-0.2, 0.4]])
```
```hint Verifying the result
An exact comparison with `np.eye(n)` fails for most inputs. Think about where the small errors come from before you decide how much to trust the output.
```
### Constraints & Assumptions
- `A` is square (n x n) and invertible.
- The entries are real numbers.
### What a Strong Answer Covers
- How the input is converted and what dtype the result has.
- How to verify the result against the identity, and why a tolerance is needed.
- How NumPy computes the inverse and what that costs for an n x n matrix.
- What happens when `A` is singular or close to singular.
### Follow-up Questions
- When should you prefer `np.linalg.solve` over computing an explicit inverse?
- How would you handle a singular or near-singular `A`?
- You need to solve systems with the same `A` for many right-hand sides that arrive one at a time. What would you compute once and reuse?
Overview: Write a NumPy function that inverts a square, invertible matrix with np.linalg.inv and verifies the result against the identity within floating-point tolerance. It tests how NumPy computes an inverse, when to solve a linear system instead, and how to handle singular or ill-conditioned matrices.
Write inverse(A) that returns the inverse of an invertible square NumPy array A using np.linalg.inv. The result must satisfy A @ inverse(A) equal to np.eye(n) within floating-point tolerance, as checked by np.allclose(A @ inverse(A), np.eye(n)).