Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It would be handy to have minIndexBy, minIndex, maxIndexBy, maxIndex. #110

Open
ilcasinistareloaded opened this issue Dec 22, 2020 · 0 comments

Comments

@ilcasinistareloaded
Copy link

Hello, it would be handy to have minIndexBy, minIndex, maxIndexBy, maxIndex. I adapted them from Data.Vector as follows:

   {-# INLINE minIndexBy #-}
    minIndexBy :: Massiv.Stream v Massiv.Ix1 a => (a -> a -> Ordering) -> Massiv.Vector v a -> Massiv.Ix1
    minIndexBy cmpr = fst . Massiv.sfoldl1' imin . Massiv.szip (Massiv.sfromList [0..])
      where
      imin (i,x) (j,y) = i `seq` j `seq` case cmpr x y of
        GT -> (j,y)
        _  -> (i,x)
    
    {-# INLINE minIndex #-}
    minIndex :: Ord a => Massiv.Stream v Massiv.Ix1 a => Massiv.Vector v a -> Massiv.Ix1
    minIndex = minIndexBy compare
    
    {-# INLINE maxIndexBy #-}
    maxIndexBy :: Massiv.Stream v Massiv.Ix1 a => (a -> a -> Ordering) -> Massiv.Vector v a -> Massiv.Ix1
    maxIndexBy cmpr = fst . Massiv.sfoldl1' imax . Massiv.szip (Massiv.sfromList [0..])
      where
      imax (i,x) (j,y) = i `seq` j `seq` case cmpr x y of
        LT -> (j,y)
        _  -> (i,x)
    
    {-# INLINE maxIndex #-}
    maxIndex :: Ord a => Massiv.Stream v Massiv.Ix1 a => Massiv.Vector v a -> Massiv.Ix1
    maxIndex = maxIndexBy compare
    
    {-
    minIndex $ ((Massiv.fromList Massiv.Seq ([4,3,2,1,0,1,2])) :: Massiv.Vector Massiv.P Massiv.Ix1) -- == 4
    [index]...................................0 1 2 3 4 5 6
    maxIndex $ ((Massiv.fromList Massiv.Seq ([4,3,2,1,9,1,2])) :: Massiv.Vector Massiv.P Massiv.Ix1) -- == 4
    -}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant