write the function to calculate density