How was this result: a e^(a t) Log[e] computed from D[u[t],t]? - wolfram-mathematica

How was the result computed in Out[43]? Instead of capital E, I used lower case, but I thought the result would have been a*e^(a t) anyways. Computation at [42] has no impact. Left it there as a log.
In[41]:=u[t_] = e^(a t)
Out[41]=u[t_] = e^(a t)
In[42]:=u[4]
Out[42]=e^(4 a)
In[43]:=D[u[t], t]
Out[43]:=a e^(a t) Log[e]

D[E^(a t), t] produces a E^(a t)
but you want to know why D[e^(a t), t] produces a e^(a t) Log[e]
So using x instead of e to completely avoid confusion with Euler's number, this is the step-by-step solution from Mathematica using WolframAlpha
WolframAlpha["D[x^(a t),t]"]
or using the shortcut == to open the WolframAlpha interpreter
If x were E then Log[E] would simplify to 1 and the answer would be a E^(a t).
By the way, you can type in the special e character for Euler's number using Esc e e Esc. It is equivalent to E.

Related

Church numerals in lambda calculus

I need to find a function P such that (using Beta - reduction)
P(g, h, i) ->* (h, i, i+1).
I am allowed to use the successor function succ. From wikipedia I got
succ = λn.λf.λx.f(n f x)
My answer is P = λx.λy.λz.yz(λz.λf.λu.f(z f u))z
but I'm not quite sure about it. My logic was the λx would effectively get rid of the g term, then the λy.λz would bring in the h and i via the yz. Then the succ function would bring in i+1 last. I just don't know if my function actually replicates this.
Any help given is appreciated
#melpomene points out that this question is unanswerable without a specific implementation in mind (e.g. for tuples). I am going to presume that your tuple is implemented as:
T = λabcf.f a b c
Or, if you prefer the non-shorthand:
T = (λa.(λb.(λc.(λf.f a b c))))
That is, a function which closes over a, b, and c, and waits for a function f to pass those variables.
If that is the implementation in mind, and assuming normal Church numerals, then the function you spec:
P(g, h, i) ->* (h, i, i+1)
Needs to:
take in a triple (with a, b, and c already applied)
construct a new triple, with
the second value of the old triple
the third value of the old triple
the succ of the third value of the old triple
Here is such a function P:
P = λt.t (λghi.T h i (succ i))
Or again, if you prefer non-shorthand:
P = (λt.t(λg.(λh.(λi.T h i (succ i)))))
This can be partially cleaned up with some helper functions:
SND = λt.t (λabc.b)
TRD = λt.t (λabc.c)
In which case we can write P as:
P = λt.T (SND t) (TRD t) (succ (TRD t))

Prolog factorial non-recursive

How would you convert this to non-recursive. This code puts out the factorial of N.
fakultaet(0, 1).
fakultaet(N, F) :-
N > 0,
N1 is N – 1,
fakultaet(N1, F1),
F is N * F1.
One way to do it without a recursive call in your definition would be:
factorial(0, 1).
factorial(1, 1).
factorial(N, F) :-
% the call to numlist/3 will fail if N < 2
numlist(2, N, [X|Xs]), % [X|Xs] = [2,3,...,N]
foldl(mult, Xs, X, P), % P = 2*3*...*N
F is P.
mult(A, B, B*A).
This approach avoids recursion on syntactic level in your definition. Both numlist/2 and foldl/4 would most probably have a recursive definition, but you don't have to look at it. This probably falls into the "d) something else I am missing" category from my comment to your question.
if your Prolog has global variables, you can do
fakultaet(N,F) :-
nb_setval(f,1),
forall(between(2,N,I), (nb_getval(f,T), G is T*I, nb_setval(f,G))),
nb_getval(f,F).
This particular usage of nb_setval/2, nb_getval/2 could be partially simulated with assert/1, retract/1, but the resulting program would be very, very inefficient

Extracting symbols from a given formula

I'm trying to extract symbols from formula. Ex:
?- formula((p v q) & (q v r), U).
U = [p, q, v].
What I've done so far:
simbol_formula([],[]).
simbol_formula(negation X, [X]).
simbol_formula(X or Y, [X,Y]).
simbol_formula(X and Y, [X,Y]).
I believe what I did is correct but incomplete. I am stuck. It obviously works for simple formulas but for more complex formulas it does not. I know I have to define something as simbol_formula(F,U) :-. Using recursion somehow or break the given formula into "smaller" ones.
The core problem in your case is the use of defaulty data structures.
In your representation, you cannot, by pattern matching alone, distinguish between:
a symbol
other formulas.
To overcome this shortcoming, I suggest to uniquely mark symbols with the (arbitrary) functor s/1.
For example, the formula (p v q) & (q v r) would be represented as:
(s(p) &vee; s(q)) & (s(q) &vee; s(r))
Then, we can use a DCG to relate formulas to symbols:
symbols(s(X)) --> [X].
symbols(negation(F)) --> symbols(F).
symbols(X ∨ Y) --> symbols(X), symbols(Y).
symbols(X & Y) --> symbols(X), symbols(Y).
Sample query:
?- phrase(symbols((s(p) ∨ s(q)) & (s(q) ∨ s(r))), Ls).
Ls = [p, q, q, r].
I leave defining the suitable operators as an exercise, so that the above compiles.
The above can also be used to enumerate formulas, albeit unfairly:
?- phrase(symbols(Formula), Ls).
Formula = s(_G1010),
Ls = [_G1010] ;
Formula = negation(s(_G1012)),
Ls = [_G1012] ;
Formula = negation(negation(s(_G1014))),
Ls = [_G1014] .

