how to calculate a function in prolog? - function

We have to find a function in prolog Q(a,b,n) with a < b and with the following way:
Q(a, b, n) = max { k ∈ N | b ^ k - a ^ k < = n}
here is the my code but it is always giving me a "no"
qs(A,B,N,R,C) :- R>N, R is B^C-A^C.
qs(A,B,N,R,C) :- A<B, C1 is C, R is B^C1-A^C1, C1 is C+1, R=<N, qs(A,B,N,R,C1).
qs(A,B,N,R) :- A<B, C is 0, qs(A,B,N,R,C).
For example if I had Q(2,3,18) the output should be 2.

qs(A, B, N, R) :-
A < B,
once((
length(_,K),
B^K - A^K > N
)),
R is K-1.

I believe prolog uses ** for exponentiation, not ^ (assuming that's what you want to do).

Related

Solve for the coefficients of (functions of) the independent variable in a symbolic equation

Using Octave's symbolic package, I define a symbolic function of t like this:
>> syms a b c d t real;
>> f = poly2sym([a b c], t) + d * exp(t)
f = (sym)
2 t
a⋅t + b⋅t + c + d⋅ℯ
I also have another function with known coefficients:
>> g = poly2sym([2 3 5], t) + 7 * exp(t)
g = (sym)
2 t
2⋅t + 3⋅t + 7⋅ℯ + 5
I would like to solve f == g for the coefficients a, b, c, d such that the equation holds for all values of t. That is, I simply want to equate the coefficients of t^2 in both equations, and the coefficients of exp(t), etc. I am looking for this solution:
a = 2
b = 3
c = 5
d = 7
When I try to solve the equation using solve, this is what I get:
>> solve(f == g, a, b, c, d)
ans = (sym)
t 2 t
-b⋅t - c - d⋅ℯ + 2⋅t + 3⋅t + 7⋅ℯ + 5
───────────────────────────────────────
2
t
It solves for a in terms of b, c, d, t. This is understandable since in essence there is no difference between the variables b, c and t. But I was wondering if there was a method to somehow separate the terms (using their symbolic form w. r. t. the variable t) and solve the resulting system of linear equations on a, b, c, d.
Note: The function I wrote here is a minimal example. What I am really trying to do is to solve a linear ordinary differential equation using the method of undetermined coefficients. For example, I define something like y = a*exp(-t) + b*t*exp(-t), and solve for diff(y, t, t) + diff(y,t) + y == t*exp(-t). But I believe solving the problem with simpler functions will lead me to the right direction.
I have found a terribly slow and dirty method to get the job done. The coefficients have to be linear in a, b, ... though.
The idea is to follow these steps:
Write the equation in f - g form (which equals zero)
Use expand() to separate the terms
Use children() to get the terms in the equation as a symbolic vector
Now that we have the terms in a vector, we can find those that are the same function of t and add their coefficients together. The way I checked this was by checking if the division of two terms had t as a symbolic variable
For each term, find other terms with the same function of t, add all these coefficients together, save the obtained equation in a vector
Pass the vector of created equations to solve()
This code solves the equation I wrote in the note at the end of my question:
pkg load symbolic
syms t a b real;
y = a * exp(-t) + b * t * exp(-t);
lhs = diff(y, t, t) + diff(y, t) + y;
rhs = t * exp(-t);
expr = expand(lhs - rhs);
chd = children(expr);
used = false(size(chd));
equations = [];
for z = 1:length(chd)
if used(z)
continue
endif
coefficients = 0;
for zz = z + 1:length(chd)
if used(zz)
continue
endif
division = chd(zz) / chd(z);
vars = findsymbols(division);
if sum(has(vars, t)) == 0 # division result has no t
used(zz) = true;
coefficients += division;
endif
endfor
coefficients += 1; # for chd(z)
vars = findsymbols(chd(z));
nott = vars(!has(vars, t));
if length(nott)
coefficients *= nott;
endif
equations = [equations, expand(coefficients)];
endfor
solution = solve(equations == 0);

How to define a function with Church numerals in lambda-terms?

How can I express the following function by a lambda term?
f(n) = T if n != 0.
F if n = 0.
n stands for a Church numeral.
I know that 0 := λf.λx.x where λx.x is the identity function and all other natural numbers can be expressed by n := λf.λx.f (f ... (f x)) which contains f n times more than the 0-term. E.g. 3 := λf.λx.f (f (f x)).
But how can I derive a valid λ-term for the function above? I think I need a y too for getting the T/F. Therefore I can express the number n by λf.(λxy.fxy), right? But what about the F and T? Is the following a right λ-term for the function above? λf.(λxy.fxy(yFT)) where T=λxy.x and F=λxy.y?
No, you're given the term for n. It is a function that expects two arguments, an f and a z:
isZero n = n ( ;; f, a function, expecting x
;; or the result of (f (f ... (f x) ...))
λx.
;; but we know what we want it to return, always: it is:
F ;; false, for n is _not_ 0
)
( ;; the initial x, in case n is ......... 0!
;; so we know what we want it to be, in case n is 0:
T ;; true, for n _is_ 0
)
and thus
isZero = λn.n(λx.F)T
If n was 0, isZero n will return T; and otherwise, F:
{0}(λx.F)T = T
{1}(λx.F)T = (λx.F)T = F
{2}(λx.F)T = (λx.F)((λx.F)T) = F
{3}(λx.F)T = (λx.F)((λx.F)((λx.F)T)) = F
....

Error: 'Non-constructor pattern not allowed in sequential mode' (Isabelle)

I am trying to define a function Sum f k that sums f from 0 up to k-1 such that
Sum f k = f 0 + ⋯ + f (k - 1).
I have defined it as follows:
fun Sum :: "(nat => nat) => nat => nat" where
"Sum f 1 = f 0"
| "Sum f k = f (k-1) + Sum f (k-1)"
However, this gives the following error message:
Malformed definition:
Non-constructor pattern not allowed in sequential mode.
⋀f. Sum f 1 = f 0
This error message disappears when I define Sum f 0 = f 0, but this is not the function I am trying to define. I can also use function and give a soundness proof myself, but I would be quite surprised if that was necessary
Could someone explain the error message and recommend a workaround/correction?
You can only use constructors in pattern matches. The constructors of nat are 0 and Suc. So if you write 1 as (Suc 0) it should work.

Implementing Extended Euclidean algorithm

I want to make a function combine which given two integers n and m, returns a triple of integers
(a, b, gcd(n, m)) such that:
am + bn = gcd(n, m)
Should not assume that the integers will always be positive.
gcd :: Int -> Int -> Int
gcd n m
| n == m = n
| n > m = gcd (n-m) m
| n < m = gcd n (m-n)
combine :: Int ->Int -> (Int,Int,Int)
x1=1; y1=0; x2=0; y2=1
while ( m /=0 )
( q=div n m ; r=mod n m ; n=m ; m=r
t=x2 ; x2=x1-q*x2 ; x1=t
t=y2 ; y2=y1-q*y2 ; y1=t )
combine n m = (x1,y1,gcd(n,m))
You will find a screen capture picture link. Click me---> ![link] http://prikachi.com/images.php?images/238/8749238o.png Please if someone have a solution and have idea what I could replace to create the function, would be much appreciated.
Test for the function: combine 3 2 should give this result => (1,-1,1)
I think you might be looking for something like this:
combine :: Int ->Int -> (Int,Int,Int)
combine n m = (x1, y1, gcd n m) where
(x1, y1) = gcdext n m
gcdext :: Int -> Int -> (Int, Int)
gcdext n m = gcdexthelper n m 1 0 0 1 where
gcdexthelper n m x1 y1 x2 y2
| m == 0 = (x1, y1)
| otherwise = gcdexthelper m r x1p y1p x2p y2p where
q = div n m
r = mod n m
x1p = x2
y1p = y2
x2p = x1 - q * x2
y2p = y1 - q * y2
You can of course implement the same with a while loop, but I believe recursion is much more readable in Haskell, so I used it here.
And by the way, GCD is a standard library function in Haskell, so no need to write your own.

How to let OCaml function returns a tuple containing a string and a function?

Eventually what I want is what x represents:
let x = (something, (myfunc1 para1));;
so that when calling x, I get a tuple, but when calling (snd x) para, I will get a return value of myfunc1 para.
What I'm trying is like this:
let myfunc2 para1 para2 =
let myfunc1 para2 = ... in
( (fst para1), (myfunc1 para2) );;
And I want to call myfunc2 like this:
let x = myfunc2 para1 to get what I described above. However, what I get is just a function which when called with para1 will return a regular tuple, not a (something, function) tuple
You have a useless para2 parameter in your definition. The correct way is:
let myfunc2 para1 =
let x = ... in
let myfunc1 para2 = ... in
( x, myfunc1 );;
But it would help if we could speak about a concrete example. You are misunderstanding something obvious, but I do not know what.
Here is a concrete example. Suppose we want a function f which accepts a number n and returns a pair (m, g) where m is the square of n and g is a function which adds n to its argument:
let f n =
let m = n * n in
let g k = n + k in
(m, g)
Or shorter:
let f n = (n * n, fun k => n + k)
Now to use this, we can do:
let x = f 10 ;;
fst x ;; (* gives 100 *)
snd x ;; (* gives <fun> *)
snd x 5 ;; (* gives 15, and is the same thing as (snd x) 5 *)
Now let us consider the following bad solution in which we make the kind of mistake you have made:
let f_bad n k =
let m = n * n in
let g k = n + k in
(m, g k)
Now f_bad wants two arguments. If we give it just one, we will not get a pair but a function expecting the other argument. And when we give it that argument, it will return a pair of two integers because (m, g k) means "make a pair whose first component is the integer m and the second component is g applied to k, so that is an integer, too."
Another point worth making is that you are confusing yourself by calling two different things para2. In our definition of f_bad we also confuse ourselves by calling two different things k. The k appearing in the definition of g is not the same as the other k. It is better to call the two k's different things:
let f_bad n k1 =
let m = n * n in
let g k2 = n + k2 in
(m, g k1)
Now, does that help clear up the confusion?