Adding functions to Standard Library

This commit is contained in:
Jordan Wages 2025-08-21 03:44:04 -05:00
commit 4a21ff3d46
39 changed files with 1465 additions and 51 deletions

View file

@ -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()
{ {

View file

@ -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");
}
}
}

View file

@ -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());
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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);
});
}
}
}

View file

@ -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;
}
});
}
}
}

View 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;
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View 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;
}
});
}
}
}

View 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;
});
}
}
}

View 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;
});
}
}
}

View 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);
}
}
}

View 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());
});
}
}
}

View 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));
});
}
}
}

View 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());
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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);
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View file

@ -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));
});
}
}
}

View file

@ -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));
});
}
}
}

View file

@ -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));
});
}
}
}

View file

@ -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));
});
}
}
}

View file

@ -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));
});
}
}
}

View file

@ -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));
});
}
}
}

View 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));
});
}
}
}

View 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));
});
}
}
}

View 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");
}
}
}

View file

@ -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
{ {