Expression Syntax

Analytica expressions are the language used for Definition attributes of a variable or Function. Expressions are also used in the Check, OnClick, and OnChange Attributes.

The obsolete Script attribute uses Typescript, which has a slightly different syntax from Analytica Expressions (see also Typescript Commands). The Script attribute was replaced by OnClick and OnChange attributes in Analytica 4.6 release, and is retained only for backward compatibility.

Function Calls

Analytica lets you call built-in functions and User-Defined Functions using the commonposition-based syntax for the parameters (or arguments):

F(A, B, C)

where the parameter values A, B, and C may themselves be expressions.

It also lets you specify some or all parameters using a name-based syntax. These examples show four ways to call function Uniform with the same parameter values, using position-based syntax, name-based syntax, and a combination:

Uniform(1, 100, True)   --- position-based syntax
Uniform(min: 1, max: 100, integer: True)  --- name-based syntax
Uniform(Integer: True, min: 1, max: 100)  --- name-based syntax
Uniform(1, 100, integer: true)  --- mixed syntax

Name-based syntax means you specify the parameter by name, followed by colon, followed by the a number or expression giving its value. With names, you can list the parameters in any order, as in the third example. Using mixed syntax, you can specify initial parameters by position, followed by one or more parameters identified by name.

If a function expects a parameter to be an index or variable, you must give it the identifier of a variable or index. You can specify an index using the dot-operator syntax -- e.g. A.I. But, in these cases, you may not use arbitrary expressions, including function calls, as the value.

Functions evaluate ("call") their parameters in various ways, as specified by theparameter qualifiers, and on the evaluation mode when the function is evaluated.

Array subscripting

Unlike most computer languages that use position to select elements of a multidimensional array, Analytica uses named syntax, specifying the index(es) by name:

Population[Year = 2010, State = "New York"]

This means that you don't have to remember which dimension or index is the "inner" or "outer", so this works just as well:

Population[State = "New York", Year = 2010]

It also means that you can slice out a subarray, e.g.

Population[State = "New York"]

gives the population of New York for every value of Year. And

Population[Year = 2010]

gives the population of all states for 2010. With Array abstraction, the number of dimensions may vary by version or even run of a model, so accessing Indexes by name is much more robust than by position.

Operator Precedence

Analytica uses a fairly standard precedence for arithmetic and logical operators. Exponentiation (^) binds more tightly than multiplication (*) and division (/), which binds more tightly than addition (+) and subtraction (-). Thus, this expression

1 / 2 * 3 - 4 / 5 ^ 6 + 7

would parse as

( ( 1 / 2 ) * 3 ) - ( 4 / ( 5 ^ 6 ) ) + 7

This list shows the binding precedence of Analytica operators, with the highest precedence operators first. Operators in the same bullet have the same level of precedence:

Note: And,Or have equal precedence in Analytica 4.2 and earlier

Comments

Comments can be embedded anywhere whitespace would be allowed within an expression. There are two recognized syntaxes for comments:

{ This is a comment }
/* This is a comment */ 

If you start a comment with {, then your comment cannot contain the } character within. Similarly, if you begin a comment with /*, the comment cannot contain the two character sequence */ inside. If you need to embed a comment within a comment (which sometimes is useful when temporarily commenting out a block of expression code that already contains comments), then you need to ensure that if the inner comment uses {...}, that the outer comment uses /*...*/, or vise versa.

Version Gated Comments

A version-gated comment is a portion of an expression that is viewed as a comment in some releases of Analytica, but as an actual expression in other releases of Analytica. For example, suppose you have an algorithm that uses features new in Analytica 4.0, but you still want it to work with Analytica 3.1, perhaps falling back to a less efficient algorithm or to reduced functionality. A version-gated comment lets you do this without risk of parse errors in the version that might not recognize the newer features.

Consider these equivalent forms of version gated comments:

{!40000|expression}
{!40000-|expression}}

Analytica release 4.00.00 or after will interpret the expression, but prior release will ignore it as a comment.

This gated comment executes the expression in releases 4.00.00 through 4.01.00, but releases before and after will treat it as a comment:

{!40000-40100|expression}

This version interprets the expression in any release from 4.0 up to 4.02.00, but any release thereafter treats it as a comment:

{!-40200|expression}

Important: All version-gated comments are treated as comment in Analytica releases 3.1 and earlier, regardless of what the version number indicates, since the version-gated comment feature was not introduced until Analyica 4.0.

Comments


You are not allowed to post comments.