Skip to content

Commit

Permalink
Implemented complex treatment of mult. args #101
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jun 16, 2022
1 parent 367acf1 commit 2caac88
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Mages.Core.Tests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Numerics;

[TestFixture]
public class FunctionTests
Expand Down Expand Up @@ -751,5 +752,47 @@ public void LerpWithInterpolationOfMatrix()
var result = "lerp(-5, 5, [0, 0.5, 0.75; 0.1, 0.2, 0.4])".Eval();
CollectionAssert.AreEquivalent(new[,] { { -5.0, 0.0, 2.5 }, { -4.0, -3.0, -1.0 } }, (Double[,])result);
}

[Test]
public void MinWithComplex_Issue101()
{
var result = "min(5, -3i)".Eval();
Assert.AreEqual(new Complex(0.0, -3.0), (Complex)result);
}

[Test]
public void MinWithComplexOther_Issue101()
{
var result = "min(-5, 3i)".Eval();
Assert.AreEqual(new Complex(0.0, 3.0), (Complex)result);
}

[Test]
public void MinWithComplexReal_Issue101()
{
var result = "min(-1, 3i)".Eval();
Assert.AreEqual(new Complex(-1.0, 0.0), (Complex)result);
}

[Test]
public void MaxWithComplex_Issue101()
{
var result = "max(5, -3i)".Eval();
Assert.AreEqual(new Complex(5.0, 0.0), (Complex)result);
}

[Test]
public void MáxWithComplexOther_Issue101()
{
var result = "max(-5, 3i)".Eval();
Assert.AreEqual(new Complex(-5.0, 0.0), (Complex)result);
}

[Test]
public void MaxWithComplexReal_Issue101()
{
var result = "max(-1, 3i)".Eval();
Assert.AreEqual(new Complex(0.0, 3.0), (Complex)result);
}
}
}
20 changes: 20 additions & 0 deletions src/Mages.Core/Runtime/Functions/If.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,25 @@ public static Object IsNotNull<T>(Object[] args, Func<Object, T> converter, Func

return null;
}

/// <summary>
/// Checks if any argument is of type T.
/// </summary>
/// <typeparam name="T">The type of any value.</typeparam>
/// <param name="args">The arguments to check.</param>
/// <param name="f">The callback to invoke if fulfilled.</param>
/// <returns>The result of the callback or null.</returns>
public static Object HasAny<T>(Object[] args, Func<Object[], Object> f)
{
foreach (var arg in args)
{
if (arg is T)
{
return f(args);
}
}

return null;
}
}
}
3 changes: 3 additions & 0 deletions src/Mages.Core/Runtime/Functions/StandardFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ public static class StandardFunctions
If.Is<Double[,]>(args, x => x.Reduce((a, b) => a + b)) ??
If.Is<Complex[,]>(args, x => x.Reduce((a, b) => a + b)) ??
If.Is<Dictionary<String, Object>>(args, obj => obj.Sum(m => m.Value.ToNumber())) ??
If.HasAny<Complex>(args, x => x.Select(m => m.ToComplex()).Aggregate(Complex.Zero, (sum, value) => sum + value)) ??
args.Select(m => m.ToNumber()).Sum();
});

Expand All @@ -755,6 +756,7 @@ public static class StandardFunctions
If.Is<Double[,]>(args, x => x.Reduce(Math.Min)) ??
If.Is<Complex[,]>(args, x => x.Reduce(Mathx.Min)) ??
If.Is<Dictionary<String, Object>>(args, obj => obj.Min(m => m.Value.ToNumber())) ??
If.HasAny<Complex>(args, x => x.Select(m => m.ToComplex()).OrderBy(m => m.Magnitude).FirstOrDefault()) ??
args.Select(m => m.ToNumber()).Min();
});

Expand All @@ -767,6 +769,7 @@ public static class StandardFunctions
If.Is<Double[,]>(args, x => x.Reduce(Math.Max)) ??
If.Is<Complex[,]>(args, x => x.Reduce(Mathx.Max)) ??
If.Is<Dictionary<String, Object>>(args, obj => obj.Max(m => m.Value.ToNumber())) ??
If.HasAny<Complex>(args, x => x.Select(m => m.ToComplex()).OrderByDescending(m => m.Magnitude).FirstOrDefault()) ??
args.Select(m => m.ToNumber()).Max();
});

Expand Down

0 comments on commit 2caac88

Please sign in to comment.