Julia | Push or Append in 2 Dimensional Array - julia

I am trying to push or append elements from a 2 dimensional array to a 3 dimensional array. The array 2 (2nd dimension) is associated with an index that goes like this [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5...] and I would like the last element of the same index to be pushed / appended to array 1, that is the 1st, the 3rd, the 6th, the 10th elements etc.. Here is the MWE:
arr1 = Array{Float64,3}(length, per, ro)
arr2 = Array{Float64,2}(length, per * ro * (ro + 1) ÷ 2)
length = 100
per = 20
ro = 10
for l in 1:length
for p in 1:per
for r in 1:ro
x = arr2[l, p * ro * (ro + 1) ÷ 2]
push!(arr1, x)
end
end
end
Thank you for your time.
Λ

Related

How can I write a range with vector on CPLEX?

I have a problem with range.I I want to write a range where to scroll the variable i from 1 to nj. nj is a vector of n elements.
range I = 1..nj;
int nj [J] = ...;
nj = [1, 4, 5 , 6, 9, 3, 10];
How can I write this type of range on CPLEX? Thank you!
int sizenj=7;
range I = 1..sizenj;
int nj [I] = [1, 4, 5 , 6, 9, 3, 10];
execute
{
writeln(nj);
}
works fine

What is the coefficient of x^k in (1+x)(1+x^2)(1+x^3)(1+x^4)....(1+x^n) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I AM NOT ABLE TO GENERALISE IT
I am not able to generalise the sequence i tried so hard can anyone help me with it
I tried n=1,2,3,4,5,6 but not able to generalise it
We can see that x^k coefficients are formed from sum of coefficients of the previous polynom using powers 1*x^k and x^n * x^(k-n) (if the latter does exist).
So C(n,k)=C(n-1,k)+C(n-1,k-n) with some limits:
def expand(n, k):
if n == 0:
return 0 if k else 1
if k > n * (n + 1) // 2:
return 0
res = expand(n - 1, k)
if k >= n:
res += expand(n - 1, k - n)
return res
n = 3
for i in range(1 + n * (n + 1) // 2):
print(expand(n, i), end = ' ')
print()
n = 5
for i in range(1 + n * (n + 1) // 2):
print(expand(n, i), end = ' ')
1 1 1 2 1 1 1
1 1 1 2 2 3 3 3 3 3 3 2 2 1 1 1
For calculation of a lot of coefficients it would be wise to fill a table row-by-row to
avoid extensive recursion (a kind of dynamic programming)
def maketable(n):
table = [[1],[1,1]]
for i in range(2, n + 1):
table.append([0]*(1+i*(i+1)//2))
for row in range(2, n+1):
for k in range(1+row*(row-1)//2):
table[row][k] = table[row-1][k]
for k in range(row, 1+row*(row+1)//2):
table[row][k] += table[row-1][k - row]
return table
print(maketable(5))
[[1],
[1, 1],
[1, 1, 1, 1],
[1, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1],
[1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, 1]]

Best way to identify duplicates in two lists or a dictionary?

Lets say you have two lists such as:
list1 = [-2, -1, 0, 1, 2, 3]
list2 = [4, 1, 0, 1, 4, 9]
...and the two lists were zipped into a dictionary to produce:
dict1 = {-2: 4,
-1: 1,
0: 0,
1: 1,
2: 4,
3: 9}
...where list1 is the key, and list 2 is the value.
You will notice that some of the elements in list2 are duplicates such as 4 and 1. They show up twice in list 2, and consequently in the dictionary.
-2 corresponds to 4
2 corresponds to 4
-1 corresponds to 1
1 corresponds to 1
I am trying to figure out a way either using the lists or the dictionary to identify the duplicate items in list2, and return their keys from list 1.
So the returned values I would expect from the two lists above would be:
(-2, 2) #From list 1 since they both correspond to 4 in list2
(-1, 1) #from list 1 since they both correspond to 1 in list2
In this example, list2 happens to be the square of list1. But this will not always be the case.
So ultimately, what I am looking for is a way to return those keys based on their duplicate values.
Any thoughts on how to approach this? I am able to identify the duplicates in list2, but I am completely stuck on how to identify their corresponding values in list 1.
In python3:
from itertools import groupby
list1 = [-2, -1, 0, 1, 2, 3]
list2 = [4, 1, 0, 1, 4, 9]
pairs = zip(list2, list1)
ordered = sorted(pairs, key=lambda x: x[0])
groups = ((k, list(g)) for k,g in groupby(ordered, key=lambda x: x[0])) # generator
duplicates = (k for k in groups if len(k[1])>1) # generator
for k,v in duplicates :
print(str(k) + " : " + str(list(v)))
result:
1 : [(1, -1), (1, 1)]
4 : [(4, -2), (4, 2)]
Bonus: in functional c#:
var list1 = new[] { -2, -1, 0, 1, 2, 3 };
var list2 = new[] { 4, 1, 0, 1, 4, 9 };
var g = list1.Zip(list2, (a, b) => (a, b)) //create tuples
.GroupBy(o => o.b, o => o.a, (k, group) => new { key = k, group = group.ToList() }) //create groups
.Where(o => o.group.Count > 1) // select group with minimum 2 elements
.ToList(); // no lazy
foreach (var kvp in g)
Console.WriteLine($"{kvp.key}: {string.Join(",", kvp.group)}");
result:
4: -2,2
1: -1,1

From a list of ints extract all consecutive repetitions in a list of lists

Extract all consecutive repetitions in a given list:
list1 = [1,2,2,3,3,3,3,4,5,5]
It should yield a list like this
[[2,2],[3,3,3,3],[5,5]]
I tried the code below. I know it is not the proper way to solve this problem but I could not manage how to solve this.
list1 = [1,2,2,3,3,3,3,4,5,5]
list2 = []
for i in list1:
a = list1.index(i)
if list1[a] == list1[a+1]:
list2.append([i,i])
print(list2)
You can use this to achieve it. There are "easier" solutions using itertools and groupby to get the same result, this is how to do it "by hand":
def FindInnerLists(l):
'''reads a list of int's and groups them into lists of same int value'''
result = []
allResults = []
for n in l:
if not result or result[0] == n: # not result == empty list
result.append(n)
if result[0] != n: # number changed, so we copy the list over into allResults
allResults.append(result[:])
result = [n] # and add the current to it
# edge case - if result contains elements, add them as last item to allResults
if result:
allResults.append(result[:])
return allResults
myl = [2, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 2, 7, 1, 1, 1,2,2,2,2,2]
print(FindInnerLists(myl))
Output (works for 2.6 and 3.x):
[[2], [1], [2], [1, 1, 1, 1], [2, 2, 2], [1], [2], [7], [1, 1, 1], [2, 2, 2, 2, 2]]
Another way to do it:
list1 = [1, 2, 2, 3, 3, 3, 3, 4, 5, 5]
result = [[object()]] # initiate the result with object() as a placeholder
for element in list1: # iterate over the rest...
if result[-1][0] != element: # the last repeated element does not match the current
if len(result[-1]) < 2: # if there was less than 2 repeated elements...
result.pop() # remove the last element
result.append([]) # create a new result entry for future repeats
result[-1].append(element) # add the current element to the end of the results
if len(result[-1]) < 2: # finally, if the last element wasn't repeated...
result.pop() # remove it
print(result) # [[2, 2], [3, 3, 3, 3], [5, 5]]
And you can use it on any kind of a list, not just numerical.
This would work:
list1 = [1,2,2,3,3,3,3,4,5,5]
res = []
add = True
last = [list1[0]]
for elem in list1[1:]:
if last[-1] == elem:
last.append(elem)
if add:
res.append(last)
add = False
else:
add = True
last = [elem]
print(res)
Output:
[[2, 2], [3, 3, 3, 3], [5, 5]]

Prolog recursive list split

I'm new to Prolog and I'm having trouble with the first part of my programming assignment:
Create a predicate split that that takes as input three parameters. The first and third parameters are lists and the second parameter is an element. You can think of the first parameter as being the input and the last two parameters being the output. The method computes all possible way of splitting a list into an element and the rest of the list. Here is an example run.
?- split([1,2,3,4,5],X,Y).
X = 1,
Y = [2, 3, 4, 5] ;
X = 2,
Y = [1, 3, 4, 5] ;
X = 3,
Y = [1, 2, 4, 5] ;
X = 4,
Y = [1, 2, 3, 5] ;
X = 5,
Y = [1, 2, 3, 4] ;
There are two rules in defining the predicate. The first rule simply gets the first element of the list and returns it as the second parameter and the rest of the list as the third parameter. The second rule generates the list by copying the first element of the list in the result (i.e., third parameter) and then recursively applying the method to the rest of the elements.
split([H|T], H, T).
split([H|T], X, [H|Y]) :-
split(T, X, Y).
There are two ways to take an element out of a list:
Take the head (the first element)
Set the head aside and take an element out of the tail (the rest of the list)
Notice that the predicate can run both ways; if the second and the third parameters are defined, it will yield all possible ways these two can be combined to form a list.
split(List,Elem,Rest) :- select(Elem,List,Rest).
| ?- select(X,[1,2,3],Y).
X = 1,
Y = [2,3] ? ;
X = 2,
Y = [1,3] ? ;
X = 3,
Y = [1,2] ? ;
no
and with split/3 ;
| ?- split([1,2,3,4],X,Y).
X = 1,
Y = [2,3,4] ? ;
X = 2,
Y = [1,3,4] ? ;
X = 3,
Y = [1,2,4] ? ;
X = 4,
Y = [1,2,3] ? ;
no
with Sicstus-prolog u need to export select from library/lists
:- use_module(library(lists)).

Resources