Most active commenters
  • mtantaoui(4)
  • (4)
  • legobmw99(3)
  • antononcube(3)

123 points mtantaoui | 48 comments | | HN request time: 1.995s | source | bottom

Integrate is a fast, small, lightweight Rust library for performing numerical integration of real-valued functions. It is designed to integrate functions, providing a simple and efficient way to approximate definite integrals using various numerical methods.

Integrate supports a variety of numerical integration techniques: - Newton-Cotes methods:

  - Rectangle Rule.
  - Trapezoidal Rule.
  - Simpson's Rule.
  - Newton's 3/8 Rule.
- Gauss quadrature methods:

  - Gauss-Legendre.
  - Gauss-Laguerre.
  - Gauss-Hermite.
  - Gauss-Chebyshev First Kind.
  - Gauss-Chebyshev Second Kind.
- Adaptive Methods:

  - Adaptive Simpson's method
- Romberg’s method.
1. ◴[] No.42182785[source]
2. ◴[] No.42183106[source]
3. JanisErdmanis ◴[] No.42183942[source]
It looks a bit sloppy to hardcode so many constants in a single file: `src/gauss_quadrature/legendre.rs`. Isn't it possible to generate them with the help of rust macros in the same way Julia uses metaprogramming?
replies(3): >>42184353 #>>42185117 #>>42185548 #
4. wjholden ◴[] No.42184001[source]
I was always amazed that R can do:

  > integrate(dnorm, -Inf, +Inf)
  1 with absolute error < 9.4e-05
Can we do the same in this library?
replies(4): >>42184765 #>>42184870 #>>42185462 #>>42185618 #
5. ◴[] No.42184024[source]
6. ok123456 ◴[] No.42184353[source]
Gaussian quadrature points are typically solved numerically. There's a good chance these ultimately came from a table.

Additionally, compile time floating-point evaluation is limited. When I looked around recently, I didn't see a rust equivalent of gcem; any kind of transcendental function evaluation (which finding Gaussian quadrature points absolutely would require) would not allow compile-time evaluation.

replies(2): >>42185264 #>>42186782 #
7. Buttons840 ◴[] No.42184765[source]
How many evaluations of the underlying function does it make? (Hoping someone will fire up their R interpreter and find out.)

Or, probably, dnorm is a probability distribution which includes a likeliness function, and a cumulative likeliness function, etc. I bet it doesn't work on arbitrary functions.

replies(1): >>42184883 #
8. legobmw99 ◴[] No.42184870[source]
It seems like it is lacking the functionality R's integrate has for handling infinite boundaries, but I suppose you could implement that yourself on the outside.

For what it's worth,

    use integrate::adaptive_quadrature::simpson::adaptive_simpson_method;
    use statrs::distribution::{Continuous, Normal};

    fn dnorm(x: f64) -> f64 {
        Normal::new(0.0, 1.0).unwrap().pdf(x)
    }
    
    fn main() {
        let result = adaptive_simpson_method(dnorm, -100.0, 100.0, 1e-2, 1e-8);
        println!("Result: {:?}", result);
    }
prints Result: Ok(1.000000000053865)

It does seem to be a usability hazard that the function being integrated is defined as a fn, rather than a Fn, as you can't pass closures that capture variables, requiring the weird dnorm definition

9. thrasibule ◴[] No.42184883{3}[source]
R integrate is just a wrapper around quadpack. It works with arbitrary functions, but arguably dnorm is pretty well behaved.
10. two_handfuls ◴[] No.42185117[source]
Probably but that would slow down compilation a lot.
replies(3): >>42186409 #>>42186665 #>>42190112 #
11. AlotOfReading ◴[] No.42185264{3}[source]
Support for float const fns was merged just a couple months ago and hasn't been officially announced yet.
replies(2): >>42185347 #>>42185528 #
12. ok123456 ◴[] No.42185347{4}[source]
IIRC, that only supports elementary arithmetic operations. Useful but not general.
replies(1): >>42185534 #
13. Philpax ◴[] No.42185427[source]
Um... clicking through to each integration method, they clearly have examples and documentation on how they work:

