normalizeBy :: U.Vector Double -> ContactMap -> ContactMap
normalizeBy normVec c = c{_matrix = MS.SymMatrix n vec'}
  where
    (MS.SymMatrix n vec) = _matrix c
    vec' = U.create $ do
        v <- U.thaw vec
        loop 0 0 v
        return v
    loop i j v | i < n && j < n = do
                   let i' = idx n i j
                       x = (normVec U.! i) * (normVec U.! j)
                       x' | x == 0 = 0
                          | otherwise = 1 / x
                   GM.read v i' >>= GM.write v i' . (*x')
                   loop i (j+1) v
               | i < n = loop (i+1) (i+1) v
               | otherwise = return ()

normalizeBy :: U.Vector Double -> ContactMap -> ContactMap
normalizeBy normVec c = c{_matrix = MS.SymMatrix n vec'}
  where
    (MS.SymMatrix n vec) = _matrix c
    vec' = U.create $ do
        v <- U.thaw vec
        loop 0 0 v
        return v
    loop i j v | i < n && j < n = do
                   let i' = idx n i j
                       x = (normVec U.! i) * (normVec U.! j)
                       x' | x == 0 = 0
                          | otherwise = 1 / x
                   GM.read v i' >>= GM.write v i' . (*x')
                   loop i (j+1) v
               | i < n = loop (i+1) (i+1) v
               | otherwise = return ()

normalizeBy normVec c = c{_matrix = MS.SymMatrix n vec'}
  where
    (MS.SymMatrix n vec) = _matrix c
    vec' = U.create $ do
        v <- U.thaw vec
        loop 0 0 v
        return v
    loop i j v | i < n && j < n = do
                   let i' = idx n i j
                       x = (normVec U.! i) * (normVec U.! j)
                       x' | x == 0 = 0
                          | otherwise = 1 / x
                   GM.read v i' >>= GM.write v i' . (*x')
                   loop i (j+1) v
               | i < n = loop (i+1) (i+1) v
               | otherwise = return ()
