Implementing a Laplacian in a Reaction Diffusion Simulation - math

I'm trying to implement a Reaction Diffusion Simulation by following this article: https://www.karlsims.com/rd.html. Where I'm running into trouble is the implementation of the 2D Laplacian function mentioned in this diagram:
The article later says: "The Laplacian is performed with a 3x3 convolution with center weight -1, adjacent neighbors .2, and diagonals .05." From what I've gathered from a little research, I think the convolution would look something like this:
a b c
d e f
g h i
Convolution = 0.2(b + f + h + d) + 0.05(a + c + i + g) - e
What is a 2D Laplacian, and how would I implement it to work on the result of my convolution? Pseudo-code would be much appreciated.

Related

How can i draw graph of Elliptic Curve mod p by using pari-gp?

Suppose E:y^2=x^3+Ax+B mod p, I have two questions?
how can I draw the graph of E with pari-gp.
how can I get the list of all points over the E.
thank you for all.
To define an Elliptic Curve with SageMath use
E = EllipticCurve(GF(131),[0,1,0,1,0])
print(E)
and outputs
Elliptic Curve defined by y^2 = x^3 + x^2 + x over Finite Field of size 131
In your case ( simplified Weierstrass from)
E = EllipticCurve(GF(p),[A,B])
will be enough.
To plot a curve
E.plot()
is enough
To iterate the points
for T in E.points():
print(T)
is enough.
Try online on SageMathCell.
And notice the symmetry!
Pari-GP
From a tutorial
a=ffgen(P,’a)
Es = ellinit([a^4,a^6],a);

How to approximately match bowtie graphs using igraph?

I'm trying to learn how match_vertices works in igraph so that I can use it to join node and attribute information from graphs generated by a few different black box computer programs. I tried looking at the single provided example, but it was too complicated for me to understand. So, I tried to throw together an even simpler toy example which I am trying to understand.
library(igraph)
bow = make_graph(~ A - B - C - A - D - E - A)
tie = make_graph(~ a - e - d - a - c - b - a)
isomorphic(bow, tie)
It looks to me like the code should be:
A = get.adjacency(bow)
B = get.adjacency(tie)
P0 = diag(nrow(A))
corr = match_vertices(A, B,
start = P0,
m = 0,
iteration = 30)
corr$P
The permutation matrix didn't seem to change. The resulting permutation matrix is the same as the
starting permutation matrix. Why is that?
I defined a random permutation matrix and repeated the exercise to
see if that was always the case. It was!
random_permutation = function(n, ...) {
P = diag(n)
i = sample(1:n, ...)
P[i,,drop=FALSE]
}
Can anyone recommend some simple toy examples of using match_vertices that
demonstrate its main features?
show how to approximately match two graphs
show how to approximately match two graphs when some vertices are known
show how to approximately match two graphs when they have different
numbers of vertices
Also, are there any ways of matching graphs if even if the seeds aren't known
a priori, the nodes have maximally consistent attributes?

Linear regression / line finding for non-function lines

