So I have this piece of prolog code:
my_avalia(A, R) :-
A == "Koza" -> koza(R, 0, 0, e, 89).
koza(R, _, _, _, 87) :-
!,
write(R).
koza(R, X, Y, V, C) :-
movex(V, X, X1),
movey(V, Y, Y1),
confirma(X1, Y1, Z),
Z == 1 -> (append(R, [emFrente], U),
L is (C - 1),
koza(U, X1, Y1, V, L)).
The matter is that when I write the "R" at koza(), it has the correct values, however it ends up with a empty list in my_avalia when I call it like this:
my_avalia("Koza",R).
My recursion might be incorrect but I don't really know what's wrong with it.
Thanks in advance.
The other functions:
movex(X,Y,R):-(X==o)->(R is Y-1).
movex(X,Y,R):-(X==n)->(R is Y).
movex(X,Y,R):-(X==s)->(R is Y).
movex(X,Y,R):-(X==e)->(R is Y+1).
movey(X,Y,R):-(X==n)->(R is Y-1).
movey(X,Y,R):-(X==s)->(R is Y+1).
movey(X,Y,R):-(X==o)->(R is Y).
movey(X,Y,R):-(X==e)->(R is Y).
confirma(X,Y,R):-(santafe(X,Y),R is 1); (R is 0).
I figured it out.. Such a silly mistake.
koza([], _, _, _, 87) :-!.
koza(R, X, Y, V, C) :-
movex(V, X, X1),
movey(V, Y, Y1),
confirma(X1, Y1, Z),
Z == 1 -> (L is (C - 1),
koza(U, X1, Y1, V, L),
append(U, [emFrente], R)).
Thanks anyway.
koza([], _, _, _, 87) :-!.
koza(R, X, Y, V, C) :-
movex(V, X, X1),
movey(V, Y, Y1),
confirma(X1, Y1, Z),
Z == 1 -> (L is (C - 1),
koza(U, X1, Y1, V, L),
append(U, [emFrente], R)).
Related
I have two arrays of Float64: arr_x and arr_y. I have a 2D array arr_z storing some function z(x,y) computed on the set of points (x,y) with x in arr_x, y in arr_y.
How can I do a 2D interpolation of arr_z using arr_x and arr_y? That is, I would like a function itp_z(x,y) which does the equivalent of linear interpolation but now in 2D on the array arr_z.
Interpolations.jl should work for that:
using Interpolations, Plots
# Data
x = range(-2, 3, length=20)
y = range(3, 4, length=10)
z = #. cos(x) + sin(y')
# Interpolatant object
itp = LinearInterpolation((x, y), z)
# Fine grid
x2 = range(extrema(x)..., length=300)
y2 = range(extrema(y)..., length=200)
# Interpolate
z2 = [itp(x,y) for y in y2, x in x2]
# Plot
p = heatmap(x2, y2, z2, clim=(-2,2), title="Interpolated heatmap")
scatter!(p, [x for _ in y for x in x], [y for y in y for _ in x], zcolor=z[:]; lab="original data", clim=(-2,2))
gives you
I have a number of points in three dimensions that I am plotting using plot3d from the rgl library. The point coordinates are stored in three vectors x, y and z. I would like to annotate each point in the plot with its coordinates, i. e. (x, y, z)
Using text3d I would have to create a vector of label strings from the coordinate vectors. The only solution I found so far is looping over the coordinate vectors:
library(rgl);
x = c(1, 2, 3)
y = c(4, 5, 6)
z = c(7, 8, 9)
label_vector = 1:3
for (i in 1:3) {
l = paste("(", x[i], y[i], z[i], ")", collapse=" ")
label_vector[i] = l
}
plot3d(x, y, z)
text3d(x, y, z, label_vector)
Is there a more elegant way to do this? Ideally, but not necessarily, in the (x, y, z) instead of the ( x y z ) format from my example.
paste works on vectors. from ?paste
If the arguments are vectors, they are concatenated term-by-term to
give a character vector result.
So, you can generate the label_vector by running just one line
label_vector = paste( "(" , x, ", " , y , ", " , z , ")", sep = "")
I think this will give you a lot cleaner output
library(rgl);
x = c(1, 2, 3)
y = c(4, 5, 6)
z = c(7, 8, 9)
label_vector <- paste0("[",x,",",y,",",z,"]")
plot3d(x, y, z, col= "red" , type ="s", radius=0.02)
text3d(x, y, z, label_vector,adj=c(-0.25,0))
Thank you
I would like to create a lambda calculus function P such that (P x y z) gives ((x y)(x P)(P z)). I have tried using variants of the Y-combinator/Turing combinator, i.e. functions of the form λg.(g g), since I need to reproduce the function itself, but I can't see any way forward. Any help would be greatly appreciated.
Basically you want to solve "β-equation" P x y z = (x y) (x P) (P z).
There is a general method of solving equations of the form M = ... M ....
You just wrap the right-hand side in a lambda, forming a term L, where all occurrences of M are replaced by m:
L = λm. ... m ...
Then using a fixed-point combinator you get your solution. Let me illustrate it on your example.
L = λp. (λxyz. (x y) (x p) (p z)),
where λxyz. is a shorthand for λx. λy. λz.
Then, P = Y L, unfolding Y and L we get:
P = (λf. (λg. f (g g)) (λg. f (g g))) (λp. (λxyz. (x y) (x p) (p z)))
->β
(λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g))
// the previous line is our "unfolded" P
Let's check if P does what we want:
P x y z
= // unfolding definition of P
(λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) x y z
->β
((λp. (λxyz. (x y) (x p) (p z))) ((λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)))) x y z
->β
(λxyz. (x y) (x ((λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)))) (((λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g))) z)) x y z
->β
(x y) (x ((λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)))) (((λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g))) z)
= // folding 1st occurrence of P
(x y) (x P) (((λg. (λp. (λxyz. (x y) (x p) (p z))) (g g)) (λg. (λp. (λxyz. (x y) (x p) (p z))) (g g))) z)
= // folding 2nd occurrence of P
(x y) (x P) (P z)
Q.E.D.
U-combinator should help you create a self-referential lambda abstraction.
Here is Ω, the smallest non-terminating program that exhibits the U-combinator nicely.
((λf. (f f))
(λf. (f f)))
If you can give it a name
Ω := λf.(f f)
Here's what it might look like with your abstraction
((λP. (P P x y z))
(λP. λx. λy. λz. ((x y) (x P) (P z))))
Or using Ω
λx. λy. λz. Ω (λP. λx. λy. λz. ((x y) (x P) (P z))) x y z
I have two vectors which I want to integrate in Matematica. Let the vectors be
r = {x, y};
Q = {x1, y1};
then I write this command
Integrate[
1/Norm[-((a*Q)/c) + r],
{a, 0, 1},
Assumptions -> (a*x1)/c > x && x ->
Real && (a*x1)/c ->
Real && x > 0 && (a*y1)/c -> Real && (a*y1)/c > y && y > 0
]
Where c is a positive constant. The output yields the same
Integrate[1/Norm[-((a Q)/c) + r], {a, 0, 1},
Assumptions -> (a x1)/c > 0 && (a x1)/c > x && x ->
Real && (a x1)/c -> Real && x > 0 && (a y1)/c > y && y > 0]
Could you please tell me where I am making a mistake?
I would be grateful if you could help me,
Thanks
r = {x, y};
Q = {x1, y1};
Integrate[1/Sqrt[(-((a*Q)/c) + r).(-((a*Q)/c) + r)], {a, 0, 1},
Assumptions -> Element[{x, y, x1, y1, a, c}, Reals]]
Returns:
(*
(1/Sqrt[x1^2 + y1^2])c (-Log[c (-x x1 - y y1 +Sqrt[(x^2 + y^2) (x1^2 + y1^2)])]+
Log[x1^2 + y1^2 - c (x x1 + y y1) +
(c Sqrt[(x1^2 + y1^2) (x1^2 + c^2 (x^2 + y^2) + y1^2 - 2 c (x x1 + y y1))])/
Abs[c]])
*)
I have a problem while programing in Mathematica 8, here is my code:
f[t_, y_] := {y, y};
RungeKutta3[a_, b_, Alpha_, n_, f_] :=
Module[{h, j, k1, k2, k3},
h = (b - a)/n;
Y = T = Table[0, {100 + 1}];
Y[[1]] = Alpha;
T[[1]] = a;
For[j = 1, j <= n, ++j,
k1 = f[T[[j]], Y[[j]]];
k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)h];
Y[[j + 1]] = Y[[j]] + h/6(k1 + 4 k2 + k3);
(* Print[j, "----->", Y[[j]]];*)
T[[j + 1]] = T[[j]] + h;
];];
RungeKutta3[0., 1., {300., 500}, 2, f];
The thing is, I'm trying to implement a Runge-Kutta method. And I was successful actually, but only with a function f[x_] that had 1 dimension. This code is for 2 dimensions, but it simply doesn't work and I don't know why. Here is an example for a code with 1 dimension only (notice that I have to change the first line to define the function and the last line, when I call "RungeKutta3").
f[t_, y_] := y;
RungeKutta3[a_, b_, Alpha_, n_, f_] :=
Module[{h, j, k1, k2, k3},
h = (b - a)/n;
Y = T = Table[0, {100 + 1}];
Y[[1]] = Alpha;
T[[1]] = a;
For[j = 1, j <= n, ++j,
k1 = f[T[[j]], Y[[j]]];
k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)*h];
Y[[j + 1]] = Y[[j]] + h/6*(k1 + 4 k2 + k3);
(* Print[j, "----->", Y[[j]]];*)
T[[j + 1]] = T[[j]] + h;
];];
RungeKutta3[0., 1., 300., 100, f];
To sum up, how do I implemented the Runge-Kutta method for a function with 2 dimensions??
If you could help me out I would be grateful.
Thanks in advance!
PS: the Runge-Kutta method is order 3
----------------------
Problem solved! Check the code, if anybody needs help with anything, just ask!
f[t_, y1_, y2_] := 3 t*y2 + Log[y1] + 4 y1 - 2 t^2 * y1 - Log[t^2 + 1] - t^2;
F[t_, {y1_, y2_}] := {y2, f[t, y1, y2]};
RungeKutta3[a_, b_, [Alpha]_, n_, f_] :=
Module[{h, j, k1, k2, k3, Y, T, R},
h = (b - a)/n;
Y = T = Table[0, {n + 1}];
Y[[1]] = [Alpha]; T[[1]] = a;
For[j = 1, j <= n, ++j,
k1 = f[T[[j]], Y[[j]]];
k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)*h];
Y[[j + 1]] = Y[[j]] + h/6*(k1 + 4 k2 + k3);
T[[j + 1]] = T[[j]] + h;
];
R = Table[0, {n + 1}];
For[j = 1, j <= n + 1, j++, R[[j]] = Y[[j]][[1]]];
Print[ListPlot[Transpose[{T, R}]]]
];
RungeKutta3[0., 1, {1., 0.}, 1000, F];
I know basically have a mathematica program that can solve ANY 2nd order equation! Through Runge-Kutta method. just insert your function on
f[t_, y1_, y2_]:= [Insert your function here]
where t is the independent value, y1 is the function itself y(t), y2 is y'(t).
Call the function through:
RungeKutta3[a, b, [Alpha], n, F];
where a is the initial "t" value, b the final "t" value, [Alpha] the initial value of your function and the first derivative (given in the form {y1(a),y2(a0)}), n the number of points equally spaced you want to represent. F is the function you have to insert despite of the function you give to f
Any questions feel free to ask!!
PS: The Runge-Kutta problem solves differential equations with problems of initial values, i used this program as a base to solve a problem of boundary values, if you want it just text me!
Doesn't your code just implement what is already built into Mathematica, namely, if you were to use the option
Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 3}
to NDSolve?
(This is not to suggest there's no value in "rolling your own": perhaps you want to do it as a learning exercise for yourself or for students, or as a student yourself.)