找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1371|回复: 0
打印 上一主题 下一主题
收起左侧

Symbolism-master C#计算库下载

[复制链接]
跳转到指定楼层
楼主


Symbolism-master C#源程序如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. using Symbolism.Has;

  6. using Symbolism.Trigonometric;

  7. using Symbolism.CoefficientGpe;
  8. using Symbolism.AlgebraicExpand;

  9. using Symbolism.DegreeGpe;

  10. namespace Symbolism.IsolateVariable
  11. {
  12.     public static class Extensions
  13.     {
  14.         public static MathObject IsolateVariableEq(this Equation eq, Symbol sym)
  15.         {
  16.             if (eq.Operator == Equation.Operators.NotEqual) return eq;

  17.             if (eq.FreeOf(sym)) return eq;

  18.             // sin(x) / cos(x) == y   ->   tan(x) == y

  19.             if (eq.a is Product && (eq.a as Product).elts.Any(elt => elt == new Sin(sym)) &&
  20.                 eq.a is Product && (eq.a as Product).elts.Any(elt => elt == 1 / new Cos(sym)))
  21.                 return
  22.                     (eq.a / new Sin(sym) * new Cos(sym) * new Tan(sym) == eq.b).IsolateVariableEq(sym);

  23.             // A sin(x)^2 == B sin(x) cos(x)   ->   A sin(x)^2 / (B sin(x) cos(x)) == 1

  24.             if (
  25.                 eq.a is Product &&
  26.                 (eq.a as Product).elts.Any(elt =>
  27.                     (elt == new Sin(sym)) ||
  28.                     ((elt is Power) && (elt as Power).bas == new Sin(sym) && (elt as Power).exp is Number)) &&

  29.                 eq.b is Product &&
  30.                 (eq.b as Product).elts.Any(elt =>
  31.                     (elt == new Sin(sym)) ||
  32.                     ((elt is Power) && (elt as Power).bas == new Sin(sym) && (elt as Power).exp is Number))
  33.                 )
  34.                 return
  35.                     (eq.a / eq.b == 1).IsolateVariableEq(sym);

  36.             if (eq.b.Has(sym)) return IsolateVariableEq(new Equation(eq.a - eq.b, 0), sym);

  37.             if (eq.a == sym) return eq;

  38.             // (a x^2 + c) / x == - b

  39.             if (eq.a is Product &&
  40.                 (eq.a as Product).elts.Any(
  41.                     elt =>
  42.                         elt is Power &&
  43.                         (elt as Power).bas == sym &&
  44.                         (elt as Power).exp == -1))
  45.                 return IsolateVariableEq(eq.a * sym == eq.b * sym, sym);

  46.             //if (eq.a is Product &&
  47.             //    (eq.a as Product).elts.Any(
  48.             //        elt =>
  49.             //            elt is Power &&
  50.             //            (elt as Power).bas == sym &&
  51.             //            (elt as Power).exp is Integer &&
  52.             //            ((elt as Power).exp as Integer).val < 0))
  53.             //    return IsolateVariableEq(eq.a * sym == eq.b * sym, sym);


  54.             // if (eq.a.Denominator() is Product &&
  55.             //     (eq.a.Denominator() as Product).Any(elt => elt.Base() == sym)
  56.             //
  57.             //


  58.             // (x + y)^(1/2) == z
  59.             //
  60.             // x == -y + z^2   &&   z >= 0

  61.             if (eq.a is Power && (eq.a as Power).exp == new Integer(1) / 2)
  62.                 return IsolateVariableEq((eq.a ^ 2) == (eq.b ^ 2), sym);

  63.             // 1 / sqrt(x) == y

  64.             if (eq.a is Power && (eq.a as Power).exp == -new Integer(1) / 2)
  65.                 return (eq.a / eq.a == eq.b / eq.a).IsolateVariable(sym);

  66.             // x ^ 2 == y
  67.             // x ^ 2 - y == 0

  68.             if (eq.a.AlgebraicExpand().DegreeGpe(new List<MathObject>() { sym }) == 2 &&
  69.                 eq.b != 0)
  70.             {
  71.                 return
  72.                     (eq.a - eq.b == 0).IsolateVariable(sym);
  73.             }

  74.             // a x^2 + b x + c == 0

  75.             if (eq.a.AlgebraicExpand().DegreeGpe(new List<MathObject>() { sym }) == 2)
  76.             {
  77.                 var a = eq.a.AlgebraicExpand().CoefficientGpe(sym, 2);
  78.                 var b = eq.a.AlgebraicExpand().CoefficientGpe(sym, 1);
  79.                 var c = eq.a.AlgebraicExpand().CoefficientGpe(sym, 0);

  80.                 if (a == null || b == null || c == null) return eq;

  81.                 return new Or(

  82.                     new And(
  83.                         sym == (-b + (((b ^ 2) - 4 * a * c) ^ (new Integer(1) / 2))) / (2 * a),
  84.                         (a != 0).Simplify()
  85.                         ).Simplify(),

  86.                     new And(
  87.                         sym == (-b - (((b ^ 2) - 4 * a * c) ^ (new Integer(1) / 2))) / (2 * a),
  88.                         (a != 0).Simplify()
  89.                         ).Simplify(),

  90.                     new And(sym == -c / b, a == 0, (b != 0).Simplify()).Simplify(),

  91.                     new And(
  92.                         (a == 0).Simplify(),
  93.                         (b == 0).Simplify(),
  94.                         (c == 0).Simplify()
  95.                         ).Simplify()

  96.                 ).Simplify();
  97.             }


  98.             // (x + y == z).IsolateVariable(x)

  99.             if (eq.a is Sum && (eq.a as Sum).elts.Any(elt => elt.FreeOf(sym)))
  100.             {
  101.                 var items = ((Sum)eq.a).elts.FindAll(elt => elt.FreeOf(sym));

  102.                 //return IsolateVariable(
  103.                 //    new Equation(
  104.                 //        eq.a - new Sum() { elts = items }.Simplify(),
  105.                 //        eq.b - new Sum() { elts = items }.Simplify()),
  106.                 //    sym);


  107.                 //var new_a = eq.a; items.ForEach(elt => new_a = new_a - elt);
  108.                 //var new_b = eq.b; items.ForEach(elt => new_b = new_b - elt);

  109.                 var new_a = new Sum() { elts = (eq.a as Sum).elts.Where(elt => items.Contains(elt) == false).ToList() }.Simplify();
  110.                 var new_b = eq.b; items.ForEach(elt => new_b = new_b - elt);

  111.                 // (new_a as Sum).Where(elt => items.Contains(elt) == false)

  112.                 return IsolateVariableEq(new Equation(new_a, new_b), sym);

  113.                 //return IsolateVariable(
  114.                 //    new Equation(
  115.                 //        eq.a + new Sum() { elts = items.ConvertAll(elt => elt * -1) }.Simplify(),
  116.                 //        eq.b - new Sum() { elts = items }.Simplify()),
  117.                 //    sym);
  118.             }

  119.             // a b + a c == d

  120.             // a + a c == d

  121.             if (eq.a is Sum && (eq.a as Sum).elts.All(elt => elt.DegreeGpe(new List<MathObject>() { sym }) == 1))
  122.             {
  123.                 //return
  124.                 //    (new Sum() { elts = (eq.a as Sum).elts.Select(elt => elt / sym).ToList() }.Simplify() == eq.b / sym)
  125.                 //    .IsolateVariable(sym);

  126.                 return
  127.                     (sym * new Sum() { elts = (eq.a as Sum).elts.Select(elt => elt / sym).ToList() }.Simplify() == eq.b)
  128.                     .IsolateVariable(sym);
  129.             }

  130.             // -sqrt(x) + z * x == y

  131.             if (eq.a is Sum && eq.a.Has(sym ^ (new Integer(1) / 2))) return eq;

  132.             // sqrt(a + x) - z * x == -y

  133.             if (eq.a is Sum && eq.a.Has(elt => elt is Power && (elt as Power).exp == new Integer(1) / 2 && (elt as Power).bas.Has(sym)))
  134.                 return eq;

  135.             if (eq.a is Sum && eq.AlgebraicExpand().Equals(eq)) return eq;

  136.             if (eq.a is Sum) return eq.AlgebraicExpand().IsolateVariable(sym);
  137.                         
  138.             // (x + 1) / (x + 2) == 3

  139.             if (eq.a.Numerator().Has(sym) && eq.a.Denominator().Has(sym))
  140.             {
  141.                 return IsolateVariableEq(eq.a * eq.a.Denominator() == eq.b * eq.a.Denominator(), sym);
  142.             }
  143.             
  144.             // sqrt(2 + x) * sqrt(3 + x) == y

  145.             if (eq.a is Product && (eq.a as Product).elts.All(elt => elt.Has(sym))) return eq;

  146.             if (eq.a is Product)
  147.             {
  148.                 var items = ((Product)eq.a).elts.FindAll(elt => elt.FreeOf(sym));

  149.                 return IsolateVariableEq(
  150.                     new Equation(
  151.                         eq.a / new Product() { elts = items }.Simplify(),
  152.                         eq.b / new Product() { elts = items }.Simplify()),
  153.                     sym);
  154.             }

  155.             // x ^ -2 == y

  156.             if (eq.a is Power &&
  157.                 (eq.a as Power).bas == sym &&
  158.                 (eq.a as Power).exp is Integer &&
  159.                 ((eq.a as Power).exp as Integer).val < 0)
  160.                 return (eq.a / eq.a == eq.b / eq.a).IsolateVariableEq(sym);

  161.             if (eq.a is Power) return eq;

  162.             // sin(x) == y

  163.             // Or(x == asin(y), x  == Pi - asin(y))

  164.             if (eq.a is Sin)
  165.                 return
  166.                     new Or(
  167.                         (eq.a as Sin).args[0] == new Asin(eq.b),
  168.                         (eq.a as Sin).args[0] == new Symbol("Pi") - new Asin(eq.b))
  169.                         .IsolateVariable(sym);
  170.             
  171.             // tan(x) == y

  172.             // x == atan(t)
  173. ……………………

  174. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
Symbolism-master.zip (62.31 KB, 下载次数: 4)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表