示例#1
0
        protected string ToString(FuncNode parent,
                                  KnownFuncType funcType,
                                  IList <KnownFuncType> types)
        {
            var builder = new StringBuilder();

            if (parent == null || parent.Name == null || parent.Children.Count <= 1)
            {
                AppendMathFunctionNode(builder, funcType);
                return(builder.ToString());
            }

            var funcNodeParent = parent as FuncNode;

            if (funcNodeParent != null && funcNodeParent.IsKnown)
            {
                if (types.Contains((KnownFuncType)funcNodeParent.FunctionType))
                {
                    AppendMathFunctionNode(builder, funcType);
                    return(builder.ToString());
                }
            }

            builder.Append("(");
            AppendMathFunctionNode(builder, funcType);
            builder.Append(")");
            return(builder.ToString());
        }
示例#2
0
 private void AppendMathFunctionNode(StringBuilder builder, KnownFuncType funcType)
 {
     builder.Append(Children[0].ToString(this) + " ");
     for (int i = 1; i < Children.Count; i++)
     {
         builder.AppendFormat("{0} {1} ", KnownFunc.BinaryFuncsNames[funcType], Children[i].ToString(this));
     }
     builder.Remove(builder.Length - 1, 1);
 }
 protected void PushOrRemoveFunc(KnownFuncType funcType)
 {
     if (ArgsCount[ArgsCount.Count - 1] == 1 && ArgsFuncTypes[ArgsFuncTypes.Count - 1] == funcType)
     {
         ArgsCount.RemoveAt(ArgsCount.Count - 1);
         ArgsFuncTypes.RemoveAt(ArgsFuncTypes.Count - 1);
     }
     else
     {
         PushFunction(KnownFunc.BinaryFuncsNames[funcType]);
     }
 }
示例#4
0
		public FuncNode(KnownFuncType type, IList<MathFuncNode> args)
		{
			FunctionType = type;
			string name;
			if (KnownFunc.UnaryFuncsNames.TryGetValue(type, out name))
				Name = name;
			else if (KnownFunc.BinaryFuncsNames.TryGetValue(type, out name))
				Name = name;
			else
				Name = FunctionType.ToString();
			foreach (var arg in args)
				Childs.Add(arg);
		}
示例#5
0
        public FuncNode(KnownFuncType type, IEnumerable <MathFuncNode> args)
        {
            FunctionType = type;
            string name;

            if (KnownFunc.UnaryFuncsNames.TryGetValue(type, out name))
            {
                Name = name;
            }
            else if (KnownFunc.BinaryFuncsNames.TryGetValue(type, out name))
            {
                Name = name;
            }
            else
            {
                Name = FunctionType.ToString();
            }
            foreach (MathFuncNode arg in args)
            {
                Children.Add(arg);
            }
        }
示例#6
0
 public FuncNode(KnownFuncType type, MathFuncNode arg1, MathFuncNode arg2, MathFuncNode arg3)
     : this(type, new List <MathFuncNode>() { arg1, arg2, arg3 })
 {
 }
示例#7
0
 public FuncNode(KnownFuncType type, MathFuncNode arg)
     : this(type, new List <MathFuncNode>() { arg })
 {
 }
示例#8
0
 public FuncNode(KnownFuncType type)
     : this(type, new List <MathFuncNode>())
 {
 }
	protected void PushFunc(KnownFuncType funcType, int argCount = 1)
	{
		ArgsCount.Add(argCount);
		ArgsFuncTypes.Add(funcType);
	}
示例#10
0
		public FuncNode(KnownFuncType type, MathFuncNode arg1, MathFuncNode arg2, MathFuncNode arg3)
			: this(type, new List<MathFuncNode>() { arg1, arg2, arg3 })
		{
		}
示例#11
0
		public FuncNode(KnownFuncType type, MathFuncNode arg)
			: this(type, new List<MathFuncNode>() { arg })
		{
		}
示例#12
0
		public FuncNode(KnownFuncType type)
			: this(type, new List<MathFuncNode>())
		{
		}
示例#13
0
		private void AppendMathFunctionNode(StringBuilder builder, KnownFuncType funcType)
		{
			builder.Append(Childs[0].ToString(this) + " ");
			for (int i = 1; i < Childs.Count; i++)
				builder.AppendFormat("{0} {1} ", KnownFunc.BinaryFuncsNames[funcType], Childs[i].ToString(this));
			builder.Remove(builder.Length - 1, 1);
		}
