News for hackers. Stuff that matters.
Re:Show HN: A Mathematical Integration rust crate by mtantaoui on Tuesday November 19, 2024 @08:20AM (#42183106)
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.
Re:Show HN: A Mathematical Integration rust crate by antononcube on Tuesday November 19, 2024 @11:46AM (#42185440)
I think the project should be called "NIntegrate".

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

Re:Show HN: A Mathematical Integration rust crate by zppln on Tuesday November 19, 2024 @02:12PM (#42187011)
Fast, small, lightweight compared to what?
Re:Show HN: A Mathematical Integration rust crate by akkad33 on Tuesday November 19, 2024 @03:36PM (#42187833)
Those are adjectives not comparatives.
Re:Show HN: A Mathematical Integration rust crate by aDyslecticCrow on Wednesday November 20, 2024 @09:57AM (#42194434)
It's a minimal implementation (small, lightweight) of well-known and established algorithms (fast).
Re:Show HN: A Mathematical Integration rust crate by legobmw99 on Tuesday November 19, 2024 @03:57PM (#42188015)
Is there a technical reason to now allow closures as the integrand?
Re:Show HN: A Mathematical Integration rust crate by n_plus_1_acc on Tuesday November 19, 2024 @04:10PM (#42188132)
Mayve because they aren't guaranteed to be actual functions (in the mathematical sense) and could return random values
Re:Show HN: A Mathematical Integration rust crate by legobmw99 on Tuesday November 19, 2024 @04:17PM (#42188173)
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
Re:Show HN: A Mathematical Integration rust crate by JanisErdmanis on Tuesday November 19, 2024 @09:42AM (#42183942)
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?
Re:Show HN: A Mathematical Integration rust crate by ok123456 on Tuesday November 19, 2024 @10:18AM (#42184353)
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.

Re:Show HN: A Mathematical Integration rust crate by AlotOfReading on Tuesday November 19, 2024 @11:29AM (#42185264)
Support for float const fns was merged just a couple months ago and hasn't been officially announced yet.
Re:Show HN: A Mathematical Integration rust crate by BD103 on Tuesday November 19, 2024 @11:54AM (#42185528)
Support for constant float operations was released in Rust 1.82! https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html
Re:Show HN: A Mathematical Integration rust crate by ok123456 on Tuesday November 19, 2024 @11:37AM (#42185347)
IIRC, that only supports elementary arithmetic operations. Useful but not general.
Re:Show HN: A Mathematical Integration rust crate by AlotOfReading on Tuesday November 19, 2024 @11:54AM (#42185534)
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.
Re:Show HN: A Mathematical Integration rust crate by zokier on Tuesday November 19, 2024 @01:44PM (#42186782)
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.
Re:Show HN: A Mathematical Integration rust crate by dhosek on Tuesday November 19, 2024 @02:39PM (#42187277)
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.
Re:Show HN: A Mathematical Integration rust crate by n_plus_1_acc on Tuesday November 19, 2024 @04:09PM (#42188113)
There are many crates with build.rs scripts on crates.io, because they host just the source code with the Cargo.{toml,lock}.
Re:Show HN: A Mathematical Integration rust crate by dhosek on Tuesday November 19, 2024 @05:42PM (#42188859)
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
Re:Show HN: A Mathematical Integration rust crate by Arnavion on Tuesday November 19, 2024 @09:20PM (#42190262)
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.
Re:Show HN: A Mathematical Integration rust crate by mtantaoui on Tuesday November 19, 2024 @11:55AM (#42185548)
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.

Re:Show HN: A Mathematical Integration rust crate by two_handfuls on Tuesday November 19, 2024 @11:19AM (#42185117)
Probably but that would slow down compilation a lot.
Re:Show HN: A Mathematical Integration rust crate by simlevesque on Tuesday November 19, 2024 @01:06PM (#42186409)
Exactly, it's not like the constants are gonna change.
Re:Show HN: A Mathematical Integration rust crate by blharr on Tuesday November 19, 2024 @01:32PM (#42186665)
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?
Re:Show HN: A Mathematical Integration rust crate by dataflow on Tuesday November 19, 2024 @08:52PM (#42190112)
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.
Re:Show HN: A Mathematical Integration rust crate by wjholden on Tuesday November 19, 2024 @09:49AM (#42184001)
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?
Re:Show HN: A Mathematical Integration rust crate by legobmw99 on Tuesday November 19, 2024 @10:57AM (#42184870)
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

Re:Show HN: A Mathematical Integration rust crate by antononcube on Tuesday November 19, 2024 @11:48AM (#42185462)
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...

Re:Show HN: A Mathematical Integration rust crate by mtantaoui on Tuesday November 19, 2024 @12:01PM (#42185618)
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.
Re:Show HN: A Mathematical Integration rust crate by Buttons840 on Tuesday November 19, 2024 @10:48AM (#42184765)
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.

Re:Show HN: A Mathematical Integration rust crate by thrasibule on Tuesday November 19, 2024 @10:58AM (#42184883)
R integrate is just a wrapper around quadpack. It works with arbitrary functions, but arguably dnorm is pretty well behaved.
[dead]
Re:Show HN: A Mathematical Integration rust crate by Philpax on Tuesday November 19, 2024 @11:44AM (#42185427)
Re:Show HN: A Mathematical Integration rust crate by antononcube on Tuesday November 19, 2024 @11:49AM (#42185475)
Thanks for showing this! It is very motivating to develop (and finish) my Raku numerical integration project.
Re:Show HN: A Mathematical Integration rust crate by mtantaoui on Tuesday November 19, 2024 @12:21PM (#42185883)
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.

Re:Show HN: A Mathematical Integration rust crate by cozzyd on Tuesday November 19, 2024 @12:53PM (#42186272)
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... )
Re:Show HN: A Mathematical Integration rust crate by jvanderbot on Tuesday November 19, 2024 @12:59PM (#42186347)
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.

Re:Show HN: A Mathematical Integration rust crate by cozzyd on Tuesday November 19, 2024 @09:08PM (#42190200)

    ymmv
Or zmmv if avx-512 is supported?
Re:Show HN: A Mathematical Integration rust crate by jvanderbot on Wednesday November 20, 2024 @11:51AM (#42195786)
This is a good joke, if anyone is wondering.
Re:Show HN: A Mathematical Integration rust crate by wiz21c on Tuesday November 19, 2024 @02:50PM (#42187378)
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...

Re:Show HN: A Mathematical Integration rust crate by zokier on Tuesday November 19, 2024 @05:37PM (#42188835)
herbie recommends `fma(h, (i + 0.5), a)`, but also doesn't report any accuracy problems with the original either
Re:Show HN: A Mathematical Integration rust crate by wiz21c on Wednesday November 20, 2024 @03:40AM (#42191905)
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 :-)
Re:Show HN: A Mathematical Integration rust crate by zackangelo on Tuesday November 19, 2024 @03:05PM (#42187535)
> 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?

Re:Show HN: A Mathematical Integration rust crate by im3w1l on Tuesday November 19, 2024 @05:49PM (#42188927)
Well in both cases it comes down to aliasing I think (high frequency wave presents as low frequency wave with too big step size)
Re:Show HN: A Mathematical Integration rust crate by dawnofdusk on Tuesday November 19, 2024 @06:09PM (#42189046)
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.
Re:Show HN: A Mathematical Integration rust crate by alleycat5000 on Tuesday November 19, 2024 @06:58PM (#42189391)
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.