Sharing global variables across modules in Julia - global-variables

With the information kindly provided here: How to define global variables to be shared later in Julia I keep working on global variables in Julia.
file: global.jl
module Global
export data
data = zeros(Int32, 20, 12, 31, 24, 60, 5);
end
file: main.jl
# MAIN PROGRAM
# INCLUDE GLOBAL
include("global.jl")
import .Global
println(">> Main Before include test.jl")
println(">> ", Global.data[ 14, 1, 15, 18, 1, 1])
include("test.jl")
println(">> Main After include test.jl")
println(">> ", Global.data[ 14, 1, 15, 18, 1, 1])
file: test.jl
module Test
include("global.jl")
import .Global
println(" >> Inside Test")
println(" ", Global.data[ 14, 1, 15, 18, 1, 1])
Global.data[ 14, 1, 15, 18, 1, 1] = 123456
println(" ", Global.data[ 14, 1, 15, 18, 1, 1])
end
What I expect is that module Test will modify the Global array data and therefore it will later be reflected by main.jl. I am obviously still missing something as this is the output:
$ /usr/local/julia-1.2.0/bin/julia main.jl
>> Main Before include test.jl
>> 0
>> Inside Test
0
123456
>> Main After include test.jl
>> 0

The key issue you need to understand is that include works like copy-paste.
In you code you define module Global twice. Once in Main module, and once in Test module. These two modules have the same code but are separate (the latter is nested in Test module). See:
julia> Main.Test.Global.data[ 14, 1, 15, 18, 1, 1]
123456
julia> Main.Global.data[ 14, 1, 15, 18, 1, 1]
0
This is the way you should define the Test module to reuse the Global module defined earlier:
module Test
import ..Global
println(" >> Inside Test")
println(" ", Global.data[ 14, 1, 15, 18, 1, 1])
Global.data[ 14, 1, 15, 18, 1, 1] = 123456
println(" ", Global.data[ 14, 1, 15, 18, 1, 1])
end

Related

Find Number of Nodes which have Links NetworkX

suppose i have a graph that looks like this:
Is there any possible way to only count nodes that have links? So instead of 6, it counts 5, since there is one node that doesn't have link.
Also, if I open a graph with read_edgelist, and afterwards use number_of_nodes function, is the function counts all nodes whether those have links or not or only those which has links/edges, since I opened it with read_edgelist? Thank you for your help.
If you want to filter out single nodes, you can iterate through graph nodes and keep only nodes that have neighbors.
With generator:
import networkx as nx
G = nx.Graph()
G = nx.fast_gnp_random_graph(40, 0.05, directed=False, seed=1)
print([n for n in G.nodes if len(list(G.neighbors(n))) > 0])
or filter function:
print(list(filter(lambda n: len(list(G.neighbors(n))) > 0, G.nodes)))
Both will print the same:
[0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 38, 39]
If I understand the second part correctly, networkx returns you the number of all nodes in the graph, not only that have edges.

how to create non-perfect binary tree with diagrammeR?

I use diagrammeR library in R to create and render binary trees. I find it very simple to use and it creates high quality renders. However creating a tree that is not fully (perfect) generates messy renders.
Here is what I get when my tree has 16 leaves (h = 4):
Fully binary tree
To be clear, every node label is the row name of nodes data.frame which indicates the sequence of nodes passed to the graph:
nodes$label = rownames(nodes)
And here is what I get if I add one node [32] from node [31] - either manually or by add_node() and add_edge() functions:
Non-perfect binary tree
As you can see, everything goes messy. I would like to have node [32] directly under node [31] with edge of straight vertical line. Is it even possible with this library? I can't figure out the proper order of nodes in nodes data.frame.
Here is how my full code looks like:
library(DiagrammeR)
from = c(1, 1, 2, 2, 3, 3, 4, 4, 7, 7, 10, 10, 11, 11, 14, 14, 17, 17, 18, 18, 19, 19, 22, 22, 25, 25, 26, 26, 29, 29)
to = c(2, 17, 3, 10, 4, 7, 5, 6, 8, 9, 11, 14, 12, 13, 15, 16, 18, 25, 19, 22, 20, 21, 23, 24, 26, 29, 27, 28, 30, 31)
h=4
n = 2^(h+1)-1
edges = data.frame(from, to)
nodes = data.frame(id = 1:n, label=1:n, shape='circle')
g1 = create_graph(nodes, edges)
render_graph(g1, layout='tree', title='g1')
# add node [32] and edge [31-32]
edges2 = rbind(edges, c(31, 32))
nodes2 = nodes
nodes2[32, 1:2] = 32
nodes2[32, 3] = 'circle'
g2 = create_graph(nodes2, edges2)
render_graph(g2, layout='tree', title='g2')

How to code adjacency lists in a dodecahedron graph?

