Skip to main content

Stylgebra: stylable algebraic expressions

Example

Consider power expressions in SymPy:

>>> from sympy import symbols
>>> x = symbols('x')
>>> expr1 = x**(-1)
>>> expr2 = x**(-2)
>>> expr1
1/x
>>> expr2
x**(-2)

By converting to a Stylgebra expression, we can set the rule for how to handle negative exponents, like applying CSS styling to a web page:

>>> from stylgebra.adapt import adapt_sympy_expr
>>> f = adapt_sympy_expr(expr2)
>>> f.format(rules={'Power': {'negative': 'sup'}})
'x^{-2}'
>>> f.format(rules={'Power': {'negative': 'frac'}})
'\\frac{1}{x^{2}}'
>>> f.format(rules={'Power': {'negative': 'inline'}})
'1/x^{2}'

The Problem

This package supports the automatic generation of mathematical expressions, from classes representing mathematical objects.

The problem is tricky because, while it is easy to represent any given mathematical entity internally, we want quite fine-grained control over the way it is expressed symbolically.

For example, if p = 7, here are several different ways of expressing the same sum:

a_0 + a_1 + a_2 + ... + a_{p-2}
a_0 + a_1 + ... + a_{p-2}
a_0 + a_1 + a_2 + a_3 + a_4 + a_5
a_{p-2} + a_{p-3} + ... + a_0
a_{7-2} + a_{7-3} + ... + a_0
a_5 + a_4 + ... + a_0

This problem is essentially the same one that was solved in web browsers by HTML + CSS. The HTML represents a hierarchical structure; the CSS lets us encode rules to address elements of that structure and control the manner in which they are displayed.

In the example above, the outermost structure might be "Sum". Its "styling" includes choices like whether the terms within it should be ordered with rising or falling subscripts, and whether an ellipsis should be used, and if so how many terms should come before it.

Within the "Sum" structure are the individual "Term" structures. Within these would be a "Base" and "Subscript". Within some of the Subscripts in our example appears a "Variable" p. Among the "styling" modalities for a Variable would be the choice to appear by name ("p") or by value ("7" in this case).

Solution

Our solution begins with a Formal class, and a whole suite of subclasses representing the various kinds of structural elements suggested in our example, like sums, subscripts, variables, etc. This is the "HTML" side of our solution, i.e. the elements out of which to build the hierarchical structure that is a mathematical object.

As for the "styling" or "CSS" side of our solution, each Formal class has a format method, which accepts a mode and an optional rule set. The mode determines the manner of expression of this first element of the structure; it is like everything that goes between one pair of braces {} in CSS. The rule set allows us to define the modes for all the elements lying below the one on which the format method was called; it is like a whole CSS file.

Where CSS rules are defined using "selectors" based on ids, classes, and other properties, we instead ensure that each element of a formal structure has a path (a tree address), as well as an id, and our rules are based on regular expression matching of ids and/or paths.

Documentation

Sorry, at the moment proper docs are in the works.

A lot can be learned from the docstring of the format() method of each of the Formal___ classes in expressions.py and other modules.

Metadata

Release files for stylgebra 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stylgebra 0.1.0
File Size Uploaded Provenance Actions
stylgebra-0.1.0.tar.gz 41.4 kB Without provenance Details for stylgebra-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stylgebra 0.1.0
File Interpreter ABI Platform Actions
stylgebra-0.1.0-py3-none-any.whl Python 3 none any Details for stylgebra-0.1.0-py3-none-any.whl

Total release size: 81.6 kB

Release files / stylgebra-0.1.0.tar.gz

Download URL stylgebra-0.1.0.tar.gz
Size 41.4 kB
Tags Source
SHA-256 checksum
How to use checksums
359754229f4512fdf67768f55901228f2c2d5d1f9aa30e18a98871e81339d62b
BLAKE2b-256 checksum
How to use checksums
985bdc1caf0c7f17118b34ec01af592c5fea14f043e14fc1bda8cdec66396af0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.12

Release files / stylgebra-0.1.0-py3-none-any.whl

Download URL stylgebra-0.1.0-py3-none-any.whl
Size 40.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68232ea063d49b59c1ccbddc1a71a6e2f16e27520144e5cfab897114ac5e9bc6
BLAKE2b-256 checksum
How to use checksums
5babfb58441990c36a37579fa72b36fce6c5b41cef5a7fc97459a0f683c1e410
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.12

Security for stylgebra 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page