11个重大版本

0.12.0 2024年8月8日
0.11.0 2024年3月29日
0.10.0 2024年3月22日
0.8.0 2022年9月26日
0.2.0 2018年11月27日

#112 in 数学

Download history 104/week @ 2024-08-05

104 每月下载量

GPL-3.0-or-later

230KB
5.5K SLoC

系列

这是一个用于处理关于零的单变量截断Laurent级数和Laurent多项式的crate,即以下形式的表达式

s = a_n0*x^n0 + ... + a_N*x^N + O(x^{N+1})
p = a_n0*x^n0 + ... + a_N*x^N

其中 n0N 是整数,^ 表示指数运算。这样的表达式可以进行加法、减法和乘法运算。对于Laurent级数,还实现了除法、级数的幂、自然对数和指数等一些额外的简单函数。

可以执行的操作类型取决于变量的数据类型和系数。例如,如果我们对Laurent级数取对数,通常必须计算首项系数和展开变量的对数。因此,这个crate与提供至少基本符号数学的库结合使用最为有用。

使用方法

将以下内容添加到您的Cargo.toml文件中

[dependencies]
series = "0.9"

示例

use series::{MulInverse, SeriesIn, PolynomialIn};
use series::ops::{Ln,Exp,Pow};

// Create a new series in x, starting at order x^2 with coefficients 1, 2, 3,
// i.e. s = 1*x^2 + 2*x^3 + 3*x^4 + O(x^5).
let s = SeriesIn::new("x", 2, vec![1, 2, 3]);
println!("s = {}", s);

// The corresponding polynomial
// p = 1*x^2 + 2*x^3 + 3*x^4.
let p = PolynomialIn::new("x", 2, vec![1, 2, 3]);
assert_eq!(p, PolynomialIn::from(s));

// series with a cutoff power of 7
// s = 1*x^2 + 2*x^3 + 3*x^4 + O(x^7).
let s = SeriesIn::with_cutoff("x", 2, 7, vec![1, 2, 3]);

// To show various kinds of operations we now switch to floating-point
// coefficients

// Now s = 1 - x + O(x^5).
let s = SeriesIn::with_cutoff("x", 0, 5, vec![1., -1.]);
// Expand 1/(1-x) up to x^4.
let t = (&s).mul_inverse();
println!("1/(1-x) = {}", t);

// Series and polynomials can be added, subtracted, multiplied.
// Series can also be divided by other series.
// We can either move the arguments or use references
println!("s+t = {}", &s + &t);
println!("s-t = {}", &s - &t);
println!("s*t = {}", &s * &t);
println!("s/t = {}", &s/t);

// We can also multiply or divide each coefficient by a number
println!("s*3 = {}", &s * 3.);
println!("s/3 = {}", &s / 3.);

// More advanced operations on Laurent series in general require the
// variable type to be convertible to the coefficient type by
// implementing the From trait.
// In the examples shown here, this conversion is actually never used,
// so we can get away with a dummy implementation.
#[derive(Debug,Clone,PartialEq)]
struct Variable<'a>(&'a str);

impl<'a> From<Variable<'a>> for f64 {
    fn from(_s: Variable<'a>) -> f64 {
        panic!("Can't convert variable to f64")
    }
}

impl<'a> std::fmt::Display for Variable<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

// Now we can calculate logarithms, exponentials, and powers:
let s = SeriesIn::new(Variable("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));

依赖项

~140–405KB