support non-diagonal metric and automatic derivation of metric gradient