Pushing arrays of one variable in one array - julia

I have this piece of code:
for i=1:10
v=[2i,i]
#show v
end
and I get this result:
v = [2, 1]
v = [4, 2]
v = [6, 3]
v = [8, 4]
v = [10, 5]
v = [12, 6]
v = [14, 7]
v = [16, 8]
v = [18, 9]
v = [20, 10]
Now what I want to do is to collect all these outputs of into one array of arrays, something like:
[[2,1],[4,2],[6,3]]
and I don't really know how to do it, I've tried several solutions that didn't work.

You can use array comprehensions for this:
julia> x = [[2i,i] for i in 1:10]
10-element Array{Array{Int64,1},1}:
[2, 1]
[4, 2]
[6, 3]
[8, 4]
[10, 5]
[12, 6]
[14, 7]
[16, 8]
[18, 9]
[20, 10]
or go with the manual route of constructing an empty initial array, and pushing the inner arrays into it one-by-one:
julia> y = []
0-element Array{Any,1}
julia> for i in 1:10
push!(y,[2i,i])
end
julia> y
10-element Array{Any,1}:
[2, 1]
[4, 2]
[6, 3]
[8, 4]
[10, 5]
[12, 6]
[14, 7]
[16, 8]
[18, 9]
[20, 10]

Related

Julia JuMP: define a multidimensional variable when a dimension depends on other dimension

While I am defining a linear programming variables, I have to consider
index_i = 1:3 index_j = J = [1:2, 1:5, 1:3]
I want to define a variable x indexed with both i and j such that i is {1,2,3} and j is in {1,2} if i is 1, {1,2,3,4,5} if i is 2 and {1,2,3} if i is 3.
I tried several syntaxes but non of them delivered it successfully. Any suggestion?
I wonder why this is not working
#variable(m, e[i for i in I, j for j in J[i]])
I m expecting a result like this
e[1,1]
e[1,2]
e[1,3]
e[2,1]
e[2,2]
e[2,3]
e[2,4]
e[2,5]
e[3,1]
e[3,2]
e[3,3]
Assuming I=1:3 and J=[1:2, 1:5, 1:3]
you can do:
julia> #variable(m, e[i in I, j in J[i]])
JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}} with 10 entries:
[1, 1] = e[1,1]
[1, 2] = e[1,2]
[2, 1] = e[2,1]
[2, 2] = e[2,2]
[2, 3] = e[2,3]
[2, 4] = e[2,4]
[2, 5] = e[2,5]
[3, 1] = e[3,1]
[3, 2] = e[3,2]
[3, 3] = e[3,3]

Is there a basic implementation of bellman ford's algorithm in julia?

I'm a first year student who's learning Julia as a first programming language. I have a project about bellman ford's algorithm but it seems every code is a bit more advanced than I can currently understand. Is there a basic code like Dfs or Bfs for this that a starter could understand, if u have, do share.
This is implemented in LightGraphs
using LightGraphs
g = erdos_renyi(20, 100, seed=1)
bf_state = bellman_ford_shortest_paths(g, 1)
And now we can display all paths found in the graph:
julia> enumerate_paths(bf_state)
20-element Vector{Vector{Int64}}:
[]
[1, 4, 2]
[1, 3]
[1, 4]
[1, 5]
[1, 11, 6]
[1, 7]
[1, 3, 8]
[1, 3, 9]
[1, 7, 10]
[1, 11]
[1, 12]
[1, 3, 13]
[1, 3, 14]
[1, 15]
[1, 4, 16]
[1, 17]
[1, 3, 18]
[1, 19]
[1, 5, 20]

Sum of integers with restrictrions

Getting right to the gist of the problem:
In how many ways can we add k positive integers to reach a sum of exactly n if each number is smaller or equal to given number m?
The problem is solvable with dynamic programming but I am stuck because I cannot find the optimal substructure or recursion for the solution.
Here's a simple function in Python 3 that should fit your description. I assume that 0 is not an acceptable value but it's a trivial change if it is.
def howMany(k, n, m):
def sub(pos, currentSum, path):
if currentSum == n and pos == k: # reached the sum, print result and increase counter by 1
print(path)
return 1
elif currentSum < n and pos < k: # still worth trying
count = 0
for i in range(1, m):
count += sub(pos + 1, currentSum + i, path+[i])
return count
else: # abort
return 0
return sub(0, 0, [])
print(howMany(3, 10, 6))
yields
[1, 4, 5]
[1, 5, 4]
[2, 3, 5]
[2, 4, 4]
[2, 5, 3]
[3, 2, 5]
[3, 3, 4]
[3, 4, 3]
[3, 5, 2]
[4, 1, 5]
[4, 2, 4]
[4, 3, 3]
[4, 4, 2]
[4, 5, 1]
[5, 1, 4]
[5, 2, 3]
[5, 3, 2]
[5, 4, 1]
18
It could be optimised but that would obfuscate the logic at this stage.

Sum of column elements of dictionary values made of lists in Python

I have a dictionary as follows
d = {0:[1,2,3], 1:[2,3,4], 2:[3,4,5], 3:[4,5,6]}
What wold be the most compact form in Python to sum the same column elements of the dictionary values which are lists or how can I get the following result out of dictionary values ?
[(1+2+3+4), (2+3+4+5), (3+4+5+6)]=[10,14,18]
Without NumPy
>>> d
{0: [1, 2, 3], 1: [2, 3, 4], 2: [3, 4, 5], 3: [4, 5, 6]}
>>> map(sum, zip(*d.values()))
[10, 14, 18]
with NumPy
>>> import numpy as np
>>> d
{0: [1, 2, 3], 1: [2, 3, 4], 2: [3, 4, 5], 3: [4, 5, 6]}
>>> map(np.sum, zip(*d.values()))
[10, 14, 18]

Does RSpec support recursive approximate matching?

In RSpec I can do something like this:
[1, 2, 3].should =~ [2, 3, 1]
Is there a built-in way to make this recursively approximate? For example:
x = [
[1, 2],
[3, 4]
]
y = [
[4, 3],
[2, 1]
]
x.should =~ y
If there's no built-in way, I realize I can (and will) just write this myself.

Resources