Related
I have a MatrixXi, say
[0, 1, 2]
[0, 2, 3]
[4, 7, 6]
[4, 6, 5]
[0, 4, 5]
[0, 5, 1]
[1, 5, 6]
I get a part of it by doing:
MatrixXi MR = F.middleRows(first, last);
with first and last at will. Now I'd like to turn those n rows into a column VectorXi, like:
[0,
1,
2,
0,
2,
3]
possibly without using a for loop. I've tried:
VectorXi VRT(MR.rows() * MR.cols());
VRT.tail(MR.rows() * MR.cols()) = MR.array();
But I get:
Assertion failed: (rows == this->rows() && cols == this->cols() && "DenseBase::resize() does not actually allow to resize."), function resize, file /Users/max/Developer/Stage/Workspace/AutoTools3D/dep/libigl/external/eigen/Eigen/src/Core/DenseBase.h, line 257.
How do I get that? I'm using Eigen before v4 so I cannot use reshape...
Thank you
As pointed out by chtz, this works:
Eigen::VectorXi VR(MR.size());
Eigen::MatrixXi::Map(VR.data(), MR.cols(), MR.rows()) =
MR.transpose();
I just would like to understand the passages of how this piece of code operates:
llist = [5, 2, 1, 4, 3]
for i in range(len(llist)):
for j in range(i+1):
if llist[j] < llist[i]:
temp = llist[i]
llist[i] = llist[j]
llist[j] = temp
print(llist)
I tried to meticulously go through every passage of the nested loop, but I can't understand how at the end of all that new-variable-assigning the elements of the list get sorted from highest to lowest.
Thank you very much to anyone who can help
With a bit of printing:
llist = [5, 2, 1, 4, 3]
for i in range(len(llist)):
print()
print(f'current pivot is {llist[i]} on position {i}')
for j in range(i+1):
if llist[j] < llist[i]:
print('-----------------------------------------------------------------')
print((f'\t {llist[j]} in position {j} and {llist[i]} on position {i} are in'
' the wrong order'))
print(f'\t before swapping our list is : {llist}')
temp = llist[i]
llist[i] = llist[j]
llist[j] = temp
print(f'\t after swapping we have {llist}')
print('-----------------------------------------------------------------')
print(f'list at the end of iteration: {llist}')
You get:
current pivot is 5 on position 0
list at the end of iteration: [5, 2, 1, 4, 3]
current pivot is 2 on position 1
list at the end of iteration: [5, 2, 1, 4, 3]
current pivot is 1 on position 2
list at the end of iteration: [5, 2, 1, 4, 3]
current pivot is 4 on position 3
-----------------------------------------------------------------
2 in position 1 and 4 on position 3 are in the wrong order
before swapping our list is : [5, 2, 1, 4, 3]
after swapping we have [5, 4, 1, 2, 3]
-----------------------------------------------------------------
-----------------------------------------------------------------
1 in position 2 and 2 on position 3 are in the wrong order
before swapping our list is : [5, 4, 1, 2, 3]
after swapping we have [5, 4, 2, 1, 3]
-----------------------------------------------------------------
list at the end of iteration: [5, 4, 2, 1, 3]
current pivot is 3 on position 4
-----------------------------------------------------------------
2 in position 2 and 3 on position 4 are in the wrong order
before swapping our list is : [5, 4, 2, 1, 3]
after swapping we have [5, 4, 3, 1, 2]
-----------------------------------------------------------------
-----------------------------------------------------------------
1 in position 3 and 2 on position 4 are in the wrong order
before swapping our list is : [5, 4, 3, 1, 2]
after swapping we have [5, 4, 3, 2, 1]
-----------------------------------------------------------------
list at the end of iteration: [5, 4, 3, 2, 1]
So we you can see, every time it finds that a number on the list has another number to its left that is smaller than the number at hand, then it swaps them.
For 5, it does nothing as there are no numbers to its left.
For 2, it does nothing as 5 is the only number to the left and it's > 2.
For 1, it does nothing as 5 and 2 are the only numbers to the left and they are both > 1.
For 4, it finds 2 as a number to its left, so we swap them and have:
[5,2,1,4,3] -> [5,4,1,2,3]
So now we continue and find 1 to the left of 2 (since now 2 is in the 3rd position), so we swap again
[5,4,1,2,3] -> [5,4,2,1,3]
Does that help?
I'm writing a simple code generating a simple list with 5 numbers whose first variable should be positive and I'm trying to understand why this code fails
test([H|T]) :- H > 0, length(T,4).
when I call with
length(X,5), test(X).
it shows me the following error:
ERROR: Arguments are not sufficiently instantiated
When I debug the code, the H variable in test isn't instantiated.
Anyone know why?
The issue here is that your rule for test([H|T]) doesn't describe in Prolog that H is a positive integer. It only tests if H > 0, which fails since H has not instantiation. Just attempting to compare an uninstantiated variable with a number (H > 0 in this case) doesn't cause Prolog to assume you intended H to be a number, and further, doesn't instantiate H.
Further, your rule for test/1 doesn't describe the rest of the list (T) other than to force that it be length 4. Since you're query establishes the rule that the length of the original list be 5, this stipulation is redundant.
You appear to be wanting to define test(L) such that it means L is an arbitrary list of positive integers. This is generally done using CLP(FD):
:- use_module(library(clpfd)).
test(X) :- X ins 1..10000.
This rule says that X is a list whose values are in the range 1 to 10000. The appropriate query to generate the lists of length 5 would then be:
?- length(X, 5), test(X), label(X).
X = [1, 1, 1, 1, 1] ;
X = [1, 1, 1, 1, 2] ;
X = [1, 1, 1, 1, 3] ;
X = [1, 1, 1, 1, 4] ;
X = [1, 1, 1, 1, 5] ;
...
If you want to restrict it further and say that elements need to be unique, you can use all_different/1:
test(X) :- X ins 1..10000, all_different(X).
?- length(X, 5), test(X), label(X).
X = [1, 2, 3, 4, 5] ;
X = [1, 2, 3, 4, 6] ;
X = [1, 2, 3, 4, 7] ;
X = [1, 2, 3, 4, 8] ;
X = [1, 2, 3, 4, 9] ;
X = [1, 2, 3, 4, 10] ;
...
Here is a snippet of code which gives the output: 0 1 2 2. I had expected the output 3 3 3 3 since a[-1] accesses the number 3 in the list. The explanation given online says "The value of a[-1] changes in each iteration" but I don't quite understand how or why. Any explanations would be great!
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1])
While doing for a[-1] in a, you actually iterate through the list and temporary store the value of the current element into a[-1].
You can see the loop like these instructions:
a[-1] = a[0] # a = [0, 1, 2, 0]
print(a[-1]) # 0
a[-1] = a[1] # a = [0, 1, 2, 1]
print(a[-1]) # 1
a[-1] = a[2] # a = [0, 1, 2, 2]
print(a[-1]) # 2
a[-1] = a[3] # a = [0, 1, 2, 2]
print(a[-1]) # 2
So, when you are on the third element, then 2 is stored to a[-1] (which value is 1, but was 0 before and 3 on start).
Finally, when it comes to the last element (and the end of the iteration), the last value stored into a[-1] is 2 which explains why it is printed twice.
What's happening here is a list is mutated during looping.
Let's consider following code snippet:
a = [0, 1, 2, 3]
for a[-1] in a:
print a
Output is:
[0, 1, 2, 0]
[0, 1, 2, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]
Each iteration:
reads value from position currently pointed by internal pointer
immediately assigns it to last element in list
after that last element is printed on standard output
So it goes like:
internal pointer points to first element, it's 0, and last element is overwritten with that value; list is [0, 1, 2, 0]; printed value is 0
internal pointer points to second element, it's 1, and last element is overwritten with that value; list is [0, 1, 2, 1]; printed value is 1
(...)
at last step, internal pointer points to last element; last element is overwritten by itself - list does not change on last iteration; printed element also does not change.
This code
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1])
is equivalent to
a = [0, 1, 2, 3]
for i in range( len(a) ):
a[-1] = a[i] # update last element of list(updated) with element at i'th position
print(a[-1]) # print last element of list
the output will be 0 1 2 2
Explanation:
len(a) = 4, range( len(a) ) = [0, 1, 2, 3]
1st loop, current list = [0, 1, 2, 3], i = 0 => a[i] = a[0] = 0, updated list = [0, 1, 2, 0], last element a[-1] = 0
2nd loop, current list = [0, 1, 2, 0], i = 1 => a[i] = a[1] = 1, updated list = [0, 1, 2, 1], last element a[-1] = 1
3rd loop, current list = [0, 1, 2, 1], i = 2 => a[i] = a[2] = 2, updated list = [0, 1, 2, 2], last element a[-1] = 2
4th loop, current list = [0, 1, 2, 2], i = 3 => a[i] = a[3] = 2, updated list = [0, 1, 2, 2], last element a[-1] = 2
I'm trying to write a 4X4 grid in python where the last two rows contain the same numbers as the first two rows.
The end result should be exactly this:
0 1 2 3
4 5 6 7
0 1 2 3
4 5 6 7
The goal is to make a game where the above grid is traversable. I've tried list comprehensions and concatenating two lists and it's not producing the right answers.
Concatenating two lists should work. The code for concatenating two lists
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
l3 = [l1 , l2];
l4 = l3+l3
print l4
should yield [[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]