如何创建算法类型?
假设我有两个数字序列,A 和 B。
如何创建一个对象来描述两个序列之间的关系?
例如:
A:0、1、2、3、4、5、6、7、8、9...
B:0、2、4、6 , 8, 10, 12, 14, 16, 18...
B = 2A
关系,f() 是我们从 A 得到的> 到B。
但是给定两个任意序列,我如何构造f?
另外,如何将 f 返回到调用方法,以便它可以立即将其与任何数字一起使用? -- 可以使用delegate
作为返回类型吗?
我有一个想法,但也许你可以建议我:我可以使用装饰器模式来构建一个包含各种运算符和常量等的对象......然后生成代码。这非常混乱,我不想使用这种方法。
我不是在问如何找到 f,我可以做到。我问的是如何建模f。
抱歉,如果一切都不清楚,我不知道还能如何解释。
Say I have two sequences of numbers, A and B.
How can I create an object to describe the relationship between the two sequences?
For example:
A: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9...
B: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18...
B = 2A
The relationship, f() is how we get from A to B.
But given two arbitrary sequences, how can I construct f?
Also, how can I return f to the calling method so that it can simply use it straight away with any number? -- Can you use delegate
as a return type?
I have one idea but maybe you could advise me on it: I could use a decorator pattern to build an object containing various operators and constants etc... Then just generate the code. This is very messy and I don't want to use this method.
I'm not asking how to find f, I can do that. I'm asking how to model f.
Sorry if all that is not clear, I don't know how else to explain it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 LINQ 表达式树:
或者(如果函数已知)
或(没有表达式树)
用法:
请参阅:表达式类
另请参阅:表达式树基础知识
You could use LINQ expression trees:
or (if the function is known)
or (without expression trees)
Usage:
See: Expression Class
See also: Expression Tree Basics
听起来像是某种合适的程序的工作。您的 A 序列看起来像自变量,而 B 序列是因变量。
最小二乘拟合通常用于解决此类问题。你必须能够对函数的形式做出合理的猜测,计算一些参数,并看看你的猜测/形式/参数有多好。
Sounds like a job for a fitting program of some kind. Your A sequence looks like the independent variable, and B sequence is the dependent variable.
Least squares fitting is usually used to solve these kinds of problems. You have to be able to make a reasonable guess for the form of the function, calculate some parameters, and see how good your guess/form/parameters are.
如果你甚至不能做出 @duffymo 所说的那种假设,那么我知道的唯一方法就是在代数表达式树的空间中进行广度优先搜索。它是蛮力的,而且非常慢,但如果不是太复杂,它会找到公式。
更新:关于表示,这非常简单。如果您决定不使用 LINQ(我对此了解不多,但 @dtb 给出的示例看起来非常好,我不知道为什么您不这样做),您可以非常轻松地推出自己的(当然,这些不会很好地自动编译,你必须解释它们):
只需创建
Expressions
的嵌套对象,它可以是Value
或Function
。Value
可以是变量
(x
) 或常量
(1
),Function
可以是UnaryFunction
(Sin
) 或BinaryFunction
(Plus
)。这些类本质上是空的(构造函数和递归evaluate
函数)。如果您使用任何其他语言(例如 Haskell)(仅使用 ADT 而不是类,但在这种情况下区别相当微不足道),这是您将采用的标准方法。
If you can't even make a hypothesis of the kind @duffymo speaks of, then the only way I know is breadth-first search in the space of algebraic expression trees. It is brute-force, and extremely slow, but it will find the formula if it is not too complex.
UPDATE: Regarding the representation, it is very easy. If you decide not to use LINQ (I don't know much about it, but the example given by @dtb looks very nice and I don't know why you wouldn't), you can roll your own very easily (of course, these won't auto-compile so nicely, you'd have to interpret them):
Just make nested objects of
Expressions
, which can beValue
orFunction
.Value
can be aVariable
(x
) or aConstant
(1
),Function
can beUnaryFunction
(Sin
) orBinaryFunction
(Plus
). The classes are essentially empty (constructor and a recursiveevaluate
function).This is the standard approach you'd take if you took it up with any other language, say Haskell (only with ADTs instead of classes, but the distinction is rather trivial in this case).