public void CalculateTest2() { var functions = new FunctionCollection(); var func = new UserFunction("f", new IExpression[] { new Number(1) }, 1); Assert.AreEqual(Math.Log(1), func.Calculate(functions)); }
public void CalculateTest1() { var functions = new FunctionCollection(); functions.Add(new UserFunction("f", new IExpression[] { new Variable("x") }, 1), new Ln(new Variable("x"))); var func = new UserFunction("f", new IExpression[] { new Number(1) }, 1); Assert.AreEqual(Math.Log(1), func.Calculate(functions)); }
public void ExecuteTest2() { var functions = new FunctionCollection(); var func = new UserFunction("f", new IExpression[] { new Number(1) }, 1); Assert.Throws<KeyNotFoundException>(() => func.Execute(functions)); }
public void CalculateTest3() { var uf1 = new UserFunction("func", new[] { new Variable("x") }, 1); var func = new DelegateExpression(p => (double)p.Parameters["x"] == 10 ? 0 : 1); var funcs = new FunctionCollection(); funcs.Add(uf1, func); var uf2 = new UserFunction("func", new[] { new Number(12) }, 1); var result = uf2.Calculate(new ExpressionParameters(funcs)); Assert.AreEqual(1.0, result); }
public void UndefFuncTest() { var key1 = new UserFunction("f", 0); var key2 = new UserFunction("f", 1); var functions = new FunctionCollection { { key1, new Number(1) }, { key2, new Number(2) } }; var undef = new Undefine(key1); undef.Calculate(functions); Assert.IsFalse(functions.ContainsKey(key1)); Assert.IsTrue(functions.ContainsKey(key2)); }
public void DefineFuncWithParamsTest() { var uf = new UserFunction("s", 1); var func = new Sin(new Variable("x")); var exp = new Define(uf, func); var parameters = new ExpressionParameters(); var result = exp.Execute(parameters); Assert.Equal(func, parameters.Functions[uf]); Assert.Equal("The expression 'sin(x)' was assigned to the function 's(x1)'.", result); }
public void DefineFuncTest() { var uf = new UserFunction("s", new IExpression[0], 0); var func = new Sin(new Number(1)); var exp = new Define(uf, func); var parameters = new ExpressionParameters(); var result = exp.Execute(parameters); Assert.Equal(func, parameters.Functions[uf]); Assert.Equal("The expression 'sin(1)' was assigned to the function 's()'.", result); }
public void ExecuteRecursiveTest() { var expParams = new ExpressionParameters(); var exp = new If(new Equal(new Variable("x"), new Number(0)), new Number(1), new Mul(new Variable("x"), new UserFunction("f", new[] { new Sub(new Variable("x"), new Number(1)) }, 1))); expParams.Functions.Add(new UserFunction("f", new[] { new Variable("x") }, 1), exp); var func = new UserFunction("f", new[] { new Number(4) }, 1); Assert.Equal(24.0, func.Execute(expParams)); }
public void UndefFuncWithParamsTest() { var key1 = new UserFunction("f", new IExpression[0], 0); var key2 = new UserFunction("f", 1); var functions = new FunctionCollection { { key1, new Number(1) }, { key2, new Number(2) } }; var undef = new Undefine(key2); var result = undef.Execute(functions); Assert.True(functions.ContainsKey(key1)); Assert.False(functions.ContainsKey(key2)); Assert.Equal("The 'f(x1)' function is removed.", result); }
public FunctionViewModel(UserFunction function, IExpression value) { this.function = function; this.value = value; }
public void UserFunc() { var exp = new UserFunction("f", new IExpression[] { new Mul(new Number(2), new Number(2)) }, 1); var expected = new UserFunction("f", new IExpression[] { new Number(4) }, 1); SimpleTest(exp, expected); }
public void UserFunctionDerivTest() { var parameters = new FunctionCollection(); var uf = new UserFunction("f", new IExpression[] { new Variable("x") }, 1); parameters.Add(uf, new Sin(new Variable("x"))); var diff = Differentiate(uf, "x", parameters); Assert.Equal("cos(x) * 1", diff.ToString()); }
public void UserFunctionDerivNullTest() { var uf = new UserFunction("f", new IExpression[] { new Variable("x") }, 1); Assert.Throws<ArgumentNullException>(() => Differentiate(uf, "x", null)); }
/// <summary> /// Differentiates the user function. /// </summary> /// <param name="expression">The expression.</param> /// <param name="variable">The variable.</param> /// <param name="parameters">The parameters.</param> /// <returns>Returns the derivative.</returns> protected virtual IExpression UserFunction(UserFunction expression, Variable variable, ExpressionParameters parameters) { var func = parameters.Functions[expression]; return _Differentiate(func, variable, parameters); }