Prolog - simplify derivative

so I just got started with Prolog this semester, and got the homework to implement a pretty basic d(function, variable, derivative) which I did like this:
d(X,X,1) :- !.
d(C,X,0) :- atomic(C). %, (C \= X).
d(X**E,X,E*X**(E-1)).
d(U+V,X,A+B) :- d(U,X,A), d(V,X,B).
d(U-V,X,A-B) :- d(U,X,A), d(V,X,B).
d(U*V,X,DU*V+U*DV) :- d(U,X,DU), d(V,X,DV).
d(U/V,X,(DU*V-U*DV)/(V*V)) :- d(U,X,DU), d(V,X,DV).
I know this is not complete, but it covers all the tasks required in the exercise.
However,
?- d((x*x+2*x+3)/(3*x),x,R).
leads to
R = ((1*x+x*1+ (0*x+2*1)+0)* (3*x)- (x*x+2*x+3)* (0*x+3*1))/ (3*x* (3*x)).
which doesn't look pretty at all. is/2 unfortunately doesn't like my x as it is not a number...
Is there a simple solution to achieve a cleaner result?
I would rather see this as two separate problems:
First, get derivation right (you're probably getting close, depending on your concrete requirements).
Then, work on simplifying expressions on an algebraic level. Exploit algebraic identities, see if applying the laws of commutativity / associativity / distributivity on some subexpressions enable their rewriting into something equivalent (but simpler / more compact).
As a starting point, you may want to look at the somewhat related question "Replacing parts of expression in prolog".
Here's a simplistic sketch how you could do the simplification—using iwhen/2 to safeguard against insufficient instantiation:
expr_simplified(A, B) :-
iwhen(ground(A), xpr_simplr(A,B)).
xpr_simplr(A, B) :-
( atomic(A)
-> A = B
; ( A = X+0 ; A = 0+X ; A = 1*X ; A = X*1 )
-> xpr_simplr(X, B)
; ( A = 0*_ ; A = _*0 )
-> B = 0
; A = X+X
-> B = X*2
; A = X*X
-> B = X**2
; A = X**1
-> B = X
; A =.. [F|Xs0], % defaulty catch-all
maplist(xpr_simplr, Xs0, Xs),
B =.. [F|Xs]
).
Let's see what it does with the expression you gave. We apply expr_simplified/2 until we reach a fixed point:
?- A = ((1*x+x*1+(0*x+2*1)+0)*(3*x)-(x*x+2*x+3)*(0*x+3*1))/(3*x*(3*x)),
expr_simplified(A,B),
expr_simplified(B,C),
expr_simplified(C,D).
A = ((1*x+x*1+(0*x+2*1)+0)*(3*x)-(x*x+2*x+3)*(0*x+3*1))/(3*x*(3*x)),
B = ((x+x+(0+2))*(3*x)-(x**2+2*x+3)*(0+3))/(3*x)**2,
C = ((x*2+2)*(3*x)-(x**2+2*x+3)*3)/(3*x)**2,
D = C. % fixed point reached
As imperfect as the simplifier is, the expression got a lot more readable.
a possibility to get a number is to replace each instance of variable x with a value, visiting the derived tree. You should do writing a clause to match each binary operator, or use a generic visit, like
set_vars(E, Vs, Ev) :-
E =.. [F,L,R],
set_vars(L, Vs, Lv),
set_vars(R, Vs, Rv),
Ev =.. [F,Lv,Rv].
set_vars(V, Vs, N) :- memberchk(V=N, Vs).
set_vars(V, _, V).
that yields
?- d((x*x+2*x+3)/(3*x),x,R), set_vars(R,[x=5],E), T is E.
R = ((1*x+x*1+ (0*x+2*1)+0)* (3*x)- (x*x+2*x+3)* (0*x+3*1))/ (3*x* (3*x)),
E = ((1*5+5*1+ (0*5+2*1)+0)* (3*5)- (5*5+2*5+3)* (0*5+3*1))/ (3*5* (3*5)),
T = 0.29333333333333333
but, there is a bug in your first clause, that once corrected, will allow to evaluate directly the derived expression:
d(X,V,1) :- X == V, !.
...
now, we can throw away the utility set_vars/3, so
?- d((T*T+2*T+3)/(3*T),T,R), T=8, V is R.
T = 8,
R = ((1*8+8*1+ (0*8+2*1)+0)* (3*8)- (8*8+2*8+3)* (0*8+3*1))/ (3*8* (3*8)),
V = 0.3177083333333333.

Robust distance comparing predicate

I need a robust predicate defined by the following code:
CompareResult compareDistance(Point a, Point b, Point c, Point d) {
if (distance(a, b) > distance(c, d))
return Larger;
else if (distance(a, b) == distance(c, d))
return Equal;
else
return Smaller;
}
Due to the floating point arithmetic limitations we can't compute distance exactly (even its square), so if we just directly implement this code, the predicate will not be robust. I tried to find it in CGAL library, but couldn't.
Somewhat close to the predicate I need is compare_distance_to_point(Point p, Point q, Point r) predicate. It returns Smaller if distance(p, q) < distance(p, r), Equal if distance(p, q) == distance(p, r) and Larger otherwise. The first thought is to shift c and d by (c - a) vector, so we could call compare_distance_to_point(a, b, d + (c - a)), but this will violate robustness again. So, does anyone have an idea for adapting it?
If you take a kernel with exact predicates such as
Exact_predicates_inexact_constructions_kernel,
you can use the functor Compare_distance_3 which is a model of the concept CompareDistance_3.
Kernel::Compare_distance_3 cmp;
return cmp(a,b,c,d);

Resources