Elex.Inverter (Elex v0.2.0)

View Source

Inverts simple arithmetic expressions with a single variable.

Given an expression like "value + 5", it returns the inverse "value - 5". This is useful for creating inverse transformations.

Supported operations:

  • Addition (+) → Subtraction (-)
  • Subtraction (-) → Addition (+)
  • Multiplication (*) → Division (/)
  • Division (/) → Multiplication (*)

Examples

context = Elex.new_context() |> Elex.add_variable("value", 0)
{:ok, ast, _} = Elex.Parser.parse("value + 5", context, validate: false)
Elex.Inverter.invert(ast, "value")
#=> {:ok, {:-, [{:var, "value"}, #Decimal<5>]}}

{:ok, ast, _} = Elex.Parser.parse("value * 3", context, validate: false)
Elex.Inverter.invert(ast, "value")
#=> {:ok, {:/, [{:var, "value"}, #Decimal<3>]}}

See Elex.Parser for parsing and Elex.Evaluator for evaluating the inverted AST.

Summary

Functions

Inverts an expression AST for the given target variable.

Functions

invert(ast, target_var)

@spec invert(term(), String.t()) :: {:ok, term()} | {:error, String.t()}

Inverts an expression AST for the given target variable.

Parameters

  • ast - The parsed expression AST
  • target_var - The name of the variable to solve for (string)

Returns

  • {:ok, ast} - The inverted AST that represents the inverse operation
  • {:error, reason} - A human-readable error message when inversion is not possible