Removes out-of-bounds access to first element in optimizeCallback when
the provided array/matrix is empty. Also adds numerous unit tests for such
edge cases, and fixes a previously unreported internal bug in SparseMatrix,
violating its own invariants on empty matrices.
Resolves#3564.
---------
Co-authored-by: Glen Whitney <glen@studioinfinity.org>