Difference between revisions of "ComputedBy"

m
Line 11: Line 11:
  
 
== Examples ==
 
== Examples ==
[[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 quantities together, using [[Using References|references]], and then unbundle them.  With [[ComputedBy]], this can be accomplished as follows:
+
[[ComputedBy]] is particularly convenient within an [[Iterate]] function, when you want to update multiple quantities for each iteration.  For example:
  
 
:<code>Variable A := ComputedBy(The_iteration)</code>
 
:<code>Variable A := ComputedBy(The_iteration)</code>
Line 20: Line 20:
 
::<code>maxIter: 100)</code>
 
::<code>maxIter: 100)</code>
  
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:
+
In other cases, when computing one variable, it is convenient to compute another in the same calculation, for example:
  
 
:<code>Variable Component_Variance := ComputedBy(Principle_Components)</code>
 
:<code>Variable Component_Variance := ComputedBy(Principle_Components)</code>

Revision as of 00:43, 17 September 2017


ComputedBy(X)

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.

Examples

ComputedBy is particularly convenient within an Iterate function, when you want to update multiple quantities for each iteration. For example:

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, when computing one variable, it is convenient to compute another in the same calculation, for example:

Variable Component_Variance := ComputedBy(Principle_Components)
Variable Principle_Components :=
Var eig := EigenDecompose(X, I, J);
Component_Variance := eig[.item = 'value'];
#eig[.item = 'vector']

More details

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.

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.

See Also

Comments


You are not allowed to post comments.