When the Jacobian of the problem is singular, compute a solution to the least squares problem instead of crashing. Closes #1889 (cherry picked from commit 82a2aeaa)