Monoid over reciprocal of reciprocal sum

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Monoid over reciprocal of reciprocal sum

박신환

e.g. Parallel resistors, serial capacitors, parallel inductors.

 

newtype RecipSum a = RecipSum {getRecipSum :: a}

 

instance Fractional a => Semigroup (RecipSum a) where

    RecipSum x <> RecipSum y = RecipSum (recip (recip x + recip y))

    sconcat (x :| xs) = mconcat (x : xs). 

    stimes n (RecipSum x) = RecipSum (x / fromIntegral n) 

 

instance Fractional a => Monoid (ResipSum a) where

    mempty = RecipSum (1 / 0)

    mconcat xs = RecipSum (recip . getSum $ foldMap (Sum . recip . getRecipSum) xs)


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Monoid over reciprocal of reciprocal sum

박신환

e.g. Parallel resistors, serial capacitors, parallel inductors.

 

EDIT: Corrected typo. 

 

newtype RecipSum a = RecipSum {getRecipSum :: a}

 

instance Fractional a => Semigroup (RecipSum a) where

    RecipSum x <> RecipSum y = RecipSum (recip (recip x + recip y))

    sconcat (x :| xs) = mconcat (x : xs)

    stimes n (RecipSum x) = RecipSum (x / fromIntegral n) 

 

instance Fractional a => Monoid (RecipSum a) where

    mempty = RecipSum (1 / 0)

    mconcat xs = RecipSum (recip . getSum $ foldMap (Sum . recip . getRecipSum) xs)


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Monoid over reciprocal of reciprocal sum

Henning Thielemann

On Sun, 6 May 2018, 박신환 wrote:

> instance Fractional a => Monoid (RecipSum a) where
>
>     mempty = RecipSum (1 / 0)

This would fail for Rational. I'd prefer to simply use Sum monoid on
reciprocal values. It also has the advantage that you only need to call
'recip' once after a sequence of (<>).
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries