Nick S
1/27/2006 10:43:00 AM
Sorry, I'm clearly not making myself clear.
I'll try and explain myself.
I'm writing an iteration based mathematical/physical modeller. In this
model we have different variables as declared in the @m.declare_vars
line.
When the model is "run" (i.e. iterated) the model increments the
independent variable by the timestep (step_size), then looks for all
variables that have some sort of a relationship to the independent
variable, making appropriate adjustments to them.
The code above is all real, working code, except for the
@m.relationship calls. These are hypothetical and represent how I would
like to be able to define relationships in the model between different
variables.
The problem is that I need to be able to make some sense out of what
gets passed into the block.
To give you an example, using the code above, what *should* happen is
this (for example):
- the independent var, :t gets incremented.
- as a result of the @m.relationship call above, the variable t
(Variable is a class in this modeller) knows about its reverse
dependencies (the other variables in the model, in this case x, that
depend on t).
- it calls an update method on x, passing itself as a parameter
- in x, the update method in turn passes the call on to a
Relationship::Linear class's #update_with_value call passing itself and
t (which was passed to it by t) as parameters.
- because we keep a history of previous values within the Variable
class, we can implement a linear relationship like so:
------------------- ** -------------------
class Linear
def initialize
@factor = factor
@const = const
end
def update_value_with(var, upstream)
val = value_from_raw(
upstream.value/upstream.value(-1) *
raw_from_value(var.value)
)
var.set_value(val)
end
def raw_from_value(value)
(value - @const) / @factor
end
def value_from_raw(raw)
(raw * @factor) + @const
end
end
------------------- ** -------------------
What I hope you can see that we're doing here (in this earlier example)
is getting, as it were, the 'raw' value of our variable (i.e. the bit
that is *actually* proportional to t, rather than the whole value),
then multiplying that by the factor by which the upstream variable has
been multiplied, then putting on the @factor and @const again.
This works, except it requires you to declare the relationship in a
cumbersome and restrictive manner. What I'd like to do is to declare
the relationships in an intuitive manner as in the first code sample,
and then somehow be able to parse and rearrange the algebra that I'm
parsed, so that, for example, I could get t in terms of x, rather than
the other way round.
I currently can't see how to do this, and was hoping you could help.
Here's hoping I've now made myself sufficiently clear =)
Many thanks in advance,
Nick