Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions src/reference-manual/types.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ real values, and declares `m` to be a two-dimensional array of
size $6 \times 7$ containing values that are $3 \times 3$ matrices.

Prior to 2.26 Stan models used a different syntax, with the array
dimensions defined after the variable identifier.
dimensions defined after the variable identifier.
Equivalent declarations of `x` and `m` with the pre-2.26 syntax:

```stan
Expand Down Expand Up @@ -351,7 +351,7 @@ model {
}
```

Recall that the centered parameterization is achieved with the code
Recall that the centered parameterization is achieved with the code

```stan
parameters {
Expand Down Expand Up @@ -1249,6 +1249,42 @@ Similarly, `vector[ , ]` is the type of a two-dimensional array of
vectors.



## Variable declaration
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is going to be the first place we talk about this, we want to include some more examples, like

array[N] y;
array[5] matrix[3, 4] A;

There are really three different syntaxes:

  • block variables (sized, constrained)
  • local variables (sized, unconstrained)
  • function argument variables (unsized, unconstrained)

Somehow highlighting this distinction up front would help.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider copying/moving this table from the users guide?
https://mc-stan.org/docs/2_27/stan-users-guide/basic-functions-section.html#type-declarations-for-functions

(though it seems to not include offset/multiplier yet...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about it for this PR. I opened a new issue: #383


Variables in Stan are declared by giving a type and a name. For example

```stan
int N;
vector[N] y;
array[5] matrix[3, 4] A;
```

declares a variable `N` that is an integer, a variable `y` that is
a vector of length `N` (the previously declared variable), and a
variable `A`, which is a length-5 array where each element is a 3 by 4
matrix.

There are several different places a variable is declared in Stan. They are
block variables, like those inside `data`, which can have
[constraints](#constrained-data-types) and must include sizes for their types,
like in the above examples. Local variables, like those defined inside loops
or local blocks cannot be constrained, but still include sizes. Finally,
variables declared as [function parameters](#argument-types-and-qualifiers)
are not constrained types and _exclude_ sizes.

```stan
// valid block variables, but not locals or function parameters
vector<lower=0>[N] u;

// valid as a block or local variable, but not a function parameter
array[3] int is;

// function parameters exclude sizes and cannot be constrained
void pretty_print_tri_lower(matrix x) { ... }

```

## Compound variable declaration and definition

Stan allows assignable variables to be declared and defined in a
Expand Down Expand Up @@ -1324,3 +1360,44 @@ Any variable that is in scope and any function that is available in
the block in which the compound declaration and definition appears may
be used in the expression on the right-hand side of the compound
declaration and definition statement.


## Declaring multiple variables at once

Stan will interpret multiple comma-separated variable names following a single
type as declaring multiple new variables.
This is available for all variable declarations in all blocks.

### Types for multiple declarations {-}

The code:

```stan
real x, y;
```
is equivalent to

```stan
real x;
real y;
```

As a result, all declarations on the same line must be of the same type.

### Combining with other features {-}

The ability to declare multiple variables can be combined with assignments
whenever a declare-define is valid, as documented in
[the section introducing compound declarations and definitions](#compound-variable-declaration-and-definition)
:

```stan
real x = 3, y = 5.6
```

[Constrained data types](#constrained-data-types) can also be declared
together, so long as the constraint for each variable is the same:

```stan
real<lower=0> x, y;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces around operators, always.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this is in reference to. Would you prefer real <lower = 0> x, y;? If so, should also update all of the examples in 5.3 and the rest of the chapter

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we can do that later. There is a style guide: https://mc-stan.org/docs/2_27/stan-users-guide/stan-program-style-guide.html

I wouldn't worry about it for this PR---the whole doc needs to be updated. Also, you might want to weigh in on the style guide before we rewrite all the doc examples.

```