Skip to main content

No project description provided

Project description

Latex-VM (Virtual Machine)

Coverage Bugs Quality Gate Status Code Smells

Latex-VM is a simple context environment for mathematical expressions in LaTeX. It supports variable and function declaration.

Type Aliases

Type Definition
Varname str
ExpressionStr str
EnvironmentVariables Dict[Varname, Any]

API

class ActionResult:
    def ok(self) -> bool

class GraphSession:
    @staticmethod
    def new(env: EnvironmentVariables = {}, rules: Dict[str, str] = {}) -> "GraphSession"
    def execute(self, input: str, simplify: bool = False) -> ActionResult[CalculatorAction, str]
    def get_env_functions(self) -> EnvironmentVariables
    def get_env_variables(self) -> EnvironmentVariables
    def get_env(self) -> EnvironmentVariables
    def force_resolve_function(self, input: str, use_sub_rule: bool = True) -> ActionResult[None, str]
    def add_sub_rule(self, pattern: str, replacement: str) -> None
    def remove_sub_rule(self, pattern: str) -> None
    def get_sub_rules(self) -> Dict[str, str]
    def clear_session(self) -> None

Example Usage

# Create a new graph session
session = GraphSession.new()

# Assignment expression
session.execute("y = 20")                                 # Action: CalculatorAction.VARIABLE_ASSIGNMENT, Ok(20)

# Function declaration expression
session.execute("double(x) = x * 2")                      # Action: CalculatorAction.FUNCTION_DEFINITION, Ok(double_func(x) = x * 2)
session.execute("pow(x) = x^2")                           # Action: CalculatorAction.FUNCTION_DEFINITION, Ok(pow_func(x) = x^2)

# Statement expression
session.execute("double(pow(2))")                         # Action: CalculatorAction.STATEMENT_EXECUTION, Ok(8)

# Function force-resolve (Does not converge to a value)
session.force_resolve_function("double(pow(x+2))")        # Ok((x + 2)**2*2)

Environment Variables and Value Retrieval

Note: All getters returns a copy in order to prevent mutation of internal variables.

session = GraphSession.new()
session.execute("y = 20")
session.execute("double(x) = x * 2")

# Get all environment variables
session.get_env()                       # {'y': '20', 'double_func': (['x'], 'x * 2')}

# Get only function varaibles
session.get_env_functions()             # {'double_func': (['x'], 'x * 2')}

# Get only variables
session.get_env_variables()             # {'y': '20'}

# Session can be cleared
session.clear_session()

Result and Error Handling

Methods which returns the ActionResult type conforms to the following handling.
Note: ActionResult.action holds the action value.

...

result = session.execute(...)

if result.ok():
  ...
  # use the result value
  # Note: The result.message contains the value
else:
  ...
  # handle errors...
  # Note: The result.message contains the error message

Session Loading

Sessions can easily be loaded by passing in pre-existing environment variables.

session_1 = GraphSession.new()
session_1.execute("double(x) = x * 2")

session_2 = GraphSession.new(session.get_env())
res = session_2.execute("double(5)")
print(res.message) # 10

Simplification

Expression simplification is optional. In cases where the input latex is very complex, it can greatly affect performance. For this reason, the simplify flag is defaulted to False. Addtionally, the simplify function is run with a timeout of 3 seconds, if failed, it will just yield the result without simplifying.

session = GraphSession.new()
# Action: CalculatorAction.FUNCTION_DEFINITION, Ok(f_func(x) = \frac{x}{3} + 3 + 20 + 20 + 20)
session.execute(r"f(x) = \frac{x}{3} + 3 + y + y + y")

# Action: CalculatorAction.FUNCTION_DEFINITION, Ok(f_func(x) = \frac{x}{3} + 63)
session.execute(r"f(x) = \frac{x}{3} + 3 + y + y + y", simplify=True)

Substitution Rules

The GraphSession.force_resolve_function method returns a Pythonic representation of the input. However, this representation may need to be modified in order to be usable. For this reason, there are substitution rules that can be applied to the output.

This behavior is enabled default, to resolve without using substitution rules, set use_sub_rule to False.

gs.execute(r"make_abs(x) = \left|x\right|")
gs.execute(r"pow(a, b) = a^{b}")

# Without any substituion rule
print(gs.force_resolve_function(r"pow(make_abs(x), make_abs(y))"))   # Ok(Abs(x)**Abs(y))

# We want abs(...) instead of Abs(...)
gs.add_sub_rule("Abs", "abs")
gs.force_resolve_function(r"pow(make_abs(x), make_abs(y))")   # Ok(abs(x)**abs(y))

# We need pow expressed using '^'
gs.add_sub_rule(r"\*\*", "^")
gs.force_resolve_function(r"pow(make_abs(x), make_abs(y))")   # Ok(abs(x)^abs(y))

# Resolve without using substitution rules
gs.force_resolve_function(r"pow(make_abs(x), make_abs(y))", use_sub_rule=False) # Ok(Abs(x)**Abs(y))

LaTeX override

You can override LaTeX functions by declaring a function with the same name. This allows you to customize the functionality of the function according to your needs.

# Works perfectly fine
print(gs.force_resolve_function(r"\sin\left(x\right)")) # Ok(sin(x))

# Override
gs.execute("double(x) = 2 x")
gs.execute("sin(a) = double(a)")

# Sin is now overridden
print(gs.force_resolve_function(r"\sin\left(x\right)")) # Ok(x*2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

latexvm-0.1.6.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

latexvm-0.1.6-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file latexvm-0.1.6.tar.gz.

File metadata

  • Download URL: latexvm-0.1.6.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.6 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for latexvm-0.1.6.tar.gz
Algorithm Hash digest
SHA256 86094a46312354b9455db2839bdc092eb34bcf2eff932014f13af6ecca533093
MD5 db5b139c632c10a18ba238ed2de78722
BLAKE2b-256 80d8d1098028e68a3d498f808fdd54d8d870540991cd9a2725b09e1231c62473

See more details on using hashes here.

File details

Details for the file latexvm-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: latexvm-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.6 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for latexvm-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 83898d3a9d86d18d74ab64f1c4398e8bbd7340ec99e817d094d464d07e53e546
MD5 331393d5bc910efc306ff7ce34502a88
BLAKE2b-256 dd31b01189c67c9e55b478f7b8f505c283d11291873fb8db3ff2cc372ecd09e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page