Expanding Standard Library
Expanding functions in the standard library.
This commit is contained in:
parent
9dba1a2dfd
commit
5556010c91
12 changed files with 366 additions and 3 deletions
50
src/StandardLibrary/Functions/Lerp.cs
Normal file
50
src/StandardLibrary/Functions/Lerp.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Lerp: FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "lerp";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("start", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("end", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("ammount", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
var input2 = _args[1].Value;
|
||||
var input3 = _args[2].Value;
|
||||
|
||||
decimal start = Convert.ToDecimal(input);
|
||||
decimal end = Convert.ToDecimal(input2);
|
||||
decimal ammount = Convert.ToDecimal(input3);
|
||||
|
||||
if (start == end)
|
||||
{
|
||||
return new FunctionValue(FunctionValueType.Numeric, start);
|
||||
}
|
||||
|
||||
var lerp = start + (ammount * (end - start));
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, lerp);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
39
src/StandardLibrary/Functions/Log.cs
Normal file
39
src/StandardLibrary/Functions/Log.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Log: FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "log";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("base", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
var input2 = _args[1].Value;
|
||||
double number = Convert.ToDouble(input.Value);
|
||||
double _baseNumber = Convert.ToDouble(input2.Value);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Log(number, _baseNumber));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
58
src/StandardLibrary/Functions/Map.cs
Normal file
58
src/StandardLibrary/Functions/Map.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Map: FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "map";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("oldMinimum", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("oldMaximum", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("newMinimum", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("newMaximum", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
var input2 = _args[1].Value;
|
||||
var input3 = _args[2].Value;
|
||||
var input4 = _args[3].Value;
|
||||
var input5 = _args[4].Value;
|
||||
|
||||
decimal number = Convert.ToDecimal(input);
|
||||
decimal oldMinimum = Convert.ToDecimal(input2);
|
||||
decimal oldMaximum = Convert.ToDecimal(input3);
|
||||
decimal newMinimum = Convert.ToDecimal(input4);
|
||||
decimal newMaximum = Convert.ToDecimal(input5);
|
||||
|
||||
if (oldMinimum == oldMaximum)
|
||||
{
|
||||
return FunctionValue.ZERO;
|
||||
}
|
||||
|
||||
var oldRange = oldMaximum - oldMinimum;
|
||||
var newRange = newMaximum - newMinimum;
|
||||
var newNumber = newMinimum + ((number - oldMinimum) * (newRange / oldRange));
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, newNumber);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/StandardLibrary/Functions/Natural Log.cs
Normal file
36
src/StandardLibrary/Functions/Natural Log.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Natural_Log: FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "ln";
|
||||
}
|
||||
}
|
||||
|
||||
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 input = _args[0].Value;
|
||||
double number = Convert.ToDouble(input.Value);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Log(number));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
50
src/StandardLibrary/Functions/Normalize.cs
Normal file
50
src/StandardLibrary/Functions/Normalize.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Normalize : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "normalize";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("minimum", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("maximum", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
var input2 = _args[1].Value;
|
||||
var input3 = _args[2].Value;
|
||||
|
||||
decimal number = Convert.ToDecimal(input);
|
||||
decimal minimum = Convert.ToDecimal(input2);
|
||||
decimal maximum = Convert.ToDecimal(input3);
|
||||
|
||||
if (minimum == maximum)
|
||||
{
|
||||
return FunctionValue.ZERO;
|
||||
}
|
||||
|
||||
var normalization = (number - minimum) / (maximum - minimum);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, normalization);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ namespace CSMic.StandardLibrary.Functions.NumberTheory
|
|||
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||
|
||||
if (first <= 0 || second <= 0)
|
||||
if (first <= 0 && second <= 0)
|
||||
{
|
||||
return FunctionValue.ZERO;
|
||||
}
|
||||
|
|
|
|||
39
src/StandardLibrary/Functions/Power.cs
Normal file
39
src/StandardLibrary/Functions/Power.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Power : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "pow";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("base", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("exponent", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
var input2 = _args[1].Value;
|
||||
double _base = Convert.ToDouble(input.Value);
|
||||
double exponent = Convert.ToDouble(input2.Value);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Pow(_base, exponent));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/StandardLibrary/Functions/SmoothStep.cs
Normal file
46
src/StandardLibrary/Functions/SmoothStep.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class SmoothStep : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "lerp";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("startEdge", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("endEdge", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
var input2 = _args[1].Value;
|
||||
var input3 = _args[2].Value;
|
||||
|
||||
decimal startEdge = Convert.ToDecimal(input);
|
||||
decimal endEdge = Convert.ToDecimal(input2);
|
||||
decimal value = Convert.ToDecimal(input3);
|
||||
|
||||
var normalization = Math.Clamp((value - startEdge) / (endEdge - startEdge), 0, 1);
|
||||
var polynomialization = normalization * normalization * (3 - (2 * normalization));
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, polynomialization);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/StandardLibrary/Functions/SquareRoot.cs
Normal file
36
src/StandardLibrary/Functions/SquareRoot.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CSMic.StandardLibrary.Functions
|
||||
{
|
||||
public class SquareRoot: FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "sqrt";
|
||||
}
|
||||
}
|
||||
|
||||
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 input = _args[0].Value;
|
||||
double number = Convert.ToDouble(input.Value);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Sqrt(number));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -64,6 +64,7 @@ namespace CSMic.StandardLibrary
|
|||
inputInterpreter.RegisterFunction(new Sign());
|
||||
inputInterpreter.RegisterFunction(new Min());
|
||||
inputInterpreter.RegisterFunction(new Max());
|
||||
inputInterpreter.RegisterFunction(new SquareRoot());
|
||||
}
|
||||
|
||||
/// <summary> Initializes the angle-related functions. </summary>
|
||||
|
|
@ -177,7 +178,7 @@ namespace CSMic.StandardLibrary
|
|||
inputInterpreter.Interpret("tau :: 6.2831853071795862");
|
||||
inputInterpreter.Interpret("phi :: 1.6180339887498948");
|
||||
inputInterpreter.Interpret("goldenratio :: 1.6180339887498948");
|
||||
inputInterpreter.Interpret("eurler :: 0.5772156649015329");
|
||||
inputInterpreter.Interpret("euler :: 0.5772156649015329");
|
||||
inputInterpreter.Interpret("omega :: 0.5671432904097839");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class NumberTheoryFunctionsTests
|
|||
{
|
||||
AssertSuccess(_interp.Interpret("gcd(54, 24)"), 6m, _interp);
|
||||
AssertSuccess(_interp.Interpret("gcd(7, 3)"), 1m, _interp);
|
||||
AssertSuccess(_interp.Interpret("gcd(0, 5)"), 0m, _interp);
|
||||
AssertSuccess(_interp.Interpret("gcd(0, 5)"), 5m, _interp);
|
||||
AssertSuccess(_interp.Interpret("gcd(5.5, 2)"), 0m, _interp);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,6 +90,14 @@ public class StdlibFunctionsTests
|
|||
AssertSuccess(result, expected, _interp);
|
||||
}
|
||||
|
||||
[TestCase("sqrt(25)", 5)]
|
||||
[TestCase("sqrt(1)", 1)]
|
||||
public void Sqrt_Works(string expr, decimal expected)
|
||||
{
|
||||
var result = _interp.Interpret(expr);
|
||||
AssertSuccess(result, expected, _interp);
|
||||
}
|
||||
|
||||
[TestCase("pi", "3.1415926535897931")]
|
||||
[TestCase("e", "2.7182818284590451")]
|
||||
[TestCase("tau", "6.2831853071795862")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue