use flatten to handle arbitrary nesting in lambdify