• Stars
    star
    35
  • Rank 726,001 (Top 15 %)
  • Language
    Julia
  • License
    Other
  • Created almost 10 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bijection datatype for Julia.

Bijections

A Bijection data type for Julia.

A Dict in Julia is not one-to-one. Two different keys might have the same value. This data structure behaves just like a Dict except it blocks assigning the same value to two different keys.

Getting Started

After using Bijections we create a new Bijection in one of the following ways:

  • b = Bijection(): This gives a new Bijection in which the keys and values are of Any type.

  • b = Bijection{S,T}(): This gives a new Bijection in which the keys are of type S and the values are of type T.

  • b = Bijection(x,y): This gives a new Bijection in which the keys are type typeof(x), the values are type typeof(y) and the key-value pair (x,y) is inserted into the Bijection.

  • b = Bijection(dict::AbstractDict{S, T}): This gives a new Bijection in which the keys are type S, the values are type T and all key-value pairs in dict are inserted into the Bijection.

  • b = Bijection(pair_list::Vector{Pair{S, T}}): Create a new Bijection using a list of pairs.

Adding and deleting pairs

Once a Bijection, b, is created, we add a new key-value pair in the same manner as with a Dict:

julia> b[1] = "hello"
"hello"

julia> b[2] = "bye"
"bye"

Notice, however, that if we add a new key with a value that already exists in the Bijection an error ensues:

julia> b = Bijection{Int, String}()
Bijection Dict{Int64, String}()

julia> b[3] = "hello"
ERROR: One of x or y already in this Bijection

Likewise, if a key already has a value it cannot be changed by giving it a new value:

julia> b[1] = "ciao"
ERROR: One of x or y already in this Bijection

If we wish to change the value associated with a given key, the pair must first be deleted using delete!:

julia> delete!(b,1)
Bijection Dict{Int64, String} with 1 entry:
  2 => "bye"

julia> b[1] = "ciao"
"ciao"

Using a Bijection

To access a value associated with a given key, we use the same syntax as for a Dict:

julia> b[1]
"ciao"

julia> b[2]
"bye"

If the key is not in the Bijection an error is raised:

julia> b[3]
ERROR: KeyError: 3 not found

Since the values in a Bijection must be distinct, we can give a value as an input and retrieve its associate key. The function inverse(b,y) finds the value x such that b[x]==y. However, we provide the handy short cut b(y):

julia> b("bye")
2

julia> b("ciao")
1

Naturally, if the requested value is not in the Bijection an error is raised:

julia> b("hello")
ERROR: KeyError: hello not found

Creating an inverse Bijection

There are two functions that take a Bijection and return a new Bijection that is the functional inverse of the original: inv and active_inv.

Independent inverse: inv

Given a Bijection b, calling inv(b) creates a new Bijection that is the inverse of b. The new Bijection is completely independent of the original, b. Changes to one do not affect the other:

julia> b = Bijection{Int,String}()
Bijection Dict{Int64, String}()

julia> b[1] = "alpha"
"alpha"

julia> b[2] = "beta"
"beta"

julia> bb = inv(b)
Bijection Dict{String, Int64} with 2 entries:
  "alpha" => 1
  "beta"  => 2

julia> bb["alpha"]
1

julia> bb["alpha"]
1

julia> b[3] = "gamma"
"gamma"

julia> bb["gamma"]
ERROR: KeyError: key "gamma" not found

Active inverse: active_inv

The active_inv function also creates an inverse Bijection, but in this case the original and the inverse are actively tied together. That is, modification of one immediately affects the other. The two Bijections remain inverses no matter how either is modified.

julia> b = Bijection{Int,String}()
Bijection Dict{Int64, String}()

julia> b[1] = "alpha"
"alpha"

julia> b[2] = "beta"
"beta"

julia> bb = active_inv(b)
Bijection Dict{String, Int64} with 2 entries:
  "alpha" => 1
  "beta"  => 2
  
julia> b[3] = "gamma"
"gamma"

julia> bb["gamma"]
3

Iteration

Bijections can be used in a for statement just like Julia dictionaries:

julia> for (x,y) in b; println("$x --> $y"); end
2 --> beta
3 --> gamma
1 --> alpha

Inspection

