如何创建算法类型?

发布于 2024-09-26 12:23:34 字数 686 浏览 6 评论 0原文

假设我有两个数字序列,AB

如何创建一个对象来描述两个序列之间的关系?

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-10-03 12:23:34

您可以使用 LINQ 表达式树

var x = Expression.Parameter(typeof(int), "x");
var body = Expression.Multiply(Expression.Constant(2), x);
var lambda = Expression.Lambda<Func<int, int>>(body, x);
var f = lambda.Compile();

或者(如果函数已知)

Expression<Func<int, int>> lambda = x => 2 * x;
var f = lambda.Compile();

或(没有表达式树)

Func<int, int> f = x => 2 * x;

用法:

var a = new int[] { 0, 1, 2, 3, 4, 5 };
var b = a.Select(f).ToArray();
// b == new int[] { 0, 2, 4, 6, 8, 10 };

请参阅:表达式类

另请参阅:表达式树基础知识

You could use LINQ expression trees:

var x = Expression.Parameter(typeof(int), "x");
var body = Expression.Multiply(Expression.Constant(2), x);
var lambda = Expression.Lambda<Func<int, int>>(body, x);
var f = lambda.Compile();

or (if the function is known)

Expression<Func<int, int>> lambda = x => 2 * x;
var f = lambda.Compile();

or (without expression trees)

Func<int, int> f = x => 2 * x;

Usage:

var a = new int[] { 0, 1, 2, 3, 4, 5 };
var b = a.Select(f).ToArray();
// b == new int[] { 0, 2, 4, 6, 8, 10 };

See: Expression Class

See also: Expression Tree Basics

要走干脆点 2024-10-03 12:23:34

听起来像是某种合适的程序的工作。您的 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.

埋葬我深情 2024-10-03 12:23:34

如果你甚至不能做出 @duffymo 所说的那种假设,那么我知道的唯一方法就是在代数表达式树的空间中进行广度优先搜索。它是蛮力的,而且非常慢,但如果不是太复杂,它会找到公式。

更新:关于表示,这非常简单。如果您决定不使用 LINQ(我对此了解不多,但 @dtb 给出的示例看起来非常好,我不知道为什么您不这样做),您可以非常轻松地推出自己的(当然,这些不会很好地自动编译,你必须解释它们):

只需创建 Expressions 的嵌套对象,它可以是 ValueFunctionValue 可以是变量 (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 be Value or Function. Value can be a Variable (x) or a Constant (1), Function can be UnaryFunction (Sin) or BinaryFunction (Plus). The classes are essentially empty (constructor and a recursive evaluate 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文