# ui/autolayout/layoutrow (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/autolayout/layoutrow/

A row inside the solver. (LayoutRow of the original ui/autolayout/layoutrow.js)
- One row of the tableau. Cells (symbol -> coefficient) and a constant.
- All functions that change it change it in place.
- Differences from the original:
  - Cells are a LayoutSymbolMap that keeps insertion order like the original Map.
  - The original copy() is Clone. (a copy identical down to the order)
  - coefficient, which could be omitted (default 1), is required.
  - The near-zero check that the original kept separately in the row and the solver is shared as a single near_zero.

## LayoutRow (struct) (Clone, Debug)

A row inside the solver.
- A value used inside the solver. User code usually uses only LayoutVariable and LayoutSolver.

Example: Solving 10 + 2x + 4s = 0 for x

```rust
use vanilla::ui::autolayout::layoutrow::LayoutRow;
use vanilla::ui::autolayout::layoutsymbol::LayoutSymbol;
use vanilla::ui::autolayout::layoutsymboltype::LayoutSymbolType;

let x = LayoutSymbol::new(LayoutSymbolType::External);
let slack = LayoutSymbol::new(LayoutSymbolType::Slack);
let mut row = LayoutRow::new(10.0);
row.insert_symbol(x, 2.0);
row.insert_symbol(slack, 4.0);
row.solve_for(x);
assert_eq!(row.get_constant(), -5.0);
assert_eq!(row.coefficient_for(slack), -2.0);
assert_eq!(row.coefficient_for(x), 0.0);
```

- `pub fn new(constant: f64) -> LayoutRow` (constructor, since 0.4.0): Constructor. (constant is the initial constant)
  - `constant` f64: Initial constant.
  - returns LayoutRow: A new row with no cells.
- `pub fn get_cells(&self) -> &LayoutSymbolMap<f64>` (method, since 0.4.0): Returns the cell map.
  - returns &LayoutSymbolMap<f64>: Reference to the cell map (symbol -> coefficient, insertion order).
- `pub fn get_constant(&self) -> f64` (method, since 0.4.0): Returns the constant term.
  - returns f64: Constant term.
- `pub fn set_constant(&mut self, constant: f64)` (method, since 0.4.0): Sets the constant.
  - `constant` f64: New constant.
- `pub fn add_constant(&mut self, value: f64) -> f64` (method, since 0.4.0): Adds a value to the constant. (returns the new constant)
  - `value` f64: Value to add to the constant.
  - returns f64: The constant after adding.
- `pub fn insert_symbol(&mut self, symbol: LayoutSymbol, coefficient: f64)` (method, since 0.4.0): Adds coefficient to the symbol's coefficient. (removes it from the cells if the result is near 0)
  - `symbol` LayoutSymbol: Symbol whose coefficient to add to. (starts from coefficient 0 if not in the cells)
  - `coefficient` f64: Coefficient to add.
- `pub fn insert_row(&mut self, other_row: &LayoutRow, coefficient: f64)` (method, since 0.4.0): Multiplies another row by coefficient and adds it to this row.
  - `other_row` &LayoutRow: Row to add. (both the constant and the cells)
  - `coefficient` f64: Number to multiply other_row by.
- `pub fn remove_symbol(&mut self, symbol: LayoutSymbol)` (method, since 0.4.0): Removes a symbol.
  - `symbol` LayoutSymbol: Symbol to remove. (unchanged if not in the cells)
- `pub fn reverse_sign(&mut self)` (method, since 0.4.0): Flips the sign. (multiplies the constant and every coefficient by -1)
- `pub fn solve_for(&mut self, subject: LayoutSymbol)` (method, since 0.4.0): Solves the row for subject.
  - Removes subject's cell and multiplies the remaining cells and the constant by -1 / subject coefficient. (Kiwi convention)
  - If subject is not in the cells, it is the caller's mistake.
  - `subject` LayoutSymbol: Symbol to solve for. (must be in the cells. If not, dividing by 0 makes the constant and coefficients infinite or NaN)
- `pub fn solve_for_pair(&mut self, lhs: LayoutSymbol, rhs: LayoutSymbol)` (method, since 0.4.0): Solves a row in lhs = rhs form again for rhs. (puts lhs in with -1 and solves for rhs)
  - `lhs` LayoutSymbol: Left symbol. (added with coefficient -1)
  - `rhs` LayoutSymbol: Symbol to solve for again.
- `pub fn coefficient_for(&self, symbol: LayoutSymbol) -> f64` (method, since 0.4.0): Returns the symbol's coefficient. (0 if absent)
  - `symbol` LayoutSymbol: Symbol to find.
  - returns f64: Coefficient. 0 if not in the cells.
- `pub fn substitute(&mut self, symbol: LayoutSymbol, substitution_row: &LayoutRow)` (method, since 0.4.0): Substitutes substitution_row for symbol. (unchanged if symbol is absent)
  - `symbol` LayoutSymbol: Symbol to substitute.
  - `substitution_row` &LayoutRow: Row to put in place of symbol. (multiplied by symbol's coefficient and added)

## Functions and constants

- `pub fn near_zero(value: f64) -> bool` (method, since 0.4.0): Whether a floating point number is near 0. (same as Kiwi's nearZero)
  - `value` f64: Value to check.
  - returns bool: true if the absolute value is less than 1e-8.