Thinking of a Bijection as a mapping between finite sets, we provide the functions domain and image. These return, respectively, the set of keys and the set of values of the Bijection.

julia> domain(b)
Set(Any[2,1])

julia> image(b)
Set(Any["bye","ciao"])

The collect function returns the Bijection as an array of key-value pairs:

julia> collect(b)
2-element Array{Tuple{Any,Any},1}:
 (2,"bye")
 (1,"ciao")

The length function returns the number of key-value pairs:

julia> length(b)
2

The isempty function returns true exactly when the Bijection contains no pairs:

julia> isempty(b)
false

Composition

Given two Bijections a and b, their composition c = a*b is a new Bijection with the property that c[x] = a[b[x]] for all x in the domain of b.

julia> a = Bijection{Int,Int}(); a[1] = 10; a[2] = 20;

julia> b = Bijection{String,Int}(); b["hi"] = 1; b["bye"] = 2;

julia> c = a * b;

julia> c["hi"]
10

More Repositories

1

LatexPrint.jl

Print Julia objects in a form suitable for LaTeX mathematics mode.
Julia
70
star
2

Permutations.jl

Permutations class for Julia.
Julia
52
star
3

LinearAlgebraX.jl

Exact linear algebra functions
Julia
39
star
4

SimpleGraphs.jl

Convenient way to handle simple graphs and digraphs
Julia
37
star
5

InvitationToDynamicalSystems

Download my book "Invitation to Dynamical Systems" and its solution manual.
MATLAB
34
star
6

Mods.jl

Easy modular arithmetic for Julia
Julia
30
star
7

Multisets.jl

Finite multisets in Julia
Julia
15
star
8

matgraph

Matlab tools for working with simple graphs
HTML
13
star
9

SimpleGraphAlgorithms.jl

Additional algorithms for the `SimpleGraphs` module that rely on integer programming
Julia
11
star
10

SampleMathPaper

A sample mathematics paper to illustrate basic ideas in LaTeX
TeX
11
star
11

SimplePosets.jl

Simple partially ordered sets for Julia
Julia
10
star
12

RiemannComplexNumbers.jl

Reimplemented complex arithmetic in Julia with a single infinity and NaN.
Julia
9
star
13

DrawSimpleGraphs.jl

Drawing functions for `SimpleGraphs`
Julia
6
star
14

SimpleWorld.jl

Package to load all my other "Simple" packages
Julia
6
star
15

RationalGenerators.jl

Iterate positive rational numbers without repetition
Julia
5
star
16

SimplePolynomials.jl

Basic polynomials with exact coefficients
Julia
5
star
17

Sudoku.jl

Solve Sudoku puzzles using integer programming
Julia
5
star
18

SimpleTropical.jl

Julia implementation of tropical arithmetic
Julia
4
star
19

Counters.jl

Count things easily.
Julia
4
star
20

Mazes.jl

Create grid mazes
Julia
4
star
21

ImplicitGraphs.jl

Implicitly defined graphs (possibly infinite)
Julia
4
star
22

PythagoreanTriples.jl

Pythagorean Triples
Julia
4
star
23

ClosedIntervals.jl

Closed intervals of the form [a,b].
Julia
4
star
24

SimpleDrawing.jl

Convenient drawing tools derived from Plots
Julia
4
star
25

HyperbolicPlane.jl

Implementation of basic hyperbolic geometry (Poincare disc model)
Julia
3
star
26

Cplusplus-For-Mathematicians-Code

C
3
star
27

SimplePosetAlgorithms.jl

Additional algorithms for the SimplePoset type.
Julia
3
star
28

LatinSquares.jl

Creating Latin squares and pairs of orthogonal Latin squares
Julia
3
star
29

SimpleSolver.jl

Easy interface for solving equations
Julia
3
star
30

RingLists.jl

Circular lists
Julia
3
star
31

ChooseOptimizer.jl

Tool to select different optimization engines and set options.
Julia
3
star
32

SimplePartitions.jl

Module for set partitions
Julia
3
star
33

QuadraticRationals.jl

Numbers in a simple quadratic extension of the rational numbers
Julia
2
star
34

SimpleLife.jl

Conway's Game of Life
Julia
2
star
35

Spirograph.jl

Julia implementation of a spirograph
Julia
2
star
36

