How to plot spheres with Julia? - julia

Simple Problem, I have a data set with x-,y-,z-coordinates and the volume of each sphere.
Should be enough data to plot these spheres? Isn't it?
I'm new to Julia and I actually have no clue how to do this.
Couldn't find any similar and helpful answer...
Thank you,
Tom

I think Makie would be a good option. The documentation for meshscatter gives an example that can be adapted to better fit what you'd like to achieve:
using Makie
x = 100*rand(10)
y = 100*rand(10)
z = 100*rand(10)
scene = meshscatter(x, y, z, markersize = 10, color = :white)
Makie.save("plot.png", scene)

Another option is PyPlot:
using PyPlot
N = 32
u = range(0, stop=2π, length=N)
v = range(0, stop=π, length=N)
x = cos.(u) .* sin.(v)'
y = sin.(u) .* sin.(v)'
z = repeat(cos.(v)',outer=[N, 1])
PyPlot.surf(x,y,z)
PyPlot.savefig("plot.png")

Related

R: Trapezoidal integration over the third dimension of an array

EDIT: I changed my example at the end but I'm still stumped.
How can I apply a trapezoidal integration function so that each matrix point is integrated through the 3rd dimension of an array?
I am working with a large array of data (2160, 4320, 46), and I want to use trapezoidal integration over the third dimension of the array (each matrix point down through the 46 stacks of the third dimension). So for example, point 1,1,1 ; 1,1,2 ; 1,1,3 , etc are inputted as the Y points in the integration function.
EDIT: I have been told that this function can be found in CRAN package caTools by Jarek Tuszynski
I have a custom function from my PI for the trapezoidal integration:
trapz = function(x, y){
idx = 2:length(x)
return (as.double( (x[idx] - x[idx-1]) %*% (y[idx] + y[idx-1])) / 2)
}
And I have used apply functions on such arrays before like this:
data_output = apply(X = data, MARGIN = 1:2 , FUN = function(k) (mean(na.omit(k))) )
But I can't figure out how to get the trapz function to work along the margins that I want.
Working with simple example code, if I create an array like this:
x.mat = matrix(1:100, nrow = 10, ncol = 10)
y.mat = matrix(1:100, nrow = 10, ncol = 10)
library(abind)
x.array = abind(x.mat,(x.mat+1),(x.mat+2),along = 3, force.array = T)
y.array = abind(y.mat,(y.mat+1),(y.mat+2),along = 3, force.array = T)
apply(MARGIN = 1:2, FUN = trapz, X = x.array, y = y.array)
The output is a matrix of the correct dimensions (10,10) but every number is a 4.
Please help me understand what I'm doing wrong.

How to draw a 3-D plot with 3 input variables as well as somewhere infinite in R?

I am writing a log-likelihood surface for the function:
ln[Pr(Y_A=186,Y_B=38,Y_{AB}=13,Y_O=284)]
= ln(G+186*ln(A^2+2*A*O)+38*ln(B^2+2*B*O)+13*ln(2*A*B)+284*ln(O^2))
Thanks to one answerer, I have changed my code to the following but facing new problems:
A = seq(0.0001, .9999,length=50)
B = A
O = A
G = 1.129675e-06
f = function(A,B,O){F = ifelse(A+B+O==1,
G+186*log(A*A+2*A*O)+38*log(B*B+2*B*O)+13*log(2*A*B)+284*log(O*O), O)}
Z <- outer(A, B, O, f)
png()
persp(A,B,Z, theta=60, phi=30 )
dev.off()
The error told me that there isn't object "O".
Error in get(as.character(FUN), mode = "function", envir = envir)
What I mean to do is to input A, B and O under the constraint that A+B+O=1, and then to plot the log-likelihood surface letting A:x-axis, B:y-axis, log-likelihood:z-axis.
I cannot get rid of "O" cause the instruction commands that the parameter of the function should be a 3-dimensional vector: A,B,O.
So what should I do to improve my current code?
If I need to change a function, can anyone suggest a function to use?
(I think maybe I can use barycentric coordinates but I consider it as the last thing I want to do.)
It might be better to avoid regions of A and B where you know you will get into trouble. And use Z rather than f for the z-argument:
A = seq(0.0001, .9999,length=50)
B = A
G=1 # throws an error if not foundf
f = function(A,B){O <- 1-A-B; O <- ifelse(O==0, 0.00000001, O)
G+186*log(A*A+2*A*O)+38*log(B*B+2*B*O)+13*log(2*A*B)+284*log(O*O)}
Z <- outer(A, B, f)
png(); persp(A,B,Z, theta=60, phi=30 ); dev.off()

BVP in R: how to get a numerical solution?

Good day to you all :)
I'm need some help with the boundary value problem.
This is my equation:
sin(1)*y'' + (1+cos(1)*x^2)*y = -1
-1 < x < 1
y(-1) = y(1) = 0
And I'd like to solve this equation by built-in method.
So, I asked the same question on Maple forum (https://www.mapleprimes.com/questions/225225-Dsolve-Does-Not-Work-solving-A-Boundary-Value-Problem) and there is no build-in method.
Maple have No built-in collocation methods, least squares method, Galerkin method to solve ode's.
dsolve it have method series,but in your case dosen't work.
After that, I decided to try to solve this equation with R.
library('bvpSolve')
fun <- function(x,y,pars) {
d1 = sin(1)*y[2]
d2 = -1 - (1 + cos(1))*y[1]
return(list(c(d1, d2)))
}
init = c(y = 0, dy = NA)
end = c(y = 0, dy = NA)
sol = bvpcol(yini = init, x = seq(-1, 1, 0.01),
func = fun, yend = end, parms = NULL)
x = sol[, 1]
y = sol[, 2]
plot(x, y, type = "l")
It builds graphs (probably correct) (at least they match the graphs in Maple)), but I still can't get the numerical solution.
I'd like to get the same result (http://fredrik-j.blogspot.com/2009/02/galerkins-method-in-sympy.html), but in R.
I'd be very grateful if you could suggest some built-in numerical R methods that would help solve this equation.
I don't realy think it's a good idea to explicitly interpolate the function result (as suggested by the Maple community).Is there a simple and effective way to do this?

