Working with Packages


Starting Julia loads Julia kernel and Base module. The Base (core) is kept small and all other functionality is accessible through packages which need to be individually included by the user.

Currently there are 1500+ registered packages listed at Julia Package Listing.

In this notebook, we demonstrate how to use packages.

Prerequisites

Read section Packages of the Julia manual (15 min).

Competences

The reader should be able to install and use registered and unregistered packages and create own packages.

Credits

Some examples are taken from The Julia Manual.


Pkg.status()


In [1]:
?Pkg.status()


Out[1]:
status()

Prints out a summary of what packages are installed and what version and state they're in.


In [2]:
# This is slow due to communication with GitHub
Pkg.status()


14 required packages:
 - Arrowhead                     0.0.1+             master
 - Gadfly                        0.6.5
 - GraphPlot                     0.2.0
 - GraphViz                      0.1.1              master
 - Graphs                        0.9.0
 - IJulia                        1.7.0
 - IJuliaPortrayals              0.0.4
 - Interact                      0.7.1
 - LightGraphs                   0.12.0
 - MetaGraphs                    0.4.0
 - SimpleWeightedGraphs          0.4.0
 - SpecialMatrices               0.1.3+             master
 - ToeplitzMatrices              0.3.0
 - Winston                       0.13.2
77 additional packages:
 - AbstractFFTs                  0.3.1
 - AxisAlgorithms                0.2.0
 - BinDeps                       0.8.7              master
 - BinaryProvider                0.2.8
 - BufferedStreams               0.4.0
 - Cairo                         0.5.1+             master (dirty)
 - Calculus                      0.2.2
 - CodecZlib                     0.4.3
 - ColorTypes                    0.6.7
 - Colors                        0.8.2
 - CommonSubexpressions          0.1.0
 - Compat                        0.63.0
 - Compose                       0.5.5
 - Conda                         0.7.1
 - Contour                       0.4.0
 - CoupledFields                 0.0.1
 - DataArrays                    0.6.2
 - DataFrames                    0.10.1
 - DataStructures                0.7.4
 - DiffEqDiffTools               0.4.0
 - DiffResults                   0.0.3
 - DiffRules                     0.0.4
 - Distances                     0.6.0
 - Distributions                 0.15.0
 - DoubleDouble                  0.2.1
 - DualNumbers                   0.3.0
 - FFTW                          0.0.4
 - FileIO                        0.7.0
 - FixedPointNumbers             0.4.6
 - ForwardDiff                   0.7.5
 - GZip                          0.3.0
 - Graphics                      0.2.0
 - Hexagons                      0.1.0
 - IniFile                       0.4.0
 - Interpolations                0.7.3
 - IterTools                     0.2.1
 - JLD2                          0.0.5
 - JSON                          0.17.2
 - Juno                          0.4.0
 - KernelDensity                 0.4.1
 - Libz                          0.2.4
 - LineSearches                  3.2.5
 - LinearAlgebra                 0.0.0-             master (unregistered)
 - Loess                         0.3.0
 - MacroTools                    0.4.0
 - MatrixDepot                   0.6.0
 - MbedTLS                       0.5.8
 - Measures                      0.2.0
 - Media                         0.3.0
 - Missings                      0.2.9
 - NLSolversBase                 4.4.1
 - NaNMath                       0.3.1
 - Nullables                     0.0.5
 - Optim                         0.14.1
 - PDMats                        0.8.0
 - Parameters                    0.8.1
 - Polynomials                   0.2.2
 - PositiveFactorizations        0.1.0
 - QuadGK                        0.2.1
 - Ratios                        0.2.0
 - Reactive                      0.6.0
 - Reexport                      0.1.0
 - Rmath                         0.3.2
 - SHA                           0.5.6
 - ShowItLikeYouBuildIt          0.2.0
 - Showoff                       0.1.1
 - SimpleTraits                  0.6.0
 - SortingAlgorithms             0.2.0
 - SpecialFunctions              0.4.0
 - StaticArrays                  0.7.0
 - StatsBase                     0.20.1
 - StatsFuns                     0.5.0
 - Tk                            0.5.2
 - TranscodingStreams            0.5.2
 - URIParser                     0.3.1
 - WoodburyMatrices              0.2.2
 - ZMQ                           0.5.1

Pkg.add()

This command adds registered package from Julia Package Listing. Adding the package downloads the package source code (and all other required packages) to your .julia/v0.6/ directory.

GitHub repository names of registered Julia packages always end with the extension .jl, which is ommited in Pkg.add() command. The example below installs the package from the GitHub repository https://github.com/JuliaLang/LightGraphs.jl.

N.B. There are other registered packages dealing with graphs, please check them out.


In [3]:
?Pkg.add


Out[3]:
add(pkg, vers...)

