# ComputedBy

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

Enable comment auto-refresher