Stylable algebraic expressions
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file stylgebra-0.1.0.tar.gz
.
File metadata
- Download URL: stylgebra-0.1.0.tar.gz
- Upload date:
- Size: 41.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 359754229f4512fdf67768f55901228f2c2d5d1f9aa30e18a98871e81339d62b |
|
MD5 | 8610ef36797ae4cad63db9123aff642f |
|
BLAKE2b-256 | 985bdc1caf0c7f17118b34ec01af592c5fea14f043e14fc1bda8cdec66396af0 |
File details
Details for the file stylgebra-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: stylgebra-0.1.0-py3-none-any.whl
- Upload date:
- Size: 40.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68232ea063d49b59c1ccbddc1a71a6e2f16e27520144e5cfab897114ac5e9bc6 |
|
MD5 | bdacbee352115f7a10266064683b077f |
|
BLAKE2b-256 | 5babfb58441990c36a37579fa72b36fce6c5b41cef5a7fc97459a0f683c1e410 |