Add a requirement entry for pkg to Pkg.dir("REQUIRE") and call Pkg.resolve(). If vers are given, they must be VersionNumber objects and they specify acceptable version intervals for pkg.


In [4]:
# Pkg.add("LightGraphs")

In [5]:
# This is Julia's default directory
# Windows version:  a=readdir("/Users/Ivan/.julia/v0.6")
a=readdir("/home/slap/.julia/v0.6")


Out[5]:
96-element Array{String,1}:
 "AbstractFFTs"        
 "Arrowhead"           
 "AxisAlgorithms"      
 "BinaryProvider"      
 "BinDeps"             
 "BufferedStreams"     
 ".cache"              
 "Cairo"               
 "Calculus"            
 "CodecZlib"           
 "Colors"              
 "ColorTypes"          
 "CommonSubexpressions"
 ⋮                     
 "SpecialMatrices"     
 "StaticArrays"        
 "StatsBase"           
 "StatsFuns"           
 "Tk"                  
 "ToeplitzMatrices"    
 "TranscodingStreams"  
 ".trash"              
 "URIParser"           
 "Winston"             
 "WoodburyMatrices"    
 "ZMQ"                 

In [6]:
println(a)


String["AbstractFFTs", "Arrowhead", "AxisAlgorithms", "BinaryProvider", "BinDeps", "BufferedStreams", ".cache", "Cairo", "Calculus", "CodecZlib", "Colors", "ColorTypes", "CommonSubexpressions", "Compat", "Compose", "Conda", "Contour", "CoupledFields", "DataArrays", "DataFrames", "DataStructures", "DiffEqDiffTools", "DiffResults", "DiffRules", "Distances", "Distributions", "DoubleDouble", "DualNumbers", "FFTW", "FileIO", "FixedPointNumbers", "ForwardDiff", "Gadfly", "Graphics", "GraphPlot", "Graphs", "GraphViz", "GZip", "Hexagons", "IJulia", "IJuliaPortrayals", "IniFile", "Interact", "Interpolations", "IterTools", "JLD2", "JSON", "Juno", "KernelDensity", "Libz", "LightGraphs", "LinearAlgebra", "LineSearches", "Loess", "MacroTools", "MatrixDepot", "MbedTLS", "Measures", "Media", "META_BRANCH", "METADATA", "MetaGraphs", "Missings", "NaNMath", "NLSolversBase", "Nullables", "Optim", "Parameters", "PDMats", "Polynomials", "PositiveFactorizations", "QuadGK", "Ratios", "Reactive", "Reexport", "REQUIRE", "Rmath", "SHA", "ShowItLikeYouBuildIt", "Showoff", "SimpleTraits", "SimpleWeightedGraphs", "SortingAlgorithms", "SpecialFunctions", "SpecialMatrices", "StaticArrays", "StatsBase", "StatsFuns", "Tk", "ToeplitzMatrices", "TranscodingStreams", ".trash", "URIParser", "Winston", "WoodburyMatrices", "ZMQ"]

Contents of a package

