wolframalpha conditional solving - wolfram-mathematica

I try to put the following expression, but it cannot recognize the condition(assumptions). How can it be done?
solve[(abs[y-x] +abs[z-x] + abs[y-z] ) z, y, x Assumptions -> x<y<0<z]

Related

Mathematicas: How to solve equation in terms of the variable

I have a quick question. Say you got x+y=2x, and you want to solve this equation in terms of y as some function of x in mathematica, how do you do that?
just use the Solve function. In your case it becomes :
Solve[x + y == 2 x, y]
that gives:
{{y -> x}}

Basic PROLOG counting

I'm new to prolog I'm trying to write a predicate which counts the following:
the predicates name is s2int when given:
s2int(0,Y) it shoud "return" Y=0.
s2int(s(0),Y) => Y=1.
s2int(s(s(0)),Y) => Y=2.
s2int(S(s(s(0))),Y) => Y=3.
and so on..
here is what i tried to write(very poorly),
at first i tried this code:
s2intAux(0,Y).
s2intAux(X,Y):- X = s(Z) ,Y1 is Y+1, s2intAux(Z,Y1).
but whenever i try to run it by typing s2intAux(s(0),Y) i get an error saying :"ERROR: is/2: Arguments are not sufficiently instantiated"
i get that error well because Y is undefined.
then i tried this one:
s2intAux(0,Y).
s2intAux(X,Y):- X = s(Z) ,Y1 is Y+1, s2intAux(Z,Y1).
s2int(X,Y):- Y1 is 0, s2intA(X,Y1).
(i tried to start Y with the value zero but this one didn't work at all)
I've been stuck for a couple of hours now which is why I'm turning to you guys, please help!
thank you.
You need the following to resolve the most trivial case:
s2intAux(0,0).
This will cause s2intAux(0,Y) to be true when Y is instantiated to 0.
In your subsequent lines, you don't have a statement that resolves Z to 0 when you run out of the s(.). For that, you need to take care of the single s(0) case. Then you can do the general case:
s2intAux(X,Y) :- X = s(0), Y is 1.
s2intAux(X,Y) :- X = s(Z), s2intAux(Z,Y1), Y is Y1 + 1.
Note that on the general case, we have to traverse down to get to the Y is 1 before we can unravel back up and finally assign Y to Y1 + 1.
You can also write that first line as just:
s2intAux(s(0),Y) :- Y is 1.
Final answer looks like this:
s2intAux(0,0).
s2intAux(s(0),Y) :- Y is 1.
s2intAux(X,Y) :- X = s(Z), s2intAux(Z,Y1), Y is Y1 + 1.

SWI-Prolog predicates are not called

I am trying to make a game similar to minesweeper and i am trying to count the number of bombs near a point on a map but it only enters in one countneighbour and it stops, how can I make it enter the other countneighbor predicates?
checkneighbours(X,Y) :- nb_setval(vecini,0),
X1 is X-1,
X2 is X+1,
Y1 is Y-1,
Y2 is Y+1,
countneighbours(X1,Y),
countneighbours(X1,Y1),
countneighbours(X1,Y2),
countneighbours(X,Y1),
countneighbours(X,Y2),
countneighbours(X2,Y1),
countneighbours(X2,Y),
countneighbours(X2,Y2),
nb_getval(V,vecini),
write(V).
countneighbours(X,Y) :- map(X,Y,Z),
( Z=:= "O"
-> nb_getval(V,vecini),
V1 is V+1,
nb_setval(vecini,V1)
).
The whole approach is a bit questionable, global variables, copy-pasting instead of using back-tracking, etc. How do you represent the whole playing field?
Anyway, the if-else construct will fail when the else hits. You need to write something like:
( if_condition
-> action
; true
).
if there is no action associated with the else.
But it could be something else, of course... What does map do?

Prolog - Variable as operator

I have an operator stored in a variable Op and two integers are stored in X and Y. Now, I want to do something like (Z is X Op Y), but this syntax seems not to be correct.
Does anybody know if there is a way to do this in Prolog?
Thanks for your answers
you can do it by building the predicate using the =.. operator.
try it like:
compute(X,Y,Op,Z) :-
Eq=..[Op, X, Y],
Z is Eq.
An operator is really just like any other functor.
You can mimic the effect:
operator(Z,X,plus,Y):-Z is X + Y.
operator(Z,X,times,Y):-Z is X * Y.
I tried this on ideone.com for SWI-Prolog with:
OP=times, operator(Z,3,OP,8).
And I got:
OP = times,
Z = 24.

prolog unification resolution

Why does this work:
power(_,0,1) :- !.
power(X,Y,Z) :-
Y1 is Y - 1,
power(X,Y1,Z1),
Z is X * Z1.
And this gives a stack overflow exception?
power(_,0,1) :- !.
power(X,Y,Z) :-
power(X,Y - 1,Z1),
Z is X * Z1.
Because arithmetic operations are only performed on clauses through the is operator. In your first example, Y1 is bound to the result of calculating Y - 1. In the later, the system attempts to prove the clause power(X, Y - 1, Z1), which unifies with power(X', Y', Z') binding X' = X, Y' = Y - 1, Z' = Z. This then recurses again, so Y'' = Y - 1 - 1, etc for infinity, never actually performing the calculation.
Prolog is primarily just unification of terms - calculation, in the "common" sense, has to be asked for explicitly.
Both definitions do not work properly.
Consider
?- pow(1, 1, 2).
which loops for both definitions because the second clause can be applied regardless of the second argument. The cut in the first clause cannot undo this. The second clause needs a goal Y > 0 before the recursive goal. Using (is)/2 is still a good idea to get actual solutions.
The best (for beginners) is to start with successor-arithmetics or clpfd and to avoid prolog-cut altogether.
See e.g.: Prolog predicate - infinite loop

Resources