I want to find a line having a number of points which are around that line. Line is in 2D space and is defined by two points, or by one point and an angle. What would be the algorithm for it?
There's a lot about this on SO and in internet and also in Numerical Receipes, but all examples seem to focus on function form of the line (y=ax+b) which is not going to work well for (almost) vertical lines.
I could possibly detect if the line is more horizontal or more vertical, and swap coordinates in the other case, but maybe there exists some more elegant solution?
I'm using C# ATM but can translate from any code probably.
I'm sorry I can't provide a reference, but here's how:
Suppose your N (2d) data points are p[] and you want to find a vector a and a scalar d to minimise
E = Sum{ i | sqr( a'*p[i] - d) }/N
(The line is { q | a'*q = d} E is the sum of the squares of the distances of the data points from the line).
Some tedious algebra shows that
E = a'*C*a + sqr(d - a'*M)
where M is the mean and C the covariance of the data, ie
M = Sum{ i | p[i] } / N
C = Sum{ i | (p[i]-M)*(p[i]-M)' } / N
E will be minimised by choosing d = a'*M, and a to be an eigenvector of C corresponding to the smaller eigenvalue.
So the algorithm is:
Compute M and C
Find the smaller eigenvalue of C and the corresponding eigenvector a
Compute d = a'*M
(Note that the same thing works in higher dimensions too. For example in 3d we would find the 'best' plane).

Local interpolation of surfaces using normal vectors

I need to interpolate a 3D surface given it's points and normal vectors.
Given a point on it's surface, I need to find where that point would be in space once the interpolation has been accounted for. I need to be able to do this for each triangle in isolation.
Here's what I'm trying to describe. I need the position of the point once the curve / surface has been interpolated.
If I was working in 2D:
3D:
I've come across this paper "Simple local interpolation of surfaces using normal vectors - Takashi Nagata" which I think demonstrates exactly what I'm looking for (section 2.2. Interpolation of a patch using normals), but the math is just beyond me.
What I'm trying to extract from it is a set of equations where the position and normals of the points comprising the triangle go in, as well as the point on the triangle, and the adjusted point comes out (like magic).
The paper looks like its trying to fit a quadratic surface so that it matches the points and normals you have. The resulting surface is given by
p(s,t) = c00 + c10 s + c01 t + c11 s t + c20 s^2 + c02 t^2
where s,t are the two variables, c00 etc are all vectors with three coordinates. s,t are chosen so at s=0,t=0 its your first point, s=1, t=0 is your second point and s=1,t=1 is your third point. Assuming we can find the various c00's you can pick some values of s,t in the triangle to give a middle point, s=2/3, t=1/3 might be a find candidate.
Find c00 etc will take some work. You probably need to implement eqn 15, which gives a curvature, as a function
vec3 c(vec3 D,vec3 n0,vec3 n1) {
vec3 v = (n0 + n1)/2; // 12a
vec3 dv = (n0 - n1)/2; // 12b
double d = D.dot(v); // 13a
double dd = D.dot(dv); // 13b
double c = n0.dot(n0 - 2*dv); // 14a
double dc = n0.dot(dv); // 14b
vec3 res;
if( c == -1 || c==1 )
res = vec3.zeroVector;
else
res = dd / (1-dc) * v + d / dc * dv;
return res;
}
assuming you have a vec3 class which can do basic vector operators.
With that defined, use 35, 36 to define the starting vectors and normals. Use 39 to define differences between pairs of points d1, d2, d3 and curvatures c1, c2, c3. The use eq 44
x(η, ζ ) = x00(1 − η) + x10(η − ζ ) + x11ζ
− c1(1 − η)(η − ζ ) − c2(η − ζ )ζ − c3(1 − η)ζ
and your done.
For the records, and because I wanted to have this information somewhere on the web.
This is the 2d interpolation using the paper posted by the OP.
Where 1a and 1b are the boundary conditions, and the equations 4a and 4b are the x and y components of the vector c needed for the interpolation.

How to implement surface calculation of a elliptical cone and elliptical paraboloid (in R)?

How can I calculate the surface of an elliptical paraboloid in R, having only the two major axes a and b, and the height h?
Background: got a population of things which which resemble elliptical paraboloids. (Think of hummocks, e.g.). What I'm statistically interested in is: how large is the mean and SD error I introduce when I approximate the surfaces of the population as rectangular (a * b) instead of properly modelling their surface? I could just go for a cone, but I've got some really long ellipses in that population of data. This made me look for how to calculate an elliptical paraboloid, and and elliptic cone. Which did not help me answering my question so far.
I can't translate the neither Wolfram nor Wikipedia properly, just started for the cone with
paraboloid.surf <- function(a,b,h){2*a*sqrt(b^2+h^2)* E * sqrt((1-b^2/a^2)/(1+b^2/a^2))}
without knowing how to calculate E, or even understanding if this is E multiplied by the term, or a function E(k) of k = the term.
Can somebody help me out and provide some code? I would be interested in both the conical as the parabolical solution, but any is better than none. I hope this is really non-trivial, and I'm not totally stupid. =)
So I assume you want the surface area of an elliptic paraboloid. The basic framework is given here, from which the following image is taken.
So the question is how to compute this in R? The hardest part is the double integral, which can be evaluated using the adaptIntegrate(...) function in the cubature package.
A <- function(a,b,h) {
require(cubature)
integrand <- function(x,a,b){
u <- x[1]
v <- x[2]
E <- 1+((a*cos(v))^2 + (b*sin(v))^2)/(4*u)
F <- (b^2 - a^2)*sin(2*v)/4
G <- u*((a*sin(v))^2+(b*cos(v))^2)
return(sqrt(E*G-F^2))
}
adaptIntegrate(integrand,
lowerLimit=c(0,0), upperLimit=c(h,2*pi), a=a,b=b)$integral
}
We can confirm this by noting that when
a = b = h = 1
E = 1 + 1/4u
F = 0
G = u
A = 2π ∫du sqrt(u+1/4)
Which can be evaluated in closed form as:
A = 4π/3 [ (5/4)3/2 - (1/4)3/2 ] = 5.330414
A(1,1,1)
# [1] 5.330413
(4*pi/3)*((5/4)^(3/2) - (1/4)^(3/2))
# [1] 5.330414
From https://en.wikipedia.org/wiki/Elliptic_paraboloid the defining equation for an elliptic parabola is z/c = x^2/a^2 + y^2/b^2. We can transform this to make a hill with height 'h' and base at z=0 being an ellipse.
For a elliptic parabola
z = h*(1 - ((x/a)^2+(y/b)^2)).
Where a and b are the semi-major and semi-minor axis of the ellipse generated by cutting the cone by z=0 and h is the height of the point above (0,0).
For a elliptical cone
z = h*(1 - sqrt((x/a)^2+(y/b)^2))`.
Where a and b are the semi-major and semi-minor axis of the ellipse generated by cutting the cone by z=0.
To plot both you need to clip by the plane z=0 use max(0,h*(1 - ((x/a)^2+(y/b)^2)).
Volumes
The area of an ellipse is pi a b, the volume of a cone is 1/3 basearea * h so volume of the cone is
1/3 pi a b h.
As mentioned elsewhere the volume of the elliptic paraboloid is a bit tricky.
We can try doing it by slicing in the z-direction.
int_{z=0}^{h} area of ellipse at height z dz.
If the ellipse at height z has semis a(z) and b(z) then the area is
A(z) = pi a(z) b(z).
To find the a(z) and b(z) slice the paraboloid by the planes y=0, x=0, these give two parabolas z = h *(1 - (x/a)^2), z = h *(1 - (y/b)^2). Rearrange z/h = 1 - (x/a)^2, (x/a)^2 = 1 - z/h x/a = sqrt(1-z/h) x=a*sqrt(1-z/h) this is our a(z). Similary b(z)=b*sqrt(1-z/h). The area of the ellipse at height z is
A(z) = pi a(z) b(z)
= pi a sqrt(1-z/h) b sqrt(1-z/h)
= pi a b (1-z/h)
Integrate
int_{z=0}^h pi a b (1-z/h)
= pi a b [z - z^2/(2 h)]_0^h
= pi a b (h - h^2/(2 h))
= 1/2 pi a b h
remarkably simple.
For comparison the volume of the cuboid with height h and lengths 2a and 2b would be
4 a b h.
Surface areas
From https://en.wikipedia.org/wiki/Elliptic_integral#Complete_elliptic_integral_of_the_second_kind the circumference of a ellipse with semi-major axis a and eccentricity e is
4 a E(e)
where E(e) is a complete Elliptical integral of the second kind. The eccentricity e is
e = sqrt( 1 - b^2/a^2 ).
In both the cone and elliptic paraboloid cases the eccentricity is constant for all horizontal slices. The integrals for surface areas then simplify nicely. For the cone
a(z) = a * ( h - z). So
int_z 4 a(z) E(e) dz
= 4 E(e) int_z a(z) dz
= 4 E(e) int_{z=0}^h a * (h -z ) dz
= 4 a E(e) int_{z=0}^h h - z dz
= 4 a E(e) [h z - z^2/2]_{z=0}^h
= 4 a E(e) h^2/2
= 2 a E(e) h^2
For the elliptic paraboloid a(z)=a*sqrt(1-z/h) and the integral is
int_z 4 a(z) E(e) dz
= 4 E(e) int_z a(z) dz
= 4 E(e) int_{z=0}^h a * sqrt(1-z/h) dz
change variables with w=1-z/h limits are 1 and 0 dz/dw = -h so
= 4 E(e) int_{w=1}^0 a * sqrt(w) * -h dw
= 4 a E(e) h int_{w=0}^1 sqrt(w) dw // switching limits round
= 4 a E(e) h [ 2 w^(3/2) /3]_{w=0}^1
= 8/3 a E(e) h.
Multivariate normal distribution
An alternative and more standard model is as a 2D gaussian https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Density_function
z = 1/(2 pi a b) exp( -( (x/a)^2 + (y/b)^2 )/2).
This will look pretty close to the paraboloid apart from at the base.

Resources