Adding functions to Standard Library
This commit is contained in:
parent
808d4ca420
commit
4a21ff3d46
39 changed files with 1465 additions and 51 deletions
|
@ -17,6 +17,7 @@ namespace CSMic
|
||||||
public static readonly FunctionValue NONE = new FunctionValue(FunctionValueType.None, null);
|
public static readonly FunctionValue NONE = new FunctionValue(FunctionValueType.None, null);
|
||||||
public static readonly FunctionValue NUMBER = new FunctionValue(FunctionValueType.Numeric, 0m);
|
public static readonly FunctionValue NUMBER = new FunctionValue(FunctionValueType.Numeric, 0m);
|
||||||
public static readonly FunctionValue STRING = new FunctionValue(FunctionValueType.String, string.Empty);
|
public static readonly FunctionValue STRING = new FunctionValue(FunctionValueType.String, string.Empty);
|
||||||
|
public static readonly FunctionValue ZERO = new FunctionValue(FunctionValueType.Numeric, 0m);
|
||||||
|
|
||||||
public FunctionValue()
|
public FunctionValue()
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
using CSMic;
|
|
||||||
|
|
||||||
namespace CSMic.StandardLibrary
|
|
||||||
{
|
|
||||||
public static class Constants
|
|
||||||
{
|
|
||||||
public static void Initialize(InputInterpreter inputInterpreter)
|
|
||||||
{
|
|
||||||
if(inputInterpreter == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
|
||||||
}
|
|
||||||
|
|
||||||
inputInterpreter.Interpret("pi :: 3.1415926535897931");
|
|
||||||
inputInterpreter.Interpret("e :: 2.7182818284590451");
|
|
||||||
inputInterpreter.Interpret("tau :: 6.2831853071795862");
|
|
||||||
inputInterpreter.Interpret("phi :: 1.6180339887498948");
|
|
||||||
inputInterpreter.Interpret("goldenratio :: 1.6180339887498948");
|
|
||||||
inputInterpreter.Interpret("eurler :: 0.5772156649015329");
|
|
||||||
inputInterpreter.Interpret("omega :: 0.5671432904097839");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
using CSMic;
|
|
||||||
using CSMic.StandardLibrary.Functions;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace CSMic.StandardLibrary
|
|
||||||
{
|
|
||||||
public static class Functions
|
|
||||||
{
|
|
||||||
public static void Initialize(InputInterpreter inputInterpreter)
|
|
||||||
{
|
|
||||||
if (inputInterpreter == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
|
||||||
}
|
|
||||||
|
|
||||||
inputInterpreter.RegisterFunction(new AbsoluteValue());
|
|
||||||
inputInterpreter.RegisterFunction(new Sign());
|
|
||||||
inputInterpreter.RegisterFunction(new Min());
|
|
||||||
inputInterpreter.RegisterFunction(new Max());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
33
src/StandardLibrary/Functions/Angle/Degrees.cs
Normal file
33
src/StandardLibrary/Functions/Angle/Degrees.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Angle
|
||||||
|
{
|
||||||
|
public class Degrees : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "degrees";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, double.DegreesToRadians((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Angle/Radians.cs
Normal file
33
src/StandardLibrary/Functions/Angle/Radians.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Angle
|
||||||
|
{
|
||||||
|
public class Radians : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "radians";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, double.RadiansToDegrees((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
src/StandardLibrary/Functions/Angle/WrapAngle.cs
Normal file
43
src/StandardLibrary/Functions/Angle/WrapAngle.cs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Angle
|
||||||
|
{
|
||||||
|
public class WrapAngle : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "wrapangle";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("periodStart", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("periodEnd", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputValue = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(inputValue.Value);
|
||||||
|
var inputPeriodStart = _args[1].Value;
|
||||||
|
decimal periodStart = Convert.ToDecimal(inputPeriodStart.Value);
|
||||||
|
var inputPeriodEnd = _args[2].Value;
|
||||||
|
decimal periodEnd = Convert.ToDecimal(inputPeriodEnd.Value);
|
||||||
|
|
||||||
|
// Perform modulo.
|
||||||
|
decimal width = periodEnd - periodStart;
|
||||||
|
decimal modulus = value % width;
|
||||||
|
modulus = modulus < 0 ? modulus + width : modulus;
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, modulus);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
using CSMic;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.NumberTheory
|
||||||
|
{
|
||||||
|
public class BinomialCoefficient : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "ncr";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("first", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("second", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputFirst = _args[0].Value;
|
||||||
|
var inputSecond = _args[1].Value;
|
||||||
|
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||||
|
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||||
|
|
||||||
|
if (first < 0 || first > 20 || second < 0 || second > 20 || first - second < 0)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Math.Floor(first) != first || Math.Floor(second) != second)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = Convert.ToInt32(first);
|
||||||
|
int r = Convert.ToInt32(second);
|
||||||
|
decimal nFac = Factorial.INTEGER_FACTORIAL_LOOKUP[n];
|
||||||
|
decimal rFac = Factorial.INTEGER_FACTORIAL_LOOKUP[r];
|
||||||
|
decimal nmrFac = Factorial.INTEGER_FACTORIAL_LOOKUP[n - r];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
decimal combinations = (nFac) / (rFac * nmrFac);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, combinations);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Last chance emergency fail if the decimal value is exceeded.
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
110
src/StandardLibrary/Functions/NumberTheory/Factorial.cs
Normal file
110
src/StandardLibrary/Functions/NumberTheory/Factorial.cs
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
using CSMic;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.NumberTheory
|
||||||
|
{
|
||||||
|
public class Factorial : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
private static readonly double[] LANCZOS_APPROXIMATION =
|
||||||
|
{
|
||||||
|
0.99999999999980993,
|
||||||
|
676.5203681218851,
|
||||||
|
-1259.1392167224028,
|
||||||
|
771.32342877765313,
|
||||||
|
-176.61502916214059,
|
||||||
|
12.507343278686905,
|
||||||
|
-0.13857109526572012,
|
||||||
|
9.9843695780195716e-6,
|
||||||
|
1.5056327351493116e-7
|
||||||
|
};
|
||||||
|
|
||||||
|
public static readonly decimal[] INTEGER_FACTORIAL_LOOKUP =
|
||||||
|
{
|
||||||
|
/* 0 */ 1,
|
||||||
|
/* 1 */ 1,
|
||||||
|
/* 2 */ 2,
|
||||||
|
/* 3 */ 6,
|
||||||
|
/* 4 */ 24,
|
||||||
|
/* 5 */ 120,
|
||||||
|
/* 6 */ 720,
|
||||||
|
/* 7 */ 5040,
|
||||||
|
/* 8 */ 40320,
|
||||||
|
/* 9 */ 362880,
|
||||||
|
/* 10 */ 3628800,
|
||||||
|
/* 11 */ 39916800,
|
||||||
|
/* 12 */ 479001600,
|
||||||
|
/* 13 */ 62207020800,
|
||||||
|
/* 14 */ 87178291200,
|
||||||
|
/* 15 */ 1307674368000,
|
||||||
|
/* 16 */ 20922789888000,
|
||||||
|
/* 17 */ 355687428096000,
|
||||||
|
/* 18 */ 6402373705728000,
|
||||||
|
/* 19 */ 121645100408832000,
|
||||||
|
/* 20 */ 2432902008176640000
|
||||||
|
};
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "fac";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputValue = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(inputValue.Value);
|
||||||
|
|
||||||
|
if (value < 0)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
if (value > 20)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.Floor(value) == value)
|
||||||
|
{
|
||||||
|
// Input is an integer. We'll just use the lookup table.
|
||||||
|
var index = Convert.ToInt32(value);
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, INTEGER_FACTORIAL_LOOKUP[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
double gammaFactorial = Gamma(Convert.ToDouble(value) + 1);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Convert.ToDecimal(gammaFactorial));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double Gamma(double shiftedGama)
|
||||||
|
{
|
||||||
|
if (shiftedGama < 0.5)
|
||||||
|
return Math.PI / (Math.Sin(Math.PI * shiftedGama) * Gamma(1 - shiftedGama));
|
||||||
|
|
||||||
|
shiftedGama -= 1;
|
||||||
|
double accumulator = LANCZOS_APPROXIMATION[0];
|
||||||
|
for (int i = 1; i < LANCZOS_APPROXIMATION.Length; i++)
|
||||||
|
accumulator += LANCZOS_APPROXIMATION[i] / (shiftedGama + i);
|
||||||
|
|
||||||
|
double shiftedVariable = shiftedGama + 7.5;
|
||||||
|
return Math.Sqrt(2 * Math.PI) * Math.Pow(shiftedVariable, shiftedGama + 0.5) * Math.Exp(-shiftedVariable) * accumulator;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
using CSMic;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.NumberTheory
|
||||||
|
{
|
||||||
|
public class GreatestCommonDevisor : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "gcd";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("first", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("second", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputFirst = _args[0].Value;
|
||||||
|
var inputSecond = _args[1].Value;
|
||||||
|
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||||
|
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||||
|
|
||||||
|
if (first <= 0 || second <= 0)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.Floor(first) != first || Math.Floor(second) != second)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
decimal gcd = EuclideanAlgorithm(first, second);
|
||||||
|
decimal lcd = (first * second) / gcd;
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, lcd);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public decimal EuclideanAlgorithm(decimal first, decimal second)
|
||||||
|
{
|
||||||
|
if(second == 0)
|
||||||
|
{
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EuclideanAlgorithm(second, first % second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
using CSMic;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.NumberTheory
|
||||||
|
{
|
||||||
|
public class LeastCommonMultiple : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "gcd";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("first", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("second", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputFirst = _args[0].Value;
|
||||||
|
var inputSecond = _args[1].Value;
|
||||||
|
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||||
|
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||||
|
|
||||||
|
if (first <= 0 || second <= 0)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.Floor(first) != first || Math.Floor(second) != second)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, EuclideanAlgorithm(first, second));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public decimal EuclideanAlgorithm(decimal first, decimal second)
|
||||||
|
{
|
||||||
|
if(second == 0)
|
||||||
|
{
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EuclideanAlgorithm(second, first % second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
67
src/StandardLibrary/Functions/NumberTheory/Permutations.cs
Normal file
67
src/StandardLibrary/Functions/NumberTheory/Permutations.cs
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
using CSMic;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.NumberTheory
|
||||||
|
{
|
||||||
|
public class Permutations : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "npr";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("first", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("second", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputFirst = _args[0].Value;
|
||||||
|
var inputSecond = _args[1].Value;
|
||||||
|
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||||
|
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||||
|
|
||||||
|
if (first < 0 || first > 20 || second < 0 || second > 20 || first - second < 0)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Math.Floor(first) != first || Math.Floor(second) != second)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = Convert.ToInt32(first);
|
||||||
|
int r = Convert.ToInt32(second);
|
||||||
|
decimal nFac = Factorial.INTEGER_FACTORIAL_LOOKUP[n];
|
||||||
|
decimal nmrFac = Factorial.INTEGER_FACTORIAL_LOOKUP[n - r];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
decimal combinations = (nFac) / (nmrFac);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, combinations);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Last chance emergency fail if the decimal value is exceeded.
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
45
src/StandardLibrary/Functions/Random/Bernoulli.cs
Normal file
45
src/StandardLibrary/Functions/Random/Bernoulli.cs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
using CSMic;
|
||||||
|
using CSMic.StandardLibrary.Functions.Random;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public class Bernoulli : RandomBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "bern";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("p", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal pValue = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
if(pValue < 0m || pValue > 1m)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(pValue), "The p value must be between 0 and 1.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(NextDecimal() < pValue)
|
||||||
|
{
|
||||||
|
return FunctionValue.TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FunctionValue.FALSE;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
src/StandardLibrary/Functions/Random/FairFlip.cs
Normal file
37
src/StandardLibrary/Functions/Random/FairFlip.cs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
using CSMic;
|
||||||
|
using CSMic.StandardLibrary.Functions.Random;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public class FairFlip : RandomBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "flip";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
if(NextDecimal() < 0.5m)
|
||||||
|
{
|
||||||
|
return FunctionValue.TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FunctionValue.FALSE;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Random/RandomBase.cs
Normal file
33
src/StandardLibrary/Functions/Random/RandomBase.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public abstract class RandomBase : FunctionBase
|
||||||
|
{
|
||||||
|
protected static System.Random RandomNumberGenerator
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return System.Random.Shared;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static decimal NextDecimal()
|
||||||
|
{
|
||||||
|
return Convert.ToDecimal(RandomNumberGenerator.NextDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static decimal NextDecimalNormal()
|
||||||
|
{
|
||||||
|
double u1 = 1.0 - RandomNumberGenerator.NextDouble();
|
||||||
|
double u2 = 1.0 - RandomNumberGenerator.NextDouble();
|
||||||
|
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
|
||||||
|
Math.Sin(2.0 * Math.PI * u2);
|
||||||
|
return Convert.ToDecimal(randStdNormal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
src/StandardLibrary/Functions/Random/RandomNormal.cs
Normal file
32
src/StandardLibrary/Functions/Random/RandomNormal.cs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
using CSMic;
|
||||||
|
using CSMic.StandardLibrary.Functions.Random;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public class RandomNormal : RandomBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "randn";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, NextDecimalNormal());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
src/StandardLibrary/Functions/Random/RandomNormalSpread.cs
Normal file
43
src/StandardLibrary/Functions/Random/RandomNormalSpread.cs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
using CSMic;
|
||||||
|
using CSMic.StandardLibrary.Functions.Random;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public class RandomNormalSpread : RandomBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "randns";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("lower", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("upper", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputLower = _args[0].Value;
|
||||||
|
var inputUpper = _args[1].Value;
|
||||||
|
decimal lower = Convert.ToDecimal(inputLower.Value);
|
||||||
|
decimal upper = Convert.ToDecimal(inputUpper.Value);
|
||||||
|
|
||||||
|
if(upper <= lower)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, NextDecimalNormal() * (upper - lower));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
src/StandardLibrary/Functions/Random/RandomUniform.cs
Normal file
32
src/StandardLibrary/Functions/Random/RandomUniform.cs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
using CSMic;
|
||||||
|
using CSMic.StandardLibrary.Functions.Random;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public class RandomUniform : RandomBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "rand";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, NextDecimal());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
src/StandardLibrary/Functions/Random/RandomUniformSpread.cs
Normal file
43
src/StandardLibrary/Functions/Random/RandomUniformSpread.cs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
using CSMic;
|
||||||
|
using CSMic.StandardLibrary.Functions.Random;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Random
|
||||||
|
{
|
||||||
|
public class RandomUniformSpread : RandomBase, ICodedFunction
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "rands";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("lower", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("upper", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return base.Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputLower = _args[0].Value;
|
||||||
|
var inputUpper = _args[1].Value;
|
||||||
|
decimal lower = Convert.ToDecimal(inputLower.Value);
|
||||||
|
decimal upper = Convert.ToDecimal(inputUpper.Value);
|
||||||
|
|
||||||
|
if(upper <= lower)
|
||||||
|
{
|
||||||
|
return FunctionValue.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, NextDecimal() * (upper - lower));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Rounding/Ceiling.cs
Normal file
33
src/StandardLibrary/Functions/Rounding/Ceiling.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Rounding
|
||||||
|
{
|
||||||
|
public class Ceiling : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "ceiling";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Ceiling(value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
src/StandardLibrary/Functions/Rounding/Clamp.cs
Normal file
39
src/StandardLibrary/Functions/Rounding/Clamp.cs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Rounding
|
||||||
|
{
|
||||||
|
public class Clamp : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "clamp";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("low", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("high", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputValue = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(inputValue.Value);
|
||||||
|
var inputLow = _args[1].Value;
|
||||||
|
decimal low = Convert.ToDecimal(inputLow.Value);
|
||||||
|
var inputHigh = _args[2].Value;
|
||||||
|
decimal high = Convert.ToDecimal(inputHigh.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, value < low ? low : value > high ? high : value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Rounding/Floor.cs
Normal file
33
src/StandardLibrary/Functions/Rounding/Floor.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Rounding
|
||||||
|
{
|
||||||
|
public class Floor : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "floor";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Floor(value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Rounding/Fractional.cs
Normal file
33
src/StandardLibrary/Functions/Rounding/Fractional.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Rounding
|
||||||
|
{
|
||||||
|
public class Fractional : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "frac";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, value - Math.Truncate(value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
src/StandardLibrary/Functions/Rounding/Round.cs
Normal file
37
src/StandardLibrary/Functions/Rounding/Round.cs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Rounding
|
||||||
|
{
|
||||||
|
public class Round : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "round";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputValue = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(inputValue.Value);
|
||||||
|
var inputPrecision = _args[1].Value;
|
||||||
|
decimal precision = Convert.ToDecimal(inputValue.Value);
|
||||||
|
precision = Math.Round(precision);
|
||||||
|
int precisionInt = Convert.ToInt32(precision);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Round(value, precisionInt));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Rounding/Truncate.cs
Normal file
33
src/StandardLibrary/Functions/Rounding/Truncate.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Rounding
|
||||||
|
{
|
||||||
|
public class Trancate : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "truncate";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Truncate(value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Trigonometry/Acos.cs
Normal file
33
src/StandardLibrary/Functions/Trigonometry/Acos.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Acos : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "acos";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Acos((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Trigonometry/Asin.cs
Normal file
33
src/StandardLibrary/Functions/Trigonometry/Asin.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Asin : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "asin";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Asin((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Trigonometry/Atan.cs
Normal file
33
src/StandardLibrary/Functions/Trigonometry/Atan.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Atan : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "atan";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Atan((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
src/StandardLibrary/Functions/Trigonometry/Atan2.cs
Normal file
36
src/StandardLibrary/Functions/Trigonometry/Atan2.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Atan2 : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "atan2";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var inputFirst = _args[0].Value;
|
||||||
|
decimal valueFirst = Convert.ToDecimal(inputFirst.Value);
|
||||||
|
var inputSecond = _args[1].Value;
|
||||||
|
decimal valueSecond = Convert.ToDecimal(inputSecond.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Atan2((double)valueFirst, (double)valueSecond));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Trigonometry/Cos.cs
Normal file
33
src/StandardLibrary/Functions/Trigonometry/Cos.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Cos : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "cos";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Cos((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry.Hyperbolic
|
||||||
|
{
|
||||||
|
public class Acosh : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "acosh";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Acosh((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry.Hyperbolic
|
||||||
|
{
|
||||||
|
public class Asinh : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "asinh";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Asinh((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry.Hyperbolic
|
||||||
|
{
|
||||||
|
public class Atanh : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "atanh";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Atanh((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry.Hyperbolic
|
||||||
|
{
|
||||||
|
public class Cosh : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "cosh";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Cosh((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry.Hyperbolic
|
||||||
|
{
|
||||||
|
public class Sinh : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "sinh";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Sinh((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry.Hyperbolic
|
||||||
|
{
|
||||||
|
public class Tanh : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "tanh";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Tanh((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Trigonometry/Sin.cs
Normal file
33
src/StandardLibrary/Functions/Trigonometry/Sin.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Sin : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "sin";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Sin((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
src/StandardLibrary/Functions/Trigonometry/Tan.cs
Normal file
33
src/StandardLibrary/Functions/Trigonometry/Tan.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
namespace CSMic.StandardLibrary.Functions.Trigonometry
|
||||||
|
{
|
||||||
|
public class Tan : FunctionBase, ICodedFunction
|
||||||
|
{
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "tan";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
|
{
|
||||||
|
return Execute(args, (_args) =>
|
||||||
|
{
|
||||||
|
var input = _args[0].Value;
|
||||||
|
decimal value = Convert.ToDecimal(input.Value);
|
||||||
|
|
||||||
|
return new FunctionValue(FunctionValueType.Numeric, Math.Tan((double)value));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
76
src/StandardLibrary/Initializer.cs
Normal file
76
src/StandardLibrary/Initializer.cs
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
using CSMic.StandardLibrary.Functions;
|
||||||
|
using CSMic.StandardLibrary.Functions.Angle;
|
||||||
|
|
||||||
|
namespace CSMic.StandardLibrary
|
||||||
|
{
|
||||||
|
public static class Initializer
|
||||||
|
{
|
||||||
|
public static void InitializeAll(InputInterpreter interpreter)
|
||||||
|
{
|
||||||
|
InitializeAllFunctions(interpreter);
|
||||||
|
InitializeConstants(interpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void InitializeAllFunctions(InputInterpreter inputInterpreter)
|
||||||
|
{
|
||||||
|
if (inputInterpreter == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
InitializeBaseFunctions(inputInterpreter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void InitializeBaseFunctions(InputInterpreter inputInterpreter)
|
||||||
|
{
|
||||||
|
if (inputInterpreter == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
inputInterpreter.RegisterFunction(new AbsoluteValue());
|
||||||
|
inputInterpreter.RegisterFunction(new Sign());
|
||||||
|
inputInterpreter.RegisterFunction(new Min());
|
||||||
|
inputInterpreter.RegisterFunction(new Max());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void InitializeAngleFunctions(InputInterpreter inputInterpreter)
|
||||||
|
{
|
||||||
|
if (inputInterpreter == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
inputInterpreter.RegisterFunction(new Degrees());
|
||||||
|
inputInterpreter.RegisterFunction(new Radians());
|
||||||
|
inputInterpreter.RegisterFunction(new WrapAngle());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void InitializeNumberTheoryFunctions(InputInterpreter inputInterpreter)
|
||||||
|
{
|
||||||
|
if (inputInterpreter == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register functions...
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void InitializeConstants(InputInterpreter inputInterpreter)
|
||||||
|
{
|
||||||
|
if (inputInterpreter == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
inputInterpreter.Interpret("pi :: 3.1415926535897931");
|
||||||
|
inputInterpreter.Interpret("e :: 2.7182818284590451");
|
||||||
|
inputInterpreter.Interpret("tau :: 6.2831853071795862");
|
||||||
|
inputInterpreter.Interpret("phi :: 1.6180339887498948");
|
||||||
|
inputInterpreter.Interpret("goldenratio :: 1.6180339887498948");
|
||||||
|
inputInterpreter.Interpret("eurler :: 0.5772156649015329");
|
||||||
|
inputInterpreter.Interpret("omega :: 0.5671432904097839");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@ namespace CSMic.StandardLibrary.Functions
|
||||||
{
|
{
|
||||||
public class Sign : FunctionBase, ICodedFunction
|
public class Sign : FunctionBase, ICodedFunction
|
||||||
{
|
{
|
||||||
public const decimal POSITIVE = 1;
|
private const decimal POSITIVE = 1;
|
||||||
public const decimal NEGATIVE = -1;
|
private const decimal NEGATIVE = -1;
|
||||||
|
|
||||||
public string Name
|
public string Name
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue