# MBMath **Repository Path**: mini-bag/MBMath ## Basic Information - **Project Name**: MBMath - **Description**: 数值计算库,目前完成了初等函数、复合函数的求值,求n阶导函数。实现了泰勒展示式,泰勒展示后的误差分析。 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-18 - **Last Updated**: 2025-05-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 数值计算库 ## 一、初等数学数值解与导函数 > 简述: 初等数学模块针对小初高数学计算,不包含平面几何。 ### 1.1 函数基类 > 简述:设计初衷是作为所有函数的基类,四则运算和常数,也被看作函数使用。向外提供了求值,求n阶导函数,转成数学公式 ```c# public abstract class Function { // 求F(x) public abstract double Calculate(double x); // 求F(x)的n阶导函数 public abstract Function DerivativeFunction(int n); // 转成数学公式 public abstract override string ToString(); // 初步简化 f(x)=(x)+((x)*3)-->f(x)=x+x*3 public Function simaplify() { Function func = this.simaplifyOnce(); if(func == null) { return this; } return func.simaplify(); } public abstract Function simaplifyOnce(); } ``` ### 1.2 初等函数 #### 1.2.1 常数 > 简述:看作$f(x) = n$, 导函数是n=0的常数函数 ```c public class Constant : Function { // 对应的常数 private double n; public Constant(double n) { this.n = n; } // f(x) = n public override double Calculate(double x) { return n; } // f'(x) = 0 public override Function DerivativeFunction(int n) { return new Constant(0); } public override string ToString() { return n.ToString(); } public override Function simaplifyOnce() { return null; } } ``` #### 1.2.2 加法函数 > 计算: $f(x) = func1(x)+func2(x)$ ```c# public class Add : Function { // func1 + func2 private Function func1; private Function func2; public Add(Function func1, Function func2) { this.func1 = func1; this.func2 = func2; } // f(x) = func1(x) + func2(x) public override double Calculate(double x) { return this.func1.Calculate(x) + this.func2.Calculate(x); } // 求n阶导函数 public override Function DerivativeFunction(int n) { Function one = new Add(func1.DerivativeFunction(1), this.func2.DerivativeFunction(1)); if (n == 1) { return one; } return one.DerivativeFunction(n - 1); } // 优化计算 .... } ``` #### 1.2.3 减法函数 > 计算: $f(x) = func1(x)-func2(x)$ ```c# public class Sub : Function { private Function func1; private Function func2; public Sub(Function func1, Function func2) { this.func1 = func1; this.func2 = func2; } // f(x) = func1(x)-func2(x) public override double Calculate(double x) { return this.func1.Calculate(x) - this.func2.Calculate(x); } // f^(n)(x) = func1^(n)(x) - func2^(n)(x) public override Function DerivativeFunction(int n) { return new Sub(this.func1.DerivativeFunction(n), this.func2.DerivativeFunction(n)); } } ``` #### 1.2.4 乘法函数 > 计算:$f(x) = func1(x)*func2(x)$ ```c# public class Mul : Function { private Function func1; private Function func2; public Mul(Function func1, Function func2) { this.func1 = func1; this.func2 = func2; } // f(x) = func1(x) * func2(x) public override double Calculate(double x) { return this.func1.Calculate(x) * this.func2.Calculate(x); } // f'(x) = func1'(x)*func2(x) + func2'(x)*func1(x) // f^(n)(x) = [ (f^(n-1))(x) ]' public override Function DerivativeFunction(int n) { Function one = new Add(new Mul(this.func1.DerivativeFunction(1), this.func2), new Mul(this.func1, this.func2.DerivativeFunction(1))); if (n == 1) { return one; } return one.DerivativeFunction(n - 1); } } ``` #### 1.2.5 除法运算 > 计算:$f(x) = func1(x)/func2(x)$ ```c# public class Div : Function { private Function func1; private Function func2; public Div(Function func1, Function func2) { this.func1 = func1; this.func2 = func2; } public override double Calculate(double x) { return this.func1.Calculate(x) / this.func2.Calculate(x); } public override Function DerivativeFunction(int n) { Function one = new Div(new Sub(new Mul(this.func1.DerivativeFunction(n), this.func2), new Mul(this.func1, this.func2.DerivativeFunction(n))), new Pow(this.func2, 2)); if (n == 1) { return one; } return one.DerivativeFunction(n - 1); } } ``` #### 1.2.5 指数运算 > 计算:$f(x) = [func(x)]^n$ ```c# public class Pow : Function { private Function func; private double n; // f(x) = [func(x)]^n public Pow(Function func, double n) { this.func = func; this.n = n; } // f(x) = [func(x)]^n public override double Calculate(double x) { return Math.Pow(this.func.Calculate(x), this.n); } public override Function DerivativeFunction(int n) { Function one = new Mul(new Mul(new Constant(this.n), new Pow(this.func, this.n-1)), this.func.DerivativeFunction(1)); if (n == 1) { return one; } return one.DerivativeFunction(n - 1); } } ``` #### 1.2.6 sin函数 > 计算: $f(x) = sin(func(x))$ ```c# public class Sin : Function { private Function func; // f(x) = sin(func(x)) public Sin(Function func) { this.func = func; } public override double Calculate(double x) { return Math.Sin(func.Calculate(x)); } } ``` ## 1.2 复合函数数值解与导函数 #### 1.2.1 $f(x) = x^3+2x+1$ ```c# // f(x) = x^3+2x+1 X x = new X(); // x^3 Function pow3 = new Pow(x, 3); // 2x Function mult2 = new Mul(x, new Constant(2)); // 2x+1 Function add1 = new Add(mult2, new Constant(1)); // f(x)=x^3+2x+1 Function f = new Add(pow3, add1); // f(x) = ((x)^3 + ((x * 2) + 1)) Console.WriteLine("f(x) = " + f.ToString()); // f(3) = 34 Console.WriteLine("f(3) = " + f.Calculate(3)); // f'(x) = ((3 * (x)^2) + 2) Console.WriteLine("f'(x) = " + f.DerivativeFunction(1).simaplify()); // f''(x) = (3 * (2 * x)) Console.WriteLine("f''(x) = " + f.DerivativeFunction(2).simaplify()); // f'''(x) = 6 Console.WriteLine("f'''(x) = " + f.DerivativeFunction(3).simaplify()); ``` ## 二、高等数学 ### 2.1 泰勒公式 > 简述:泰勒展开后阶段成一个函数,初等数学数值库里包含了求n阶导函数的接口。 #### 2.1.1 构造n阶泰勒公式 > $f(x)$在$x=x_0$处泰勒展开 $n阶多项式$ ```c# public TaylorsFormula(Function func, int n, double x_0) { this.func = func; this.n = n; this.x_0 = x_0; this.taylor = new Constant(this.func.Calculate(this.x_0)); for (int i = 1; i <= this.n; i++) { Function c = new Constant(this.func.DerivativeFunction(i).Calculate(this.x_0)); this.taylor = new Add(this.taylor, new Mul(new Div(new Constant(this.func.DerivativeFunction(i).Calculate(this.x_0)), new Constant(Constants.Factorial(i))), new Pow(new Sub(new X(), new Constant(this.x_0)), i))); this.taylor = this.taylor.simaplify(); } } ``` #### 2.1.2 求值 ```c# public override double Calculate(double x) { return this.taylor.Calculate(x); } ``` #### 2.1.3 利用余项计算误差 ```c# public double epsilon(double x) { double left = Math.Min(this.x_0, x); double right = Math.Max(this.x_0, x); double step = (right - left) / 100; double max = 0; Function dir = this.func.DerivativeFunction(this.n+1); for (double i = left; i <= right; i += step) { double val = Math.Abs(dir.Calculate(i)); if (val > max) { max = val; } } return max / Constants.Factorial(n + 1) * Math.Pow(Math.Abs(x-this.x_0),n+1); } ``` #### 2.1.4 测试 > $f(x)=sin(x^3+2x+1)$ ```c# // f 是 1.2.1 中的复合函数 Sin sin = new Sin(f); TaylorsFormula formula = new TaylorsFormula(sin, 7, 3.25); // g(x) = sin(((x)^3 + ((x * 2) + 1))) Console.WriteLine("g(x) = "+sin.ToString()); // g(x)在x=3处泰勒展开3阶多项式:= (((((((-0.834607855128768 + (-18.5565760806231 * (x - 3.25))) + ((936.411299380621 / 2) * ((x - 3.25))^2)) + ((22700.3591547656 / 6) * ((x - 3.25))^3)) + ((-1000107.6549967 / 24) * ((x - 3.25))^4)) + ((-29976208.1016648 / 120) * ((x - 3.25))^5)) + ((992468637.563431 / 720) * ((x - 3.25))^6)) + ((40914460630.8471 / 5040) * ((x - 3.25))^7)) Console.WriteLine("g(x)在x=3处泰勒展开3阶多项式:= "+formula.ToString()); // g(3.3) = -0.430726072283907 t(3.3) = -0.429626034471011 Console.WriteLine("g(3.3) = " + sin.Calculate(3.3) + " t(3.3) = " + formula.Calculate(3.3)); // 预计误差: 0.00194708853595577 Console.WriteLine("预计误差: "+formula.epsilon(3.3)); ```