Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Coefficient Access

Read and write the linear (bias) and quadratic (coupling) coefficients of a Model register. Missing entries read as \(0\); writes create the entry on the first call. All coefficient values are i64; reg must hold Model.

Linear Coefficients

CodeMnemonicArgumentsStack EffectRegister EffectDescription
0x60GETLINEreg: Register\([\ldots, i] \to [\ldots, h_i]\)readPop \(i\). Push \(\text{linear}[i]\) (\(0\) if absent).
0x61SETLINEreg: Register\([\ldots, i, v] \to [\ldots]\)mutatePop \(v\), then \(i\). Set \(\text{linear}[i] \leftarrow v\).
0x62ADDLINEreg: Register\([\ldots, i, \delta] \to [\ldots]\)mutatePop \(\delta\), then \(i\). Accumulate: \(\text{linear}[i] \mathrel{+}= \delta\).

Quadratic Coefficients

CodeMnemonicArgumentsStack EffectRegister EffectDescription
0x63GETQUADreg: Register\([\ldots, i, j] \to [\ldots, J_{ij}]\)readPop \(j\), then \(i\). Push \(\text{quad}[i,j]\) (\(0\) if absent).
0x64SETQUADreg: Register\([\ldots, i, j, v] \to [\ldots]\)mutatePop \(v\), then \(j\), then \(i\). Set \(\text{quad}[i,j] \leftarrow v\).
0x65ADDQUADreg: Register\([\ldots, i, j, \delta] \to [\ldots]\)mutatePop \(\delta\), then \(j\), then \(i\). Accumulate: \(\text{quad}[i,j] \mathrel{+}= \delta\).

Sparse Storage

Coefficients are stored in sparse BTreeMap structures:

  • Linear: \(\text{BTreeMap}\langle\text{usize}, \text{i64}\rangle\) keyed by variable index.
  • Quadratic: \(\text{BTreeMap}\langle(\text{usize}, \text{usize}), \text{i64}\rangle\) keyed by variable pair.

Missing entries implicitly have value \(0\). Setting a coefficient to \(0\) removes it from the map, keeping memory usage proportional to the number of non-zero terms.

Key Normalisation

Quadratic coefficient keys are normalised so that \(i \le j\). Calling SETQUAD or ADDQUAD with \(i > j\) silently swaps the indices. This means \(\text{quad}[3, 5]\) and \(\text{quad}[5, 3]\) refer to the same entry.