views:

115

answers:

3

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.

+7  A: 

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

dtb
Thank you. Any good resources on this?
rmx
@rmx: I've added two links. The blog post seems very helpful to get started.
dtb
+3  A: 

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
Hi, I am not concerned with finding f, that would be a purely mathematical question and I'm aware of least-squares. I am trying to find how to describe f and pass it around as an object.
rmx
+2  A: 

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).

Amadan