Videos
One thing I learned this year is that maybe I should figure out how to use some math libraries/packages.
There are two problems that I ended up using a little outside help with. One was doing a polyfit. Especially, I have a list of input values and output results and I want the computer to tell me a function that matches. This is good for things where I'm sure there is a simple math relation, I just can't see it. For example in one problem, I had a series of numbers and needed polyfit to tell me I was actually looking at 5 + 2x + x^2 or something like that.
I ended up using python and numpy for that, which worked but I'd like to add that sort of capability to my Java tool kit.
The other, I kinda worked around but it would be nice to have is a symbolic math solver.
I've poked at Symja which has a enticing named solver:
https://bitbucket.org/axelclk/symja_android_library/wiki/Solve
But I'm having trouble getting the dependencies to run. Anyone have anything they like?
In case it matters, I'm using bazel to build, which means I can easily include anything that is on maven.
The Apache Commons Math library contains many advanced mathematical functions and algorithms that you can use. It has a section, Numerical Analysis which have many algorithms for common math problems, such as roots finding, derivatives and integrals.
Please refer to the hyperlinks for more information and documentation about the library.
The workflow of computation of a derivatives of an expression y=f(x) is the following one.
First we configure an input parameter x of type DerivativeStructure so it will drive the function to compute all derivatives up to order 3 for example.
Then we compute y=f(x) normally by passing this parameter to the f function.
At the end, we extract from y the value and the derivatives we want.
As we have specified 3rd order when we built x, we can retrieve the derivatives up to 3rd order from y.
Copyint params = 1;
int order = 3;
double xRealValue = 2.5;
DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue);
DerivativeStructure y = f(x);
You can then, get the derivatives:
CopySystem.out.println("y = " + y.getValue();
System.out.println("y' = " + y.getPartialDerivative(1);
System.out.println("y'' = " + y.getPartialDerivative(2);
System.out.println("y''' = " + y.getPartialDerivative(3);
You don't need to import anything, the static class java.lang.Math will satisfy your needs.
Example:
Copydouble result = Math.sqrt(112);
would Math fall into this category?
Yes. It is documented as part of the JDK javadoc (since JDK 1.0) so you are guaranteed that it will exist in any JRE you'll ever encounter.
Note that since it resides in java.lang, you do not have to import it explicitly; you could:
import java.lang.Math;
but since all classes in java.lang are automatically imported (that includes String and Integer for instance), you need not do that.
This is a peculiar class in the sense that it cannot be instantiated and it only contains static methods and constants; apart from that you are sure to have it available, and that methods and constants obey the defined contract.
It comes with the SDK, if that is what "standard" meant.
It is part of the java.lang package, thus does not require import.