Lagrange Interpolation in java using BigInteger - java

I am trying to find a function to perform Lagrange Interpolation in java. I have 3 (x,y) pairs, where x and y are BigInteger objects, and would like to use some interpolation function to determine f(0) for the polynomial f used to calculate my x,y these pairs. Something like this seems perfect, except that this class doesn't seem to actually belong to a package I can import: http://nssl.eew.technion.ac.il/files/Projects/thresholddsaimporvement/doc/javadoc/Lagrange.html
Forgive me if my question is naive, any help I can get would really be appreciated.

A similar class seems to be in Apache Commons Math. This is probably going to be much more reliable than what you found.
http://commons.apache.org/math/apidocs/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.html
It would appear that you construct it with PolynomialFunctionLagrangeForm(double[] x, double[] y), then call value(0) to get the value at x = 0.

Related

Method to calculate the distance between points and return point's distance to the origin in a 3D array (java)

I'm trying to make a program that calculates the distance between one point to another in a 3D array, and then returns the point's distance to the origin. What kind of method(s) could I use for this? How should I think? Are there any good tutorials about this (that aren't too basic)? I'm a beginner and very new to methods and would really appreciate some help. :)
Here's the math basics about calculating distance between two points in 3D: https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
The relevant Java functions that will help you are:
Math.sqrt: square root (e.g. Math.sqrt(6) => 2.44948974278)
Math.pow: raise first argument to the power of the second (e.g. Math.pow(2,4) => 16)
Give it a try and post your code if you want more specific help.

Conversion with multiple steps in Java

I'm sorry if the title is misleading, but I am not really sure how else to describe it. I am trying to write a program in Java that would convert data, given only some of the conversion factors. For example, if I have these conversion factors:
Convert a to b by multiplying a by 10
Convert b to c by multiplying b by 20
Convert c to a by dividing c by 200
How would I write a method that would convert a to c? I know that it would have to first convert a to b and then b to c but I do not know how to teach Java to recognize patterns like that. I know that I could just write if statements to check for these directly, however, I have a lot of these conversions and all those if statements seem terribly inefficient. Is there a better way?
You could build a Graph. Every arc between two nodes are a convertion. Finally, you need to run an algorithm like Dijkstra's algorithm to find the shortest way to convert x to y.
This is a StackOverFlow question, with answers providing links and implementation of Graphs and Dijkstra's algorithm in Java.
Edit: it seems somebody else has suggested the same idea :)
You can model this problem as graph search problem. Model each a, b, and c, into a node. Create a directed edge between node u and v if u can be converted into v.
Using this graph, you can find method to convert x to y by simply finding a path from x to y. You can use standard shortest path algorithm like Dijkstra algorithm to find such path.

Calculate exact area under curve in Java

Are there any methods which do that? I have an application where I need the area under the curve, and I am given the formula, so if I can do the integration on hand, I should be able to do it programatically? I can't find the name of the method I'm referring to, but this image demonstrates it: http://www.mathwords.com/a/a_assets/area%20under%20curve%20ex1work.gif
Edit: to everyone replying, I have already implemented rectangular, trapezoidal and Simpson's rule. However, they take like 10k+ stripes to be accurate, and should I not be able to find programatically the integrated version of a function? If not, there must be a bloody good reason for that.
Numerical integration
There are multiple methods, which can be used. For description, have a look in Numerical Recipes: The Art of Scientific Computing.
For Java there is Apace Commons library, which can be used. Integration routines are in Numerical Analysis section.
Symbolic integration
Check out jScience. Functions module "provides support for fairly simple symbolic math analysis (to solve algebraic equations, integrate, differentiate, calculate expressions, and so on)".
If type of function is given, it can be possible to integrate faster in that specific case than when using some standard library.
To compute it exactly, you would need a computer algebra system library of some sort to perform symbolic manipulations. Such systems are rather complicated to implement, and I am not familiar with any high quality, open source libraries for Java. An alternative, though, assuming it meets your requirements, would be to estimate the area under the curve using the trapezoidal rule. Depending on how accurate you require your result to be, you can vary the size of the subdivisions accordingly.
I would recommend using Simpsons rule or the trapezium rule, because it could be excessively complicated to integrate every single type of graph.
See Numerical analysis specifically numerical integration. How about using the Riemann sum method?
You can use numerical integration, using some rule, like already mentioned Simpsons, Trapezoidal, or Monte-Carlo simulation. It uses pseudo random generator.
You can try some libraries for symbolic integration, but I'm not sure that you can get symbolic representation of every integral.
Here's a simple but efficient approach:
public static double area(DoubleFunction<Double> f, double start, double end, int intervals) {
double deltaX = (end - start)/intervals;
double area = 0.0;
double effectiveStart = start + (deltaX / 2);
for (int i=0; i<intervals; ++i) {
area += f.apply(effectiveStart + (i * deltaX));
}
return deltaX * area;
}
This is a Riemann sum using the midpoint rule, which is a variation of the trapezoidal rule, except instead of calculating the area of a trapezoid, I use a rectangle from f(x) at the middle of the interval. This is faster and gives a better result. This is why my effective starting value of x is at the middle of the first interval. And by looping over an integer, I avoid any round-off problems.
I also improve performance by waiting till the end of the loop before multiplying by deltaX. I could have written the loop like this:
for (int i=0; i<intervals; ++i) {
area += deltaX * f.apply(effectiveStart + (i * deltaX)); // this is x * y for each rectangle
}
But deltaX is constant, so it's faster to wait till the loop is finished.
One of the most popular forms of numeric integration is the Runge-Kutta order 4 (RK4) technique. It's implementations is as follows:
double dx, //step size
y ; //initial value
for(i=0;i<number_of_iterations;i++){
double k1=f(y);
double k2=f(y+dx/2*k1);
double k3=f(y+dx/2*k2);
double k4=f(y+dx*k3);
y+= dx/6*(k1+2*k2+2*k3+k4);
}
and will converge much faster than rectangle, trapezoids, and Simpson's rule. It is one of the more commonly used techniques for integration in physics simulations.

