macro_rules! function {
($( $(+)? $c0:literal $(x $( ^ $d0:literal )?)? )+) => { ... };
($name:ident (x) = $($rest:tt)+ ) => { ... };
(const $name:ident (x) = $($rest:tt)+ ) => { ... };
(static $name:ident (x) = $($rest:tt)+ ) => { ... };
}Expand description
Macro to generate a monomial polynomial function.
This is good for using as a data source for testing
- Terms can be listed in any order
- Same-power terms are summed
- Missing terms are 0
The only major limitation is that it needs a space between the coefficient and the variable:
20.0 x^3is valid, but20.0x^3is not.
Syntax:
function!(
[const | static]?
[<name>(<x>) = ]?
[ [+]? <coef> [ x [ ^ <deg> ]? ]? ]+
)ยงExample
function!(test(x) = 20.0 x^3 + 3.0 x^2 - 2.0 x + 4.0); // Normal let-binding
function!(const test2(x) = 20.0 x^3 + 3.0 x^2 - 2.0 x + 4.0); // const! can live outside functions
function!(static test3(x) = 20.0 x^3 + 3.0 x^2 - 2.0 x + 4.0); // I added static for some reason
let test4 = function!(20.0 x^3 + 3.0 x^2 - 2.0); // No auto bindings
// Evaluate at x = 5
println!("{}", test.y(5.0));
// You could visualize it too:
// polyfit::plot_function!(test, x_range = 0.0..1000.0);