Related
How to use maplist on inner term?
Assuming the KB is:
gate(not(o), i).
gate(not(i), o).
gate(and(o, i), o).
gate(and(i, o), o).
gate(and(B, B), B).
bits([i,o,o,i,i]).
The following is not working:
?- bits(BITS), maplist(gate(not(X), Y), BITS, ANS)
How can I map over the list so that:
[i,o,o,i,i] -> [not(i), not(o), not(o), not(i), not(i)] ->[o,i,i,o,o]
This is to be done on any list length:
:- bits([A,B,C,D,E]), gate(not(A), Anew), gate(not(B), Bnew), gate(not(C), Cnew), gate(not(D), Dnew), gate(not(E), Enew), ANS = [Anew, Bnew, Cnew, Dnew, Enew].
So that the answer would be: ANS = [o, i, i, o, o]
With a helper predicate:
not(A, Z) :-
gate(not(A), Z).
?- bits(BITS), maplist(not, BITS, ANS).
ANS = [o, i, i, o, o],
BITS = [i, o, o, i, i]
But if you are going to add two lines of helper, skip the gate() stuff and write directly:
not(o, i).
not(i, o).
?- bits(BITS), maplist(not, BITS, ANS).
ANS = [o, i, i, o, o],
BITS = [i, o, o, i, i]
If you don't want to do that, or cannot change the database at all, the only way I know is using a lambda library such as yall in SWI Prolog, others may be available to write this:
?- bits(BITS), maplist([In, Out]>>gate(not(In), Out), BITS, ANS).
ANS = [o, i, i, o, o],
BITS = [i, o, o, i, i]
(It might be bad form to name it not, but this is not/2 and the builtin is not/1 so I don't think it will clash).
0
https://docs.google.com/spreadsheets/d/1siXWH_83AFnymr58f72xBmwFlPG1O5ZHZgA1JqQNt0w/edit?usp=sharing Above is test document. I am trying to import information from sheet "AM Trip" to Sheet "BL AM", I only need some columns and only depending on if column T,U, or V has a 1 in it and that seems to be working. My problem is I wish the dropdown list in Cell B2 to change the information being imported which is starting from cell B6. I used below formula in Cell B6, but I have a parse error and not sure how to fix it.
=IFS(B2="That", "=QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W WHERE (U is not null)", false)", B2="This", "=QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W WHERE (V is not null)", false)", B2="Other", "=QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W WHERE (T is not null)", false)")
When I look into the cell the section from "Select" to "is not null)" it is black and I am sure that is the issue but not sure how to fix it. Thanks for any help. Paul.
I'm new at this, but I think I found a couple of minor problems in your sheet.
First, your data validation list for, B2, does not require quotes. And I changed your list of values for B2 from "This","That","Other" to Soca,Salsa,Calypso.
The main issue was that your formula in B6 had quotes around the Query functions.
Lastly, I don't believe that IFS and QUERY work reliably together, so changed it to a multiple IF formula.
So I changed this:
=IFS(B2="Salsa", "=QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W
WHERE (U is not null)", false)",
B2="Soca", "=QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W
WHERE (V is not null)", false)",
B2="Calypso", "=QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W
WHERE (T is not null)", false)")
to this:
=IF( B2="Soca", QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W
WHERE (U is not null)", false),
IF( B2="Salsa", QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W
WHERE (V is not null)", false),
IF(B2="Calypso", QUERY('AM trip'!B7:W42,"SELECT C, E, G, L, O, P, Q, R, S, W
WHERE (T is not null)", false), "Blank")))
Here is a working example - your sheet was locked for editing, so I made a copy.
https://docs.google.com/spreadsheets/d/1HgyZURww4K_UTynxKCwZq6MxLh-58j3fccSC0fvxBa0/edit?usp=sharing
Let me know if you still have issues after seeing this.
I'm working on a Prolog program which should load every nth element of a list into another list. For example:
?- pred([a,b,c,d,e,f,g,h,i,j],3,R) =>
R = [c,f,i]
Where pred is the predicate I'm attempting to implement.
But I honestly don't know how to do it. I know that I need a counter, which represents the current position of my Head, so it's gonna be a /4 predicate summarized into a /3 one later one, like
nth(list,number,result) :- nth(list,number,result,counter) or similiar.
Though, I don't know how to give the head a position number that can reset itself. Because once it hits n (let's say n=3, which is the c in the list), it has to turn back to 1 logically and count again up to 3, give out the element, and so on.
How can I work around these specific problems in my implementation?
An example of how this could be implemented:
nth_pos(L, N, R):-
nth_pos(L, 1, N, [], R).
nth_pos([], I, N, Acc, Acc).
nth_pos([H|T], I, N, Acc, R):-
I =:= N,
append(Acc, [H], Acc2),
I2 is 1,
nth_pos(T, I2, N, Acc2, R).
nth_pos([H|T], I, N, Acc, R):-
I < N,
I2 is I + 1,
nth_pos(T, I2, N, Acc, R).
Test-run:
?- nth_pos([a,b,c,d,e,f,g,h,i,j],3,R).
R = [c, f, i] .
?- nth_pos([a,b,c,d,e,f,g,h,i,j],1,R).
R = [a, b, c, d, e, f, g, h, i|...]
But I honestly don't know how to do it. I know that I need a counter,
which represents the current position of my Head, so it's gonna be a
/4 predicate summarized into a /3 one later one, like
nth(list,number,result) :- nth(list,number,result,counter) or
similiar.
Yes you are on the right track, note that an accumulator is used as well to build up the list so we get pred/5. Hope it helps. Note that this is not the only way to solve it.
I'm new to computer vision and OpenCV, so please mind the immature language. Can someone explain me what's the function of cv2.min?
I have this code that coverts from BGR to RGV (red, green ,value) from OpenCV book:
b, g, r = cv2.split(src)
cv2.min(b, g, b)
cv2.min(b, r, b)
cv2.merge((b, g, r), dst)
where src and dst are source and destination vectors for the image.
My specific questions are:
What is cv2.min doing to b in both the iterations? How are values being assigned to b since it's being evaluated two times for both r and g ?
Please let me know what's happening in this code.
Can someone explain me what's the function of cv2.min?
Look at the doc:
Python: cv2.min(src1, src2[, dst]) → dst
The functions min calculate the per-element minimum of two arrays, or array and scalar
How are values being assigned to b since it's being evaluated two times for both r and g ?
You can break down like this:
cv2.min(b, g, b1) # b1 contains the minimum values between b and g
cv2.min(b1, r, b2) # b2 contains the minimum values between b1 and r
b = b2
What is cv2.min doing to b in both the iterations?
The i-th element of b will be the minimum element in b(i), g(i), r(i):
# Pseudocode
for each row
for each col
b(row, col) = min( b(row, col), g(row, col), r(row, col) )
However, this probably this is not correct, since the V value in HSV is computed as max(R,G,B), and the order of your channels is inverted. To get RGV color space you need to do this:
b, g, r = cv2.split(src)
cv2.max(b, g, b)
cv2.max(b, r, b)
# now 'b' contains the 'v = max(r,g,b)'
cv2.merge((r, g, b), dst)
I'm currently working on a Prolog program and having a lot of trouble figuring out how to implement it.
I want to put a list such as [1,2,2,1] into a function. I want it to multiply into itself to make a new matrix by doing this example.
1 * [1,2,2,1] which would yield [1,2,2,1]
2 * [1,2,2,1] which would yield [2,4,4,2]
2 * [1,2,2,1] which would yield [2,4,4,2]
1 * [1,2,2,1] which would yield [1,2,2,1]
And I want it to create all those together in a matrix like:
[[1,2,2,1],[2,4,4,2],[2,4,4,2],[1,2,2,1]].
Last part would be I want to zero out when I multiply by itself. So the 2nd spot would zero out the second spot making the final matrix:
[[0,2,2,1],[2,0,4,2],[2,4,0,2],[1,2,2,0]].
I want to have a predicate that calls another which makes each list. So heres my thoughts:
main(A,O):-
second(A,A,O).
second([],_,[]).
second([A|As],B,[O|Os]):- %creates the list of lists.
third(A,B,O),
second(As,B,Os).
third(_,[],[]).
third(A,[B|Bs],[O|Os]):-
fourth(A,B,O),
third(A,Bs,Os). %multiplies single digit by list.
fourth(A,B,0):- A == B.
fourth(A,B,O):- O is A * B.
I am getting the correct matrix but can not get the zero diagonal.
I just cant figure out a correct way to get the matrix with zeros down the diagonal. Any thoughts?
You can do the zeroes by introducing indices that indicate row and column you are at and check for a match:
main(A, O) :-
second(A, A, 0, O).
second([], _, _, []).
second([A|As], B, R, [O|Os]) :- %creates the list of lists.
third(A, B, 0, R, O),
R1 is R + 1,
second(As, B, R1, Os).
third(_, [], _, _, []).
third(A, [B|Bs], C, R, [O|Os]) :-
fourth(A, B, C, R, O),
C1 is C + 1,
third(A, Bs, C1, R, Os). %multiplies single digit by list.
fourth(_, _, X, X, 0).
fourth(A, B, C, R, O) :- C \== R, O is A * B.
Check:
| ?- main([1,2,2,1], L).
L = [[0,2,2,1],[2,0,4,2],[2,4,0,2],[1,2,2,0]] ? ;
no
Another interesting approach would be to create a maplist_with_index predicate which works just like maplist but manages an index and implicitly assumes the given predicate accepts the index as its first argument:
maplist_with_index(Pred, L, M) :-
maplist_with_index_(Pred, 0, L, M).
maplist_with_index_(Pred, I, [H|T], [M|Ms]) :-
Pred =.. [P|Pt],
append([P,I|Pt], [H], NewPred),
Call =.. NewPred,
call(Call, M),
I1 is I + 1,
maplist_with_index_(Pred, I1, T, Ms).
maplist_with_index_(_, _, [], []).
Then, the matrix program, using this predicate, looks like:
main(A, O) :-
second(A, A, O).
second(M, A, O) :-
maplist_with_index(third(A), M, O).
third(R, A, E, O) :-
maplist_with_index(fourth(R, E), A, O).
fourth(X, X, _, _, 0).
fourth(C, R, A, B, O) :- C \== R, O is A * B.