using System; using System.Linq; using System.Linq.Expressions; using Irony.Interpreter; using Irony.Parsing; namespace HeuristicLab.BenchmarkGenerator { public class CustomLanguageRuntime : LanguageRuntime { public CustomLanguageRuntime(LanguageData data) : base(data) { } protected override void InitOperatorImplementations() { base.InitOperatorImplementations(); AddBinary(ExpressionType.AddChecked, typeof(Object[]), (x, y) => ComponentSum((object[])x, (object[])y)); AddBinary(ExpressionType.SubtractChecked, typeof(Object[]), (x, y) => ComponentDifference((object[])x, (object[])y)); AddBinary(ExpressionType.MultiplyChecked, typeof(Object[]), (x, y) => ComponentProduct((object[])x, (object[])y)); AddBinary(ExpressionType.Divide, typeof(Object[]), (x, y) => ComponentDivision((object[])x, (object[])y)); // AddBinary(ExpressionType.Multiply, typeof(DoubleArray), (x, y) => (DoubleArray)x * (DoubleArray)y); // AddBinary(ExpressionType.Add, typeof(DoubleArray), (x, y) => (DoubleArray)x + (DoubleArray)y); // AddBinary(ExpressionType.Divide, typeof(DoubleArray), (x, y) => (DoubleArray)x / (DoubleArray)y); // AddBinary(ExpressionType.Subtract, typeof(DoubleArray), (x, y) => (DoubleArray)x - (DoubleArray)y); // AddConverter(typeof(double[]), typeof(DoubleArray), x => new DoubleArray((double[])x)); } private object[] ComponentProduct(object[] a, object[] b) { var aa = a.Cast(); var bb = b.Cast(); return aa.Zip(bb, (v1, v2) => v1 * v2).Cast().ToArray(); } private object[] ComponentDivision(object[] a, object[] b) { var aa = a.Cast(); var bb = b.Cast(); return aa.Zip(bb, (v1, v2) => v1 / v2).Cast().ToArray(); } private object[] ComponentSum(object[] a, object[] b) { var aa = a.Cast(); var bb = b.Cast(); return aa.Zip(bb, (v1, v2) => v1 + v2).Cast().ToArray(); } private object[] ComponentDifference(object[] a, object[] b) { var aa = a.Cast(); var bb = b.Cast(); return aa.Zip(bb, (v1, v2) => v1 - v2).Cast().ToArray(); } } }