It has:
20 vertices
30 edges
G{20,30}
How can you generate the adjacency lists for each vertex 1,2,...20?
V1 is linked with 2 and 3
v2 is linked with 1 and 4
v3 is linked with 1 and 5 ??
Is there like a formula? This is for a game project, don't know if I missed a math class or something? What should I study to understand the solution?
Manually constructed:
int adjs[] = { 1, 4, 7, 0, 2, 9, 1, 3, 11, 2, 4, 13, 0, 3, 5, 4, 6, 14, 5, 7, 16, 0, 6, 8, 7, 9, 17, 1, 8, 10, 9, 11, 18, 2, 10, 12, 11, 13, 19, 3, 12, 14, 5, 13, 15, 14, 16, 19, 6, 15, 17, 8, 16, 18, 10, 17, 19, 12, 15, 18 };
for (int i = 0, int j = 0; i < 20; i++, j = i * 3) {
caves[i] = Cave(i);
for (int c = j; c < j + 3; c++) {
caves[i].adjsListy.addAdj(adjs[c]);
}
I found help in the solution here: https://rosettacode.org/wiki/Hunt_The_Wumpus/C%2B%2B
I think the link list is usually manually constructed.
However you can take a look at the special case of a 2:1 pyritohedron in https://en.wikipedia.org/wiki/Dodecahedron to get an understanding of how to go from a cube to a dodecahedron. (Also take a look at the animated gif in the section "Cartesian Coordinates")
What this tells us is that a dodecahedron can be constructed by inserting new vertices bisecting each of the cubes 12 edges. Then insert new edges connecting these new vertices such that they bisect each face of the 6 faces of the cube in alternating directions.
I think this understanding can be helpful, either for constructing an algorithm, or just to help you construct a manual link list.
This of-course only takes care of the vertex-links, to find the coordinates of each vertex, see e.g. How to generate/calculate vertices of dodecahedron?

Errors when trying to create and use a random permutation test function in R

I am trying to complete a random permutation test in RStudio and keep getting the following two errors:
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
#create groups/data vectors
drinks = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
mosquito = c(27, 20, 21, 26, 27, 3, 24, 21, 20, 19, 23, 24, 28, 19, 24, 29, 18, 20, 17, 31, 20, 25, 28, 21, 27, 21, 22, 15, 12, 21, 19, 15, 22, 24, 19, 23, 13, 22, 20, 24, 18, 20)
#create function
rpermut = function(group, outcome, permutation){
diff = rep(NA, permutation)
for(i in 1:permutation){
outcome = sample(outcome)
diff[i] = mean(outcome[group==levels(group)[1]]) - mean(outcome[group==levels(group)[2]])}
diff
}
#adding values to function
mosrep = rpermut(group=drinks, outcome=mosquito, permutation=1000)
I am not sure what the error codes mean nor how to fix things so that the function will run. I would greatly appreciate any assistance you may be able to provide on where I am going wrong here!
So it seems to work for me with a few changes.
Firstly I assume that both drinks and mosquito should be the same length which in your question is not the case.
> length(drinks)
[1] 43
> length(mosquito)
[1] 42
Secondly, levels() works on factors whereas those objects drinks and mosquito are numeric vectors.
> class(drinks)
[1] "numeric"
> class(mosquito)
[1] "numeric"
to therefore make this function work on my machine i had to then adjust the function to be this:
rpermut = function(group, outcome, permutation){
diff = c()
group = as.factor(group)
for(i in 1:permutation){
outcome = sample(outcome)
diff[i] = mean(outcome[group==levels(group)[1]]) - mean(outcome[group==levels(group)[2]])
}
return(diff)
}
This is just changing the group to a factor with as.factor()
I also changed diff = rep(NA, permutation) to just diff = c() which is creating an empty vector. There is no need to assign NA to all the values as you can simply fill an entry with diff[i] the same way.
So the vectors need to be the same length and then this should work, a check could simply be added as well.
if(length(group) != length(outcome)){
stop("input vector lengths does not match!")
}
All together:
rpermut = function(group, outcome, permutation){
if(length(group) != length(outcome)){
stop("input vector lengths does not match!")
}
diff = c()
group = as.factor(group)
for(i in 1:permutation){
outcome = sample(outcome)
diff[i] = mean(outcome[group==levels(group)[1]]) - mean(outcome[group==levels(group)[2]])
}
return(diff)
}

Display character " using ViewBag

I am using ASP.NET MVC 4 with aspx.
I want from my controller to produce code at my view.
Therefore I have a line at my controller which is
ViewBag.Chart = "var c = r.barchart(10, 10, 600, 440, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]], { stacked: true, type: \"soft\" }).hoverColumn(fin2, fout2);";
However, when it shows at my code, it does not shows the character (") but instead it replaces it with a & quot;
What I can do in order to produce the character " at the code?
You need to use %= instead of %:
<%= ViewBag.Chart %>
if you are using %: it is automatically HTML encodes your string while the %= does not.
Sidenote: it is not a good practice to emit JavaScript from your controller into your view...
Hi try placing \ in front of the quote and before closing the quotes. It works fine for me
\"var c = r.barchart(10, 10, 600, 440, [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55]], { stacked: true, type: \"soft\" }).hoverColumn(fin2, fout2);\";

Resources