https://docs.rs/integrate/latest/integrate/adaptive_quadratu...

https://docs.rs/integrate/latest/integrate/gauss_quadrature/...

https://docs.rs/integrate/latest/integrate/romberg/fn.romber...

14. antononcube ◴[] No.42185440[source]
I think the project should be called "NIntegrate".

BTW, that is not a serious suggestion; it is just that Wolfram Language (aka Mathematica) has both `Integrate` and `NIntegrate` for symbolic and numeric integration, respectively.

replies(1): >>42186612 #
15. antononcube ◴[] No.42185462[source]
You will be completely blown away, then, from what Wolfram Language (aka Mathematica) can do. (When it comes to numerical integration.)

https://reference.wolfram.com/language/tutorial/NIntegrateOv...

16. antononcube ◴[] No.42185475[source]
Thanks for showing this! It is very motivating to develop (and finish) my Raku numerical integration project.
replies(1): >>42185883 #
17. BD103 ◴[] No.42185528{4}[source]
Support for constant float operations was released in Rust 1.82! https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html
18. AlotOfReading ◴[] No.42185534{5}[source]
It's relatively straightforward to build transcendental functions out of the basic operations and the stdlib support will eventually get there, but rust's float story is still a work in progress. They're trying to do things more properly and document semantics better than C and C++ have.
19. mtantaoui ◴[] No.42185548[source]
this was mainly to use an Iteration free method in this paper: https://www.cfm.brown.edu/faculty/gk/APMA2560/Handouts/GL_qu...

this method is much faster and simpler.

