commons-math differentiation result is 0 - java

I'm trying to use commons-math library for some numerical differentiation task. I've built a very simple function using DerivativeStructures which I thought would work; apparently I was wrong.
public static void main(String[] args) {
DerivativeStructure x0 = new DerivativeStructure(2, 2, 2.0);
DerivativeStructure y0 = new DerivativeStructure(2, 2, 4.0);
DerivativeStructure xi = x0.pow(2);
DerivativeStructure yi = y0.pow(2);
DerivativeStructure f = xi.add(yi);
System.out.println(f.getValue());
System.out.println(f.getPartialDerivative(1, 0)); // (?)
System.out.println(f.getPartialDerivative(0, 1)); // (?)
}
I'm trying to get the 1st and 2nd order partial derivatives of a the multivariate function f(x)=x^2+y^2 at point (2.0, 4.0). As a result I'd expect 4.0 for df/dx and 8.0 for df/dy as first order partials. 2.0 for second order partials. I however am getting the correct f(x,y) value and I don't even have the slightest idea from this javadoc. I saw a couple questions here on stackoverflow with some comments about the opaque documentation for commons-math but not a working example on multivariate functions. Univariate I can work out, but not this...
Any tips would be appreciated!

In your code you haven't really specified 2 independent variables x0, y0 but only 1. With DerivativeStructure x0, y0 are actually seen as functions themselves depending on an implicit vector of variables p. For each independent variable you have to give a different index into the p vector of independent variables. What you need to do is:
DerivativeStructure x0 = new DerivativeStructure(2, 2, 0, 2.0);
DerivativeStructure y0 = new DerivativeStructure(2, 2, 1, 4.0);
Where the third parameter(s) 0 and 1 indicate 2 different indexes in the p vector therefore two different independent variables. If you omit this parameter when creating a DerivativeStructure, 0 is assumed so in your code x0, y0 are not independent.
Further Reading

Related

Gain/Persistence in Simplex Noise based on slope

I'm currently working on a terrain engine and I'm experimenting a little bit with noise. It's so fascinating to see what different structures, functions and pure imagination can create with just a few lines of code. Recently I saw this post: http://squall-digital.com/ProceduralGeneration.html, I was definitely intrigued by all of these techniques, but especially the first one caught my attention. The programmer made the gain (or persistence) of the noise to be proportional to the slope of the noise on that point. I'm currently trying to achieve this but I don't think I'm on the right track.
I'm currently using simplex noise. I know the author of the article uses Perlin Noise and yes, I have seen how to calculate the derivative of Perlin Noise, but obviously this implementation wouldn't work because of the fundamental differences in how Perlin and Simplex noise are generated. I thus set out on my own way to try and approximate the slope of noise on a given position.
I came up with the following "algorithm":
Calculate neighboring points of noise [(x + 1, z), (x - 1, z), (x, z + 1), (x, z - 1)].
Calculate their respective noise value
Calculate differenceX and differenceZ in noise values on the x-axis and the z-axis respectively
Create vectors from origin: (2, differenceX, 0) and (0, differenceZ, 2)
Scale to vectors of length 1
Add y-components of the resulting unit vectors
use this y-component as the "slope" approximated at the given point.
Now I have implemented this in code (I added "3D" vectors for the purpose of ease of understanding)
private static float slope(OpenSimplex2F simplex, float x, float z, float noise) {
float[] neighbours = getStraightNeighbours(simplex, x, z);
float xSlope = (neighbours[1] - neighbours[0]) / (2.0f * x);
float zSlope = (neighbours[3] - neighbours[2]) / (2.0f * z);
float[] vecX = new float[] { 1, xSlope, 0 };
float[] vecZ = new float[] { 0, zSlope, 1 };
float scaleX = Maths.sqrt(1.0f + xSlope * xSlope);
float scaleZ = Maths.sqrt(1.0f + zSlope * zSlope);
for (int i = 0; i < 3; i++) {
vecX[i] /= scaleX;
vecZ[i] /= scaleZ;
}
float[] grad = new float[] {
vecX[0] + vecZ[0],
vecX[1] + vecZ[1],
vecX[2] + vecZ[2]
};
return grad[1];
}
Now this gives me extremely underwhelming and rest assured, wrong results: Result
Is there anyone that can explain me if this is a good technique to approximate the slope of if this is completely wrong. I'm not the biggest math genius so I was already happy I could figure this out and that it produced a result in the first place. If anyone has a resource linked to the derivative of simplex noise (which would be a life saver, obviously), it'd be really appreciated!

