purescript-quantities icon indicating copy to clipboard operation
purescript-quantities copied to clipboard

Add geometric mean primitive

Open sternj opened this issue 2 years ago • 3 comments

I'd like to add a geometric mean primitive to Insect and this seems like the first step! I've never seen Purescript before now so this is probably going to be quite broken.

sternj avatar Aug 18 '23 23:08 sternj

So I've realized that I just... can't do division? please advise

sternj avatar Aug 19 '23 00:08 sternj

Okay, so the code that I have is

geomean ∷ NonEmptyList Quantity → Result
geomean xs = (_ `pow` ( one / n)) <$> foldM (⊗) (head xs) (tail xs)
  where
    n = Decimal.fromInt (length xs)

and the error at hand is

at src/Data/Quantity/Math.purs:183:45 - 183:48 (line 183, column 45 - line 183, column 48)

  Could not match type
            
    Quantity
            
  with type
               
    t2 Quantity
               

while checking that type Quantity -> Quantity -> Quantity
  is at least as general as type t0 -> t1 -> t2 t0
while checking that expression qMultiply
  has type t0 -> t1 -> t2 t0
in value declaration geomean

where t2 is an unknown type
      t1 is an unknown type
      t0 is an unknown type

sternj avatar Aug 19 '23 15:08 sternj

Thank you very much for creating this — and sorry for not getting back to you earlier. I have been working on a rewrite of Insect over the past year. I will likely not make further updates to Insect. If you are still interested in this feature, maybe you could add it to Numbat instead? (https://github.com/sharkdp/numbat)

There are three places to look (following the example of mean):

  • Actual implementation: https://github.com/sharkdp/numbat/blob/edc96d7e7bbd7a81ff7dbb9f5b7e501ccbfd4247/numbat/src/ffi.rs#L640-L657
  • Register the function: https://github.com/sharkdp/numbat/blob/edc96d7e7bbd7a81ff7dbb9f5b7e501ccbfd4247/numbat/src/ffi.rs#L276-L283
  • Add the function to the prelude: https://github.com/sharkdp/numbat/blob/edc96d7e7bbd7a81ff7dbb9f5b7e501ccbfd4247/numbat/modules/math/functions.nbt#L47

sharkdp avatar Oct 12 '23 20:10 sharkdp