Skip to main content

An efficient python library for working with and manipulating binary relations.

Project description

Pyrel is an efficient python library for working with and manipulating binary relations. Pyrel exposes the C library Kure which provides a very fast implementation of binary relations based on Binary Decision Diagrams.

The following operations of relation algebra are provided:

  • empty

  • universal

  • identity

  • meet (intersection)

  • join (union)

  • transpose (converse)

  • complement (negation)

  • compose (multiplication)

  • equals

  • isSuperset

  • isSubset

  • isStrictSuperset

  • isStrictSubset

Installation

using pip:

pip3 install pyrel

or downloading with git:

git clone https://github.com/Peter-Roger/pyrel.git
cd pyrel/
python3 setup.py build install

Requirements

Pyrel works on Ubuntu and macOS. It has not been tested on other systems but it is possible it could work, perhaps with some modification.

C Library Dependencies:

  • glib-2.0

  • gmp

If you are missing these libraries just install them with your package manager.

Pyrel includes a modified version of Kure and its dependency Cudd-2.5.1 (Colorado University Decision Diagram) as source. As long as the aforementioned dependencies are installed in standard locations on your system, Kure will be built automatically during installation.

Quick Start

The following provides some examples as a tutorial on how to use pyrel. For a complete description you should consult the documented module source code.

Relations are represented as boolean matrices and can be visualised by printing them. An ‘X’ at col x and row y denotes that x is related to y in the relation. Contrariwise a ‘.’ denotes that x is not related to y.

Creating relations

import pyrel

# create a pyrel context
context = pyrel.PyrelContext()

# create a new 3x3 empty relation
rel = context.new(3,3)
print(rel)
...
...
...

Setting bits

# a list of ordered pairs
bits = [(0,0),(0,1),(0,2)]
rel = context.new(3,3)
rel.set_bits(bits)
print(rel)

# set bits at random
rel.random()
print(rel)

# unsets all bits
rel.clear()
print(rel)
XXX
...
...

.X.
..X
X.X

...
...
...
# set bits at creation
bits = [(0,0),(0,1),(0,2)]
rel = context.new(3,3,bits)
print(rel)

# set single bit
rel.set_bit(2,2)
print(rel)

# unset bit
rel.set_bit(0,1,yesno=False)
print(rel)

# unset bits
rel.set_bits([(0,0),(2,2)],yesno=False)
print(rel)
XXX
...
...

XXX
...
..X

X.X
...
..X

..X
...
...

Operations

rel = context.new(3,3).identity()
print(rel)

r = context.new(3,3, [(0,0),(0,1),(0,2)])
print(r)

s = r.transpose()
print(s)

m = r1.meet(r2)
print(m)

j = r1.join(r2)
print(j)
X..
.X.
..X

XXX
...
...

X..
X..
X..

X..
...
...

XXX
X..
X..
r = context.new(3,3, [(0,1),(0,2),(2,1)])
print(r)

s = context.new(3,3, [(1,1),(2,2)])
print(s)

g = r.composition(s)
print(g)

g.isSubset(g.universal())
.XX
...
.X.

...
.X.
..X

.XX
...
.X.

>>> True

Vectors

A vector is a row constant relation. All columns are identical. It represents a subset.

rel = new(5,5)
rel.vector(2) # row 2 (0-indexed)
print(rel)
rel.vector_next()
print(rel)
.....
.....
XXXXX
.....
.....

.....
.....
.....
XXXXX
.....

Possible Future Work

  • import relations from a file

  • export relations to a file

  • extend support for more relation operations

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

pyrel-0.2.0.tar.gz (2.9 MB view details)

Uploaded Source

File details

Details for the file pyrel-0.2.0.tar.gz.

File metadata

  • Download URL: pyrel-0.2.0.tar.gz
  • Upload date:
  • Size: 2.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for pyrel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0619806cfdaa5dec6de36dfd21ba6aeecd95ed07616449da1a70b8c7798dc3f3
MD5 676e19172c0ee8cb1872b06b82ebdcd6
BLAKE2b-256 d4a16ad8134086b376c40ea3ec590cfdda2754bfa73a0d706c7e569251719505

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