Modular arithmetic

This Jupyter notebook provides an example of using the Python package gravis. The .ipynb file can be found here.

It demonstrates how calculations with modular arithmetic can be visualized as a directed graph.

References

Data generation

Create a directed graph where

  • nodes represent integers modulo p, i.e. the members x of a finite field

  • edges represent the relationship y = x^k (modulo p) where x is the source and y the target. This means the target is the k-th power of x.

[1]:
import gravis as gv
import networkx as nx
[2]:
def generate_digraph(p, k):
    # Generate the graph
    graph = nx.DiGraph()
    solutions = set()
    for x in range(p):
        y = (x ** k) % p
        solutions.add(y)
        graph.add_edge(x, y)
    print('Finite field with {p} elements. Found a set of {n} unique solutions '
          'for the function y = x^{k} % {p}.'.format(k=k, p=p, n=len(solutions)))

    # Assign node properties: size and color by indegree
    for i in graph.nodes:
        node = graph.nodes[i]
        node['size'] = 5 + graph.in_degree(i) * 3
        node['color'] = 'red' if graph.in_degree[i] > 0 else 'black'
        node['hover'] = ''

    # Assign edge properties: label and label color
    for i, j in graph.edges:
        edge = graph.edges[(i, j)]
        node = graph.nodes[j]
        division_text = '{x}^{k} % {p} = {y}'.format(x=i, y=j, k=k, p=p)
        edge['label'] = division_text
        node['hover'] += division_text + '\n'
        edge['label_color'] = 'green'
    return graph

Visualization

[3]:
def plot_digraph(digraph):
    fig = gv.d3(
        digraph,
        node_hover_neighborhood=True,
        show_edge_label=True,
        edge_label_data_source='label',
        edge_label_size_factor=0.8,
    )
    return fig

Integers modulo p=17

  • Formula used to determine edges: y = x^2 % 17

  • Source node: x (=any integer of the field as input)

  • Target node: y (=some integer of the field as output, due to closure under multiplication)

[4]:
dg = generate_digraph(p=17, k=2)
plot_digraph(dg)
Finite field with 17 elements. Found a set of 9 unique solutions for the function y = x^2 % 17.
[4]:
Details for selected element
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force

Integers modulo p=23

[5]:
dg = generate_digraph(p=23, k=2)
plot_digraph(dg)
Finite field with 23 elements. Found a set of 12 unique solutions for the function y = x^2 % 23.
[5]:
Details for selected element
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force

Integers modulo p=71

[6]:
dg = generate_digraph(p=71, k=2)
plot_digraph(dg)
Finite field with 71 elements. Found a set of 36 unique solutions for the function y = x^2 % 71.
[6]:
Details for selected element
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force

Integers modulo p=73

[7]:
dg = generate_digraph(p=73, k=2)
plot_digraph(dg)
Finite field with 73 elements. Found a set of 37 unique solutions for the function y = x^2 % 73.
[7]:
Details for selected element
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force

Integers modulo p=321

[8]:
dg = generate_digraph(p=321, k=2)
plot_digraph(dg)
Finite field with 321 elements. Found a set of 108 unique solutions for the function y = x^2 % 321.
[8]:
Details for selected element
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force