Prolog arithmetic on tuples - math

The idea is as follows: Suppose I have a list P = [(1,0),(4,3)] or similar. I want to evaluate the polynomial that's defined by this list in the manner: 1X^0 + 4X^3.
To do this, I've written the following:
evaluate(P,X,Y) :- evaluate(P,X,Y,0).
evaluate([],_,S,S).
evaluate([P1,P2|Ps],X,Y,S) :-
S1 is S+P1*X^P2,
evaluate(Ps,X,Y,S1).
Which is supposed to succeed when Y is the sum of the polynomial P, given x=X.
The problem is that when I try and run this code, I get the error:
is/2: Arithmetic: `(',')/2' is not a function
But I have no idea where this is coming from or how to fix it.
I did try splitting the S1 is up in to its segments, but doing that didn't help.
EDIT: Ok, I found out that it's about the way the list is written down. How do I work with tuples in this way within the bounds of Prolog?

Your problem is that your data structure for each item in the list is a tuple as you noted and where you access the values of tuple in the list is not correct.
This
evaluate([P1,P2|Ps],X,Y,S) :-
should be
evaluate([(P1,P2)|Ps],X,Y,S) :-
Notice the parenthesis around P1,P2.
When I run with the change I get
?- evaluate([(1,0),(4,3)],5,Y).
Y = 501.
Also it is common to put the output arguments at the end,
evaluate_01(P,X,Y,0).
as
evaluate_01(P,X,0,Y).
and then change the other predicates as necessary.
evaluate_02(P,X,Y) :- evaluate_02(P,X,0,Y).
evaluate_02([],_,S,S).
evaluate_02([(P1,P2)|Ps],X,S,Y) :-
S1 is S+P1*X^P2,
evaluate_02(Ps,X,S1,Y).

As an interesting option, this can be done with maplist/3 and sumlist/2:
evaluate_poly(Poly, X, R) :-
maplist(evaluate_term(X), Poly, EvaluatedTerms),
sumlist(EvaluatedTerms, R).
evaluate_term(X, (Coeff, Power), TermValue) :-
TermValue is Coeff * (X ^ Power).

Related

How to properly iterate through two-dimensions array and sum all the lengths of elements?

I've encountered a problem when trying to iterate through two dimension array and summing up the lengths of all elements inside in prolog.
I've tried iterating through a simple 1D array and result was just as expected. However, difficulties appeared when I started writing the code for 2D array. Here's my code :
findsum(L):-
atom_row(L, Sum),
write(Sum).
atom_row([Head|Tail], Sum) :-
atom_lengths(Head, Sum),
atom_row(Tail, Sum).
atom_row([], 0).
atom_lengths([Head|Tail], Sum):-
atom_chars(Head, CharList),
length(CharList, ThisLenght),
atom_lengths(Tail, Temp),
Sum is Temp + ThisLenght,
write(ThisLenght).
atom_lengths([], 0).
For example, sum of the elements in array [[aaa, bbbb], [ccccc, dddddd]] should be equal to 18. And this is what I get:
?- findsum([[aaa, bbbb], [ccccc, dddddd]]).
436
false.
The output comes from write(ThisLength) line after each iteration.
Typically it helps (a lot) by splitting the problem into simpeler sub-problems. We can solve the problem, for example, with the following three steps:
first we concatenate the list of lists into a single one-dimension list, for example with append/2;
next we map each atom in that list to the length of that atom, with the atom_length/2 predicate; and
finally we sum up these values, for example with sum_list/2.
So the main predicate looks like:
findsum(LL, S) :-
append(LL, L),
maplist(atom_length, L, NL),
sumlist(NL, S).
Since maplist/3 is a predicate defined in the library(apply), we thus don't need to implement any other predicates.
Note: You can see the implementions of the linked predicates by clicking on the :- icon.
For example:
?- findsum([[aaa, bbbb], [ccccc, dddddd]], N).
N = 18.

Permutation predicate in Prolog

I have written the following in Prolog (I am using version 7.4.0-rc1), trying to define a predicate insertPermutation/2 which is true if and only if both arguments are lists, one a permutation of the other.
delete(X,[X|T],T). % Base case, element equals head.
delete(X,[A|B],[A|C]) :- delete(X,B,C). % And/or repeat for the tail.
insert(X,Y,Z) :- delete(X,Z,Y). % Inserting is deletion in reverse.
insertPermutation([],[]). % Base case.
insertPermutation([H|T],P) :- insertPermutation(Q,T), insert(H,Q,P). % P permutation of T, H inserted.
I have already been made aware that delete is not a good name for the above helper predicate. We are required to write these predicates, and we cannot use the built-in predicates. This is why I wrote the above code in this way, and I chose the name I did (because I first wrote it to delete an element). It is true if and only if the third argument is a list, equal to the list in the second argument with the first instance of the first argument removed.
The insertPermutation predicate recursively tests if P equals a permutation of the tail of the first list, with the head added in any position in the permutation. This way it works to the base case of both being empty lists.
However, the permutation predicate does not behave the way I want it to. For instance, to the query
?- insertPermutation([1,2,2],[1,2,3]).
Prolog does not return false, but freezes. To the query
?- insertPermutation(X,[a,b,c]).
Prolog responds with
X = [a, b, c] ;
X = [b, a, c] ;
X = [c, a, b] ;
X = [a, c, b] ;
X = [b, c, a] ;
X = [c, b, a] ;
after which it freezes again. I see these problems are related, but not how. Can someone point out what case I am missing?
Edit: Two things, this is homework, and I need to solve this problem using an insert predicate. I wrote this one.
The answer is to change the last line
% P permutation of T, H inserted.
insertPermutation([H|T],P) :-
insertPermutation(Q,T),
insert(H,Q,P).
% P permutation of T, H inserted.
insertPermutation(P,[H|T]) :-
insertPermutation(Q,T),
insert(H,Q,P).
The use cases only needed to check if the first element is a permutation of the latter, not the other way around (or vice versa). Anti-climatic, but the answer to my problem.

Prolog sum all throwing error

I have to write a predicates that sums all the items in a list. The items can be a list with list of list in it. for example,
sum1([1,[2,3],4,[5]],X).
i have some code that SHOULD work, but is giving an arguments are not sufficiently instantiated error. I am very new to prolog but these look alright to me. here is my full code for this predicate (function)
suml([],0).
suml([H|T],X) :- atomic(H),S2 is H + X, suml(T,S2).
suml([H|T],X) :- suml(H,S1), S3 is S1 + X, suml(T,S3).
Change the order of the following two conditions: S2 is H + X, suml(T,S2) and change the way you use is/2. Although this is logic programming, the order of the conditions in a rule matters. Put sum1(T, S2) first because you need S2 to be instantiated before computing the sum. Second, is arithmetically evaluates the right part and unifies it with the left part. So, you actually want to sum S2 and H to get X:
suml([H|T],X) :- atomic(H), suml(T,S2), X is H + S2.

Passing constant arguments to a multidimensional rootfinder in Sage

I have a five-dimensional rootfinding problem I'd like to solve from within a Sage notebook, but the functions I wish to solve depend on other parameters that shouldn't be varied during the rootfinding. Figuring out how to set up a call to, say, scipy.optimize.newton_krylov has got me stumped. So let's say I have (with a,b,c,d,e the parameters I want to vary, F1,F2,F3,F4,F5 the five expressions I which to solve to be equal to F1Val,F2Val,F3Val,F4Val,F5Val, values I already know, and posVal another known parameter)
def func(a, b, c, d, e, F1Val, F2Val, F3Val, F4Val, F5Val, posVal):
F1.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F2.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F3.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F4.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
F5.subs(x1=a,x2=b,x3=c,x4=d,x5=e,position=posVal)
return (F1-F1Val, F2-F2Val, F3-F3Val, F4-F4Val, F5-F5Val)
and now I want to pass this to a rootfinding function to yield func = (0,0,0,0,0). I want to pass an initial guess (a0, b0, c0, d0, e0) vector and a set of arguments (F1Val, F2Val, F3Val, F4Val, F5Val, posVal) for the evaluation, but I can't figure out how to do this. Is there a standard technique for this sort of thing? The multidimensional rootfinders in scipy seem to be lacking the args=() variable that the 1D rootfinders offer.
Best,
-user2275987
Well, I'm still not sure how to actually employ the Newton-Raphson method here, but using fsolve works, for functions that accept a vector of variables and a vector of constant arguments. I'm reproducing my proof of concept here
def tstfunc(xIn, constIn):
x = xIn[0]
y = xIn[1]
a = constIn[0]
b = constIn[1]
out = [x+2*y+a]
out.append(a*x*y+b)
return out
from scipy.optimize import fsolve
ans = fsolve(tstfunc, x0=[1,1], args=[0.3, 2.1])
print ans

Prolog - Declaring arithmetic clauses

A simple question, how would I go about declaring a clause which would produce the number specified +1 +2 and +3? I have tried:
addup(Thenumber,Thenumber+1).
addup(Thenumber,Thenumber+2).
addup(Thenumber,Thenumber+3).
but when I run it with say, Thenumber=5, it just returns 5+1 5+2 5+3. I have tried using 'is' to force it to evaluate but it doesn't seem to work. Any help would be appreciated.
Try this:
addup(X, Y) :- Y is X + 1.
or
addup(X, X+1).
and you question should be addup(2, X)
then X should be 3. If you want to parametrize your addup parameter just make it:
addup(X, Y, X + Y).
and ask with addup(5, 6, X).

Resources