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

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

Linear expression. (LayoutExpression of the original ui/autolayout/layoutexpression.js, term list + constant)
- All arithmetic functions return a new expression. (immutable value)
- equal_to, less_than_or_equal_to, greater_than_or_equal_to return a LayoutConstraint. (required strength)
- Arguments take numbers (f64, i32), LayoutVariable, LayoutTerm, LayoutExpression. (impl `Into<LayoutExpression>`)
- Differences from the original:
  - The runtime type check of the original toExpression moved into From implementations. Unaccepted types are stopped at compile time.
  - multiply and divide take only f64, since the original also takes only numbers. (the original's 'scalars only' exception does not occur)
  - Division by zero is a programming mistake, so it panics with the same message as the original.
  - The original uses a late loading trick to avoid importing each other with LayoutConstraint. The Rust version just uses use.

## LayoutExpression (struct) (Clone, Debug, From<f64>, From<i32>, From<&LayoutVariable>, From<LayoutVariable>, From<LayoutTerm>, From<&LayoutTerm>, From<&LayoutExpression>)

Linear expression.
- The sum of a list of terms (coefficient * variable) and a constant. Terms of the same variable are not merged and are kept in insertion order.

Example: Building a center expression from left and width

```rust
use vanilla::{LayoutExpression, LayoutVariable};

let left = LayoutVariable::new("left");
let width = LayoutVariable::new("width");
left.set_value(20.0);
width.set_value(100.0);
let center_x = LayoutExpression::from_variable(&left).add(LayoutExpression::from_variable(&width).divide(2.0));
assert_eq!(center_x.get_terms().len(), 2);
assert_eq!(center_x.get_value(), 70.0);

let shifted = center_x.subtract(10).multiply(2.0);
assert_eq!(shifted.get_constant(), -20.0);
assert_eq!(shifted.get_value(), 120.0);
```

- `pub fn new(terms: Vec<LayoutTerm>, constant: f64) -> LayoutExpression` (constructor, since 0.4.0): Constructor. (term list and constant)
  - `terms` Vec<LayoutTerm>: Term list.
  - `constant` f64: Constant term.
  - returns LayoutExpression: New expression.
- `pub fn from_variable(variable: &LayoutVariable) -> LayoutExpression` (static, since 0.4.0): Builds an expression from one variable. (1 * variable + 0)
  - `variable` &LayoutVariable: Variable to put in the expression. (holds a cloned handle)
  - returns LayoutExpression: An expression with one term (1 * variable) and constant 0.
- `pub fn from_constant(value: f64) -> LayoutExpression` (static, since 0.4.0): Builds an expression with only a constant.
  - `value` f64: Constant term.
  - returns LayoutExpression: An expression with no terms and value as the constant.
- `pub fn to_expression(input: impl Into<LayoutExpression>) -> LayoutExpression` (static, since 0.4.0): Turns an acceptable input into an expression.
  - `input` impl Into<LayoutExpression>: Thing to convert. (numbers f64, i32, LayoutVariable, LayoutTerm, LayoutExpression and their references)
  - returns LayoutExpression: The converted expression. (a number becomes a constant expression, a variable 1 * variable, a term just that term, an expression a copy)

  Example: Inputs of various forms into an expression

  ```rust
  use vanilla::{LayoutExpression, LayoutTerm, LayoutVariable};
  
  let x = LayoutVariable::new("x");
  assert!(LayoutExpression::to_expression(12).is_constant());
  assert_eq!(LayoutExpression::to_expression(2.5f64).get_constant(), 2.5);
  assert_eq!(LayoutExpression::to_expression(&x).get_terms()[0].get_coefficient(), 1.0);
  assert_eq!(LayoutExpression::to_expression(LayoutTerm::new(x.clone(), 3.0)).get_terms()[0].get_coefficient(), 3.0);
  ```

- `pub fn get_terms(&self) -> &[LayoutTerm]` (method, since 0.4.0): Returns the term list.
  - returns &[LayoutTerm]: Slice of the term list. (insertion order)
- `pub fn get_constant(&self) -> f64` (method, since 0.4.0): Returns the constant term.
  - returns f64: Constant term.
- `pub fn is_constant(&self) -> bool` (method, since 0.4.0): Whether there are no terms. (whether there is only the constant)
  - returns bool: true if there are no terms.
- `pub fn get_value(&self) -> f64` (method, since 0.4.0): Returns the evaluated value. (sum of term values + constant)
  - returns f64: The sum of coefficient * current variable value for each term, plus the constant.
- `pub fn add(&self, input: impl Into<LayoutExpression>) -> LayoutExpression` (method, since 0.4.0): Addition.
  - `input` impl Into<LayoutExpression>: Thing to add. (number, variable, term, expression)
  - returns LayoutExpression: A new expression self + input. (appends input's terms after self's terms and adds the constants)
- `pub fn subtract(&self, input: impl Into<LayoutExpression>) -> LayoutExpression` (method, since 0.4.0): Subtraction.
  - `input` impl Into<LayoutExpression>: Thing to subtract. (number, variable, term, expression)
  - returns LayoutExpression: A new expression self - input. (multiplies input by -1 and adds it)
- `pub fn multiply(&self, coefficient: f64) -> LayoutExpression` (method, since 0.4.0): Scalar multiplication. (nonlinear products such as multiplying variables together are not accepted)
  - `coefficient` f64: Number to multiply by.
  - returns LayoutExpression: A new expression with every coefficient and the constant multiplied by coefficient.
- `pub fn divide(&self, denominator: f64) -> LayoutExpression` (method, since 0.4.0): Scalar division. (panics on division by zero)
  - `denominator` f64: Number to divide by. (panics if 0)
  - returns LayoutExpression: A new expression self * (1 / denominator).
- `pub fn equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint` (method, since 0.4.0): Creates an equality constraint. (self == input)
  - `input` impl Into<LayoutExpression>: Thing to match. (number, variable, term, expression)
  - returns LayoutConstraint: A required constraint whose expression is self - input and whose relation is Equal. (not added to the solver)

  Example: Adding right == left + width and solving

  ```rust
  use vanilla::{LayoutExpression, LayoutSolver, LayoutVariable};
  
  let solver = LayoutSolver::new();
  let left = LayoutVariable::new("left");
  let right = LayoutVariable::new("right");
  solver.add_constraint(&LayoutExpression::from_variable(&left).equal_to(30)).unwrap();
  let right_constraint = LayoutExpression::from_variable(&right).equal_to(LayoutExpression::from_variable(&left).add(200));
  solver.add_constraint(&right_constraint).unwrap();
  solver.update_variables();
  assert!((right.get_value() - 230.0).abs() < 1e-9);
  ```

- `pub fn less_than_or_equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint` (method, since 0.4.0): Creates a less than or equal constraint. (self <= input)
  - `input` impl Into<LayoutExpression>: Thing not to exceed. (number, variable, term, expression)
  - returns LayoutConstraint: A required constraint whose expression is self - input and whose relation is LessThanOrEqual.
- `pub fn greater_than_or_equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint` (method, since 0.4.0): Creates a greater than or equal constraint. (self >= input)
  - `input` impl Into<LayoutExpression>: Thing not to fall below. (number, variable, term, expression)
  - returns LayoutConstraint: A required constraint whose expression is self - input and whose relation is GreaterThanOrEqual.