示例#14
0
		protected string ToString(FuncNode parent,
			KnownFuncType funcType,
			IList<KnownFuncType> types)
		{
			var builder = new StringBuilder();

			if (parent == null || parent.Name == null || parent.Childs.Count <= 1)
			{
				AppendMathFunctionNode(builder, funcType);
				return builder.ToString();
			}

			var funcNodeParent = parent as FuncNode;
			if (funcNodeParent != null && funcNodeParent.IsKnown)
				if (types.Contains((KnownFuncType)funcNodeParent.FunctionType))
				{
					AppendMathFunctionNode(builder, funcType);
					return builder.ToString();
				}

			builder.Append("(");
			AppendMathFunctionNode(builder, funcType);
			builder.Append(")");
			return builder.ToString();
		}
	protected void PushOrRemoveFunc(KnownFuncType funcType)
	{
		if (ArgsCount[ArgsCount.Count - 1] == 1 && ArgsFuncTypes[ArgsFuncTypes.Count - 1] == funcType)
		{
			ArgsCount.RemoveAt(ArgsCount.Count - 1);
			ArgsFuncTypes.RemoveAt(ArgsFuncTypes.Count - 1);
		}
		else
			PushFunction(KnownFunc.BinaryFuncsNames[funcType]);
	}
 protected void PushFunc(KnownFuncType funcType, int argCount = 1)
 {
     ArgsCount.Add(argCount);
     ArgsFuncTypes.Add(funcType);
 }
		private CalculatedNode CalculateValues(KnownFuncType? funcType, IList<MathFuncNode> args)
		{
			double result;
			switch (funcType)
			{
				case KnownFuncType.Add:
					result = args[0].DoubleValue;
					for (int i = 1; i < args.Count; i++)
						result += args[i].DoubleValue;
					return new CalculatedNode(result);

				case KnownFuncType.Sub:
					result = args[0].DoubleValue;
					for (int i = 1; i < args.Count; i++)
						result -= args[i].DoubleValue;
					return new CalculatedNode(result);

				case KnownFuncType.Mult:
					result = args[0].DoubleValue;
					for (int i = 1; i < args.Count; i++)
						result *= args[i].DoubleValue;
					return new CalculatedNode(result);

				case KnownFuncType.Div:
					result = args[0].DoubleValue;
					for (int i = 1; i < args.Count; i++)
						result /= args[i].DoubleValue;
					return new CalculatedNode(result);

				case KnownFuncType.Pow:
					if (args[1].DoubleValue == 0.5)
						return new CalculatedNode(Math.Sqrt(args[0].DoubleValue));
					else
						return new CalculatedNode(Math.Pow(args[0].DoubleValue, args[1].DoubleValue));

				case KnownFuncType.Neg:
					return new CalculatedNode(-args[0].DoubleValue);

				case KnownFuncType.Sgn:
					return new CalculatedNode((double)Math.Sign(args[0].DoubleValue));

				case KnownFuncType.Trunc:
					return new CalculatedNode(Math.Truncate(args[0].DoubleValue));

				case KnownFuncType.Round:
					return new CalculatedNode(Math.Round(args[0].DoubleValue));

				case KnownFuncType.Diff:
					return new CalculatedNode(0.0);

				case KnownFuncType.Sqrt:
					return new CalculatedNode(Math.Sqrt(args[0].DoubleValue));

				case KnownFuncType.Sin:
					return new CalculatedNode(Math.Sin(args[0].DoubleValue));

				case KnownFuncType.Cos:
					return new CalculatedNode(Math.Cos(args[0].DoubleValue));

				case KnownFuncType.Tan:
					return new CalculatedNode(Math.Tan(args[0].DoubleValue));

				case KnownFuncType.Cot:
					return new CalculatedNode(1 / Math.Tan(args[0].DoubleValue));

				case KnownFuncType.Arcsin:
					return new CalculatedNode(Math.Asin(args[0].DoubleValue));

				case KnownFuncType.Arccos:
					return new CalculatedNode(Math.Acos(args[0].DoubleValue));

				case KnownFuncType.Arctan:
					return new CalculatedNode(Math.Atan(args[0].DoubleValue));

				case KnownFuncType.Arccot:
					return new CalculatedNode(Math.PI / 2 - Math.Atan(args[0].DoubleValue));

				case KnownFuncType.Sinh:
					return new CalculatedNode(Math.Sinh(args[0].DoubleValue));

				case KnownFuncType.Cosh:
					return new CalculatedNode(Math.Cosh(args[0].DoubleValue));

				case KnownFuncType.Arcsinh:
					return new CalculatedNode(Math.Log(args[0].DoubleValue + Math.Sqrt(args[0].DoubleValue * args[0].DoubleValue + 1)));

				case KnownFuncType.Arcosh:
					return new CalculatedNode(Math.Log(args[0].DoubleValue + Math.Sqrt(args[0].DoubleValue * args[0].DoubleValue - 1)));

				case KnownFuncType.Ln:
					return new CalculatedNode(Math.Log(args[0].DoubleValue));

				case KnownFuncType.Log10:
					return new CalculatedNode(Math.Log10(args[0].DoubleValue));

				case KnownFuncType.Log:
					return new CalculatedNode(Math.Log(args[0].DoubleValue, args[1].DoubleValue));

				case KnownFuncType.Abs:
					return new CalculatedNode(Math.Abs(args[0].DoubleValue));

				default:
					return null;
			}
		}
