How do I generate numbers according to a parabolic distribution? - random

I have a function which plots the distribution of ions emitted as a function of the angle of emission. To simulate the emission I would like to sample values according to the function, but I'm struggling to do so.
The function is:
where theta_0 is a constant.
I've tried to use the inversion method, but I can't seem to get it to work.

Related

mathcad values of the Laplace integral function

if I want to get the tabular value of the laplace function, how should I write it in mathcad?
for example 0.34 and i need 0.1331
(i don't nedd something like this)
I have to notice that the laplace is not a function. It is an integral transform. It means you can take result of transform and put it in function
And then get result for your input. It work even for direct placing
P.S. if I understood question right.

Inpainting in multiple channels

I have a 3d matrix A=[mXnXl], which I want to inpaint, using a mask of mask=[mXn].
So each slice along the "l" is a 2D image (0-255 RGB range). I care about continuity along that axis as also along the 3rd dimenbtison.
I use the inpainting with the two following forms
im1=inpaint.inpaint_biharmonic(np.uint8(A), np.uint8(mask), multichannel=True)
for i in range(0,l):
im2[:,:,i]=inpaint.inpaint_biharmonic(np.uint8(A[:,:,i]), np.uint8(mask), multichannel=False)
How is the 3rd dimension handled in the algorithm? Will they produce the same results?
You can look at the source code of the function here:
https://github.com/scikit-image/scikit-image/blob/c221d982e493a1e39881feb5510bd26659a89a3f/skimage/restoration/inpaint.py#L76
As you can see in the for-loop in that function, the function is just doing the same thing as your for-loop, so the results should be identical.

Accurate Approximations of Fisheye lense distortion via non polynomial FET method?

I'm trying to approximate a Fisheye Lense distortion. I originally used the polynomial method described in this paper, and that worked fine for a forward transform, but I forgot that I would need some sort of interpolation so a backward transform was needed, and I would need an inverse function for this transformation, which proved problematic (I used the non alternating power sign version ie SUM( polynomial_coefficients[i] * radius^i)) so the division model didn't appear to be appropriate (and would spit out bad results if I tried to use the non alternating power version because I would be dividing by my radius). I switched to what appears to be a more accurate method (correct me if I'm wrong and provide a more accurate method) via
r_distorted = scalar * ln(1 + lambda * r_undistorted)
and
r_undistorted = (e^(r_distorted/scalar) - 1)/lambda
which was featured in the same paper. I in the source paper I didn't understand how you would ever end up with no distortion with lower values of lambda, or what the heck I was supposed to do with the scalar value. I wanted to test my code in situatiations where lense distortion was zero, however this formula does not seem to provide a way for me to set the parameters to some value where the forward transform of (r_undistorted) = r_distorted or the inverse transform (r_distorted) = r_undistorted for all r_undistorted and r_distorted. This was trivial however in the polynomial example.
Currently I have the algorithm implemented, but values of 0 for lambda and 1 for scale do not result in no distortion (indeed its obvious to see why) since 1*ln(1 - 0 *x) = 0. This source also alters the equation to be instead of terms of distance from image plane (f in the images) and tan(theta), and leaves me even more confused. It would seem that there must be another variable implicitly involved into the equation that would allow such a transformation (no transform) to happen. It also appears un-intuitive how to actually control distortion using these two equations.
In short, how do I use this equation to apply no distortion, and what do both lambda and scalar mean physically, and what do they do? Are there better methods for accuracy in approximating fisheye transform with inverse?

Get equation for 3d shape

I have 2 arrays say X and Y. Each have 5 elements. Now for each possible combination of (X,Y) I have a Z value, so Z is a 5x5 matrix.
I am looking to find a formula e.g. z=f(x,y). Any idea about how that can be done.
I tried MS Excel surface chart, but it doesn't give any equation or curve fitting on surface charts.
in general I would suggest to use some other software like SciLab or Matlab to work on this task. These products are more computatinal mathematics than Excel.
But Excel has some built-in features that maybe will help you.
First note:
You will need to use the Add-In called "Solver". This add-in comes along with Excel, but maybe is not installed as default on your installation.
One description (there are thousands available in www) how to install that add-in you will find here:
Solver Add-in
If you are done with this, the next step is to create a sheet with the data.
I tried to generate an example shown in the picture below.
The range C5:G9holds the Matrix you want to approximate by a function.
So it's the z=f(x,y) Matrix.
The Chart beside is just the 3D-Plot of your (in this case my) original data.
Now it will become a little bit mathematical....
You need a general type of function which will be used to do the approximation.
The quality of the result is depending on how good this function is able to come close to your data.
In the example I used an approach with a 2nd order approximation (maximum quadratic terms).
My example function is z=a*x^2 + b*y^2 + c *x*y + d*x + e*y +f.
If you need more, try it with a third order term (including also x^3, y^3 , ...).
I didn't want to do this in the example, because I'm hating to type long formulas in Excel.
Typing long formula is the next step:
Now we have to fill the range C15:G19 with the values of the calculated formula. But before this, we have to define the polynomial coefficiants in range J14:J19. As a starting value, you can use just 1 for all coefficients (the picture shows the solution after running the solver)
The formula in Cell C15 is =$J$14*C$14^2+$J$15*$B15^2+$J$16*C$14*$B15+$J$17*C$14+$J$18*$B15+$J$19
It should be easy to copy it to the other cells of the Matrix.
The plot beside this is showing the result of our approximation function.
Now we have to prepare the solver. The solver needs to optimize somehow.
Therefore we need to define a function which indicates the quality of our approximation.
I used the least square value... Have a look on the www for explanations.
In the range C24:G28 I calculated the squares of the differences from our approximation function to the original data. Cell C24 has the formula =(C15-C5)^2
Now we are close to be finished. Just copy this formula to the rest of the range and than add one very important cell:
Put the sum of the range C24:G28 in Cell H29
This value is the sum of the error or better to say the difference of our approximation function to the original data points.
Nowe the most important !!!
Select Cell H29 and start the solver add-in:
This window will pop-up (sorry I have a German Excel installation on my PC)
Just fill in the value fro target cell $H$29, target value =0 and the variable cells (important) $J$14;$J$19
Press "solve" and .... tada the polynomial coefficiants have changed to fit your data with the function.
Is this, what you have been searching for ???
Kindly Regards
Axel
You may google for and try ThreeDify Excel Grapher v4.5, an excel addin that includes a 3D equation fitter with an auto-equation finder.

Path Tracing algorithm - Need help understanding key point

So the Wikipedia page for path tracing (http://en.wikipedia.org/wiki/Path_tracing) contains a naive implementation of the algorithm with the following explanation underneath:
"All these samples must then be averaged to obtain the output color. Note this method of always sampling a random ray in the normal's hemisphere only works well for perfectly diffuse surfaces. For other materials, one generally has to use importance-sampling, i.e. probabilistically select a new ray according to the BRDF's distribution. For instance, a perfectly specular (mirror) material would not work with the method above, as the probability of the new ray being the correct reflected ray - which is the only ray through which any radiance will be reflected - is zero. In these situations, one must divide the reflectance by the probability density function of the sampling scheme, as per Monte-Carlo integration (in the naive case above, there is no particular sampling scheme, so the PDF turns out to be 1)."
The part I'm having trouble understanding is the part in bold. I am familiar with PDFs but I am not quite sure how they fit into here. If we stick to the mirror example, what would be the PDF value we would divide by? Why? How would I go about finding the PDF value to divide by if I was using an arbitrary BRDF value such as a Phong reflection model or Cook-Torrance reflection model, etc? Lastly, why do we divide by the PDF instead of multiply? If we divide, don't we give more weight to a direction with a lower probability?
Let's assume that we have only materials without color (greyscale). Then, their BDRF at each point can be expressed as a single valued function
float BDRF(phi_in, theta_in, phi_out, theta_out, pointWhereObjWasHit);
Here, phi and theta are the azimuth and zenith angles of the two rays under consideration. For pure Lambertian reflection, this function would look like this:
float lambertBRDF(phi_in, theta_in, phi_out, theta_out, pointWhereObjWasHit)
{
return albedo*1/pi*cos(theta_out);
}
albedo ranges from 0 to 1 - this measures how much of the incoming light is reemitted. The factor 1/pi ensures that the integral of BRDF over all outgoing vectors does not exceed 1. With the naive approach of the Wikipedia article (http://en.wikipedia.org/wiki/Path_tracing), one can use this BRDF as follows:
Color TracePath(Ray r, depth) {
/* .... */
Ray newRay;
newRay.origin = r.pointWhereObjWasHit;
newRay.direction = RandomUnitVectorInHemisphereOf(normal(r.pointWhereObjWasHit));
Color reflected = TracePath(newRay, depth + 1);
return emittance + reflected*lambertBDRF(r.phi,r.theta,newRay.phi,newRay.theta,r.pointWhereObjWasHit);
}
As mentioned in the article and by Ross, this random sampling is unfortunate because it traces incoming directions (newRay's) from which little light is reflected with the same probability as directions from which there is lots of light. Instead, directions whence much light is reflected to the observer should be selected preferentially, to have an equal sample rate per contribution to the final color over all directions. For that, one needs a way to generate random rays from a probability distribution. Let's say there exists a function that can do that; this function takes as input the desired PDF (which, ideally should be be equal to the BDRF) and the incoming ray:
vector RandomVectorWithPDF(function PDF(p_i,t_i,p_o,t_o,point x), Ray incoming)
{
// this function is responsible to create random Rays emanating from x
// with the probability distribution PDF. Depending on the complexity of PDF,
// this might somewhat involved. It is possible, however, to do it for Lambertian
// reflection (how exactly is math, not programming):
vector randomVector;
if(PDF==lambertBDRF)
{
float phi = uniformRandomNumber(0,2*pi);
float rho = acos(sqrt(uniformRandomNumber(0,1)));
float theta = pi/2-rho;
randomVector = getVectorFromAzimuthZenithAndNormal(phi,zenith,normal(incoming.whereObjectWasHit));
}
else // deal with other PDFs
return randomVector;
}
The code in the TracePath routine would then simply look like this:
newRay.direction = RandomVectorWithPDF(lambertBDRF,r);
Color reflected = TracePath(newRay, depth + 1);
return emittance + reflected;
Because the bright directions are preferred in the choice of samples, you do not have to weight them again by applying the BDRF as a scaling factor to reflected. However, if PDF and BDRF are different for some reason, you would have to scale down the output whenever PDF>BDRF (if you picked to many from the respective direction) and enhance it when you picked to little .
In code:
newRay.direction = RandomVectorWithPDF(PDF,r);
Color reflected = TracePath(newRay, depth + 1);
return emittance + reflected*BDRF(...)/PDF(...);
The output is best, however, if BDRF/PDF is equal to 1.
The question remains why can't one always choose the perfect PDF which is exactly equal to the BDRF? First, some random distributions are harder to compute than others. For example, if there was a slight variation in the albedo parameter, the algorithm would still do much better for the non-naive sampling than for uniform sampling, but the correction term BDRF/PDF would be needed for the slight variations. Sometimes, it might even be impossible to do it at all. Imagine a colored object with different reflective behavior of red green and blue - you could either render in three passes, one for each color, or use an average PDF, which fits all color components approximately, but none perfectly.
How would one go about implementing something like Phong shading? For simplicity, I still assume that there is only one color component, and that the ratio of diffuse to specular reflection is 60% / 40% (the notion of ambient light makes no sense in path tracing). Then my code would look like this:
if(uniformRandomNumber(0,1)<0.6) //diffuse reflection
{
newRay.direction=RandomVectorWithPDF(lambertBDRF,r);
reflected = TracePath(newRay,depth+1)/0.6;
}
else //specular reflection
{
newRay.direction=RandomVectorWithPDF(specularPDF,r);
reflected = TracePath(newRay,depth+1)*specularBDRF/specularPDF/0.4;
}
return emittance + reflected;
Here specularPDF is a distribution with a narrow peak around the reflected ray (theta_in=theta_out,phi_in=phi_out+pi) for which a way to create random vectors is available, and specularBDRF returns the specular intensity from Phong's model (http://en.wikipedia.org/wiki/Phong_reflection_model).
Note how the PDFs are modified by 0.6 and 0.4 respectively.
I'm by no means an expert in ray tracing, but this seems to be classic Monte Carlo:
You have lots of possible rays, and you choose one uniformly at random and then average over lots of trials.
The distribution you used to choose one of the rays was uniform (they were all equally as likely)
so you don't have to do any clever re-normalising.
However, Perhaps there are lots of possible rays to choose, but only a few would possibly lead to useful results.We therefore bias towards picking those 'useful' possibilities with higher probability, and then re-normalise (we are not choosing the rays uniformly any more, so we can't just take the average). This is
importance sampling.
The mirror example seems to be the following: only one possible ray will give a useful result.
If we choose a ray at random then the probability we hit that useful ray is zero: this is a property
of conditional probability on continuous spaces (it's not actually continuous, it's implicitly discretised
by your computer, so it's not quite true...): the probability of hitting something specific when there are infinitely many things must be zero.
Thus we are re-normalising by something with probability zero - standard conditional probability definitions
break when we consider events with probability zero, and that is where the problem would come from.

Resources