-
Notifications
You must be signed in to change notification settings - Fork 0
Functions
The backbone of calculus are algebraic functions. Almost every method in Calculatte requires the use of a function. Whether you are integrating a function, taking a volume of revolution, or finding the volume of a known cross-section—you are using an algebraic function.
In Calculatte, all functions are implements of the Function interface. The Function interface is defined under
Function.java as
/**
* The Function interface for all other functions to be based off of.
*/
public interface Function {
/**
* Calculates the y-value for a given x-value of this function.
*
* @param x The x-value to find the y-value for.
* @return The y-value for the given x-value.
*/
double f(double x);
}To create your own functions, you will need to create a new Function object. You can achieve this through three
main methods: lambdas within your code, overrides within your code, or creating a new class in a separate file. Which
method you choose is up to your project and its needs. Examples of the three methods are shown below, defining y = 2x.
// With lambdas
Function TwoX = x -> 2 * x;
// Without lambdas
Function TwoX = new Function() {
@Override
public double f(double x) {
return 2 * x;
}
};or
public class TwoX implements Function {
@Override
public double f(double x) {
return 2 * x;
}
}You can also embed functions within functions. An example of this can be seen in volume of revolutions
calculations. In the source code of Calculatte, you can see how the revolve() method works by embedding the
top and bottom function into the squared and offset version of themselves.
public double revolve(double a, double b, double axis, Function functionTop, Function functionBottom) {
// The top function with the axis offset, squared.
Function squaredFunctionTop = x -> Math.pow(axis - functionTop.f(x), 2);
// The bottom function with the axis offset, squared.
Function squaredFunctionBottom = x -> Math.pow(axis - functionBottom.f(x), 2);
// Split the volume of revolution formula into two separate integrals.
double volume = Math.PI * (integrateRaw(a, b, squaredFunctionTop) - integrate(a, b, squaredFunctionBottom));
return round(volume, revolutionRoundingDecimalPlaces);
}Make sure to return the y-value of the embedded function.
Function function = x -> embeddedFunction.f(x); // Correct.
Function function = x -> embeddedFunction; // Incorrect.Calculatte also supports the use of piecewise functions. Take the following piecewise function.
The code for the above piecewise function would be defined like so.
// With lambdas
Function piecewise = x -> {
if (x < 0) { // x < 0
return 1; // y = 1
} else if (x < 3) { // 0 <= x < 3
return x; // y = x
} else { // x >= 3
return Math.pow(x, 2); // y = x^2
}
};
// Without lambdas
Function piecewise = new Function() {
@Override
public double f(double x) {
if (x < 0) { // x < 0
return 1; // y = 1
} else if (x < 3) { // 0 <= x < 3
return x; // y = x
} else { // x >= 3
return Math.pow(x, 2); // y = x^2
}
}
};