How to calculate distance between two point over a path

I am having a list of co-ordinates that represent the path, and source and destination coordinates. So, using spatial4j,JTS,GeoTools or any other library how to calculate the distance between two points(source & destination) over a predefined path(list of coordinates).
Below is the sample which I have tried using spatail4j which is a straight line distance. But the same thing how we can achieve over a path I have used spatial4j but using different libraries like JTS,GeoTools etc.,
public static void main(String[] args) {
SpatialContext ctx = SpatialContext.GEO;
Point p1= ctx.getShapeFactory().pointXY( 77.610099,12.91502);
Point p2= ctx.getShapeFactory().pointXY( 77.59038,12.917055);
System.out.println(ctx.getDistCalc().distance(p1, p2) * DistanceUtils.DEG_TO_KM);
}
// output: 2.149124512680105
Below are the route/path geopoints:
12.91502 , 77.610099
12.91502 , 77.610092
12.913957 , 77.610069
12.913954 , 77.610033
12.91644 , 77.610048
12.916573 , 77.605512
12.916618 , 77.603053
12.916622 , 77.601803
12.916652 , 77.600092
12.916735 , 77.597653
12.916896 , 77.590946
12.916927 , 77.590242
12.916936 , 77.589467
12.917083 , 77.589466
12.917055 , 77.59038
According to the google map the value should be 2.8Km. Is there any other java library using which we achieve the same thing as the resource for spatial4j is very less.
If you want precision, then you should probably go to Haversine Formula, luckily enough, you can always find a useful solution browsing the web
public static Double getDistance(Double lat1, Double lon1, Double lat2, Double lon2) {
final int R = 6371; // Earth Radius in km, use 3959 if you want in miles
Double latDistance = toRad(lat2-lat1);
Double lonDistance = toRad(lon2-lon1);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
I've actually just adapted this to your use-case.
public static void main(String[] args) {
ArrayList<Point> points = new ArrayList<>();
points.add(new Point(1, 0));
points.add(new Point(2, 0));
points.add(new Point(3, 0));
int distance = 0;
for (int i = 1; i < points.size(); i++) {
Point smaller = points.get(i-1);
Point bigger = points.get(i);
distance += Math.sqrt(Math.pow(bigger.x-smaller.x, 2)+Math.pow(bigger.y-smaller.y, 2));
}
System.out.println(distance);
}
It can work like this. Use Pythagoras for the distance between Point 1 and 2, then Point 2 and 3. At the end add up all those calculated distances.
Other responses I see here are good, #Luiz's is simple and straightforward, and #8bit's is more precise.
No matter which implementation of them you choose (or even the one you provide in the question itself ctx.getDistCalc().distance), I'd like to give an insight for the problem it seems you're struggling the most: how to apply and accumulate the results of your distance function over the whole list?
IntStream.range(0, path.length)
.map(i -> i == 0
? 0.0d
: ctx.getDistCalc().distance(path[i - 1], path[i]))
.sum()
* DistanceUtils.DEG_TO_KM;
This assumes path is an array with your points.
More info on streams here.

How to calculate a cubic function using the Vietea's formula in Java?

I have to write a program in which I write a,b c,d (coefficients of equation 3 degree) and as a result I should get X1, X2, X3 (solutions of equation). I have to use Viete's formulas and BigDecimal for this, because my lecturer requires it from me.
I came to the conclusion that I have to solve the following system of equations:
x1+x2+x3=-b/a
x1*x2+x1*x3+x2*x3=c/a
x1*x2*x3=-d/a
I have no idea how I can do it in Java.
I tried to use the JAMA package, but I don't think I can use it to solve such a system of equations.
How can I do that?
If you want to find the roots of a cubic polynomial in Java you can do it easily using Newton-Raphson's method.
The algorithm -
1. Input: initial x, func(x), derivFunc(x)
Output: Root of Func()
2. Compute values of func(x) and derivFunc(x) for given initial x
3. Compute h: h = func(x) / derivFunc(x)
4. While h is greater than allowed error ε
- h = func(x) / derivFunc(x)
- x = x – h
Here is a demonstration for solving the cubic equation x^3-x^2+2
class XYZ {
static final double EPSILON = 0.001;
// An example function whose solution
// is determined using Bisection Method.
// The function is x^3 - x^2 + 2
static double func(double x)
{
return x * x * x - x * x + 2;
}
// Derivative of the above function
// which is 3*x^x - 2*x
static double derivFunc(double x)
{
return 3 * x * x - 2 * x;
}
// Function to find the root
static void newtonRaphson(double x)
{
double h = func(x) / derivFunc(x);
while (Math.abs(h) >= EPSILON)
{
h = func(x) / derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
}
System.out.print("The value of the"
+ " root is : "
+ Math.round(x * 100.0) / 100.0);
}
// Driver code
public static void main (String[] args)
{
// Initial values assumed
double x0 = -20;
newtonRaphson(x0);
}
}
Output - The value of root is : -1.00
To do it your way you have to solve a system of non-linear equations which is harder but can be done using the Newton Raphson's Multivariate method. You might want to look it up. Also note that this is an approximate method and guesses the roots after you put an initial 'guess' of your own (in this case its -20)
The Newton (Raphson, Kantorovich) method for the Viete equations gives you the (Weierstrass-)Durand-Kerner method of simultaneous root approximation. However, in the completed method you will no longer see the Viete identities, they kind of cancel out. You will need complex numbers over the demanded real numbers data type.
If you go with the simple Newton method like in the other answer, then after computing the one real root you can split off the linear factor belonging to it via the Horner-Ruffini scheme and then solve the remaining quadratic equation directly. Then you only need to consider the possible complex nature of the roots in constructing the output strings, as the real and imaginary parts have easy direct formulas.

dot product of two quaternion rotations

I understand that the dot (or inner) product of two quaternions is the angle between the rotations (including the axis-rotation). This makes the dot product equal to the angle between two points on the quaternion hypersphere.
I can not, however, find how to actually compute the dot product.
Any help would be appreciated!
current code:
public static float dot(Quaternion left, Quaternion right){
float angle;
//compute
return angle;
}
Defined are Quaternion.w, Quaternion.x, Quaternion.y, and Quaternion.z.
Note: It can be assumed that the quaternions are normalised.
The dot product for quaternions is simply the standard Euclidean dot product in 4D:
dot = left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
Then the angle your are looking for is the arccos of the dot product (note that the dot product is not the angle): acos(dot).
However, if you are looking for the relative rotation between two quaternions, say from q1 to q2, you should compute the relative quaternion q = q1^-1 * q2 and then find the rotation associated withq.
Just NOTE: acos(dot) is very not stable from numerical point of view.
as was said previos, q = q1^-1 * q2 and than angle = 2*atan2(q.vec.length(), q.w)
Should it be 2 x acos(dot) to get the angle between quaternions.
The "right way" to compute the angle between two quaternions
There is really no such thing as the angle between two quaternions, there is only the quaternion that takes one quaternion to another via multiplication. However, you can measure the total angle of rotation of that mapping transformation, by computing the difference between the two quaternions (e.g. qDiff = q1.mul(q2.inverse()), or your library might be able to compute this directly using a call like qDiff = q1.difference(q2)), and then measuring the angle about the axis of the quaternion (your quaternion library probably has a routine for this, e.g. ang = qDiff.angle()).
Note that you will probably need to fix the value, since measuring the angle about an axis doesn't necessarily give the rotation "the short way around", e.g.:
if (ang > Math.PI) {
ang -= 2.0 * Math.PI;
} else if (ang < -Math.PI) {
ang += 2.0 * Math.PI;
}
Measuring the similarity of two quaternions using the dot product
Update: See this answer instead.
I assume that in the original question, the intent of treating the quaternions as 4d vectors is to enable a simple method for measuring the similarity of two quaternions, while still keeping in mind that the quaternions represent rotations. (The actual rotation mapping from one quaternion to another is itself a quaternion, not a scalar.)
Several answers suggest using the acos of the dot product. (First thing to note: the quaternions must be unit quaternions for this to work.) However, the other answers don't take into account the "double cover issue": both q and -q represent the exact same rotation.
Both acos(q1 . q2) and acos(q1 . (-q2)) should return the same value, since q2 and -q2 represent the same rotation. However (with the exception of x == 0), acos(x) and acos(-x) do not return the same value. Therefore, on average (given random quaternions), acos(q1 . q2) will not give you what you expect half of the time, meaning that it will not give you a measure of the angle between q1 and q2, assuming that you care at all that q1 and q2 represent rotations. So even if you only plan to use the dot product or acos of the dot product as a similarity metric, to test how similar q1 and q2 are in terms of the effect they have as a rotation, the answer you get will be wrong half the time.
More specifically, if you are trying to simply treat quaternions as 4d vectors, and you compute ang = acos(q1 . q2), you will sometimes get the value of ang that you expect, and the rest of the time the value you actually wanted (taking into account the double cover issue) will be PI - acos(-q1 . q2). Which of these two values you get will randomly fluctuate between these values depending on exactly how q1 and q2 were computed!.
To solve this problem, you have to normalize the quaternions so that they are in the same "hemisphere" of the double cover space. There are several ways to do this, and to be honest I'm not even sure which of these is the "right" or optimal way. They do all produce different results from other methods in some cases. Any feedback on which of the three normalization forms above is the correct or optimal one would be greatly appreciated.
import java.util.Random;
import org.joml.Quaterniond;
import org.joml.Vector3d;
public class TestQuatNorm {
private static Random random = new Random(1);
private static Quaterniond randomQuaternion() {
return new Quaterniond(
random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1,
random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1)
.normalize();
}
public static double normalizedDot0(Quaterniond q1, Quaterniond q2) {
return Math.abs(q1.dot(q2));
}
public static double normalizedDot1(Quaterniond q1, Quaterniond q2) {
return
(q1.w >= 0.0 ? q1 : new Quaterniond(-q1.x, -q1.y, -q1.z, -q1.w))
.dot(
q2.w >= 0.0 ? q2 : new Quaterniond(-q2.x, -q2.y, -q2.z, -q2.w));
}
public static double normalizedDot2(Quaterniond q1, Quaterniond q2) {
Vector3d v1 = new Vector3d(q1.x, q1.y, q1.z);
Vector3d v2 = new Vector3d(q2.x, q2.y, q2.z);
double dot = v1.dot(v2);
Quaterniond q2n = dot >= 0.0 ? q2
: new Quaterniond(-q2.x, -q2.y, -q2.z, -q2.w);
return q1.dot(q2n);
}
public static double acos(double val) {
return Math.toDegrees(Math.acos(Math.max(-1.0, Math.min(1.0, val))));
}
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
var q1 = randomQuaternion();
var q2 = randomQuaternion();
double dot = q1.dot(q2);
double dot0 = normalizedDot0(q1, q2);
double dot1 = normalizedDot1(q1, q2);
double dot2 = normalizedDot2(q1, q2);
System.out.println(acos(dot) + "\t" + acos(dot0) + "\t" + acos(dot1)
+ "\t" + acos(dot2));
}
}
}
Also note that:
acos is known to not be very numerically accurate (given some worst-case inputs, up to half of the least significant digits can be wrong);
the implementation of acos is exceptionally slow in the JDK standard libraries;
acos returns NaN if its parameter is even slightly outside [-1,1], which is a common occurrence for dot products of even unit quaternions -- so you need to bound the value of the dot product to that range before calling acos. See this line in the code above:
return Math.toDegrees(Math.acos(Math.max(-1.0, Math.min(1.0, val))));
According to this cheatsheet Eq. (42), there is a more robust and accurate way of computing the angle between two vectors that replaces acos with atan2 (although note that this does not solve the double cover problem either, so you will need to use one of the above normalization forms before applying the following):
ang(q1, q2) = 2 * atan2(|q1 - q2|, |q1 + q2|)
I admit though that I don't understand this formulation, since quaternion subtraction and addition has no geometrical meaning.