Translating SAS code to R: Simulation and Dataframes

I am working with this SAS code.
data a;
do i=1 to 10000000;
x = 12 + 2.5*rannor(0);
y = 15000 + 2500*x + 5000*rannor(0);
output;
end;
I am having a hard time in my attempt to write a suitable R code that can replicate (or rather be similar) to what I've done above.
All that I've been able to do was this:
set.seed(0)
x = 12 +2.5*rnorm(1)
y = 1500+ 250*x+ 500*rnorm(1)
...but I think the SAS program actually generates 10000000 x's and y's that have values based on their equations above, so I assume a dataframe is involved.
Anyone used R or/and SAS before? Any ideas as to how I can do something similar to the SAS code?
set.seed(0)
n = 10000000
library(dplyr)
data_frame(x = 12 + 2.5*rnorm(n),
y = 1500+ 250*x+ 500*rnorm(n) )
I don't think an external package is needed here.
set.seed(0)
n <- 10000000
x <- 12+rnorm(n = n,mean = 0,sd = 1)*2.5
y <- 1500 + 250*x + 500*rnorm(n = n, mean = 0, sd = 1)
data <- cbind(x,y)
You just need to call rnorm() to include the n that you are seeking. I believe the above code will do that.

Merging two vectors at random in R

I have two vectors x and y. x is a larger vector compared to y. For example (x is set to all zeros here, but that need not be the case)
x = rep(0,20)
y = c(2,3,-1,-1)
What I want to accomplish is overlay some y's in x but at random. So in the above example, x would look like
0,0,2,3,-1,-1,0,0,0,0,2,3,-1,-1,...
Basically, I'll step through each value in x, pull a random number, and if that random number is less than some threshold, I want to overlay y for the next 4 places in x unless I've reached the end of x. Would any of the apply functions help? Thanks much in advance.
A simple way of doing it would be to choose points at random (the same length as x) from the two vectors combined:
sample(c(x, y), length(x), replace = TRUE)
If you want to introduce some probability into it, you could do something like:
p <- c(rep(2, each = length(x)), rep(1, each = length(y)))
sample(c(x, y), length(x), prob = p, replace = TRUE)
This is saying that an x point is twice as likely to be chosen over a y point (change the 2 and 1 in p accordingly for different probabilities).
Short answer: yes :-) . Write some function like
ranx <- runif(length(x)-length(y)+1)
# some loop or apply func...
if (ranx[j] < threshold) x[j:j+length(y)] <- y
# and make sure to stop the loop at length(y)-length(x)
Something like the following worked for me.
i = 1
while(i <= length(x)){
p.rand = runif(1,0,1)
if(p.rand < prob[i]){
p[i:(i+length(y))] = y
i = i+length(y)
}
i = i + 1
}
where prob[i] is some probability vector.

Resources