haskell / math-functions

Special mathematical functions
http://hackage.haskell.org/package/math-functions
BSD 2-Clause "Simplified" License
41 stars 28 forks source link

Implement erfc for GHCJS #55

Closed phadej closed 5 years ago

phadej commented 5 years ago

Test suite passes locally when build with GHCJS

phadej commented 5 years ago

ping @Shimuuar

phadej commented 5 years ago

math-functions & statistics are used by criterion.

phadej commented 5 years ago

https://en.wikipedia.org/wiki/Numerical_Recipes is the original source (this book should be in every technical library)

Screenshot from 2019-08-28 21-31-17

phadej commented 5 years ago

Btw, I see there is .travis.yml and appveyor.yml in the repository, but they are not on.

Is it possible to ping Bryan to enable at least travis for this and statistics.

I'm working on GHCJS support for https://github.com/haskell-ci/haskell-ci (the successor of https://github.com/hvr/multi-ghc-travis), and the tests do pass locally even with GHCJS. I'd make Travis say that, so you don't need to trust my word on it!

Shimuuar commented 5 years ago

GHCJS support for travis? That would be great!

AFAIR I tried pinging him and then just topok an easy road and just run tests in my fork

phadej commented 5 years ago

Let's see how https://travis-ci.org/phadej/math-functions/builds/578017312 turns out.

I'll try to make it green still today.

EDIT: another attempts:

phadej commented 5 years ago

https://travis-ci.org/phadej/math-functions/builds/578019135 is green.

Shimuuar commented 5 years ago

@phadej I ended up implementing another algorithm from numerical recipes. It provides almost full double preceision (loses 1-2 significant digits at the tails). Still big improvement over single precision (~7 digits). I still need to check

Also is there canonical way to test package with different flag set? Math-functions certainly needs to test bundled implementations of erf, log1p, expm1

phadej commented 5 years ago

@Shimuuar I'd provide both, because additional precision isn't computationally free. And on GHCJS it's slow and criterion is fine with Float precision.

https://github.com/ekmett/semigroups/blob/master/cabal.haskell-ci is an example of specifying constraint-set. You need to use configuration file for these.

Shimuuar commented 5 years ago

General policy of this library is that functions by default should provide full precision. Functions which trade precision for speed are OK too but should be clearly marked as such. Here we get into very interesting tradeoffs. On GHC we have access to erfc/erfc from libc which are both precise within 1ulp and probably fastest (I didn't benchmark different implementation of erf yet). It's likely always best choice. On GHCJS using faster erfFast may be justified.

Thanh there's performance impact. AFAIR criterion spends most of the time on running bootstrap. And I think it doesn't use erf. If I'm right erf is out of hot code path and performance impact of slower implementation would be negligible

Anyway it's not possible to say anything about usefulness of faster variant of erf without benchmarks

phadej commented 5 years ago

True. Any version would be good probably good enough for criterion use, it needs some erfc.

And then we can benchmark, so I'm happy with any result, as far as there is working erfc.

(Note: in this pr I added the table for erf results in the tests, and faster one passes those, with 1e-7 error. I used values from https://en.wikipedia.org/wiki/Error_function#Table_of_values to make that table. Maybe there are more precise tabulated values somewhere as well, so if you end up using more precise algorithm, the table and error could (should?) be updated)

Shimuuar commented 5 years ago

I think I'll just upload new version with ghcjs support in the evening and we can sort out performance questions with GHCJS later

As for tables. I just generated tables to 30 significant digits using mpmath

phadej commented 5 years ago

TIL about mpmath, :+1:

@Shimuuar i'd like to test the version you'd upload, to avoid "testing in production".

Shimuuar commented 5 years ago

It's current master of math-functions. Ready for release but not tagged yet

phadej commented 5 years ago

@Shimuuar thanks. I'll test it in next ~8 hours.

Shimuuar commented 5 years ago

I'll upload it once you say it's OK

phadej commented 5 years ago

I tested master with my “no monad-par” PR for statistics and run a criterion benchmark built with GHCJS just fine. As far as I can say, master looks great.

Shimuuar commented 5 years ago

math-functions-0.3.2.0 is on hackage. Now to statistics