Difference between revisions of "ComputedBy"

(category top-level functions)
Line 2: Line 2:
 
[[category:Top level functions]]
 
[[category:Top level functions]]
  
= ComputedBy(X) =
+
== 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».  
  
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 <code>ComputedBy(X)</code>, then the definition of «X» must contain an assignment statement, <code>A := value</code>, that sets the value of '''A'''.
  
When a variable, ''A'', has the definition
+
[[ComputedBy]] can only appear at the top level of a Variable's definition.
ComputedBy(X)
 
  
Then the definition of ''X'' must contain an assignment statement, ''A := value'', that sets the value of A.
+
== 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 can only appear at the top level of a Variable's definition.
+
:<code>Variable A := ComputedBy(The_iteration)</code>
 +
:<code>Variable B := ComputedBy(The_iteration)</code>
 +
:<code>Variable The_iteration := Iterate(initial: (A := A0; B := B0),</code>
 +
::<code>expr:(A := F(A, B); B := G(A, B)),</code>
 +
::<code>until: H(A, B),</code>
 +
::<code>maxIter: 100)</code>
  
= Referential Transparency =
+
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:
  
Analytica's modeling language is [http://en.wikipedia.org/wiki/Referential_transparency 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.
+
:<code>Variable Component_Variance := ComputedBy(Principle_Components)</code>
 +
:<code>Variable Principle_Components :=</code>
 +
::<code>Var eig := EigenDecompose(X, I, J);</code>
 +
::<code>Component_Variance := eig[.item = 'value'];</code>
 +
::<code>#eig[.item = 'vector']</code>
  
= When to Use =
+
==More details==
 +
=== Referential Transparency ===
 +
Analytica's modeling language is [http://en.wikipedia.org/wiki/Referential_transparency 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.
  
'''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:
+
=== Use in Dynamic ===
  
Variable A := ComputedBy(The_iteration)
+
When <code>A := ComputedBy(X)</code>, 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.
  Variable B := ComputedBy(The_iteration)
 
  
Variable The_iteration := Iterate(  initial:(A:=A0;B:=B0),
+
=== Evaluation Mode ===
                                    expr:(A:=F(A,B);B:=G(A,B)),
+
When <code>A := ComputedBy(X)</code>, 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.
                                    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 [[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 ==
 
* [[Iterate]]
 
* [[Iterate]]
 
* [[Dynamic]]
 
* [[Dynamic]]
 
* [[Using References]]
 
* [[Using References]]

Revision as of 23:15, 12 January 2016


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 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 references, and then unbundle them. With ComputedBy, 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']

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.