SimpleGF2.jl

An implementation of arithmetic in GF(2)
Julia
2
star
37

BigCombinatorics.jl

Combinatorial functions that always return a BigInt
Julia
2
star
38

SimpleQuantum.jl

Learning about qubits, registers, and gates.
Julia
2
star
39

ShowSet.jl

Nicer output for Set and BitSet objects in Julia.
Julia
2
star
40

Diodes.jl

Julia code for resistor-diode networks
Julia
2
star
41

DiscreteFunctions.jl

Functions to/from {1,2,...,n}
Julia
2
star
42

BalancedIncompleteBlockDesigns.jl

Use integer programming to create balanced incomplete block designs.
Julia
2
star
43

AbstractLattices.jl

Abstract lattice functions meet and join, with symbols \wedge and \vee
Julia
2
star
44

LinearFractionalTransformations.jl

Linear fractional transformations of the (extended) complex plane.
Julia
2
star
45

SimplePlanarGraphs.jl

Experimental code for planarity
Julia
2
star
46

Misc.jl

Uncategorized, but hopefully useful, Julia code.
Julia
2
star
47

PlayingCards52.jl

Standard deck of 52 playing cards
Julia
2
star
48

SimplePosetDrawings.jl

Julia module for drawing Hasse diagrams of partially ordered sets.
Julia
2
star
49

SimpleGraphConverter.jl

Convert between graphs defined in Graphs and SimpleGraphs
Julia
2
star
50

Clines.jl

Circles and lines in the plane
Julia
1
star
51

HyperbolicDrawSimpleGraphs.jl

Drawing graphs in the hyperbolic plane
Julia
1
star
52

War.jl

Computer plays the card game war with itself
Julia
1
star
53

SimplePosetRepresentations.jl

Random posets, interval orders, and the like.
Julia
1
star
54

DiceGame.jl

Analysis of dice games
Julia
1
star
55

kMeans.jl

`k`-means clustering
Julia
1
star
56

SimpleRandom.jl

Collection of Julia functions to make random things
Julia
1
star
57

WordGraphs.jl

Graphs whose vertices are words.
Julia
1
star
58

Factorions.jl

Code to hunt for factorions in any base
Julia
1
star
59

TwentyFour.jl

Julia code to solve Twenty Four puzzles
Julia
1
star
60

SimpleGraphRepresentations.jl

Extension of SimpleGraphs containing methods for dealing with intersection graphs and the like
Julia
1
star
61

FlexLinearAlgebra.jl

Flexible vectors and matrices for Julia
Julia
1
star
62

FreeCell.jl

Exercise to create a program to play FreeCell
Julia
1
star
63

SpellingBee.jl

Solve NY Times Spelling Bee puzzles
Julia
1
star
64

IntPrint.jl

Convert Julia integers to strings with separators every three digits
Julia
1
star
65

Circles.jl

This module is superseded by Clines. See that for all the functionality that used to be here (and more).
Julia
1
star
66

kMeansExtras.jl

Extra functions to support `kMeans`
Julia
1
star
67

SimpleTools.jl

Miscellaneous code useful for my SimpleWorld
Julia
1
star
68

HalfSine.jl

Reopen investigation into a function `f` such that `f(f(x)) == sin(x)`.
Julia
1
star
69

CoinRepresentations.jl

Coin Representations of 3-Connected Planar Graphs
Julia
1
star
70

SimpleQuaternions.jl

Basic implementation of Hamilton's quaternions
Julia
1
star
71

WordleSolver.jl

Solve Wordle puzzles interactively.
Julia
1
star
72

BulletsMaggots.jl

The Bullets and Maggots number guessing game
Julia
1
star
73

Fractory.jl

Simple fractals in the unit square
Julia
1
star
74

SimplePadics.jl

Nicely formatted p-adic numbers.
Julia
1
star
75

DigitsSolver.jl

Solve Digits puzzles in the New York Times
Julia
1
star
76

RationalProjectivePlane.jl

Points and lines in the projective plane (with rational homogenous coordinates)
Julia
1
star
77

SemiIsomorphism.jl

Research code for looking at semi-isomophisms of graphs
Julia
1
star
78

SetOps.jl

Easy set operations for Julia
Julia
1
star