INT,INT>作为Func键<数学平等; [英] Mathematical Equality for Func<int, int>
问题描述
我有一个序
键入一个实现 ISequence
。 ISequence
工具的IEnumerable<组件>
,其中元素
是另一个自定义类型。目前,我的序
类型存储生成一个序列作为 Func键1所述的N个项的说明; INT,INT>
。这是方便,因为它可以让我打电话给 NTERM(INT N)
用一个简单的调用函数功能
并创建序
s的一个lambda。我想避免改变这种做法。
I have a Sequence
type that implements ISequence
. ISequence
implements IEnumerable<Element>
where Element
is another custom type. Currently, my Sequence
type stores the instructions for generating the N-th term of a sequence as a Func<int, int>
. This is convenient because it allows me to call NTerm(int n)
with a simple call to the Func
and to create Sequence
s with a lambda. I would like to avoid changing that approach.
我要检查两个序列的平等对象基于两个 Func键平等
秒。我开始浏览各地,并有使用表达式
s到打破lambda表达式几个帖子和函数功能
S代表平等,但我说的是数学平等
I would like to check equality of two Sequence objects based on equality of the two Func
s. I began browsing around, and there are a few posts that use Expression
s to break down lambdas and Func
s for equality, but I'm talking about mathematical equality.
在换句话说, X => 2 * X
应该等于 C => C * 2
,随着数学表达式,如Math.Pow或更多地参与运营商的任何变化。如果我能得到那个工作,我可以比较序
S代表数学平等。
In other words,
x => 2 * x
should equalc => c * 2
, along with any variations of mathematical expressions, such as Math.Pow or more involved operators. If I can get that to work, I can compareSequence
s for mathematical equality.
我试着写我自己的扩展方法:
I tried to write my own extension method:
public static bool MathEquals(this Expression<Func<int, int>> f,
Expression<Func<int, int>> g)
我M不知道如何从那里。我有一些基本的默认写:
I'm not sure how to go from there. I have some basic defaults written:
if (ReferenceEquals (f, g)) return true;
if (f == null || g == null) return false;
if (f.NodeType != g.NodeType || f.Type != g.Type) return false;
但我需要检查两个lambda表达式的数学平等,即两个 Func键< INT,INT>
秒。可以这样做?有没有人有办法解决吗?我需要改变我的方法用于存储N个项公式?我反对检查输出,因为序列可以等于对一些输出,而不是全部。
but I need to check the mathematical equality of the two lambda expressions, i.e. the two Func<int, int>
s. Can this be done? Does anyone have a solution? Do I need to change my approach for storing the N-th term formula? I'm against checking the output because sequences can be equal for some output and not all.
如果我需要张贴任何序列码的,我会的。
If I need to post any of the Sequence code, I will.
更新:我已经决定,以纪念斯科特的答案接受。然而,工作是不完整的。这里看看第二部分。
UPDATE: I've decided to mark Scott's answer as accepted. However, the work is not complete. Check out part two here.
推荐答案
有两个部分你的问题,我怎么掰开一个表达和evaulate它和怎么办我检查两个表达式意思是相同的逻辑操作。
There are two parts to your question, "How do I break apart a expression and evaulate it", and "How do I check that two expressions mean the same logical operation".
我不知道该怎么办了第二部分,但我知道的第一个。
I don't know how to do the 2nd part, but I do know the first.
你将需要做的是建立一个的 ExpressionVisitor
并把它走过每一个的 BinaryExpression
通过覆盖 VisitBinary $ C $ 。C>和建设的所有操作的列表
What you will need to do is create a ExpressionVisitor
and have it walk through each BinaryExpression
by overriding VisitBinary
and building up a list of all operations.
public class OperationParser : ExpressionVisitor
{
public OperationParser()
{
Expressions = new List<BinaryExpression>();
}
public List<BinaryExpression> Expressions { get; private set; }
protected override Expression VisitBinary(BinaryExpression b)
{
Expressions.Add(b);
return base.VisitBinary(b);
}
}
然后,你会怎么做。
Then you would do
Expression<Func<int, int>> expression1 = (x) => x + 2;
Expression<Func<int, int>> expression2 = (y) => 2 + y;
var parser1 = new OperationParser();
parser1.Visit(expression1);
var parser2 = new OperationParser();
parser2.Visit(expression2);
//TODO: write a way to compare parser1.Expressions to parser2.Expressions to see if they "mean the same thig"
您只需要填写与你的第二个问题
You just need to fill in the TODO with "your second question"
这篇关于INT,INT>作为Func键<数学平等;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!