How do you write a F# recursive function 'something' that takes a single point (x, y) and index i as
an arguments and returns an ๐๐กโ element of the infinite list corresponding to S(x, y).
ex: let something (x,y) i =
F# function F(๐ฅ,๐ฆ) should be defined as:
F(x,y)(u, v) = (u2 โ v2 + x, 2uv + y)
F# function 'something' ๐(x, y) of a point (x, y) should be an infinite list of items:
S(x, y) = {(0, 0), F(x,y)(0, 0), F(๐ฅ,y)(F(x,y)(0, 0)), F(x,y)(F(x,y)(F(x,y)(0, 0))), โฆ}
This looks like an assignment to me. I don't mind helping out but at the same time I don't want to give the full solution.
The exercise itself looks like generating the series:
Z' = Z*Z + C
For a complex number Z and C. This is commonly done when generating the mandelbrot or julia set.
The function F can be written almost like the definition in F#:
let f (x, y) (u, v) = (u*u - v*v + x, 2.*u*v + y)
The infinite set S is generated from a starting point (0,0) and applying the output of f on itself over and over again.
An infinite set in F# can be represented using seq and you can create them using seq comprehesions
Once you have an infinite seq with the right values you can pick the ith value by using Seq.item
Related
We have the following equation, that we want to implement into our code with arrays/matrices
h(x, y, z) = ax + by + cz
pseudeo code:
X = [a, b, c]
A = [x, y, z]
Often I see the equation being implemented like this:
h = transpose(A) * T
Is there any difference of just using the dot product?
h = dotproduct(A, X)
Is there a specific reason why the transpose is used over the dotproduct?
Mathematically, there's no difference. In implementation- dotproduct(...) may actually be faster, since transpose(...) may eagerly move the elements around prior to the matrix multiplication, doing "unnecessary" work.
Suppose I want to track the state of cells in a grid. Let's assume that the grid has dimensions m x n. I can simply create a vector of length m*n and track cell state using the vector. In this case, each point in the grid (which is 2D) would map to an element in the vector (1D).
One method I've used before is something like this:
defun 2d->1d (x, y, m, n):
return m*y + x;
defun 1d->2d (i, m, n):
return [i%m, i/m];
My problem is this:
Is there a way to have a 2D->1D mapping as above when grid dimensions are not known or when grid is infinite?
Yes, there are mappings from 2D to 1D for natural numbers, known as Pairing functions.
For example, the Cantor pairing function:
defun 2d->1d (x, y):
return (1 / 2) * (x + y) * (x + y + 1) + y;
For the reverse function see the link above, it is a little more complex.
I'm trying to multiply two numbers in Prolog recursively i.e. 3*4 = 3+3+3+3 = 12.
My code is :
mult(0,Y,Y).
mult(X,Y,Z) :-
NewX is X-1,
Z is Y + mult(NewX,Y,Z).
but I keep either in an infinite loop or being told mult is not a function.
What you here constructed is a predicate. A predicate is not the same as a function in computer science, you can not write A is B + some_pred(C), or at least not as far as I know in ISO Prolog, and definitely not without adding some extra logic.
In order to pass values, one uses variables. We can thus call the mult/3 predicate recursively, and use a variable that will be unified with the result. We can then perform arithmetic with it, like:
mult(0, _, 0).
mult(X, Y, Z) :-
X1 is X - 1,
mult(X1, Y, Z1),
Z is Y + Z1.
Note that you can not reassign a (different) value to a variable. So if, like you did in the question, use Z twice, then given Y is not 0, this will fail.
The above is however still not sufficient, since it will produce a result, but then get stuck in an infinite loop since if it calls (eventually) mult(0, 4, Z) (4 is here just a value), there are two ways to resolve this: with the base case, and with the recursive case.
We thus need a "guard" for the second case, like:
mult(0, _, 0).
mult(X, Y, Z) :-
X > 0,
X1 is X - 1,
mult(X1, Y, Z1),
Z is Y + Z1.
We then obtain for example:
?- mult(14, 25, Z).
Z = 350 ;
false.
One can improve the speed of this mult/3 predicate by implementing a version with an accumulator. I leave this as an exercise.
The syntax [x, y] = X, assigning X(1) to x and X(2) to y, is definitely possible in Scilab, for example:
[p, q] = cdfnor("PQ", 0, 0, 1)
But if I try it, I get an "incompatible assignation" error:
a = [0, 1]
[x, y] = a
Incompatible assignation: trying to assign 1 values in 2 variables.
How can I replicate this behavior in my own code?
Scilab has tuple assignments such as [x, y] = (0, 1). This is somewhat close to what you want, but this parentheses syntax does not appear to correspond to any Scilab data structure (I tried this with a list, unsuccessfully).
So it appears that one needs a function with a variable number of output variables (varargout), which takes a vector and returns a list of variables. There appears to be no built-in way to convert a vector to a list, so I used a loop. The function is named deal because of the similarity to Matlab's deal function.
function varargout = deal(a)
varargout = list()
for i = 1:length(a)
varargout(i) = a(i)
end
endfunction
a = [0, 1]
[x, y] = deal(a)
I'm trying to force Mathematica to implicitly differentiate an ellipse equation of the form:
x^2/a^2+y^2/b^2 == 100
with a = 8 and b = 6.
The command I'm using looks like this:
D[x^2/a^2 + y^2/b^2 == 100/. y -> 3/4*Sqrt[6400-x^2], x]
where, y->3/4*Sqrt[6400-x^2] comes from solving y in terms of x.
I got this far by following the advice found here: http://www.hostsrv.com/webmaa/app1/MSP/webm1010/implicit
Input for this script is the conventional way that an implicit
relationship beween x and y is expressed in calculus textbooks. In
Mathematica you need to make this relationship explicit by using y[x]
in place of y. This is done automatically in the script by replacing
all occurances of y with y[x].
But the solution Mathematica gives does not have y' or dy/dx in it (like when I solved it by hand). So I don't think it's been solved correctly. Any idea on what command would get the program to solve an implicit differential? Thanks.
The conceptually easiest option (as you mentioned) is to make y a function of x and use the partial derivative operator D[]
In[1]:= D[x^2/a^2 + y[x]^2/b^2 == 100, x]
Solve[%, y'[x]]
Out[1]= (2 x)/a^2 + (2 y[x] y'[x])/b^2 == 0
Out[2]= {{y'[x] -> -((b^2 x)/(a^2 y[x]))}}
But for more complicated relations, it's best to use the total derivative operator Dt[]
In[3]:= SetOptions[Dt, Constants -> {a, b}];
In[4]:= Dt[x^2/a^2 + y^2/b^2 == 100, x]
Solve[%, Dt[y, x]]
Out[4]= (2 x)/a^2 + (2 y Dt[y, x, Constants -> {a, b}])/b^2 == 0
Out[5]= {{Dt[y, x, Constants -> {a, b}] -> -((b^2 x)/(a^2 y))}}
Note that it might be neater to use SetAttributes[{a, b}, Constant] instead of the SetOptions[Dt, Constants -> {a, b}] command... Then the Dt doesn't carry around all that extra junk.
The final option (that you also mentioned) is to solve the original equation for y[x], although this is not always possible...
In[6]:= rep = Solve[x^2/a^2 + y^2/b^2 == 100, y]
Out[6]= {{y -> -((b Sqrt[100 a^2 - x^2])/a)}, {y -> (b Sqrt[100 a^2 - x^2])/a}}
And you can check that it satisfies the differential equation we derived above for both solutions
In[7]:= D[y /. rep[[1]], x] == -((b^2 x)/(a^2 y)) /. rep[[1]]
Out[7]= True
You can substitute your values a = 8 and b = 6 anytime with replacement rule {a->8, b->6}.
If you actually solve your differential equation y'[x] == -((b^2 x)/(a^2 y[x]) using DSolve with the correct initial condition (derived from the original ellipse equation) then you'll recover the solution for y in terms of x given above.