changed zinvariants to only calculate invariants if z.all() is non zero