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

proof of concept MutableArithmetics support #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nsajko
Copy link

@nsajko nsajko commented Nov 15, 2023

This only provides support for sin, cos and sincos. The idea was to show how easy it is to add MA support, after which someone else may decide to add more full support, which would be autogenerated, I guess.

NB: I think you may also want to overload MA.operate.

Updates #118

Usage example:

julia> using Arblib, MutableArithmetics

julia> o() = one(Arb)
o (generic function with 1 method)

julia> sin(o())
[0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77]

julia> operate_to!!(o(), sin, o())
[0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77]

julia> sincos(o())
([0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77], [0.5403023058681397174009366074429766037323104206179222276700972553811003947745 +/- 4.21e-77])

julia> sc = (o(), o())
(1.0000000000000000000000000000000000000000000000000000000000000000000000000000, 1.0000000000000000000000000000000000000000000000000000000000000000000000000000)

julia> operate_to!!(sc, sincos, o())
([0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77], [0.5403023058681397174009366074429766037323104206179222276700972553811003947745 +/- 4.21e-77])

julia> sc
([0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77], [0.5403023058681397174009366074429766037323104206179222276700972553811003947745 +/- 4.21e-77])

This only provides support for `sin`, `cos` and `sincos`. The idea was
to show how easy it is to add MA support, after which someone else may
decide to add more full support, which would be autogenerated, I guess.

NB: I think you may also want to overload `MA.operate`.

Updates kalmarek#118

Usage example:

```julia-repl
julia> using Arblib, MutableArithmetics

julia> o() = one(Arb)
o (generic function with 1 method)

julia> sin(o())
[0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77]

julia> operate_to!!(o(), sin, o())
[0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77]

julia> sincos(o())
([0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77], [0.5403023058681397174009366074429766037323104206179222276700972553811003947745 +/- 4.21e-77])

julia> sc = (o(), o())
(1.0000000000000000000000000000000000000000000000000000000000000000000000000000, 1.0000000000000000000000000000000000000000000000000000000000000000000000000000)

julia> operate_to!!(sc, sincos, o())
([0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77], [0.5403023058681397174009366074429766037323104206179222276700972553811003947745 +/- 4.21e-77])

julia> sc
([0.8414709848078965066525023216302989996225630607983710656727517099919104043912 +/- 4.00e-77], [0.5403023058681397174009366074429766037323104206179222276700972553811003947745 +/- 4.21e-77])
```
@Joel-Dahne
Copy link
Collaborator

Thank you! This would certainly be helpful as a starting point! Indeed it then seems somewhat straightforward to add support for all methods in Base and SpecialFunctions.jl.

@kalmarek
Copy link
Owner

if anyone is willing to spend more time here is what i've written at some point:
https://gist.github.com/kalmarek/e7ba0413eabc78a9f3360b248f457089

with LinearAlgebra.mul!, MA.add_mul!! and its buffered version reaching

BenchmarkTools.Trial: 321 samples with 1 evaluation.
 Range (min … max):   5.154 ms … 73.135 ms  ┊ GC (min … max):  0.00% … 50.02%
 Time  (median):      6.203 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):   15.706 ms ± 22.284 ms  ┊ GC (mean ± σ):  32.09% ± 17.53%

  █▆                                                     ▁ ▁   
  █████▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▄▆███▇ ▆
  5.15 ms      Histogram: log(frequency) by time      71.3 ms <

 Memory estimate: 8.06 MiB, allocs estimate: 132096.
BenchmarkTools.Trial: 1336 samples with 1 evaluation.
 Range (min … max):  3.211 ms …   5.504 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     3.710 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   3.728 ms ± 131.205 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

                            ▃▆██▃▂▃▄▅▄▂▂                       
  ▂▁▂▂▁▂▁▁▁▁▁▁▁▂▁▂▃▂▂▃▃▃▄▃▃▅████████████▇▇▇▅▆▅▆▄▄▅▄▃▄▄▄▃▂▃▃▂▂ ▄
  3.21 ms         Histogram: frequency by time        4.09 ms <

 Memory estimate: 64 bytes, allocs estimate: 1.
BenchmarkTools.Trial: 1206 samples with 1 evaluation.
 Range (min … max):  3.264 ms …   5.987 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     4.073 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   4.125 ms ± 263.901 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

                     ▃▅█▄▄▄▁▄▄▃▂▂▂▂▁                           
  ▂▁▁▁▁▁▁▂▁▁▁▁▁▁▁▂▁▃▅███████████████▆▇▇▆▅▆▅▆▅▄▄▄▃▃▃▃▃▂▃▂▁▁▂▂▂ ▄
  3.26 ms         Histogram: frequency by time        4.97 ms <

 Memory estimate: 0 bytes, allocs estimate: 0.

the same data with BigFloats for comparison:

BenchmarkTools.Trial: 482 samples with 1 evaluation.
 Range (min … max):   8.798 ms … 16.072 ms  ┊ GC (min … max):  0.00% … 35.50%
 Time  (median):      9.241 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):   10.361 ms ±  1.871 ms  ┊ GC (mean ± σ):  11.09% ± 13.47%

   ▄▆█▃▂                                                       
  ▄█████▇▃▃▁▁▁▁▁▁▁▁▁▁▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▅▅▄▄▃▃▄▂▃▃▃▃▃▂▃▃▃▂▃▃▂ ▃
  8.8 ms          Histogram: frequency by time        14.3 ms <

 Memory estimate: 13.10 MiB, allocs estimate: 264192.
BenchmarkTools.Trial: 888 samples with 1 evaluation.
 Range (min … max):  4.871 ms …   7.825 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.598 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.617 ms ± 129.968 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

                                   ▄▄██▆▆▃                     
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▂▃▄▇████████▅▅▃▃▃▃▄▃▃▂▂▂▂▂▂▂▂▂ ▃
  4.87 ms         Histogram: frequency by time        6.03 ms <

 Memory estimate: 104 bytes, allocs estimate: 2.
BenchmarkTools.Trial: 904 samples with 1 evaluation.
 Range (min … max):  4.898 ms …  6.443 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.508 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.519 ms ± 87.339 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

                                     ▄█▇▇▆▅▂                  
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▂▁▁▁▁▂▂▃▅█████████▇▅▅▃▃▃▃▁▂▂▂▁▂▁▂ ▃
  4.9 ms         Histogram: frequency by time        5.81 ms <

 Memory estimate: 0 bytes, allocs estimate: 0.

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

Successfully merging this pull request may close these issues.

3 participants