Hi everyone I'm newbie in prolog and I have such a list: (actually It is output of my predicate not a list )
P = [1/1, 1/3] ;
P = [1/1, 2/3] ;
P = [1/3, 1/1] ;
P = [1/3, 2/1] ;
P = [2/1, 1/3] ;
P = [2/1, 2/3] ;
P = [2/3, 1/1] ;
P = [2/3, 2/1] ;
and I need to remove dublicete terms.For example [1/1,2/3] and [2/3,1/1]is same and I should remove one of them , which one is not important ,How could I do that in prolog ?? Thanks in advance
NOTE I LEARNT THAT findALL should be good way for this but still dont know the answer please help me .
Unless you actually show us your code, it's never going to be possible to give you precise answers.
I assume you have a predicate f/1 such that:
?- f(P).
produces the interactive result you show above. A simple solution is to change your query:
?- f([X,Y]), X < Y.
This will produce the following result:
X = 1/3, Y = 1/1 ;
X = 1/3, Y = 2/1 ;
X = 2/3, Y = 1/1 ;
X = 2/3, Y = 2/1 ;
findall/3 isn't sufficient to solve this particular situation, because you've defined uniqueness in a way that ignores the position in the list. In Prolog (and everything else) [X,Y] and [Y,X] are not equal, so you'd have to find a trick to get this to give you "unique" results.
Related
I need help with looping in Mata. I have to write a code for Beta coefficients for OLS in Mata using a loop. I am not sure how to call for the variables and create the code. Here is what I have so far.
foreach j of local X {
if { //for X'X
matrix XX = [mata:XX = cross(X,1 , X,1)]
XX
}
else {
mata:Xy = cross(X,1 , y,0)
Xy
}
I am getting an error message "invalid syntax".
I'm not sure what you need the loop for. Perhaps you can provide more information about that. However the following example may help you implement OLS in mata.
Load example data from bcuse:
ssc install bcuse
clear
bcuse bwght
mata
x = st_data(., ("male", "parity","lfaminc","packs"))
cons = J(rows(x), 1, 1)
X = (x, cons)
y = st_data(., ("lbwght"))
beta_hat = (invsym(X'*X))*(X'*y)
e_hat = y - X * beta_hat
s2 = (1 / (rows(X) - cols(X))) * (e_hat' * e_hat)
B = J(cols(X), cols(X), 0)
n = rows(X)
for (i=1; i<=n; i++) {
B =B+(e_hat[i,1]*X[i,.])'*(e_hat[i,1]*X[i,.])
}
V_robust = (n/(n-cols(X)))*invsym(X'*X)*B*invsym(X'*X)
se_robust = sqrt(diagonal(V_robust))
V_ols = s2 * invsym(X'*X)
se_ols = sqrt(diagonal(V_ols))
beta_hat
se_robust
end
This is far from the only way to implement OLS using mata. See the Stata Blog for another example using quadcross, I like my example because it preserves a little more of the matrix algebra in the code.
I have a list like below and need to firs add items in each list and then multiply all results 2+4 = 6 , 3+ (-2)=1, 2+3+2=7, -7+1=-6 then 6*1*7*(-6) = -252 I know how to do it by accessing indexes and it works (as below) but I also need to do it in a way that it will work no matter how many sublist there is
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
a= nested_lst[0][0] + nested_lst[0][1]
b= nested_lst[1][0] + nested_lst[1][1]
c= nested_lst[2][0] + nested_lst[2][1] + nested_lst[2][2]
d= nested_lst[3][0] + nested_lst[3][1]
def sum_then_product(list):
multip= a*b*c*d
return multip
print sum_then_product(nested_lst)
I have tried with for loop which gives me addition but I don't know how to perform here multiplication. I am new to it. Please, help
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
for i in nested_lst:
print sum(i)
Is this what you are looking for?
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]] # your list
output = 1 # this will generate your eventual output
for sublist in nested_lst:
sublst_out = 0
for x in sublist:
sublst_out += x # your addition of the sublist elements
output *= sublst_out # multiply the sublist-addition with the other sublists
print(output)
Edit: Here is the code I have so far for generating the Patient Oct-Tuples.
(thanks Anon for giving me the bost on how to calculate weighted probability/setting the seed)
fun genPatients(x:int) =
let
val seed=let
val m=Date.minute(Date.fromTimeLocal(Time.now()))
val s=Date.second(Date.fromTimeLocal(Time.now()))
in Random.rand(m,s)
end;
val survivalrate = ref(1)
val numl = ref(1)
val td = ref(1)
val xray = ref(false)
val count= ref(0)
val emnum= ref(1000)
val ageList = [1, 2, 3, 3];
val xrayList=[false,true];
val age = Random.randRange (0, 3) seed;(* random age*)
val nextInt1 = Random.randRange(0, 1)(* random xray*)
val r1 = Random.rand(1,1)
val nextInt2 = Random.randRange(1, 10000000)(* random td*)
val r2 = Random.rand(1,1)
val r1hold= ref(1);
in
while !count < x do
(
count:= !count + 1;
List.nth(ageList, age);
r1hold:= nextInt1 r1;
td:= nextInt2 r2;
(!emnum,age,survivalrate,numl,[],[],xray,td);
emnum:= !emnum + 1
)
end;
My question now is now how to go about indexing a boolean list?
So I was looking for some help defining my Oct-tuple to finish up my project and lo and behold I find someone posting the entirety of my project hoping for a handout answer. Not only that, but I'm almost certain we're in the same class, and you think posting this the night before the morning the project is due is what a responsible student does? Pretty sure nobody on SO is gonna do your homework for you anyway, in fact I'm not even sure it's allowed.
Maybe do some work and then ask for help when you've actually done anything. Or maybe in the next phase try a little harder.
EDIT: I'll give you something to get you started.
To calculate weighted probability you need a seed.
val seed=let
val m=Date.minute(Date.fromTimeLocal(Time.now()))
val s=Date.second(Date.fromTimeLocal(Time.now()))
in Random.rand(m,s)
end;
Here's one. Then you can calculate probability, at least for the age, like this:
val ageList = [1, 2, 3, 3];
val ageInt = Random.randRange (0, 3) seed;
List.nth(ageList, ageInt)
This was how I decided to do the weighted probability portion, you can equate this to the other weighted sections if you're creative. Good luck.
Hello is there a way to generate params in GMPL as example io have a funcion
min:c[i]*x[i] and constrains that looks like A[i][j]*x[i]=b[i]. Where A[i][j]=1/(i+j-1) and i,j=1,2....,n. c[i]=b[i]=sum(j=1,...,n)1/(i+j-1) where i=1,...,n.
So there is a question is there a way to generate matrix A from equation? or do i need to make this matrix manual in data section ? and one more question is there a good way to find (n) maximum size of this problem when with precision of 2 numbers without modifying objective function ?
param n := 3;
set I := 1..n;
param A{i in I, j in I} := 1/(i+j-1);
param c{i in I} := sum{j in I} 1/(i+j-1);
# or better (may be):
# param c{i in I} := sum{j in I} A[i,j];
display A,c;
end;
The output should look like:
Reading model section from x.mod...
9 lines were read
Display statement at line 8
A[1,1] = 1
A[1,2] = 0.5
A[1,3] = 0.333333333333333
A[2,1] = 0.5
A[2,2] = 0.333333333333333
A[2,3] = 0.25
A[3,1] = 0.333333333333333
A[3,2] = 0.25
A[3,3] = 0.2
c[1] = 1.83333333333333
c[2] = 1.08333333333333
c[3] = 0.783333333333333
GMPL is a subset of AMPL. You may want to read the AMPL book to understand more about the syntax.
I am afraid I don't understand your second question.
I have been trying to find the circumference of a country in Prolog.
I have the finished predicate
borders(Country1, Country2, Length)
and
setof(Item, Condition, Set)
which gives a list of all items into the set which fulfill the condition.
To get the circumference I tried doing this:
circumference(C, Country) :-
setof(X, borders(Country,_,X), Set),
sum_list(Set,C).
sum_list([], 0).
sum_list([H|T], C) :-
sum_list(T, Rest),
C is H + Rest.
... but the output I'm getting is only the length between two countries in the border predicate.
My test:
?– circumference(C,angola).
C = 201 ;
C = 1376 ;
C = 2511 ;
C = 1110.
rules:
borders(angola,namibia,1376).
borders(angola,congo,201).
borders(angola,zambia,1110).
borders(angola,zaire,2511).
Why doesn't C become the sum of these numbers?
I swapped circumference/2 arguments, but you get the idea...
circumference(Country,C) :- aggregate(sum(Y),S^borders(Country,S,Y),C).
The problem has to do with the variable binding for the other country. At first it looks crazy:
?- setof(X, borders(Country,_,X), Set).
Country = angola,
Set = [201] ;
Country = angola,
Set = [1376] ;
Country = angola,
Set = [2511] ;
Country = angola,
Set = [1110].
But if you name that variable it becomes clear what's going on:
?- setof(X, borders(Country,OtherCountry,X), Set).
Country = angola,
OtherCountry = congo,
Set = [201] ;
Country = angola,
OtherCountry = namibia,
Set = [1376] ;
Country = angola,
OtherCountry = zaire,
Set = [2511] ;
Country = angola,
OtherCountry = zambia,
Set = [1110].
It can't group them together because the other country differs, even if you're not interested in the value that binding received. The right solution with setof/3 is to make OtherCountry existentially quantified, which makes it not matter for the purposes of the grouping:
?- setof(X, OtherCountry^borders(Country,OtherCountry,X), Set).
Country = angola,
Set = [201, 1110, 1376, 2511].
In setof/3 and bagof/3, existential quantification is a way of saying that OtherCountry will take on various values but you're not interested in them for the purposes of the grouping. There are lots of scenarios where findall/3 will only produce a single result but you may want separate groupings based on some of the other variables in the Goal parameter, so I think it's good to know about this technique even though findall/3 is probably a more straightforward solution in this case. More discussion can be found on this answer.