We now have directory /Users/Ivan/.julia/v0.6/LightGraphs. Let us examine its content (this can also be done directly from the GitHub repository https://github.com/JuliaLang/LightGraphs.jl).

Files

Each package has the following three files:

  • REQUIRE
    • may contain the version of Julia needed for the package to run
    • must contain all other registered packages that the present package is using (these packages are installed automatically, if not present) and
    • may contain the version of those packages.
  • README.md is the Markdown file, which contains the descritption of the package as displayed on the repository's home page.
  • LICENSE.md contains the licensing information.

The file travis.yml, if present, defines how is the package tested on Travis-CI after every posted change (via git push command). Details on using Travis-CI for Julia projects are at https://docs.travis-ci.com/user/languages/julia. Since testing is done on machines other than yours, with operating systems other than yours, and using Julia version which may differ from yours, this is a great way to correct bugs, and also a way to give users examples of how to run your code.

Directories

The src/ directory contains the actual code of your package.

It must contan the file named as the package itself, src/LightGraphs.jl in this case, which containd the following:

  • module line starts the description of the main module, which has the same name as the package,
  • using line(s) lists other registered packages used by the package. These packages are also listed in the REQUIRE file.
  • import line lists the other modules and their components which are modified in this module
  • export line lists all component which will be accessible directly in the main namespace. The components which are not exported, can still be used but the full name (including mogule name) must be used
  • include() commands include the source files
  • end concludes the description of the module.

If Travis-CI is used, the test/ directory contains the file runtests.jl which is exaceuted during the testing, and, eventually, other files that this file is calling.

The doc/ is optional and is used to store documentation.

The deps/ directory is optional and is used to store dependencies if the package is using software written in other languages. There are many examples which can be checked out.

using and import

Package needs to be added only once, prior to the first use. We are now ready to use the package.

We have two methods to do so, which differ in their treatment of the namespace:

  • using adds all methods, constructors etc. from the package into the main namespace, so they can be called directly, like the function add_edge!() below.
  • import enables us to use all the methods, constructors, etc. from the package, but they are not included in the namespace, so they must be called together with the package name, LightGraphs.add_edge!(4).

N.B. import can also be used on a particular function(s), as we shall explain later.


In [7]:
using LightGraphs


INFO: Recompiling stale cache file /home/slap/.julia/lib/v0.6/CodecZlib.ji for module CodecZlib.
INFO: Recompiling stale cache file /home/slap/.julia/lib/v0.6/LightGraphs.ji for module LightGraphs.

In [8]:
whos(LightGraphs)


                  AbstractEdge     40 bytes  UnionAll
              AbstractEdgeIter     92 bytes  DataType
                 AbstractGraph     40 bytes  UnionAll
                    BinaryTree      0 bytes  LightGraphs.#BinaryTree
          BoundedMinkowskiCost      0 bytes  LightGraphs.#BoundedMinkowskiCost
                   CliqueGraph      0 bytes  LightGraphs.#CliqueGraph
        CompleteBipartiteGraph      0 bytes  LightGraphs.#CompleteBipartiteGraph
               CompleteDiGraph      0 bytes  LightGraphs.#CompleteDiGraph
                 CompleteGraph      0 bytes  LightGraphs.#CompleteGraph
                  CycleDiGraph      0 bytes  LightGraphs.#CycleDiGraph
                    CycleGraph      0 bytes  LightGraphs.#CycleGraph
                       DiGraph     40 bytes  UnionAll
              DoubleBinaryTree      0 bytes  LightGraphs.#DoubleBinaryTree
                          Edge     40 bytes  UnionAll
                         Graph     40 bytes  UnionAll
                          Grid      0 bytes  LightGraphs.#Grid
                      LGFormat     92 bytes  DataType
           LevelSynchronousBFS     92 bytes  DataType
                   LightGraphs    986 KB     Module
                 MinkowskiCost      0 bytes  LightGraphs.#MinkowskiCost
               Nonbacktracking     80 bytes  UnionAll
                   PathDiGraph      0 bytes  LightGraphs.#PathDiGraph
                     PathGraph      0 bytes  LightGraphs.#PathGraph
                    RoachGraph      0 bytes  LightGraphs.#RoachGraph
                 SimpleDiGraph     40 bytes  UnionAll
                   SimpleGraph     40 bytes  UnionAll
                   StarDiGraph      0 bytes  LightGraphs.#StarDiGraph
                     StarGraph      0 bytes  LightGraphs.#StarGraph
          StochasticBlockModel     80 bytes  UnionAll
                  WheelDiGraph      0 bytes  LightGraphs.#WheelDiGraph
                    WheelGraph      0 bytes  LightGraphs.#WheelGraph
                        a_star      0 bytes  LightGraphs.#a_star
                     add_edge!      0 bytes  LightGraphs.#add_edge!
                   add_vertex!      0 bytes  LightGraphs.#add_vertex!
                 add_vertices!      0 bytes  LightGraphs.#add_vertices!
              adjacency_matrix      0 bytes  LightGraphs.#adjacency_matrix
            adjacency_spectrum      0 bytes  LightGraphs.LinAlg.#adjacency_spec…
                 all_neighbors      0 bytes  LightGraphs.#all_neighbors
                  articulation      0 bytes  LightGraphs.#articulation
         attracting_components      0 bytes  LightGraphs.#attracting_components
               barabasi_albert      0 bytes  LightGraphs.#barabasi_albert
              barabasi_albert!      0 bytes  LightGraphs.#barabasi_albert!
   bellman_ford_shortest_paths      0 bytes  LightGraphs.#bellman_ford_shortest…
        betweenness_centrality      0 bytes  LightGraphs.#betweenness_centrality
                   bfs_parents      0 bytes  LightGraphs.#bfs_parents
                      bfs_tree      0 bytes  LightGraphs.#bfs_tree
        biconnected_components      0 bytes  LightGraphs.#biconnected_components
                 bipartite_map      0 bytes  LightGraphs.#bipartite_map
                       blkdiag      0 bytes  Base.SparseArrays.#blkdiag
                   blockcounts      0 bytes  LightGraphs.#blockcounts
                blockfractions      0 bytes  LightGraphs.#blockfractions
             cartesian_product      0 bytes  LightGraphs.#cartesian_product
                        center      0 bytes  LightGraphs.#center
            clique_percolation      0 bytes  LightGraphs.#clique_percolation
          closeness_centrality      0 bytes  LightGraphs.#closeness_centrality
              common_neighbors      0 bytes  LightGraphs.#common_neighbors
                    complement      0 bytes  LightGraphs.#complement
                  condensation      0 bytes  LightGraphs.#condensation
          connected_components      0 bytes  LightGraphs.#connected_components
                      contract      0 bytes  LightGraphs.LinAlg.#contract
                   core_number      0 bytes  LightGraphs.#core_number
            core_periphery_deg      0 bytes  LightGraphs.#core_periphery_deg
                     crosspath      0 bytes  LightGraphs.#crosspath
                        degree      0 bytes  LightGraphs.#degree
             degree_centrality      0 bytes  LightGraphs.#degree_centrality
              degree_histogram      0 bytes  LightGraphs.#degree_histogram
                       density      0 bytes  LightGraphs.#density
                      dfs_tree      0 bytes  LightGraphs.#dfs_tree
                      diameter      0 bytes  LightGraphs.#diameter
                    difference      0 bytes  LightGraphs.#difference
                     diffusion      0 bytes  LightGraphs.#diffusion
                diffusion_rate      0 bytes  LightGraphs.#diffusion_rate
       dijkstra_shortest_paths      0 bytes  LightGraphs.#dijkstra_shortest_pat…
                           dst      0 bytes  LightGraphs.#dst
                  eccentricity      0 bytes  LightGraphs.#eccentricity
                         edges      0 bytes  LightGraphs.#edges
                      edgetype      0 bytes  LightGraphs.#edgetype
                 edit_distance      0 bytes  LightGraphs.#edit_distance
                        egonet      0 bytes  LightGraphs.#egonet
        eigenvector_centrality      0 bytes  LightGraphs.#eigenvector_centrality
               enumerate_paths      0 bytes  LightGraphs.#enumerate_paths
                   erdos_renyi      0 bytes  LightGraphs.#erdos_renyi
               euclidean_graph      0 bytes  LightGraphs.#euclidean_graph
         expected_degree_graph      0 bytes  LightGraphs.#expected_degree_graph
 floyd_warshall_shortest_paths      0 bytes  LightGraphs.#floyd_warshall_shorte…
                    gdistances      0 bytes  LightGraphs.#gdistances
                   gdistances!      0 bytes  LightGraphs.#gdistances!
 global_clustering_coefficient      0 bytes  LightGraphs.#global_clustering_coe…
                  greedy_color      0 bytes  LightGraphs.#greedy_color
                      has_edge      0 bytes  LightGraphs.#has_edge
       has_negative_edge_cycle      0 bytes  LightGraphs.#has_negative_edge_cyc…
                      has_path      0 bytes  LightGraphs.#has_path
                has_self_loops      0 bytes  LightGraphs.#has_self_loops
                    has_vertex      0 bytes  LightGraphs.#has_vertex
                      in_edges      0 bytes  LightGraphs.#in_edges
                  in_neighbors      0 bytes  LightGraphs.#in_neighbors
              incidence_matrix      0 bytes  LightGraphs.LinAlg.#incidence_matr…
                      indegree      0 bytes  LightGraphs.#indegree
           indegree_centrality      0 bytes  LightGraphs.#indegree_centrality
              induced_subgraph      0 bytes  LightGraphs.#induced_subgraph
                   inneighbors      0 bytes  LightGraphs.#inneighbors
                     intersect      0 bytes  Base.#intersect
                  is_bipartite      0 bytes  LightGraphs.#is_bipartite
                  is_connected      0 bytes  LightGraphs.#is_connected
                     is_cyclic      0 bytes  LightGraphs.#is_cyclic
                   is_directed      0 bytes  LightGraphs.#is_directed
                    is_ordered      0 bytes  LightGraphs.#is_ordered
         is_strongly_connected      0 bytes  LightGraphs.#is_strongly_connected
           is_weakly_connected      0 bytes  LightGraphs.#is_weakly_connected
                   isgraphical      0 bytes  LightGraphs.#isgraphical
                          join      0 bytes  Base.#join
                        k_core      0 bytes  LightGraphs.#k_core
                      k_corona      0 bytes  LightGraphs.#k_corona
                       k_crust      0 bytes  LightGraphs.#k_crust
                       k_shell      0 bytes  LightGraphs.#k_shell
       karp_minimum_cycle_mean      0 bytes  LightGraphs.#karp_minimum_cycle_me…
               katz_centrality      0 bytes  LightGraphs.#katz_centrality
                     kronecker      0 bytes  LightGraphs.#kronecker
                   kruskal_mst      0 bytes  LightGraphs.#kruskal_mst
             label_propagation      0 bytes  LightGraphs.#label_propagation
              laplacian_matrix      0 bytes  LightGraphs.#laplacian_matrix
            laplacian_spectrum      0 bytes  LightGraphs.#laplacian_spectrum
                     loadgraph      0 bytes  LightGraphs.#loadgraph
                    loadgraphs      0 bytes  LightGraphs.#loadgraphs
              local_clustering      0 bytes  LightGraphs.#local_clustering
  local_clustering_coefficient      0 bytes  LightGraphs.#local_clustering_coef…
               make_edgestream      0 bytes  LightGraphs.#make_edgestream
               maximal_cliques      0 bytes  LightGraphs.#maximal_cliques
       maximum_adjacency_visit      0 bytes  LightGraphs.#maximum_adjacency_vis…
               maxsimplecycles      0 bytes  LightGraphs.#maxsimplecycles
                merge_vertices      0 bytes  LightGraphs.#merge_vertices
               merge_vertices!      0 bytes  LightGraphs.#merge_vertices!
                        mincut      0 bytes  LightGraphs.#mincut
                    modularity      0 bytes  LightGraphs.#modularity
                            ne      0 bytes  LightGraphs.#ne
              nearbipartiteSBM      0 bytes  LightGraphs.#nearbipartiteSBM
                  neighborhood      0 bytes  LightGraphs.#neighborhood
            neighborhood_dists      0 bytes  LightGraphs.#neighborhood_dists
                     neighbors      0 bytes  LightGraphs.#neighbors
       non_backtracking_matrix      0 bytes  LightGraphs.LinAlg.#non_backtracki…
   non_backtracking_randomwalk      0 bytes  LightGraphs.#non_backtracking_rand…
                normalized_cut      0 bytes  LightGraphs.#normalized_cut
                num_self_loops      0 bytes  LightGraphs.#num_self_loops
                            nv      0 bytes  LightGraphs.#nv
                     out_edges      0 bytes  LightGraphs.#out_edges
                 out_neighbors      0 bytes  LightGraphs.#out_neighbors
                     outdegree      0 bytes  LightGraphs.#outdegree
          outdegree_centrality      0 bytes  LightGraphs.#outdegree_centrality
                  outneighbors      0 bytes  LightGraphs.#outneighbors
                      pagerank      0 bytes  LightGraphs.#pagerank
parallel_betweenness_centrality      0 bytes  LightGraphs.#parallel_betweenness…
               parallel_center      0 bytes  LightGraphs.#parallel_center
 parallel_closeness_centrality      0 bytes  LightGraphs.#parallel_closeness_ce…
             parallel_diameter      0 bytes  LightGraphs.#parallel_diameter
         parallel_eccentricity      0 bytes  LightGraphs.#parallel_eccentricity
parallel_multisource_dijkstra_shortest_paths      0 bytes  LightGraphs.#paralle…
            parallel_periphery      0 bytes  LightGraphs.#parallel_periphery
 parallel_radiality_centrality      0 bytes  LightGraphs.#parallel_radiality_ce…
               parallel_radius      0 bytes  LightGraphs.#parallel_radius
    parallel_stress_centrality      0 bytes  LightGraphs.#parallel_stress_centr…
                        period      0 bytes  LightGraphs.#period
                     periphery      0 bytes  LightGraphs.#periphery
                      prim_mst      0 bytes  LightGraphs.#prim_mst
          radiality_centrality      0 bytes  LightGraphs.#radiality_centrality
                        radius      0 bytes  LightGraphs.#radius
    random_configuration_model      0 bytes  LightGraphs.#random_configuration_…
        random_regular_digraph      0 bytes  LightGraphs.#random_regular_digraph
          random_regular_graph      0 bytes  LightGraphs.#random_regular_graph
     random_tournament_digraph      0 bytes  LightGraphs.#random_tournament_dig…
                    randomwalk      0 bytes  LightGraphs.#randomwalk
                     rem_edge!      0 bytes  LightGraphs.#rem_edge!
                   rem_vertex!      0 bytes  LightGraphs.#rem_vertex!
                       reverse      0 bytes  Base.#reverse
                      reverse!      0 bytes  Base.#reverse!
                     savegraph      0 bytes  LightGraphs.#savegraph
                           saw      0 bytes  LightGraphs.#saw
                  simplecycles      0 bytes  LightGraphs.#simplecycles
    simplecycles_hadwick_james      0 bytes  LightGraphs.#simplecycles_hadwick_…
             simplecycles_iter      0 bytes  LightGraphs.#simplecycles_iter
             simplecyclescount      0 bytes  LightGraphs.#simplecyclescount
            simplecycleslength      0 bytes  LightGraphs.#simplecycleslength
                    smallgraph      0 bytes  LightGraphs.#smallgraph
             spectral_distance      0 bytes  LightGraphs.LinAlg.#spectral_dista…
                        squash      0 bytes  LightGraphs.#squash
                           src      0 bytes  LightGraphs.#src
          static_fitness_model      0 bytes  LightGraphs.#static_fitness_model
             static_scale_free      0 bytes  LightGraphs.#static_scale_free
        stochastic_block_model      0 bytes  LightGraphs.#stochastic_block_model
             stress_centrality      0 bytes  LightGraphs.#stress_centrality
 strongly_connected_components      0 bytes  LightGraphs.#strongly_connected_co…
          symmetric_difference      0 bytes  LightGraphs.#symmetric_difference
                tensor_product      0 bytes  LightGraphs.#tensor_product
       topological_sort_by_dfs      0 bytes  LightGraphs.#topological_sort_by_d…
             transitiveclosure      0 bytes  LightGraphs.#transitiveclosure
            transitiveclosure!      0 bytes  LightGraphs.#transitiveclosure!
                     triangles      0 bytes  LightGraphs.#triangles
                         union      0 bytes  Base.#union
                      vertices      0 bytes  LightGraphs.#vertices
                watts_strogatz      0 bytes  LightGraphs.#watts_strogatz
   weakly_connected_components      0 bytes  LightGraphs.#weakly_connected_comp…
                       weights      0 bytes  LightGraphs.#weights
          yen_k_shortest_paths      0 bytes  LightGraphs.#yen_k_shortest_paths
                             Δ      0 bytes  LightGraphs.#Δ
                           Δin      0 bytes  LightGraphs.#Δin
                          Δout      0 bytes  LightGraphs.#Δout
                             δ      0 bytes  LightGraphs.#δ
                           δin      0 bytes  LightGraphs.#δin
                          δout      0 bytes  LightGraphs.#δout

Example

Let us construct the famous graph of the Seven Bridges of Königsberg, plot it, and compute the number of different walks which cross 3 bridges between the north side and the center island. Can you enumerate the walks?

To work with weighted graphs and to plot we also need the following two packages, respectively.


In [9]:
using SimpleWeightedGraphs
using GraphPlot


INFO: Recompiling stale cache file /home/slap/.julia/lib/v0.6/GraphPlot.ji for module GraphPlot.

We first create s simple graph, and later switch to the weighted version.


In [10]:
g=Graph(4)


Out[10]:
{4, 0} undirected simple Int64 graph

In [11]:
add_edge!(g,1,2)
add_edge!(g,1,2)
add_edge!(g,1,3)
add_edge!(g,1,3)
add_edge!(g,1,4)
add_edge!(g,2,4)
add_edge!(g,3,4)
g


Out[11]:
{4, 5} undirected simple Int64 graph

In [12]:
gplot(g, nodelabel=1:4)


Out[12]:
1 2 3 4

The package ignores multiple edges, so will emulate them with weights.


In [13]:
gplot(g,nodelabel=1:4, edgelabel=[2,2,1,1,1] )


Out[13]:
2 2 1 1 1 1 2 3 4

In [14]:
# Number of edges and number of vertices
nv(g), ne(g)


Out[14]:
(4, 5)

In [15]:
has_edge(g,(1,2))


Out[15]:
true

In [16]:
?edges


search: edges in_edges out_edges make_edgestream readedgelist leading_ones

Out[16]:
edges(g)

Return (an iterator to or collection of) the edges of a graph. For AbstractSimpleGraphs it returns a SimpleEdgeIter. The expressions e in edges(g) and e ∈ edges(ga) evaluate as calls to has_edge.

Implementation Notes

A returned iterator is valid for one pass over the edges, and is invalidated by changes to g.


In [17]:
edges(g)


Out[17]:
SimpleEdgeIter 5

In [18]:
(1,2) in edges(g), (3,2) in edges(g)


Out[18]:
(true, false)

In [19]:
# This is not what we want
A=adjacency_matrix(g)


Out[19]:
4×4 SparseMatrixCSC{Int64,Int64} with 10 stored entries:
  [2, 1]  =  1
  [3, 1]  =  1
  [4, 1]  =  1
  [1, 2]  =  1
  [4, 2]  =  1
  [1, 3]  =  1
  [4, 3]  =  1
  [1, 4]  =  1
  [2, 4]  =  1
  [3, 4]  =  1

In [20]:
full(A)


Out[20]:
4×4 Array{Int64,2}:
 0  1  1  1
 1  0  0  1
 1  0  0  1
 1  1  1  0

We switch to weighted graph.


In [21]:
g=SimpleWeightedGraph(4)


Out[21]:
{4, 0} undirected simple Int64 graph with Float64 weights

In [22]:
add_edge!(g,1,2,2)
# add_edge!(g,1,2,2)
add_edge!(g,1,3,2)
# add_edge!(g,1,3)
add_edge!(g,1,4,1)
add_edge!(g,2,4,1)
add_edge!(g,3,4,1)
g


WARNING: Note: adding edges to this graph type is not performant.
Out[22]:
{4, 5} undirected simple Int64 graph with Float64 weights

In [23]:
has_edge(g,(1,2))


Out[23]:
true

In [24]:
edges(g).f((1,2,2))


Out[24]:
Edge 1 => 2 with weight 2

In [25]:
# Be carefull!
(1,2) in edges(g).iter, (3,2) in edges(g)


Out[25]:
(false, false)

In [26]:
W=weights(g)


Out[26]:
4×4 SparseMatrixCSC{Float64,Int64} with 10 stored entries:
  [2, 1]  =  2.0
  [3, 1]  =  2.0
  [4, 1]  =  1.0
  [1, 2]  =  2.0
  [4, 2]  =  1.0
  [1, 3]  =  2.0
  [4, 3]  =  1.0
  [1, 4]  =  1.0
  [2, 4]  =  1.0
  [3, 4]  =  1.0

In [27]:
full(W)


Out[27]:
4×4 Array{Float64,2}:
 0.0  2.0  2.0  1.0
 2.0  0.0  0.0  1.0
 2.0  0.0  0.0  1.0
 1.0  1.0  1.0  0.0

In [28]:
no_of_walks=(W^3)[1,2]


Out[28]:
22.0

Pkg.checkout()

The contents of a registered package obtained by the command Pkg.add("Package_name") is fixed at the time of registration.

The package owner may further develop the package, but those changes are not registered (until the registration of a new version).

If you want to use the latest available version, the command Pkg.checkout("Package_name") downloads the latest master.

Pkg.clone()

Adds unregistered packages or repositories. Here the full GitHub address needs to be supplied. As an example, we shall use the package LinearAlgebra.jl.

N.B. In Julia, the linear algebra routines are incorporated as wrappers of various LAPACK . This package contains several routines written directly in Julia.

By inspecting the file src/LinearAlgebra.jl, we see that nothing is exported so all methods need to be fully specified. We also see that the SVD related stuff may be in the file src/svd.jl. There we see that the sub-module SVDModule is defined, but with nothing eported, and we must specify the full command LinearAlgebra.SVDModule.svdvals!().

We shall compute the singular values of the bidiagonal Jordan form with the standard Julia function svdvals() and the function from the package.


In [29]:
# Pkg.clone("https://github.com/andreasnoack/LinearAlgebra.jl")

In [30]:
using LinearAlgebra

In [31]:
# Not much of an information
whos(LinearAlgebra)


                 LinearAlgebra    240 KB     Module
                   numnegevals      0 bytes  LinearAlgebra.#numnegevals

In [32]:
# Also no information
whos(LinearAlgebra.SVDModule)


                     SVDModule     13 KB     Module

In [33]:
methods(LinearAlgebra.SVDModule.svdvals!)


Out[33]:
7 methods for generic function svdvals!:
  • svdvals!{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64}}(A::Union{Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T}, DenseArray{T,2}, SubArray{T,2,A,I,L} where L} where I<:Tuple{Vararg{Union{Base.AbstractCartesianIndex, Int64, Range{Int64}},N} where N} where A<:Union{Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T} where N where T, DenseArray}) at linalg/svd.jl:130
  • svdvals!{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64}}(A::Union{Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T}, DenseArray{T,2}, SubArray{T,2,A,I,L} where L} where I<:Tuple{Vararg{Union{Base.AbstractCartesianIndex, Int64, Range{Int64}},N} where N} where A<:Union{Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T} where N where T, DenseArray}, B::Union{Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T}, DenseArray{T,2}, SubArray{T,2,A,I,L} where L} where I<:Tuple{Vararg{Union{Base.AbstractCartesianIndex, Int64, Range{Int64}},N} where N} where A<:Union{Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T} where N where T, DenseArray}) at linalg/svd.jl:289
  • svdvals!(A::Union{Hermitian{Complex{T},S}, Hermitian{T,S}, Symmetric{T,S}} where S where T<:Real) at linalg/symmetric.jl:456
  • svdvals!(M::Bidiagonal{#s268} where #s268<:Union{Float32, Float64}) at linalg/bidiag.jl:228
  • svdvals!{T<:Real}(B::Bidiagonal{T}) at /home/slap/.julia/v0.6/LinearAlgebra/src/svd.jl:107
  • svdvals!{T<:Real}(B::Bidiagonal{T}, tol; debug) at /home/slap/.julia/v0.6/LinearAlgebra/src/svd.jl:107
  • svdvals!(A::Union{Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T}, DenseArray{T,2}, SubArray{T,2,A,I,L} where L} where I<:Tuple{Vararg{Union{Base.AbstractCartesianIndex, Int64, Range{Int64}},N} where N} where A<:Union{Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{DenseArray, SubArray{T,N,P,I,true} where I<:Tuple{Union{Base.Slice, UnitRange},Vararg{Any,N} where N} where P where N where T} where N where T, DenseArray} where T) at /home/slap/.julia/v0.6/LinearAlgebra/src/svd.jl:256

In [34]:
# We now know how to define bidiagonal matrix
methods(Bidiagonal)


Out[34]:
6 methods for generic function Type:
  • (::Type{Bidiagonal})(dv::AbstractArray{T,1} where T, ev::AbstractArray{T,1} where T) at linalg/bidiag.jl:56
  • (::Type{Bidiagonal}){T}(dv::AbstractArray{T,1}, ev::AbstractArray{T,1}, isupper::Bool) at linalg/bidiag.jl:55
  • (::Type{Bidiagonal})(dv::AbstractArray{T,1} where T, ev::AbstractArray{T,1} where T, uplo::Char) at linalg/bidiag.jl:100
  • (::Type{Bidiagonal}){Td, Te}(dv::AbstractArray{Td,1}, ev::AbstractArray{Te,1}, isupper::Bool) at linalg/bidiag.jl:110
  • (::Type{Bidiagonal})(A::AbstractArray{T,2} where T, isupper::Bool) at linalg/bidiag.jl:145
  • (::Type{T}){T}(arg) at sysimg.jl:77

In [35]:
n=70
c=0.5
J=Bidiagonal(c*ones(n),ones(n-1),true)


Out[35]:
70×70 Bidiagonal{Float64}:
 0.5  1.0   ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   …   ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅   0.5  1.0   ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅   0.5  1.0   ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅   0.5  1.0   ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅   0.5  1.0   ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅   0.5  1.0   ⋅   …   ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   0.5  1.0      ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   0.5      ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   …   ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
 ⋮                        ⋮              ⋱            ⋮                      
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   …   ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅      1.0   ⋅    ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅      0.5  1.0   ⋅    ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅   0.5  1.0   ⋅    ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   …   ⋅    ⋅   0.5  1.0   ⋅    ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅   0.5  1.0   ⋅    ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅   0.5  1.0   ⋅ 
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅   0.5  1.0
  ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅    ⋅       ⋅    ⋅    ⋅    ⋅    ⋅    ⋅   0.5

In [36]:
@time s=svdvals(J);


  0.076652 seconds (15.80 k allocations: 884.637 KiB)

Julia uses convention that function names ending in ! overwrite the input data. Thus, we first make a copy of J.


In [37]:
J₁=deepcopy(J);

In [38]:
J₀=Bidiagonal([1.0,1,1],[1,1],true)


Out[38]:
3×3 Bidiagonal{Float64}:
 1.0  1.0   ⋅ 
  ⋅   1.0  1.0
  ⋅    ⋅   1.0

In [39]:
LinearAlgebra.SVDModule.svdvals!(J₀)


Out[39]:
3-element Array{Float64,1}:
 1.80194 
 1.24698 
 0.445042

In [40]:
@time s₁=LinearAlgebra.SVDModule.svdvals!(J₁);


  0.000212 seconds (13 allocations: 9.703 KiB)

In [41]:
typeof(s₁), s₁


Out[41]:
(Array{Float64,1}, [1.49967, 1.49867, 1.49701, 1.49468, 1.4917, 1.48805, 1.48375, 1.47879, 1.47318, 1.46693  …  0.576594, 0.561598, 0.547933, 0.535733, 0.525133, 0.516259, 0.509224, 0.504125, 0.501035, 6.35275e-22])

In [42]:
s[70], s₁[70]


Out[42]:
(6.352747104407252e-22, 6.352747104407252e-22)

Pkg.rm()

This command removes (deletes) added or cloned packages and all required packages not in use otherwise.


In [43]:
# Pkg.rm("LightGraphs")

In [44]:
# Pkg.rm("LinearAlgebra")

Creating packages

You need to use GitHub:

  1. Go to GitHub and Sign up and Sign in.
  2. Set up Git at your computer.

N.B. Check out GitHub Guides.

One way to start developing packages is

  1. Create new repository at GitHub.
  2. Clone the created package to your computer with
    git clone https://github.com/your_user_name/your_repository_name.jl
  3. Start writing your code as described in Contents of the Package.
  4. Check what you have changed
    git commit
  5. Add changes to be commited with
    git add file1 file2 ...
  6. Commit the changes (you need to supply the message)
    git commit
  7. Push the changes to your GitHub repository
    git push

N.B. There are various other possibilities and shorthands (see the Guides). For example, steps 4., 5. and 6. can be shortened with

git commit -am "your message"

Also, if you work on your package from two computers, you may need to synchronize your repository: assume that you pushed the changes that you made on computer A to GitHub, and that you want to continue to work on your repository from computer B. Then, you obviously need to synchronize computer B with the latest version from GitHub. This is done with the following commands issued on computer B:

git fetch origin
git reset --hard origin/master
git clean -f -d

Be social

You can fork other people's repositories, and use them and change them as your own. You can make pull requests to incorporate those changes to those repositories.

You can easily make different branches of your repository, and test different options.

GitHub enables you to share your work with others, so even small, undocumented packages can be very useful.


In [ ]: