How does one create a cube with Julia and Makie? - julia

The following Julia program uses Makie to create the image shown below, a "cube with holes." However, I would like to eliminate the holes, and also make the cube nearer the shape of an actual cube, with only slightly rounded corners and edges. I've tried changing the exponent to values other than ^2 and also passing the range directly to the volume! function, to no avail. Examples of the types of cubes desired can be found in three-rounded-box images at CodeSandbox.
using Makie, GLMakie
fig = Figure()
range = LinRange(-1, 1, 100) # 100-element LinRange{Float64, Int64}
cube = [ (x.^2 + y.^2 + z.^2) for x = range, y = range, z = range ] # 100×100×100 Array{Float64, 3}
ax = Axis3( fig[1,1], aspect = :data, azimuth = 1.17 * pi, viewmode = :fit, title = "Cube" )
volume!( cube, algorithm = :iso, isorange = 0.05, isovalue = 1.7 )
fig

Like this?
using Makie, GLMakie
fig = Figure()
range = LinRange(-1, 1, 100) # 100-element LinRange{Float64, Int64}
cube = [ (abs.(x).^10 + abs.(y).^10 + abs.(z).^10) for x = range, y = range, z = range ] # 100×100×100 Array{Float64, 3}
ax = Axis3( fig[1,1], aspect = :data, azimuth = 1.17 * pi, viewmode = :fit, title = "Cube" )
volume!( cube, algorithm = :iso, isorange = 0.05, isovalue = 1 )
fig
Or you can use a parametric surface, a supershape:
using Makie, GLMakie
function r(phi; a, b, m, n1, n2, n3)
return 1 / (abs(cos(m*phi/4)/a)^n2 + (abs(sin(m*phi/4)/b)^n3))^(1/n1)
end
phi = (-pi/2):0.01:(pi/2)
theta = (-pi):0.01:(pi)
x = [
r(theta; a=1, b=1, m=4, n1=10, n2=10, n3=10) * cos(theta) *
r(phi; a=1, b=1, m=4, n1=10, n2=10, n3=10) * cos(phi)
for phi in phi, theta in theta
]
y = [
r(theta; a=1, b=1, m=4, n1=10, n2=10, n3=10) * sin(theta) *
r(phi; a=1, b=1, m=4, n1=10, n2=10, n3=10) * cos(phi)
for phi in phi, theta in theta
]
z = [
r(phi; a=1, b=1, m=4, n1=10, n2=10, n3=10) * sin(phi)
for phi in phi, theta in theta
]
fig, _ = surface(x, y, z)
fig
Better to use a function:
function r(phi; a, b, m, n1, n2, n3)
return 1 / (abs(cos(m*phi/4)/a)^n2 + (abs(sin(m*phi/4)/b)^n3))^(1/n1)
end
function supershape(p1, p2)
phi = (-pi/2):0.01:(pi/2)
theta = (-pi):0.01:(pi)
x = [
r(theta; a=p1.a, b=p1.b, m=p1.m, n1=p1.n1, n2=p1.n2, n3=p1.n3) * cos(theta) *
r(phi; a=p2.a, b=p2.b, m=p2.m, n1=p2.n1, n2=p2.n2, n3=p2.n3) * cos(phi)
for phi in phi, theta in theta
]
y = [
r(theta; a=p1.a, b=p1.b, m=p1.m, n1=p1.n1, n2=p1.n2, n3=p1.n3) * sin(theta) *
r(phi; a=p2.a, b=p2.b, m=p2.m, n1=p2.n1, n2=p2.n2, n3=p2.n3) * cos(phi)
for phi in phi, theta in theta
]
z = [
r(phi; a=p2.a, b=p2.b, m=p2.m, n1=p2.n1, n2=p2.n2, n3=p2.n3) * sin(phi)
for phi in phi, theta in theta
]
return (x = x, y = y, z = z)
end
params1 = (a=1, b=1, m=4, n1=10, n2=10, n3=10)
params2 = params1
x, y, z = supershape(params1, params2)
fig, _ = surface(x, y, z)
EDIT
The rendering is not nice with surface. It's better to do a mesh:
function parametricMesh(f, umin, umax, vmin, vmax, nu, nv)
u_ = LinRange(umin, umax, nu)
v_ = LinRange(vmin, vmax, nv)
vertices = Array{Float64}(undef, nu*nv, 3)
triangles = Array{Int64}(undef, 2*(nu-1)*(nv-1), 3)
k = 1
for i in 1:nv
v = v_[i]
for j in 1:nu
vertices[k,:] = f(u_[j], v)
k = k+1
end
end
k = 1
for i in 1:(nv-1)
for j in 1:(nu-1)
a = (i-1) * nu + j
b = (i-1) * nu + j + 1
c = i*nu + j + 1
d = i*nu + j
triangles[2*(k-1)+1,:] = [b, a, d]
triangles[2*k,:] = [c, b, d]
k = k+1
end
end
return (vertices = vertices, triangles = triangles)
end
function r(phi; a, b, m, n1, n2, n3)
return 1 / (abs(cos(m*phi/4)/a)^n2 + (abs(sin(m*phi/4)/b)^n3))^(1/n1)
end
p1 = (a=1, b=1, m=4, n1=10, n2=10, n3=10)
p2 = p1
function f(phi, theta)
x = r(theta; a=p1.a, b=p1.b, m=p1.m, n1=p1.n1, n2=p1.n2, n3=p1.n3) * cos(theta) *
r(phi; a=p2.a, b=p2.b, m=p2.m, n1=p2.n1, n2=p2.n2, n3=p2.n3) * cos(phi)
y = r(theta; a=p1.a, b=p1.b, m=p1.m, n1=p1.n1, n2=p1.n2, n3=p1.n3) * sin(theta) *
r(phi; a=p2.a, b=p2.b, m=p2.m, n1=p2.n1, n2=p2.n2, n3=p2.n3) * cos(phi)
z = r(phi; a=p2.a, b=p2.b, m=p2.m, n1=p2.n1, n2=p2.n2, n3=p2.n3) * sin(phi)
return [x, y, z]
end
vertices, triangles = parametricMesh(f, -pi/2, pi/2, -pi, pi, 50, 50)
mesh(vertices, triangles, color = "yellow")

