I'm trying to understand, but so far I can not make myself. When I see the finished solution is easier to understand how to do it. This is my first exercise, the others want to do it myself, but need to understand how to implement it.
Please help me how to write a racket function.
Implement the Ackermann's function A. It takes two parameters, x and y, and works as follows:
if y = 0, then it returns 0;
if x = 0, then it returns 2*y;
if y = 1, then it returns 2;
else, it calls itself (function A) with x = x-1 and y = A ( x, (y - 1) )
The project is given
#lang racket/base
(require rackunit)
;; BEGIN
;; END
(check-equal? (A 1 10) 1024)
(check-equal? (A 2 4) 65536)
(check-equal? (A 3 3) 65536)
It's a straightforward translation, you just have to write the formula using Scheme's syntax:
(define (A x y)
(cond ((= y 0) 0) ; if y = 0, then it returns 0
((= x 0) (* 2 y)) ; if x = 0, then it returns 2*y
((= y 1) 2) ; if y = 1, then it returns 2
(else (A (- x 1) ; else it calls itself (function A) with x = x-1
(A x (- y 1)))))) ; and y = A ( x, (y - 1))
Here is one solution:
#lang racket
(define (ack x y)
(match* (x y)
[(_ 0) 0] ; if y = 0, then it returns 0
[(0 y) (* 2 y)] ; if x = 0, then it returns 2*y
[(_ 1) 2] ; if y = 1, then it returns 2
[(x y) (ack (- x 1) (ack x (- y 1)))]))
Related
I need to create matrices Dx and Dy that give the partial diferentials in the x and y directions respectivly when multipled by column stack representation of 2D area (x) so that Dx*x will give me the partial derivetives in the x direction in vector form as well (the same dimentions as x).
The derivitives are aproximated by:
f'(x,y) = 0.5*(f(x+1,y)-f(x-1,y)) for all x > 1 and x < n (the number of rows)
f'(1,y) = f(2,y)-f(1,y)
f'(n,y) = f(n,y)-f(n-1,y)
for Dx and similarly for Dy.
I tried creating the matrices using the fallowing code:
[X,Y] = meshgrid(1:n,1:m); % m,n - the dimentions of x
X = X(:)';
Y = Y(:)';
Dx = sparse(m*n,m*n);
Dy = sparse(m*n,m*n);
for x = 1:n
for y = 1:m
idx = y + (x-1)*m;
if (x == 1)
Dx(idx,:) = ((X == x+1) .* (Y == y)) - ((X == x) .* (Y == y));
elseif (x == n)
Dx(idx,:) = ((X == x) .* (Y == y)) - ((X == x-1) .* (Y == y));
else
Dx(idx,:) = 0.5*(((X == x+1) .* (Y == y)) - ((X == x-1) .* (Y == y)));
end
if (y == 1)
Dy(idx,:) = ((X == x) .* (Y == y+1)) - ((X == x) .* (Y == y));
elseif (y == m)
Dy(idx,:) = ((X == x) .* (Y == y)) - ((X == x) .* (Y == y-1));
else
Dy(idx,:) = 0.5*(((X == x) .* (Y == y+1)) - ((X == x) .* (Y == y-1)));
end
end
end
But this takes too much time, I feel that there should be a faster to create Dy and Dx?
About x: for a 2D function represented by X so that size(X) is [n m], then x=X(:) and size(x) is [n*m 1]
Follow those steps:
Represent the operation using a Filter. Derivative Filters are easy the way you defined the above.
Create the Convolution Matrix using MATLAB's convmtx2() Function.
This will get you what you wanted.
Derivative Filters in Image Processing
Horizontal Derivative:
* Forward Finite Difference - $ [-1, 1] $.
* Backward Finite Difference - $ [1, -1] $.
Vertical Derivative:
* Forward Finite Difference - $ [-1; 1] $.
* Backward Finite Difference - $ [1; -1] $.
Using (x^4) or (^ x 4) giving an error message. Is there some functions for exponent than simply using (* x x x x)?
Use the primitive function expt (see reference):
(expt x 4) ; = x⁴
If you forget expt, you can use:
(for/product ((i 4)) x)
It's easier than writing (* x x x x) for larger values.
This may be very basic, but I can't seem to figure it out: I am trying to use the output of a procedure directly as a function. The Procedure is a prefabricated one, namely the dsolve option in MAPLE. Specifically, I would like to say
dsolve({diff(y(t), t) = y(t)*t, y(1) = 1}, y(t), series, t = 1, order = 7)
The result is
y(t) = t+1-1+(t-1)^2+(2/3)*(t-1)^3+(5/12)*(t-1)^4+(13/60)*(t-1)^5+(19/180)*(t-1)^6+O((t-1)^7)
Which is great, but I can't use this as a function directly, i.e., when I type in y(3), I get y(3). I'm sure this is because the procedure is returning a statement instead of a function. I guess the most basic way around this would be to copy and paste the expression and say y:=t-> whatever, but this is inelegant. How can I get around this?
Thank you
Yes, you are getting back an equation of the form y(t)=<some series> from your dsolve call.
dsol := dsolve({diff(y(t), t) = y(t)*t, y(1) = 1}, y(t), series, t = 1, order = 5);
2 2 3 5 4 / 5\
dsol := y(t) = 1 + (t - 1) + (t - 1) + - (t - 1) + -- (t - 1) + O\(t - 1) /
3 12
You can also convert the series structure on the right-hand side of that to a polynomial (ie. get rid of the big-O term).
convert( dsol, polynom );
2 2 3 5 4
y(t) = t + (t - 1) + - (t - 1) + -- (t - 1)
3 12
You can also evaluate the expression you want, y(t), at that equation. (Or you could just use the rhs command. For sets of equations in the multivariable case the eval approach is more robust and straighforward.)
eval( y(t), convert( dsol, polynom ) );
2 2 3 5 4
t + (t - 1) + - (t - 1) + -- (t - 1)
3 12
And, finally, you can also produce an operator from this expression.
Y := unapply( eval( y(t), convert( dsol, polynom ) ), t );
2 2 3 5 4
Y := t -> t + (t - 1) + - (t - 1) + -- (t - 1)
3 12
That operator is a procedure, and can be applied to whatever point you want.
Y(3);
19
The way I have it above, the statement which assigns to Y happens to contain all the individual steps. And that's the only statement above which you'd need to execute, to get the operator that I assigned to Y.If you prefer you could do each step separately and assign each intermediate result to some name. It just depends on whether you want them for any other purpose.
restart:
dsol := dsolve({diff(y(t), t) = y(t)*t, y(1) = 1}, y(t), series, t = 1, order = 5):
peq := convert( dsol, polynom ):
p := eval( y(t), peq ):
Y := unapply( p, t ):
Y(3);
19
in scala, given the integers d & x, I would have a boolean expression which should be true if and only if y = (x^2 - 1) / d^2 is a square.
I tried this:
(Math.sqrt((x * x - 1) / (d * d)).toInt * Math.sqrt((x * x - 1) / (d * d)).toInt == ((x * x - 1) / (d * d)))
but the 3-tuple (x = 2, d = <all values tested>, y = 0.0) seems to be always an answer of my problem, which is obviously wrong.
I think my error comes from the rounding made: if x=2, d=4 (for example) then x * x - 1 == 3 and d * d == 16 so the division leads to 0.
do you know what is the good expression?
if n is a round square, then Math.sqrt(n).toInt == Math.sqrt(n). In your case:
(Math.sqrt((x * x - 1) / (d * d)).toInt == Math.sqrt((x * x - 1) / (d * d)))
But before doing that, you need to make sure that x and d are doubles.
Try in REPL:
scala> val x = 1
scala> val d = 3
scala> x/d
A Int divided by an Int will result the rounded Int, so you are applying sqrt to zero.
Also due to float point arithmetic, you may want to compare like this instead:
(Math.sqrt((x * x - 1) / (d * d)).toInt - Math.sqrt((x * x - 1) / (d * d))) <= ZERO
where ZERO is replaced by a really small number like 0.00001
Because this is integer division, you are checking whether ((x*x-1)/(d*d)).toInt is a perfect square. You can convert everything to doubles first, but if you want to stay in the realm of integers, check that the division should result in an integer:
( x*x-1 % d*d == 0 ) && Math.sqrt(y).toInt == Math.sqrt(y)
I'd like to build a function, which, given a 2D matrix and some element from that matrix, will return the indexes of the element's position:
(get-indices [[1 2 3] [4 5 6] [7 8 9]] 6)
;=> [1 2]
which, given back to get-in, will return the element itself:
(get-in [[1 2 3] [4 5 6] [7 8 9]] [1 2])
;=> 6
I wanted the function (get-indices) to be fast, so I was thinking about doing a macro which will expand to something similar to the (cond ...) part of this function (but generic for every 2D matrix of size NxN):
(defn get-indices
[matrix el]
(let [[[a b c] [d e f] [g h i]] matrix]
(cond
(= a el) [0 0]
(= b el) [0 1]
(= c el) [0 2]
(= d el) [1 0]
(= e el) [1 1]
(= f el) [1 2]
(= g el) [2 0]
(= h el) [2 1]
(= i el) [2 2])))
I came up with this macro:
(defmacro get-indices
[matrix el]
(let [size (count matrix)
flat (flatten matrix)
compare-parts (map #(list '= % el) flat)
indices (for [x (range size) y (range size)] [x y])]
(cons 'cond (interleave compare-parts indices))))
It seemed just nice... But when called with var, not a direct value, it throws an exception:
(def my-matrix [[1 2 3] [4 5 6] [7 8 9]])
(get-indices my-matrix 6)
;=> java.lang.UnsupportedOperationException: count not supported on this
; type: Symbol (NO_SOURCE_FILE:0)
To me it seems like the symbol "matrix" isn't resolved to value at macro expansion time or something like that, but I'm absolute beginner in macros...
How can I make this macro to work also with vars as arguments?
I was also thinking about using syntax-quote etc., but I'd like to avoid having (let ...) as a part of the macro output and also didn't know how to implement (interleave compare-parts indices) within the syntax-quote....
Writing this as a macro is a disastrous choice. As a function it's pretty simple, and more efficient than what you wanted your macro to expand to anyway:
(defn get-indices [matrix el]
(let [h (count matrix), w (count (first matrix))]
(loop [y 0, x 0, row (first matrix), remaining (rest matrix)]
(cond (= x w) (recur (inc y) 0 (first remaining), (rest remaining))
(= y h) nil
(= (first row) el) [y x]
:else (recur y (inc x) (rest row) remaining)))))
Conversely, as a macro it is simply impossible. Macros are for writing code at compile-time - how could you generate the cond-based code for 2D matrices at compile time, if you don't know the matrix's size until runtime?