Java normal distribution with mean diff than zero and std deviance different than one

My professor is simulating the arrival of clients to a bank.
It says that clients arrive following a normal distribution with 3.5 mean and 1.3 std deviance.
The problem is that I'm with some difficulties understanding why is using this code.
I believe that the code he provided is receiving a stream, mean and std to generate a random number
Code
public class Random {
private static double second=0;
static double normal(int stream, double mean, double std){
double v1=0, v2=0, y1, y2, x1, x2, w=2;
if (second!=0 ){
return second;
}
while(w>1){
v1=2*RandomGenerator.rand(stream)-1;
v2=2*RandomGenerator.rand(stream)-1;
w=Math.pow(v1,2)+Math.pow(v2,2);
}
y1=v1*Math.pow((-2*Math.log(w))/w, 0.5);
y2=v2*Math.pow((-2*Math.log(w))/w, 0.5);
x1=mean+y1*std;
x2=mean+y2*std;
second=x2;
return x1;
}
}
My math lessons were many years ago so it's a bit hard to understand this normal distribution and after searched the internet to how calculate normal distribution I couldn't find any formula resembling to the code given.
So, these are my questions.
What does second does? It's some kind of protection?
How does this code uses the normal distribution? Can't seem to figure it out...
I'll answer the questions in reverse order. The central part of the method is a standard technique for converting two random variables drawn from a uniform distribution on [0, 1] into two random variables drawn from a normal distribution with a given mean and standard deviation. (It's called the Box-Muller transform.) The while loop uses a generate-and-test technique to generate a random point (v1, v2) from a 2-D uniform distribution on the unit circle. The mapping from (v1, v2) to (y1, y2) converts the uniform distribution to a 2-D normal distribution with zero mean and unit standard deviation. This is then shifted and scaled to get (x1, x2) with the desired mean and standard deviation.
With a method of generating pairs of normally distributed variables now in hand, the rest of the code works as follows: if second == 0, this is a signal that new values need to be generated, so it falls through the if and generates a pair of values. It then saves one of the values in second and returns the other. The next time the method is called, it notices that there's a valid value in second and returns that immediately, instead of generating more values.
There's a major bug in the code: second should be reset to zero before it's value is returned; otherwise it will always continue returning the second random value (which won't seem so random after the second call). Even if that flaw were corrected, there's a second problem. Every other returned value will have zero probability of having value 0. Theoretically this isn't too bad because the probability of a normally distributed random sample having value exactly zero is...zero! However, it is still a flaw.
I'd rewrite the code as follows:
public class Random {
private static double second;
private static boolean secondValid = false;
static double normal(int stream, double mean, double std) {
double v1, v2, y1, y2, x1, x2, w;
if (secondValid) {
secondValid = false;
return second;
}
do {
v1 = 2 * RandomGenerator.rand(stream) - 1;
v2 = 2 * RandomGenerator.rand(stream) - 1;
w = v1 * v1 + v2 * v2;
} while (w > 1);
y1 = v1 * Math.sqrt(-2 * Math.log(w) / w);
y2 = v2 * Math.sqrt(-2 * Math.log(w) / w);
x1 = mean + y1 * std;
x2 = mean + y2 * std;
second = x2;
secondValid = true;
return x1;
}
}

Categories