public void Setup() { exprEasy = x + MathS.Sqr(x) - 3; exprMedium = MathS.Sin(x + MathS.Cos(x)) + MathS.Sqrt(x + MathS.Sqr(x)); exprHard = MathS.Sin(x + MathS.Arcsin(x)) / (MathS.Sqr(x) + MathS.Cos(x)) * MathS.Arccos(x / 1200 + 0.00032 / MathS.Cotan(x + 43)); exprSolvable = MathS.FromString("3arccos(2x + a)3 + 6arccos(2x + a)2 - a3 + 3"); }
public void Test1() { var x = MathS.Var("x"); var expr = x * x + MathS.Sin(x) * 0; Assert.IsTrue(expr.Substitute(x, 0).Simplify() == 0); }
public void Test3() { var x = MathS.Var("x"); var expr = MathS.Sin(x); Assert.IsTrue(expr.DefiniteIntegral(x, 0, 3).Real > 1.5); }
public void TestPatt2() { var y = MathS.Var("y"); var expr = (MathS.Sqr(MathS.Sin(x + 2 * y)) + MathS.Sqr(MathS.Cos(x + 2 * y))) / (2 * MathS.Sin(x - y) * MathS.Cos(x - y) + 1); Assert.IsTrue(expr.Simplify() == 1 / (MathS.Sin(2 * (x - y)) + 1)); }
public void Test2() { var x = MathS.Var("x"); var expr = MathS.Sin(x); Assert.IsTrue(expr.DefiniteIntegral(x, -1, 1) == 0); }
public void TestCosCustom() { var func = MathS.Cos(MathS.Pow(x, 3)); var expected = -3 * MathS.Sin(MathS.Pow(x, 3)) * MathS.Sqr(x); var actual = func.Derive(x).Simplify(); AssertEqEntity(expected.ToString(), actual.ToString()); }
public CompiledFuncTest() { multiFuncNotCompiled = (MathS.Log(3, x) + MathS.Sqr(x)) * MathS.Sin(x + MathS.Cosec(x)); multiFunc = multiFuncNotCompiled.Compile(x); Expression <Func <Complex, Complex> > expr = x => (Complex.Log(x, 3) + Complex.Pow(x, 2)) * Complex.Sin(x + 1 / Complex.Sin(x)); linqFunc = expr.Compile(); }
/// <summary> /// 根据旋转轴和旋转角生成单位四元数 /// </summary> /// <param name="angle"></param> /// <param name="axisNormal"></param> /// <returns>单位四元数</returns> public static Quaternion AngleAxis(float angle, Vector3 axisNormal) { float radian = MathS.DegToRad * angle * 0.5f; float real = MathS.Cos(radian); Vector3 imag = MathS.Sin(radian) * axisNormal; return(new Quaternion(imag.x, imag.y, imag.z, real)); }
public void TestCosCustom() { var func = MathS.Cos(MathS.Pow(x, 3)); var expected = -3 * MathS.Sin(MathS.Pow(x, 3)) * MathS.Sqr(x); var actual = func.Differentiate(x).Simplify(); Assert.Equal(expected, actual); }
public CacheCompiledFunc() { notCompiled = MathS.Sin(MathS.Sqr(x)) + MathS.Cos(MathS.Sqr(x)) + MathS.Sqr(x) + MathS.Sin(MathS.Sqr(x)); complexFunc = notCompiled.Compile(x); Expression <Func <Complex, Complex> > linqExpr = x => Complex.Sin(Complex.Pow(x, 2)) + Complex.Cos(Complex.Pow(x, 2)) + Complex.Pow(x, 2) + Complex.Sin(Complex.Pow(x, 2)); linqComp = linqExpr.Compile(); }
public void Test2() { var x = MathS.Var("x"); var y = MathS.Var("y"); var expr = x.Pow(x) - MathS.Sqrt(x - 3) / x + MathS.Sin(x); var expected = (3 * y).Pow(3 * y) - MathS.Sqrt(3 * y - 3) / (3 * y) + MathS.Sin(3 * y); var actual = expr.Substitute(x, 3 * y); Assert.IsTrue(expected == actual); }
private void EveryFrame(object sender, EventArgs e) { var B = MathS.Var("B"); var expr2 = B * MathS.Sin(t + B) * MathS.Pow(MathS.e, MathS.i * B * MathS.Cos(t)); var niceFunc2 = expr2.Compile(B); plotter.Clear(); plotter.PlotIterativeComplex(niceFunc2, 0, t); plotter.Render(); t += 0.0005m; }
public void TestComplicated() { Entity subExpr = "(a * x2 + b x) / (c x2 - 3)"; Entity expr = MathS.Sqrt(subExpr * 3 / MathS.Sin(subExpr) + MathS.Sin("d")); Entity.Variable x = nameof(x); Entity dest = Real.PositiveInfinity; var limit = expr.Limit(x, dest, ApproachFrom.Left); Assert.NotNull(limit); Assert.Equal("sqrt(a / c * 3 / sin(a / c) + sin(d))", limit?.Stringize()); }
public DerivationTest() { IterCount = 10000; tests = new List <Func <object> > { () => x.Derive(x), () => (MathS.Cos(x) * MathS.Sin(x)).Derive(x), () => (MathS.Sqr(MathS.Sin(x + 2 * y)) + MathS.Sqr(MathS.Cos(x + 2 * y))).Derive(x), () => (x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x)))).Derive(x) }; }
public SubsTest() { IterCount = 10000; tests = new List <Func <object> > { () => (x * MathS.Sin(x)).Substitute(x, 3).Eval(), () => (MathS.Cos(x) * MathS.Sin(x)).Substitute(x, 3).Eval(), () => (MathS.Sqr(MathS.Sin(x + 2 * x)) + MathS.Sqr(MathS.Cos(x + 2 * x))).Substitute(x, 3).Eval(), () => (x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x)))).Substitute(x, 3).Eval() }; }
public SimplificationTest() { IterCount = 1500; tests = new List <Func <object> > { () => (x * MathS.Sin(x)).Simplify(), () => (MathS.Cos(x) * MathS.Sin(x)).Simplify(), () => (MathS.Sqr(MathS.Sin(x + 2 * y)) + MathS.Sqr(MathS.Cos(x + 2 * y))).Simplify(), () => (x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x))) * x * MathS.Cos(x) / MathS.Sin(MathS.Sqrt(x / MathS.Ln(x)))).Simplify() }; }
/// <summary> /// 球面插值 /// </summary> /// <param name="q1">单位四元数</param> /// <param name="q2">单位四元数</param> /// <param name="t">0--1</param> /// <returns>单位四元数</returns> public static Quaternion Slerp(Quaternion q1, Quaternion q2, float t) { Quaternion uq = q1; t = MathS.Clamp(t, 0, 1); float dot = Quaternion.Dot(q1, q2); if ((1 - dot) > Threshold) { float radian = MathS.Acos(dot); if (radian < 0) { radian = MathS.PI - radian; } float sin = MathS.Sin(radian); float t1 = MathS.Sin((1 - t) * radian) / sin; float t2 = MathS.Sin(t * radian) / sin; uq = q1 * t1 + q2 * t2; } return(uq); }
public void TestCoTan() { var func = MathS.Cotan(2 * x); AssertEqEntity(func.Derive(x).Simplify(), -2 / MathS.Pow(MathS.Sin(2 * x), 2)); }
public void TestCusfunc() { var func = MathS.Sin(x).Pow(2); AssertEqEntity(func.Derive(x).Simplify(3), MathS.Sin(2 * x)); }
public void Test3() { var expr = MathS.Sin(x); Assert.True(MathS.Compute.DefiniteIntegral(expr, x, 0, 3).RealPart > 1.5); }
public void TestSin() { var func = MathS.Sin(x); AssertEqEntity(func.Derive(x).Simplify(), MathS.Cos(x)); }
public void Test2() { var func = (MathS.Sin(x) + MathS.Cos(x)).Compile(x); Assert.IsTrue(func.Substitute(0) == 1); }
[TestMethod] public void Set3() => Test(@"\left\{\sqrt{x},{x}^{2},\sin\left(x\right)\right\}", MathS.Sets.Finite(MathS.Sqrt(x), MathS.Sqr(x), MathS.Sin(x)));
public void TestCoTan() { var func = MathS.Cotan(2 * x); Assert.Equal(-2 / MathS.Pow(MathS.Sin(2 * x), 2), func.Differentiate(x).Simplify()); }
public void TestSin() { var func = MathS.Sin(x); Assert.Equal(MathS.Cos(x), func.Differentiate(x).Simplify()); }
/// <summary> /// Returns a set of possible roots of a function, e. g. /// sin(x) = a => /// x = arcsin(a) + 2 pi n /// x = pi - arcsin(a) + 2 pi n /// </summary> /// <param name="func"></param> /// <param name="value"></param> /// <param name="x"></param> /// <returns></returns> public static Set InvertFunctionEntity(FunctionEntity func, Entity value, Entity x) { Entity a = func.Children[0]; Entity b = func.Children.Count == 2 ? func.Children[1] : null; int arg = func.Children.Count == 2 && func.Children[1].FindSubtree(x) != null ? 1 : 0; var n = Utils.FindNextIndex(func + value, "n"); var res = new Set(); var pi = MathS.pi; Set GetNotNullEntites(Set set) { return(set.FiniteWhere(el => el.entType != Entity.EntType.NUMBER || el.GetValue().IsDefinite())); } switch (func.Name) { // Consider case when sin(sin(x)) where double-mention of n occures case "sinf": { // sin(x) = value => x = arcsin(value) + 2pi * n res.AddRange(GetNotNullEntites(FindInvertExpression(a, MathS.Arcsin(value) + 2 * pi * n, x))); // sin(x) = value => x = pi - arcsin(value) + 2pi * n res.AddRange(GetNotNullEntites(FindInvertExpression(a, pi - MathS.Arcsin(value) + 2 * pi * n, x))); return(res); } case "cosf": { // cos(x) = value => x = arccos(value) + 2pi * n res.AddRange(GetNotNullEntites(FindInvertExpression(a, MathS.Arccos(value) + 2 * pi * n, x))); // cos(x) = value => x = -arccos(value) + 2pi * n res.AddRange(GetNotNullEntites(FindInvertExpression(a, -MathS.Arccos(value) - 2 * pi * n, x))); return(res); } case "tanf": { var inverted = FindInvertExpression(a, MathS.Arctan(value) + pi * n, x); // tan(x) = value => x = arctan(value) + pi * n res.AddRange(GetNotNullEntites(inverted)); return(res); } case "cotanf": { var inverted = FindInvertExpression(a, MathS.Arccotan(value) + pi * n, x); // cotan(x) = value => x = arccotan(value) res.AddRange(GetNotNullEntites(inverted)); return(res); } case "arcsinf": // arcsin(x) = value => x = sin(value) if (EntityInBounds(value, ArcsinFrom, ArcsinTo)) { return(GetNotNullEntites(FindInvertExpression(a, MathS.Sin(value), x))); } else { return(Empty); } case "arccosf": // arccos(x) = value => x = cos(value) if (EntityInBounds(value, ArccosFrom, ArccosTo)) { return(GetNotNullEntites(FindInvertExpression(a, MathS.Cos(value), x))); } else { return(Empty); } case "arctanf": // arctan(x) = value => x = tan(value) return(GetNotNullEntites(FindInvertExpression(a, MathS.Tan(value), x))); case "arccotanf": // arccotan(x) = value => x = cotan(value) return(GetNotNullEntites(FindInvertExpression(a, MathS.Cotan(value), x))); case "logf": if (arg != 0) { // log(x, a) = value => x = a ^ value return(GetNotNullEntites(FindInvertExpression(b, MathS.Pow(a, value), x))); } else { // log(a, x) = value => a = x ^ value => x = a ^ (1 / value) return(GetNotNullEntites(FindInvertExpression(a, MathS.Pow(b, 1 / value), x))); } default: throw new SysException("Unknown function"); } }
public void Test7() => Assert.Equal(3 * x, MathS.Sin(MathS.Arcsin(x * 3)).Simplify());
public void Test4() => MathS.FromString((MathS.Sin(x) / MathS.Cos(x)).Differentiate(x).Stringize() ?? "");
[TestMethod] public void Trig() => TestSimplify(@"\sin\left(\cos\left(\tan\left(\cot\left(x\right)\right)\right)\right)", MathS.Sin(MathS.Cos(MathS.Tan(MathS.Cotan(x)))));
public void TestCusfunc() { var func = MathS.Sin(x).Pow(2); Assert.Equal(MathS.Sin(2 * x), func.Differentiate(x).Simplify(3)); }