Related

How to create a Matlab pumpkin in R?

I am trying to replicate the following visual with the following Matlab code:
% Pumpkin
[X,Y,Z]=sphere(200);
R=1-(1-mod(0:.1:20,2)).^2/12;
x=R.*X; y=R.*Y; z=Z.*R;
c=hypot(hypot(x,y),z)+randn(201)*.03;
surf(x,y,(.8+(0-(1:-.01:-1)'.^4)*.3).*z,c, 'FaceColor', 'interp', 'EdgeColor', 'none')
% Stem
s = [ 1.5 1 repelem(.7, 6) ] .* [ repmat([.1 .06],1,10) .1 ]';
[t, p] = meshgrid(0:pi/15:pi/2,0:pi/20:pi);
Xs = -(.4-cos(p).*s).*cos(t)+.4;
Zs = (.5-cos(p).*s).*sin(t) + .55;
Ys = -sin(p).*s;
surface(Xs,Ys,Zs,[],'FaceColor', '#008000','EdgeColor','none');
% Style
colormap([1 .4 .1; 1 1 .7])
axis equal
box on
material([.6 1 .3])
lighting g
camlight
I am working on the bottom but have not gotten very far (see here for reference). The code that I have is:
library(pracma)
library(rgl)
sphere <- function(n) {
dd <- expand.grid(theta = seq(0, 2*pi, length.out = n+1),
phi = seq(-pi, pi, length.out = n+1))
with(dd,
list(x = matrix(cos(phi) * cos(theta), n+1),
y = matrix(cos(phi) * sin(theta), n+1),
z = matrix(sin(phi), n+1))
)
}
# Pumpkin
sph<-sphere(200)
X<-sph[[1]]
Y<-sph[[2]]
Z<-sph[[3]]
R<- 1-(1-seq(from=0, to=20,by=0.1))^2/12
x<-R * X
y<-R * Y
z<-Z * R
c<-hypot(hypot(x,y),z)+rnorm(201)*0.3
persp3d(x,y,(0.8+(0-seq(from=1, to=-1, by=-0.01)^4)*0.3)*z,col=c)
and it gives me the following.
What is it that's going wrong in my present code? What would be a suggested fix?
As #billBokeey mentioned, there's a missing mod modulo operator function for periodic scaling factors.
In addition, the scaling on the z-axis 0.8 + (0-seq(from=1, to=-1, by=-0.01)^4) * 0.3 doesn't go well with the output from your sphere function. We maybe use Z[1,] to replace seq(from=1, to=-1, by=-0.01). phi = seq(-pi, pi, length.out = n+1)) shoud be seq(-pi/2, pi/2, length.out = n+1)) instead.
Finally, the color c needs to be convert to RGB code for persp3d.
Here's the result look like from the code below.
library(rgl)
sphere <- function(n) {
dd <- expand.grid(theta = seq(0, 2*pi, length.out = n+1),
phi = seq(-pi/2, pi/2, length.out = n+1))
with(dd,
list(x = matrix(cos(phi) * cos(theta), n+1),
y = matrix(cos(phi) * sin(theta), n+1),
z = matrix(sin(phi), n+1))
)
}
# Unit ball
sph <- sphere(200)
X <- sph[[1]]
Y <- sph[[2]]
Z <- sph[[3]]
# scaling
R <- 1 - (1 - seq(from=0, to=20, by=0.1) %% 2) ^ 2 / 12 # Modulo Operator %%
R2 <- 0.8 + (0 - seq(from=1, to=-1, by=-0.01)^4)*0.2 # didn't match with the order of z from sphere function
#R2 <- 0.8 - Z[1,]^4 * 0.2
x <- R * X # scale rows for wavy side
y <- R * Y # scale rows for wavy side
z <- t(R2 * t(Z)) # scale columns by transpose for flat oval shape
# color according to distance to [0,0,0]
hypot_3d <- function(x, y, z) {
return(sqrt(x^2 + y^2 + z^2))
}
c_ <- hypot_3d(x,y,z) + rnorm(201) * 0.03
color_palette <- terrain.colors(20) # color look-up table
col <- color_palette[ as.numeric(cut(c_, breaks = 20)) ] # assign color to 20 levels of c_
persp3d(x, y, z, color = col, aspect=FALSE)

Coding likelihood and log-likelihood function in r to perform optimization

I am working on a paper that requires me to find the MLE of Gumbel’s type I
bivariate exponential distribution. I have proved the likelihood and log-likelihood functions likelihood and log-likelihood but I am struggling to implement it in r to perform optimization with Optim function. My code generates NA values.
Below are my codes.
# likelihood function of x
likelihood.x = function(params, data) {
lambda1 = params[1]
lambda2 = params[2]
theta = params[3]
A = (1 - theta) * (lambda1 * lambda2)
B = theta * (lambda1 ^ 2) * lambda2 * data$X1
C = theta * lambda1 * (lambda2 ^ 2) * data$X2
D = (theta ^ 2) * (lambda1 ^ 2) * (lambda2 ^ 2) * data$X1 * data$X2
E = (lambda1 * data$X1) + (lambda2 * data$X2) + (theta * lambda1 * lambda2 * data$X1 * data$X2)
f = sum(log(A + B + C + D)) - sum(E)
return(exp(f))
}
# Log-likelihood function of x
log.likelihood.x = function(params, data){
lambda1 = params[1]
lambda2 = params[2]
theta = params[3]
A = (1 - theta) * (lambda1 * lambda2)
B = theta * (lambda1 ^ 2) * lambda2 * data$X1
C = theta * lambda1 * (lambda2 ^ 2) * data$X2
D = (theta ^ 2) * (lambda1 ^ 2) * (lambda2 ^ 2) * data$X1 * data$X2
E = (lambda1 * data$X1) + (lambda2 * data$X2) + (theta * lambda1 * lambda2 * data$X1 * data$X2)
f = sum(log(A + B + C + D)) - sum(E)
return(-f)
}
Here's the function for generating the data
# Simulating data
rGBVE = function(n, lambda1, lambda2, theta) {
x1 = rexp(n, lambda1)
lambda12 = lambda1 * lambda2
pprod = lambda12 * theta
C = exp(lambda1 * x1)
A = (lambda12 - pprod + pprod * lambda1 * x1) / C
B = (pprod * lambda2 + pprod ^ 2 * x1) / C
D = lambda2 + pprod * x1
wExp = A / D
wGamma = B / D ^ 2
data.frame(x1, x2 = rgamma(n, (runif(n) > wExp / (wExp + wGamma)) + 1, D))
}
data = rGBVE(n=100, lambda1 = 1.2, lambda2 = 1.4, theta = 0.5)
colnames(data) = c("X1", "X2")
My goal is to find MLE for lambda1, lambda2 and theta using Optim() in r.
Kindly assist me to implement my likelihood and log-likelihood function in r.
Thank you.
Your concern appears to be about the warning message
In log(A+B+C+D): NaNs produced
Such warnings are usually harmless — it just means that the optimization algorithm tried a set of parameters somewhere along the way that violated the condition A+B+C+D ≥ 0. Since these are reasonably complex expressions it would take a little bit of effort to figure out how one might constrain the parameters (or reparameterize the function, e.g. fitting some of the parameters on the log scale) to avoid the warning, but taking a guess that keeping the parameters non-negative will help, we can try using the L-BFGS-B algorithm (which is the only algorithm available in optim() that allows multidimensional bounded optimization).
r1 <- optim(par = c(1,2,1),
fn = log.likelihood.x,
dat = data)
r2 <- optim(par = c(1,2,1),
fn = log.likelihood.x,
lower = rep(0,3),
method = "L-BFGS-B",
dat = data)
The second does not generate warnings, and the results are close (if not identical):
all.equal(r1$par, r2$par)
## "Mean relative difference: 0.0001451953"
You might want to use bbmle, which has some additional features for likelihood modeling:
library(bbmle)
fwrap <- function(x) log.likelihood.x(x, dat = data)
parnames(fwrap) <- c("lambda1", "lambda2", "theta")
m1 <- mle2(fwrap, start = c(lambda1 = 1, lambda2 = 2, theta = 1), vecpar = TRUE,
method = "L-BFGS-B", lower = c(0, 0, -0.5))
pp <- profile(m1)
plot(pp)
confint(pp)
confint(m1, method = "quad")

Can't plot the complete Cobweb diagram in Scilab

I was trying to produce the cobweb diagram of the following equation
xn+1 = xn * e ^ a(1−xn/1000), x0 = 200 and a is a constant
But somehow I couldn't plot the original function and y = (x)
The following is my code in scilab
x0 = 200
n = 20
a = 2
deff("y = F(x)", "y = x*%e^(a*(1-x/1000))")
cw = [x0 0];
x = x0;
for i = 1:n
newx = F(x);
cw = [cw;x newx; newx newx];
x = newx
end
for x = x0;
y1 = x*%e^(a*(1-x/1000))
y2 = x
end
plot2d(cw(:,1),cw(:,2),4)
plot2d(x, y1, 5)
plot2d(x, y2, 6)
title("Cobweb Diagram")
xlabel("x(n)")
ylabel("x(n+1)")

Mapping image on spherical surface

In simple words i need to map a image to be use in a spherical surface. I being trying to do this for several hours. searching in google I din't find any proper solution (explained for dumb people).
I thinks the code from this link:
https://www.codeproject.com/articles/19712/mapping-images-on-spherical-surfaces-using-c is what i need. but (i think everything is alright) can make it work in Julia.
This is my code so far:
image = brightNoise(height,width,seed,rand=true)
arr = Array{Float64}(height,width)
function MapCoordinate(i1, i2,w1,w2,p)
return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1
end
function Rotate(angle, axisA, axisB)
return axisA * cos(angle) - axisB * sin(angle),axisA * sin(angle) + axisB * cos(angle)
end
phi0 = 0.0
phi1 = pi
theta0 = 0.0
theta1 = 2.0*pi
radius = 50
arr = Array{Float64}(height,width)
for i= 1:size(image)[1]
for j= 1:size(image)[2]
#map the angles from image coordinates
theta = MapCoordinate(0.0,width - 1,theta1, theta0, i)
phi = MapCoordinate(0.0,height - 1,phi0,phi1, j)
#find the cartesian coordinates
x = radius * sin(phi) * cos(theta);
y = radius * sin(phi) * sin(theta);
z = radius * cos(phi);
#apply rotation around X and Y axis to reposition the sphere
y,z=Rotate(1.5, y, z);
x,z=Rotate(pi/2, x, z);
#plot only positive points
if (z > 0)
color = image[i,j]
ix = floor(Int64,x)
iy = floor(Int64,y)
arr[ix,iy] = color
println(ix,iy)
end
end
end
The image is just a black and white noise generated in Julia, i need to wrap a sphere with it.
I have little idea what your code is doing, but fixing some of the indexing issues gives something that might help you get started. It looks like it's doing something spherical, anyway...
using Images, FileIO
mandrill = load(mandrill.png")
height, width = size(mandrill)
arr = colorim(Array{Float64}(height, width, 3))
function MapCoordinate(i1, i2, w1, w2, p)
return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1
end
function Rotate(angle, axisA, axisB)
return axisA * cos(angle) - axisB * sin(angle),axisA * sin(angle) + axisB * cos(angle)
end
phi0 = 0.0
phi1 = pi
theta0 = 0.0
theta1 = 2.0 * pi
radius = 200
for i = 1:size(mandrill, 1)
for j = 1:size(mandrill, 2)
# map the angles from image coordinates
theta = MapCoordinate(1.0, width - 1, theta1, theta0, i)
phi = MapCoordinate(1.0, height - 1, phi0, phi1, j)
# find the cartesian coordinates
x = radius * sin(phi) * cos(theta)
y = radius * sin(phi) * sin(theta)
z = radius * cos(phi)
# apply rotation around X and Y axis to reposition the sphere
y, z = Rotate(1.5, y, z)
x, z = Rotate(pi/2, x, z)
# plot only positive points
if z > 0
color = mandrill[i, j]
ix = convert(Int, floor(x + width/2))
iy = convert(Int, floor(y + height/2))
arr[ix, iy, :] = [color.r, color.g, color.b]
end
end
end
save("/tmp/mandrill-output.png", arr)
run(`open /tmp/mandrill-output.png`)

How to randomize points on a sphere surface evenly?

Im trying to make stars on the sky, but the stars distribution isnt even.
This is what i tried:
rx = rand(0.0f, PI*2.0f);
ry = rand(0.0f, PI);
x = sin(ry)*sin(rx)*range;
y = sin(ry)*cos(rx)*range;
z = cos(ry)*range;
Which results to:
img http://img716.imageshack.us/img716/3320/sphererandom.jpg
And:
rx = rand(-1.0f, 1.0f);
ry = rand(-1.0f, 1.0f);
rz = rand(-1.0f, 1.0f);
x = rx*range;
y = ry*range;
z = rz*range;
Which results to:
img2 http://img710.imageshack.us/img710/5152/squarerandom.jpg
(doesnt make a sphere, but opengl will not tell a difference, though).
As you can see, there is always some "corner" where are more points in average. How can i create random points on a sphere where the points will be distributed evenly?
you can do
z = rand(-1, 1)
rxy = sqrt(1 - z*z)
phi = rand(0, 2*PI)
x = rxy * cos(phi)
y = rxy * sin(phi)
Here rand(u,v) draws a uniform random from interal [u,v]
You don't need trigonometry if you can generate random gaussian variables, you can do (pseudocode)
x <- gauss()
y <- gauss()
z <- gauss()
norm <- sqrt(x^2 + y^2 + z^2)
result = (x / norm, y / norm, z / norm)
Or draw points inside the unit cube until one of them is inside the unit ball, then normalize:
double x, y, z;
do
{
x = rand(-1, 1);
y = rand(-1, 1);
z = rand(-1, 1);
} while (x * x + y * y + z * z > 1);
double norm = sqrt(x * x + y * y + z * z);
x / norm; y /= norm; z /= norm;
It looks like you can see that it's the cartesian coordinates that are creating the concentrations.
Here is an explanation of one right (and wrong) way to get a proper distribution.

Resources