Question of jacbian inverse calculation

Hello, developer
In the code, the dn_dx calculation need the jacbion tranform:

//! Compute Jacobian
template <unsigned Tdim, unsigned Tnfunctions>
inline Eigen::MatrixXd mpm::HexahedronElement<Tdim, Tnfunctions>::dn_dx(
    const VectorDim& xi, const Eigen::MatrixXd& nodal_coordinates,
    const VectorDim& particle_size,
    const VectorDim& deformation_gradient) const {
  // Get gradient shape functions
  Eigen::MatrixXd grad_sf =
      this->grad_shapefn(xi, particle_size, deformation_gradient);
  // Jacobian dx_i/dxi_j
  Eigen::Matrix<double, Tdim, Tdim> jacobian =
      (grad_sf.transpose() * nodal_coordinates);
  // Gradient shapefn of the cell
  // dN/dx = [J]^-1 * dN/dxi
  return grad_sf * (jacobian.inverse()).transpose();
}

the retrun value which related to the “jacobian.inverse()”, but maybe sometimes some value of the jacobian matrix is zero, so the inverse will be inf. (like the dn_dx_centroid_ = element_->dn_dx(xi_centroid, this->nodal_coordinates_, zero, zero) in which the jacobian is the diagonal matrix). So is this inverse function is just do the diagonal inverse or else?
I just search the cwiseInverse() and the array.inverse() function in the Eigen. So I am confused of this part. Thanks.
Best regards.

If the element is ill formed, then the Jacobian could be negative or zero. That’s a mesh issue. The Jacobian is a square matrix, not merely a diagonal matrix.