Equation of curve generated by QuadCurve2D in Java?

I have drawn a quadratic curve using the QuadCurve2d in java using start, end and one control points. Is there a way I can find out the equation of this curve? The reason why I need is that at some point of this curve it might intersect a circle and I want to know their point of intersection. So basically I want to solve two equations, but I dont know the equation of the curve generated by QuadCurve2D.
Any help will be appreicated.
Regards
S
See the Javadoc for java.awt.geom.PathIterator, specifically the SEG_QUADTO field, in which the control equation in terms of the start, end and control points is given.
Take a look at Area class. Method
public void intersect(Area rhs) {
curves = new AreaOp.IntOp().calculate(this.curves, rhs.curves);
invalidateBounds();
}
You can investigate the sources to understand how intersection point are calculated.
That function uses a quadratic bezier curve:
You can find it on Wikipedia

Best way to find Quadratic Regression Curve in Java

I've three sets of data such as:
x y
4 0
6 60
8 0
Does anyone know any (efficient) Java codes that can give me back the values of a, b, and c (the coefficients)?
I assume you want the formula in this form:
y = a * x^2 + b*x + c
If you have only three points you can describe the quadratic curve that goes through all three points with the formula:
y = ((x-x2) * (x-x3)) / ((x1-x2) * (x1-x3)) * y1 +
((x-x1) * (x-x3)) / ((x2-x1) * (x2-x3)) * y2 +
((x-x1) * (x-x2)) / ((x3-x1) * (x3-x2)) * y3
In your example:
x1 = 4, y1 = 0, x2 = 6, y2 = 60, x3 = 8, y3 = 0
To get the coefficients a, b, c in terms of x1, x2, x3, y1, y2 and y3 you just need to multiply the formula out and then collect the terms. It's not difficult, and it will run very fast but it will be quite a lot of code to type in. It would probably be better to look for a package that already does this for you, but if you want to do it yourself, this is how you could do it.
The fact that two of the y terms are zero in your example makes the formula a lot simpler, and you might be able to take advantage of that. But if that was just a coincidence and not a general rule, then you need the full formula.
The LaGrange interpolation is probably the most 'efficient' (how do you measure that?) solution you are going to find. So I'll suggest a completely general code. You did want code, right? This code can go linear, quadratic, cubic, .... for any number of points.
I didn't actually try to compile it, so I don't if the source code is up to date. You know how online demos go. Yet the applet from the associated web page is fully functional. The jar file will run standalone. With a resizable window, you really don't need to customize it.
It depends on exactly what you are looking for: Are you looking for the unique polynomial which is defined by those three points, or are you looking for a library which will generate a polynomial which passes through all points?
If you are looking at the first, the best technique is to construct the coefficient matrix(That is, the set of three linear equations which uniquely constrain this quadratic equation)and apply Gaussian Elimination to get your result. This can be done by hand the most efficiently, but you can also use The Apache Commons Math Library's Real Matrix solve methods. (EDIT Thanks for the correction--I speak before I think sometimes ;)
If you are looking at the second, this is specific case of a general class of problems called Interpolation by Polynomials, and there are several ways of solving--Splines are my personal favorite, but all have their strengths and weaknesses. Luckily, Apache Commons Math implements several such methods. I would look at the SplineInterpolator class. Splines use cubics instead of quadratics, but they tend to be very good approximations. They also don't fail if one point is a linear multiple of another.
For just three points, both methods should be about equal in performance characteristics. If you are doing more than three points, however, I would strongly recommend using interpolation, as using Guassian Elimination scales incredibly poorly( O(n^3)), and Splines(Or another interpolation technique) are less likely to fail.

Categories