I'm new to Prolog and my task requires finding all equilateral triangles which are built from DB points. As a result, I'm getting identical points of the triangles. I do not understand what the problem is. Help me pls!
:- dynamic
point/3.
%?Point_Sign, ?Abscissa, ?Ordinate
db_filling:-
point(_,_,_),!.
db_filling:-
assert(point(a,1,1)),
assert(point(b,1,2)),
assert(point(c,1,3)),
assert(point(d,2,2)),
assert(point(e,3,3)),
assert(point(f,-1,1)),
assert(point(g,-2,1)),
assert(point(h,-2,2)),
assert(point(i,-3,3)),
assert(point(j,-3,-1)),
assert(point(k,-3,-2)),
assert(point(l,-3,-3)),
assert(point(n,-1,-1)),
assert(point(m,-3,0)),
assert(point(o,3,0)),
assert(point(p,0,3)).
% +List
points_main(Xs):-
db_filling,
findall(Xs1,equilateral_triangles(Xs1),Xs).
% +Points
equilateral_triangles([P1,P2,P3]):-
point(P1,X1,Y1),
point(P2,X2,Y2),
point(P3,X3,Y3),
L1 is sqrt((X2 - X1)^2 + (Y2 - Y1)^2),
L2 is sqrt((X3 - X2)^2 + (Y3 - Y2)^2),
L3 is sqrt((X1 - X3)^2 + (Y1 - Y3)^2),
L1 = L2,
L2 = L3.
Result:
?- points_main(Res).
Res = [[a, a, a], [b, b, b], [c, c, c], [d, d, d], [e, e, e], [f, f, f], [g, g|...], [h|...], [...|...]|...].
You have a couple of fundamental issues here.
From memory, the two-dimensional coordinates of an equilateral triangle cannot all be integers. You must have at least one irrational number. Computers are not good at representing irrational numbers.
So, you're also confusing real maths with computer maths. You can't simply get the sqrt and check for equality.
To illustrate I produced these three points:
point(j1,0,0).
point(j2,1,0).
point(j3,0.5,0.866025403784439). % 1/2 & sqrt(3)/2
That's an equilateral triangle.
When I run that with your code, the lengths that get produced are like this:
[1.0,1.0000000000000004,1.0000000000000004]
They are all effectively 1.0, but of course 1.0000000000000004 is not 1.0. So, even this doesn't comeback as a equal.
So you are really forced to check the confidence as an epsilon to say two numbers are equal.
Here's what I did to do that:
points_main(Xs):-
findall(Xs1,equilateral_triangles(Xs1),Xs).
equilateral_triangles([P1,P2,P3]):-
point(P1,X1,Y1),
point(P2,X2,Y2),
P1 \= P2,
point(P3,X3,Y3),
P1 \= P3,
P2 \= P3,
L12 is sqrt((X2 - X1)^2 + (Y2 - Y1)^2),
L23 is sqrt((X3 - X2)^2 + (Y3 - Y2)^2),
L31 is sqrt((X1 - X3)^2 + (Y1 - Y3)^2),
D1223 is abs(L12 - L23),
D1223<0.00000001,
D2331 is abs(L23 - L31),
D2331<0.00000001,
D3112 is abs(L31 - L12),
D3112<0.00000001.
Now, if I run that against my points above I get this:
?- points_main(Xs).
Xs = [[j1, j2, j3], [j1, j3, j2], [j2, j1, j3], [j2, j3, j1], [j3, j1, j2], [j3, j2, j1]].
All combinations of the above three points - so, yes, they are all equilateral triangles.
If I run against your original points, as expected, there are no equilateral triangles.
A few side notes.
(1) I removed all of the assert code. It wasn't helpful nor necessary to get your code working.
(2) you could define my j3 point as point(j3,0.5,X) :- X is sqrt(3)/2. and your original maths would work. However, this is just lucky. When dealing will floating-point numbers you can never be sure that two numbers are equal even though they should be.
(3) I introduced P1 \= P2, etc, to prevent the points unifying to themselves. That's why you got [a,a,a],... etc.
The reason you get odd results is that your
...
point(P1,X1,Y1),
point(P2,X2,Y2),
point(P3,X3,Y3),
...
is producing the Cartesian Product of your set of points. If you had just 4 points defined, this would produced 43 (64) possible results.
Since you are talking triangles here, where for a given 3 points A, B, and C, whether the triangle they describe is labelled ABC, ACB, BAC, BCA, CAB, CBA is irrelevant: they all describe exactly the same triangle, so what your are looking for are combinations: a selection of items from a set where order is not important, so {a,b,c] and {c,b,a] are the same combination.
In Prolog, getting combinations is easy:
combination( [] , [] ) .
combination( [H|T] , [H|T2] ) :- combination(T,T2).
combination( [_|T] , T2 ) :- combination(T,T2).
The only caveat is that the 2nd argument to combination/2 must be seeded as a list of the desired length. To draw combinations of 3 things from a set of 5 things is as easy as:
combination( [a,b,c,d,e] , [X,Y,Z] ).
Once you have that, getting the set of points from which to draw is easy, too, using findall/3 or setof/3:
setof( p(P,X:Y) , point(P,X,Y) , Ps ) ,
The one tricky thing is that floating point arithmetic leaves much to be desired, what with floating point jitter and all. This is how I'm computing distance:
%-----------------------------------------------------------------------
% computes the distance between two points on the Cartesian plane.
% the distance, D, is returned as an integer with an implied scale of 5,
% so the distance 1.23456789 is returned as 123457 (rounded up)
% ----------------------------------------------------------------------
distance( X1:Y1 , X2:Y2 , D ) :-
V is sqrt( (X2-X1)^2 + (Y2-Y1)^2 ) ,
D is round( 100000 * V )
.
Putting it together, we get this (https://swish.swi-prolog.org/p/zHAfMTtA.pl):
equilateral_triangle( T ) :-
setof( p(P,X:Y) , point(P,X,Y) , Ps ) ,
combination(Ps,[A,B,C]),
equilateral_triangle(A,B,C,T)
.
%--------------------------------------------------------------------------------
% combination( +Set, +Combination )
%
% Takes a set (a list of distinct things) of M things and produces
% on backtracking all the combinations of M things taken N at a time.
%
% Combination, the 2nd argument MUST be initialize as a list of the desired
% length. For example, to get all the combination of 8 things taken 3 at a time,
% you'd say something like this:
%
% ?- length(C,3), combination([a,b,c,d,e],C).
%
% And get back
%
% C = [a, b, c]
% C = [a, b, d]
% C = [a, b, e]
% C = [a, c, d]
% C = [a, c, e]
% C = [a, d, e]
% C = [b, c, d]
% C = [b, c, e]
% C = [b, d, e]
% C = [c, d, e]
%
%--------------------------------------------------------------------------------
combination( [] , [] ) .
combination( [H|T] , [H|T2] ) :- combination(T,T2).
combination( [_|T] , T2 ) :- combination(T,T2).
%--------------------------------------------------------------
% 3 points comprise an equilateral triangle if the triangle
% that they describe has legs of equal length
%--------------------------------------------------------------
equilateral_triangle( p(A,Pa), p(B,Pb), p(C,Pc) , t(A,B,C) ) :-
distance(Pa,Pb,D),
distance(Pb,Pc,D),
distance(Pc,Pa,D).
%-----------------------------------------------------------------------
% computes the distance between two points on the Cartesian plane.
% the distance, D, is returned as an integer with an implied scale of 5,
% so the distance 1.23456789 is returned as 123457 (rounded up)
% ----------------------------------------------------------------------
distance( X1:Y1 , X2:Y2 , D ) :-
V is sqrt( (X2-X1)^2 + (Y2-Y1)^2 ) ,
D is round( 100000 * V )
.
point( a , 0 , 0 ) .
point( b , 5 , 0 ) .
point( c , 2.5 , 4.33012701892 ) .
point( d , 1 , 1 ) .
point( e , 6 , 1 ) .
point( f , 3.5 , 5.33012701892 ) .
I want to find out Least Common Multiple(LCM) of more than two numbers. I know the formula of lcm(a,b) = (a * b) / gcd(a,b). Let's say, I have an array of numbers: [2, 6, 8, 13] and the the lcm should be modulo M = 1000000007.
I have seen below code to calculate the LCM of multiple numbers but I am not understanding how the calculation is going on with both the loops.
int arr[] = {2, 6, 8, 13}, n = 4
long long int ans=1;
long long int M=1000000007;
for(int i=0;i<n;i++) // Calculating LCM
{
for(int j=i+1;j<n;j++)
{
arr[j]=arr[j]/__gcd(arr[i],arr[j]);
}
ans=((ans%M)*(arr[i]%M))%M;
}
return (ans)%M;
Can anyone please help me to understand the LCM calculation in the above code?
Knowing that gcd(a, b) represents the product of all the prime factors shared by a and b, what is the significance of a / gcd(a,b)?
a / gcd(a, b) is equal to the prime factors of a that are not in b.
Therefore, when you multiple that quantity by b, you get a product of all the prime factors of b and all the prime factors of a that are not in b. This is precisely lcm(a, b).
Let's extend that to an arbitrary number of integers.
The lcm(a, b) is a times all the prime factors of b not in a or:
a * (b / gcd(a, b)) = (a * b) / gcd(a, b)
Easy enough, you knew that already.
But if we have a third number, lcm(a, b, c) is a times all the prime factors of b not in a times all the prime factors of c in neither a nor b. Well, the first part is straight forward, it's the same as above:
lcm(a, b, c) = lcm(a, b) * (all the prime factors of c in neither a nor b)
How to calculate all the prime factors of c in neither a nor b might not be obvious at first, but it's not overly complicated:
all the prime factors of c in neither a nor b = c / (gcd(a, c) * gcd(b, c))
Which means that
lcm(a, b, c) = lcm(a, b) * c / (gcd(a, c) * gcd(b, c))
lcm(a, b, c) = (a * b * c) / (gcd(a, b) * gcd(a, c) * gcd(b, c))
And now, you can generalize easily:
lcm(a[0], ..., a[N]) = prod(a[0], ..., a[N]) / pairwise_gcd(a[0], ..., a[N])
But a more relevant formulation is the recursive version:
lcm(a[0], ..., a[N]) = lcm(a[0], ..., a[N-1]) * a[N] / (gcd(a[0], a[N]) * ... * gcd(a[N-1], a[N]))
Or:
lcm(a[0], ..., a[N]) = a[0] * lcm(a[1] / gcd(a[0], a[1]), ..., a[N] / gcd(a[0], a[N]))
Here's an attempt at translating your code snippet to psuedocode
Compare this to the last definition of lcm on an array, I tried to make them appear similar.
given int array = arrayOfNums
int product := 1
for number in arrayOfNums
remove all prime factors of number from all subsequent array elements
product = product * number
product is now the lcm of arrayOfNums
Hopefully, that wasn't too confusing; I admit it may not be much of an explanation, but it is a starting point. Please let me know if anything is still unclear.
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 knew that converting a regular expression to a NFA, there is a algorithm.
But I was wondering if there is a algorithm to convert a NFA to regular expression.
If there is, what is it?
And if there isn't, I am also wondering if all NFA can convert to a regular expression.
Is there a NFA that a regular expression that cannot represent?
Thank you! :D
Here is an algorithm where each transition is incrementally replaced with a regex, until there is only an initial and final state: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]
An NFA can be written as a system of inequalities (over a Kleene algebra), with one variable for each state, one inequality q ≥ 1 for each final state q and one inequality q ≥ x r for each transition on x from state q to state r. This is a right-affine fixed point system, over a Kleene algebra, whose least fixed point solution gives you, for any q, the regular expression recognized by the NFA that has q as the start state. The system can be collated, so that all the inequalities q ≥ A, q ≥ B, ..., q ≥ Z, for each given q, are combined into q ≥ A + B + ... Z. The result is a matrix system 𝐪 ≥ 𝐚 + H 𝐪, where 𝐪 is the vector of all the variables, 𝐚 the vector of the affine coefficients - 0's for non-final states, 1's for final states, but those details are not important for what follows; and H is the matrix of all the linear coefficients.
To solve a right-affine system, do so one variable at a time. In Kleene algebra, the least fixed point solution to x ≥ a + bx is x = b* a. This applies both singly and matrix-wise, so that the least fixed point solutuion to 𝐪 ≥ 𝐚 + H 𝐪, in matrix form is 𝐪 = H* 𝐚.
Matrices over Kleene algebras form a Kleene algebras, with matrix addition and matrix multiplication, respectively, for the sum and product operators and the matrix star for the Kleene star. Finding the matrix star is one and the same process as solving the corresponding system 𝐪 ≥ 𝐚 + H 𝐪.
A Generic Example:
Consider the NFA with states q, r, s, with q the start state and s the only final state, and with transitions:
a: q → q, b: q → r, c: q → s,
d: r → q, e: r → r, f: r → s,
g: s → q, h: s → r, i: s → s.
Let (x, y, z) = (0, 0, 1) denote the corresponding affine coefficients. Then, the corresponding right-affine system is:
q ≥ x + a q + b r + c s,
r ≥ y + d q + e r + f s,
s ≥ z + g q + h r + i s.
Solve for s, first, to obtain
s = i* (z + g q + h r) = i* z + i* g q + i* h r.
Substitute in the other inequalities to get:
q ≥ x + c i* z + (a + c i* g) q + (b + c i* h) r,
r ≥ y + f i* z + (d + f i* g) q + (e + f i* h) r.
Rewrite this as
q ≥ x' + a' q + b' r,
r ≥ y' + d' q + e' r,
where
x' = x + c i* z, a' = a + c i* g, b' = b + c i* h,
y' = y + f i* z, d' = d + f i* g, e' = e + f i* h.
Solve for r to get
r = e'* (y' + d' q) = e'* y' + e'* d' q.
Substitute into the inequality for q to get
q ≥ (x' + b' e'* y') + (a' + b e'* d') q
and rewrite this as
q ≥ x" + a" q
where
x" = x' + b' e'* y', a" = a' + b e'* d'.
Finally, solve this for q to get
q = a"* x".
This is also the general form for that embodies the generic fail-safe solution for NFA's with 3 states.
Since q is the start state, then a"* x" is the regular expression sought for, with a", x", a', b', d', e', x', y', x, y and z as indicated above. If you try to in-line substitute them all, the expression will blow up to a size that is exponential in the number of states and will be large in size even for three states.
An Optimized Example:
Consider the system for the NFA whose states are q, r, s, with q the start state, s the final state, and the transitions
a: q → r, a: q → q, b: q → q, b: q → s, a: s → s, b: s → s
The corresponding right-affine system is
q ≥ a r + a q + b q
r ≥ b s
s ≥ 1 + a s + b s
Solve for s first:
s ≥ 1 + a s + b s = 1 + (a + b) s ⇒ s = (a + b)*.
Substitute into the inequality for r and solve to find the least fixed point:
r ≥ b (a + b)* ⇒ r = b (a + b)*.
Finally, substitute into the inequality for q and solve to find the least fixed point:
q ≥ a b (a + b)* + (a + b) q ⇒ q = (a + b)* a b (a + b)*.
The resulting regular expression is (a + b)* a b (a + b)*. So, with chess-playing strategizing, simpler and optimal forms for the solution can be found.