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 hashes)

Uploaded Source

Supported by

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