示例#18
0
		public ValueNode SimplifyValues(KnownFuncType? funcType, IList<ValueNode> args)
		{
			Rational<long> result;
			double temp = 0.0;

			switch (funcType)
			{
				case KnownFuncType.Add:
					result = args[0].Value;
					for (int i = 1; i < args.Count; i++)
						result += args[i].Value;
					return new ValueNode(result);

				case KnownFuncType.Sub:
					result = args[0].Value;
					for (int i = 1; i < args.Count; i++)
						result -= args[i].Value;
					return new ValueNode(result);

				case KnownFuncType.Mult:
					result = args[0].Value;
					for (int i = 1; i < args.Count; i++)
						result *= args[i].Value;
					return new ValueNode(result);

				case KnownFuncType.Div:
					result = args[0].Value;
					for (int i = 1; i < args.Count; i++)
						result /= args[i].Value;
					return new ValueNode(result);

				case KnownFuncType.Exp:
					if (args[1].Value.ToDouble() == 0.5)
						temp = Math.Sqrt(args[0].Value.ToDouble());
					else
						temp = Math.Pow(args[0].Value.ToDouble(), args[1].Value.ToDouble());
					break;
				
				case KnownFuncType.Neg:
					return new ValueNode(-args[0].Value);

				case KnownFuncType.Sgn:
					return new ValueNode(new Rational<long>((long)Math.Sign(args[0].DoubleValue), 1, false));

				case KnownFuncType.Trunc:
					return new ValueNode(new Rational<long>((long)Math.Truncate(args[0].DoubleValue), 1, false));

				case KnownFuncType.Round:
					return new ValueNode(new Rational<long>((long)Math.Round(args[0].DoubleValue), 1, false));

				case KnownFuncType.Diff:
					return new ValueNode(0);

				case KnownFuncType.Sqrt:
					temp = Math.Sqrt(args[0].DoubleValue);
					break;

				case KnownFuncType.Sin:
					temp = Math.Sin(args[0].DoubleValue);
					break;

				case KnownFuncType.Cos:
					temp = Math.Cos(args[0].DoubleValue);
					break;

				case KnownFuncType.Tan:
					temp = Math.Tan(args[0].DoubleValue);
					break;

				case KnownFuncType.Cot:
					temp = 1 / Math.Tan(args[0].DoubleValue);
					break;

				case KnownFuncType.Arcsin:
					temp = Math.Asin(args[0].DoubleValue);
					break;

				case KnownFuncType.Arccos:
					temp = Math.Acos(args[0].DoubleValue);
					break;

				case KnownFuncType.Arctan:
					temp = Math.Atan(args[0].DoubleValue);
					break;

				case KnownFuncType.Arccot:
					temp = Math.PI / 2 - Math.Atan(args[0].DoubleValue);
					break;

				case KnownFuncType.Sinh:
					temp = Math.Sinh(args[0].DoubleValue);
					break;

				case KnownFuncType.Cosh:
					temp = Math.Cosh(args[0].DoubleValue);
					break;

				case KnownFuncType.Arcsinh:
					temp = Math.Log(args[0].DoubleValue + Math.Sqrt(args[0].DoubleValue * args[0].DoubleValue + 1));
					break;

				case KnownFuncType.Arcosh:
					temp = Math.Log(args[0].DoubleValue + Math.Sqrt(args[0].DoubleValue * args[0].DoubleValue - 1));
					break;

				case KnownFuncType.Ln:
					temp = Math.Log(args[0].DoubleValue);
					break;

				case KnownFuncType.Log10:
					temp = Math.Log10(args[0].DoubleValue);
					break;

				case KnownFuncType.Log:
					temp = Math.Log(args[0].DoubleValue, args[1].DoubleValue);
					break;

				case KnownFuncType.Abs:
					temp = Math.Abs(args[0].DoubleValue);
					break;

				default:
					return null;
			}

			try
			{
				if (Rational<long>.FromDecimal((decimal)temp, out result, 14, false, 4, 8))
				return new ValueNode(result);
			}
			catch
			{
			}
			
			return null;
		}