Series
This is a crate for handling truncated Laurent series and Laurent polynomials in a single variable about zero, i.e. expressions of the form
s = a_n0*x^n0 + ... + a_N*x^N + O(x^{N+1})
p = a_n0*x^n0 + ... + a_N*x^N
where n0 and N are integers and ^ denotes exponentiation. Such
expressions can be added, subtracted, and multiplied. For Laurent series,
Some additional simple functions like division, powers of series,
natural logarithms and exponentials are also implemented.
The kinds of operations that can be performed depends on the data type of the variable and the coefficients. For example, we usually have to calculate the logarithm of both the leading coefficient and the expansion variable if we take the logarithm of a Laurent series. This crate is therefore most useful in combination with a library providing at least basic symbolic math.
Usage
Run cargo add series or add this to your Cargo.toml:
[]
= "0.14"
Examples
use ;
Exponentials, logarithms, and powers
More advanced operations on Laurent series in general require the variable type to be convertible to the coefficient type by implementing the From trait:
use series::{Series, var};
use series::ops::{Ln, Exp, Pow};
var!(X);
fn main() {
// In the examples shown here, this conversion is actually never used,
// so we can get away with a dummy implementation.
impl From<X> for f64 {
fn from(_: X) -> f64 {
panic!("Can't convert variable `x` to f64")
}
}
// Now we can calculate logarithms, exponentials, and powers:
let s = Series::new(X, 0, vec![1., -3., 5.]);
println!("exp(s) = {}", s.clone().exp());
println!("ln(s) = {}", s.clone().ln());
let t = s.clone();
println!("s^s = {}", (&s).pow(&t));
println!("s^4 = {}", s.powi(4));
}
Multivariate series and polynomials
Multivariate polynomials can be created via nesting:
use series::{Polynomial, var};
var!(X);
var!(Y);
fn main() {
let p: Polynomial<X, Polynomial<Y, i32>> =
"+ 1 + 2*y + 3*y^2
+ (4 + 2*y + 3*y^2)*x
+ (2 + 4*y + 6*y^2)*x^2".parse().unwrap();
let q = Polynomial::new(Y, 0, vec![1, 2, 3]);
let q = Polynomial::new(X, 0, vec![q.clone(), &q + 3, &q * 2]);
assert_eq!(p, q);
}
Series are not supported as coefficients, since they can never be identically zero. A way around this limitation is to use the [Laurent] struct:
use series::{Laurent, Polynomial, Series, var};
var!(X);
var!(Y);
fn main() {
let s = Series::new(Y, 0, vec![1, 2, 3]);
let s = Laurent::from(s);
let p = Polynomial::new(X, 0, vec![s.clone(), &s + 3, &s * 2]);
}
Features
Default features
parse: EnableFromStrimplementation for series and polynomials.