Difference between revisions of "ComputedBy"
Line 3: | Line 3: | ||
= ComputedBy(X) = | = ComputedBy(X) = | ||
− | + | ''(Analytica 4.1 or later)'' | |
− | When a variable, A, has the definition | + | Used to indicate that the value of a variable is computed as a side-effect of a different variable ''X''. |
+ | |||
+ | When a variable, ''A'', has the definition | ||
ComputedBy(X) | ComputedBy(X) | ||
− | Then the definition of X must contain an assignment statement, ''A := value'', that sets the value of A. | + | Then the definition of ''X'' must contain an assignment statement, ''A := value'', that sets the value of A. |
ComputedBy can only appear at the top level of a Variable's definition. | ComputedBy can only appear at the top level of a Variable's definition. | ||
Line 18: | Line 20: | ||
= When to Use = | = When to Use = | ||
− | ComputedBy can be particularly convenient within an [[Iterate]] function, when multiple quantities need to be updated at each iteration. Prior to the introduction of ComputedBy, it was necessary to bundle all quantites together, using [[Using References|references]], and then unbundle them. With computed by, this can be accomplished as follows: | + | '''ComputedBy''' can be particularly convenient within an [[Iterate]] function, when multiple quantities need to be updated at each iteration. Prior to the introduction of '''ComputedBy''', it was necessary to bundle all quantites together, using [[Using References|references]], and then unbundle them. With computed by, this can be accomplished as follows: |
Variable A := ComputedBy(The_iteration) | Variable A := ComputedBy(The_iteration) | ||
Line 39: | Line 41: | ||
= Use in [[Dynamic]] = | = Use in [[Dynamic]] = | ||
− | When A := ComputedBy(X), and X is within a [[Dynamic]] loop, then the assignment within X sets the value of A for the current [[Time]]. Thus, A will also be indexed by [[Time]] when the computation completes. | + | When ''A := ComputedBy(X)'', and ''X'' is within a [[Dynamic]] loop, then the assignment within ''X'' sets the value of ''A'' for the current [[Time]]. Thus, ''A'' will also be indexed by [[Time]] when the computation completes. |
= Evaluation Mode = | = Evaluation Mode = | ||
− | When A := ComputedBy(X), if the assignment to A inside of X's definition occurs from within a [[Sample]] evaluation context, then the [[Evaluation Modes|Sample]] value of A is set. Likewise, if the assignment occurs from a [[Mid]] context, then the [[Evaluation Modes|Mid]] value of A is set | + | When ''A := ComputedBy(X)'', if the assignment to ''A'' inside of ''X'''s definition occurs from within a [[Sample]] evaluation context, then the [[Evaluation Modes|Sample]] value of A is set. Likewise, if the assignment occurs from a [[Mid]] context, then the [[Evaluation Modes|Mid]] value of A is set. |
− | |||
− | |||
− | |||
− | |||
= See Also = | = See Also = |
Revision as of 15:16, 29 April 2008
ComputedBy(X)
(Analytica 4.1 or later)
Used to indicate that the value of a variable is computed as a side-effect of a different variable X.
When a variable, A, has the definition
ComputedBy(X)
Then the definition of X must contain an assignment statement, A := value, that sets the value of A.
ComputedBy can only appear at the top level of a Variable's definition.
Referential Transparency
Analytica's modeling language is referentially transparent, so that side-effects in general are not allowed. However, in some cases it is natural to set another variable as a side-effect of a computation. ComputedBy makes this possible without a loss of referential transparency.
When to Use
ComputedBy can be particularly convenient within an Iterate function, when multiple quantities need to be updated at each iteration. Prior to the introduction of ComputedBy, it was necessary to bundle all quantites together, using references, and then unbundle them. With computed by, this can be accomplished as follows:
Variable A := ComputedBy(The_iteration) Variable B := ComputedBy(The_iteration)
Variable The_iteration := Iterate( initial:(A:=A0;B:=B0), expr:(A:=F(A,B);B:=G(A,B)), until: H(A,B), maxIter:100 )
In other cases, there are times in which a useful quantity is computed in the course of computing a primary quantity. The secondary quantity can be stored in a variable defined as ComputedBy. The following is an example:
Variable Component_Variance := ComputedBy( Principle_Components )
Variable Principle_Components := Var eig := EigenDecompose(X,I,J); Component_Variance := eig[.item='value']; #eig[.item='vector']
Use in Dynamic
When A := ComputedBy(X), and X is within a Dynamic loop, then the assignment within X sets the value of A for the current Time. Thus, A will also be indexed by Time when the computation completes.
Evaluation Mode
When A := ComputedBy(X), if the assignment to A inside of X's definition occurs from within a Sample evaluation context, then the Sample value of A is set. Likewise, if the assignment occurs from a Mid context, then the Mid value of A is set.
Enable comment auto-refresher