20. mtantaoui ◴[] No.42185618[source]
for ]-inf, inf[ integrals, you can use Gauss Hermite method, just keep in mind to multiply your function with exp(x^2).

    use integrate::{
        gauss_quadrature::hermite::gauss_hermite_rule,
    };
    use statrs::distribution::{Continuous, Normal};

    fn dnorm(x: f64) -> f64 {
        Normal::new(0.0, 1.0).unwrap().pdf(x)* x.powi(2).exp()
    }

    fn main() {
        let n: usize = 170;
        let result = gauss_hermite_rule(dnorm, n);
        println!("Result: {:?}", result);
    }

I got Result: 1.0000000183827922.
21. mtantaoui ◴[] No.42185883[source]
Thanks! That’s awesome to hear—I’d love to see how your Raku numerical integration project turns out!

You can email me if you want to, I'll be happy to help.

22. cozzyd ◴[] No.42186272[source]
I don't see any explicit SIMD in here. Is the rust compiler able to emit SIMD instructions automatically in cases like this? (I guess I could compile and disassemble to check... )
replies(1): >>42186347 #
23. jvanderbot ◴[] No.42186347[source]
In my experience Rust is very good about using simd for loading and not great at using it automatically for math. This is from some experimentation at work and checking disassembly so ymmv

There are common library extensions for that.

replies(1): >>42190200 #
24. simlevesque ◴[] No.42186409{3}[source]
Exactly, it's not like the constants are gonna change.
25. ◴[] No.42186612[source]
26. blharr ◴[] No.42186665{3}[source]
You wouldn't have to recompile them every time. What if you didn't necessarily use macros but auto-generated it in a file that you keep separate from the other code at the bottom?
27. zokier ◴[] No.42186782{3}[source]
I was under the impression that macros can execute arbitrary code, surely some FP would not be big problem. And if not macros then build.rs script certainly could do the trick.
replies(1): >>42187277 #
28. zppln ◴[] No.42187011[source]
Fast, small, lightweight compared to what?
replies(2): >>42187833 #>>42194434 #
29. dhosek ◴[] No.42187277{4}[source]
build.rs can definitely execute arbitrary code, which means that a lot of places (including, IIRC crates.io) will forbid crates that rely on build.rs. I ended up refactoring my build.rs into a separate sub-application in finl_unicode that built data tables which are then checked into git and used pre-built. I include the sub-app in the source code so that anyone with access to the repo could continue development if I were to die tomorrow.
replies(1): >>42188113 #
30. wiz21c ◴[] No.42187378[source]
In the rectangle method, there is "let x = a + i * h + (h / F1::from(2)...)"

I didn't check, but I wonder if it is not better to do x = a + (i+0.5)*h... My reasoning is that if "i" is very big, then i * h can be much bigger than h/2 and thus h/2 may be eaten by numerical precision when added to i*h... And then you have to convince the optimizer to do it the way you want. But well, it's late...

replies(1): >>42188835 #
31. zackangelo ◴[] No.42187535[source]
> Does the function oscillate over the region of integration? If it does, then make sure that the step size is chosen to be smaller than the wave length of the function.

Nyquist limit, but for numerical integration?

replies(3): >>42188927 #>>42189046 #>>42189391 #
32. akkad33 ◴[] No.42187833[source]
Those are adjectives not comparatives.
33. legobmw99 ◴[] No.42188015[source]
Is there a technical reason to now allow closures as the integrand?
replies(1): >>42188132 #
34. n_plus_1_acc ◴[] No.42188113{5}[source]
There are many crates with build.rs scripts on crates.io, because they host just the source code with the Cargo.{toml,lock}.
replies(1): >>42188859 #
35. n_plus_1_acc ◴[] No.42188132[source]
Mayve because they aren't guaranteed to be actual functions (in the mathematical sense) and could return random values
replies(1): >>42188173 #
36. legobmw99 ◴[] No.42188173{3}[source]
The Fn trait could be used, which prevents mutation, but allows a lot of useful closures. I should note, a motivated user could provide a junk function no matter what the type accepted is
37. zokier ◴[] No.42188835[source]
herbie recommends `fma(h, (i + 0.5), a)`, but also doesn't report any accuracy problems with the original either
replies(1): >>42191905 #
38. dhosek ◴[] No.42188859{6}[source]
I ran into some issues with crates.io and my build.rs when I first released the crate, although it’s long enough ago, that I don’t remember exactly what the issue was. It might also have been that the build.rs script downloaded raw data files from unicode.org
replies(1): >>42190262 #
39. im3w1l ◴[] No.42188927[source]
Well in both cases it comes down to aliasing I think (high frequency wave presents as low frequency wave with too big step size)
40. dawnofdusk ◴[] No.42189046[source]
The Nyquist sampling theorem is of course proved by considering a Fourier transform, which is given by an integral, so the relation to integration in general should not be surprising.
41. alleycat5000 ◴[] No.42189391[source]
One way to think about is that these techniques work by integrating exactly the polynomial that interpolates the function where you're sampling it, so you need to resolve the features of the function to get good accuracy.
42. dataflow ◴[] No.42190112{3}[source]
What I would do in these cases is to define the general computation function, but special-case it to return the hard-coded value for specific common inputs if it's being evaluated at compile time. Then add a test to verify both behaviors.
43. cozzyd ◴[] No.42190200{3}[source]

    ymmv
Or zmmv if avx-512 is supported?
replies(1): >>42195786 #
44. Arnavion ◴[] No.42190262{7}[source]
crates.io doesn't care what your build.rs does because it doesn't try to compile your code, neither now or ever in the past. There would be no point in it trying to compile your code; there are lots of crates that are bindings to C libraries that crates.io's builders can't be expected to have, crates that target architectures which crates.io can't be expected to have builders for, etc.
45. wiz21c ◴[] No.42191905{3}[source]
yeah, fused mul-add is certainly better. Dunno how one epxresses that in rust though :-) Ahhh seems like there at least f64::mul_add() in stdlib :-)
46. aDyslecticCrow ◴[] No.42194434[source]
It's a minimal implementation (small, lightweight) of well-known and established algorithms (fast).
47. jvanderbot ◴[] No.42195786{4}[source]
This is a good joke, if anyone is wondering.