matlog
This module is well-documented with docstrings and type hints. Use help(matlog.tokens) or your editor hints to get familiar with it.
Basic usage:
from matlog import parse
expr = parse("A & B")
print(expr.solve(A=1, B=0)) # False
print(expr.solve(B=0)) # False
print(expr.table()) # prints the truth table of expression
Installation
Simply use pip install matlog or clone repo and launch setup.py
Contents
Expression string
An expression string is a bunch of tokens (atoms, expressions, operators and literals) in a specific order.
There's a big example of expression string:
a | (b -> c) == (~a ^ 1) <- ~z
Let's review in detail:
Atoms
Any letter is considered as an atom. Atoms can be only one letter long and must be divided by operators.
Literals
A literal is 1 or 0 (True and False respectively). Literals are treated like atoms.
Expressions
Any expression can contain nested expression strings in brackets. Nested expressions are treated like atoms.
Operators
matlog expression syntax supports 6 binary operators and one unary (listed in order of priority, from high to low):
- ~ (not / negation) unary: turns True to False and vice versa.
- & (and / conjunction): True if (and only if) both operands are True
- | (or / disjunction): True if (and only if) any operand is True.
- -> (implication): False if (and only if) the left operand is True but the right is False
- == (equality / biconditional): True if (and only if) operands are equal to each other
- ^ (xor / exclusive disjunction): False if (and only if) operands are equal to each other
- <- (converse implication): -> with reversed operands order
Expression
Expression class is the main class of the module. There are three ways to create an Expression object:
- from an expression string:
matlog.parse(expr_str) - copying an existing Expression:
expr.copy()(same asmatlog.Expression(expr.tokens)) - deep-copying an existing Expression object:
expr.deep_copy() - constructing an Expression from tokens (module won't check if it is valid in this case):
matlog.Expression([token1, token2...])
Truth tables
You can use expr.table(keep=None) method to build a truth table for an Expression object.
keep parameter can be either 1 or 0 (to filter rows with corresponding values) or None if you need a full table
Evaluating expression
If you need a value for a specific set of values, you can use .solve() method like this:
from matlog import parse
expr = parse("A & B | C")
print(expr.solve(A=1, B=0, C=1)) # prints 1
print(expr.solve({"A": 1, "B": 0, "C": 1})) # you can pass a dictionary too
if you need a result value (release version would provide more convenient ways, of course):
expr.value(A=1, B=0, C=1) # 1 (raises an exception if there's not enough data to solve expression)
Partial evaluation
If you know some (but not all) values, you can also use .solve(), providing some values:
from matlog import parse
expr = parse("A & B | C")
print(expr.solve(B=0)) # prints C
print(expr.solve({"B": 0})) # prints C too
Simplify
Smart (smarter than watermelon!) algorithm, simplifies expressions better than bare .solve().
This method recursively simplifies complex expressions, so expect it to work slower than (again) bare .solve().
from matlog import parse
expr = parse("(A | B | C) & ~(A | ~B | C)")
print(expr.simplify()) # prints ~(A | ~B | C)
Equality check
If you're wondering if expressions are equal (producing the same results with any set of values), you can use Expression.equals(self, other) method:
from matlog import parse
expr1 = parse("A & B")
expr2 = parse("B & A")
print(expr1.equals(expr2)) # True
Metadata
Release files for matlog 2.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| matlog-2.3.1.tar.gz | 23.7 kB | Without provenance | Details for matlog-2.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| matlog-2.3.1-py3-none-any.whl | Python 3 | none | any | Details for matlog-2.3.1-py3-none-any.whl |
Total release size: 46.0 kB
Release files / matlog-2.3.1.tar.gz
| Download URL | matlog-2.3.1.tar.gz |
|---|---|
| Size | 23.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea186a8c61bce96798ff08a6d2a069a6fc3492e04f7f1e1383614fbf816db3d4
|
|
BLAKE2b-256 checksum How to use checksums |
f58ce16899ff6c04b1bdb22d5c19dc79c6a422e9d2e68cc3144a3c10900241f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
|
Release files / matlog-2.3.1-py3-none-any.whl
| Download URL | matlog-2.3.1-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb5e2aa5e3dc51179d5ec18c57e8f2e6eea2c4278fedbe63d3cc72a5d7de22bf
|
|
BLAKE2b-256 checksum How to use checksums |
3e2cab8538ae04be9298005a0ada8903e4f1907136fb1ab6c532f620e74c2910
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
|
Security for matlog 2.3.1
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.