-
Notifications
You must be signed in to change notification settings - Fork 1
Examples
eduardovalentim edited this page Aug 30, 2022
·
4 revisions
- Define the @Formula you need in an interface method:
import java.math.BigDecimal;
import com.github.eduardovalentim.easymath.annotations.Formula;
public interface Algebra {
@Formula("(a ^ 3) + (b ^ 3)")
public double cubesSum(Number... args);
@Formula("(a + b) * ((a ^ 2) - (a * b) + (b ^ 2))")
public BigDecimal cubesSumExpansion(Number... args);
}
- Compile the code (of course you'll need setup) and see an automagically implementation of the interface
Algebra
.
import static com.github.eduardovalentim.easymath.Numbers.*;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import javax.annotation.Generated;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.eduardovalentim.easymath.FunctionCatalog;
import com.github.eduardovalentim.easymath.functions.CoreFunctionCatalog;
@Generated("com.github.eduardovalentim.easymath.processor.mathematical.MathematicalProcessor")
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class AlgebraImpl implements Algebra {
private static MessageFactory factory = new MessageFormatMessageFactory();
private static Logger logger = LogManager.getLogger(AlgebraImpl.class, factory);
private static final java.math.BigDecimal BD2 = new BigDecimal("2");
private static final double D3 = 3D;
private FunctionCatalog catalog = new CoreFunctionCatalog();
/**
* Public constructor with catalog customization
* @param catalogs
*/
public AlgebraImpl(FunctionCatalog... catalogs) {
/*
* Method protection
*/
if (catalogs == null)
throw new IllegalArgumentException("Argument 'catalogs' cannot be null.");
/*
* Join the default catalog with all informed
*/
this.catalog = catalog.join(catalogs);
}
/**
* The implementation of the formula: (a ^ 3) + (b ^ 3)
*
* @param inputs The inputs for calculation
* @return the result of the calculation
*/
@Override
public double cubesSum(Number... inputs) {
/*
* Method protection block
*/
if (inputs == null)
throw new IllegalArgumentException("Argument 'inputs' cannot be null.");
if (inputs.length != 2)
throw new IllegalArgumentException(
"Length mismatch for argument 'inputs'. Expected '2' actual '" + inputs.length + "'");
/*
* Typecast block
*/
double a = toDouble(inputs[0], 0);
double b = toDouble(inputs[1], 1);
return cubesSum(a, b);
}
/**
* The implementation of the formula: (a ^ 3) + (b ^ 3)
*
* @param a The a input
* @param b The b input
* @return the result of the calculation
*/
protected double cubesSum(double a, double b) {
logger.debug(
"Resolving expression (a ^ 3) + (b ^ 3) with ({0, number, #.##########} ^ 3) + ({1, number, #.##########} ^ 3)",
a, b);
/*
* Function precision and rounding mode definition
*/
MathContext mc = new MathContext(7, RoundingMode.HALF_UP);
/*
* Function resolution
*/
double r1 = toDouble(catalog.solve("pow", mc, a, D3), 0);
logger.trace(
"Resolving operation a^3 with {0, number, #.##########}^{1, number, #.##########} = {2, number, #.##########}",
a, D3, r1);
double r2 = toDouble(catalog.solve("pow", mc, b, D3), 0);
logger.trace(
"Resolving operation b^3 with {0, number, #.##########}^{1, number, #.##########} = {2, number, #.##########}",
b, D3, r2);
double r0 = r1 + r2;
logger.trace(
"Resolving operation (a^3)+(b^3) with {0, number, #.##########}+{1, number, #.##########} = {2, number, #.##########}",
r1, r2, r0);
// ensure the necessary precision
logger.trace("Rounding the result {0, number, #.##########}", r0);
r0 = round(r0, mc);
/*
* Result
*/
logger.debug("Returning {0, number, #.#######} as the result!", r0);
return r0;
}
/**
* The implementation of the formula: (a + b) * ((a ^ 2) - (a * b) + (b ^ 2))
*
* @param inputs The inputs for calculation
* @return the result of the calculation
*/
@Override
public java.math.BigDecimal cubesSumExpansion(Number... inputs) {
/*
* Method protection block
*/
if (inputs == null)
throw new IllegalArgumentException("Argument 'inputs' cannot be null.");
if (inputs.length != 2)
throw new IllegalArgumentException(
"Length mismatch for argument 'inputs'. Expected '2' actual '" + inputs.length + "'");
/*
* Typecast block
*/
java.math.BigDecimal a = toBigDecimal(inputs[0], 0);
java.math.BigDecimal b = toBigDecimal(inputs[1], 1);
return cubesSumExpansion(a, b);
}
/**
* The implementation of the formula: (a + b) * ((a ^ 2) - (a * b) + (b ^ 2))
*
* @param a The a input
* @param b The b input
* @return the result of the calculation
*/
protected java.math.BigDecimal cubesSumExpansion(java.math.BigDecimal a, java.math.BigDecimal b) {
logger.debug(
"Resolving expression (a + b) * ((a ^ 2) - (a * b) + (b ^ 2)) with ({0, number, #.##########} + {1, number, #.##########}) * (({0, number, #.##########} ^ 2) - ({0, number, #.##########} * {1, number, #.##########}) + ({1, number, #.##########} ^ 2))",
a, b);
/*
* Method protection block
*/
if (a == null)
throw new IllegalArgumentException("Argument 'a' cannot be null.");
if (b == null)
throw new IllegalArgumentException("Argument 'b' cannot be null.");
/*
* Function precision and rounding mode definition
*/
MathContext mc = new MathContext(7, RoundingMode.HALF_UP);
/*
* Function resolution
*/
java.math.BigDecimal r1 = a.add(b, mc);
logger.trace(
"Resolving operation a+b with {0, number, #.##########}+{1, number, #.##########} = {2, number, #.##########}",
a, b, r1);
java.math.BigDecimal r4 = toBigDecimal(catalog.solve("pow", mc, a, BD2), 0);
logger.trace(
"Resolving operation a^2 with {0, number, #.##########}^{1, number, #.##########} = {2, number, #.##########}",
a, BD2, r4);
java.math.BigDecimal r5 = a.multiply(b, mc);
logger.trace(
"Resolving operation a*b with {0, number, #.##########}*{1, number, #.##########} = {2, number, #.##########}",
a, b, r5);
java.math.BigDecimal r3 = r4.subtract(r5, mc);
logger.trace(
"Resolving operation (a^2)-(a*b) with {0, number, #.##########}-{1, number, #.##########} = {2, number, #.##########}",
r4, r5, r3);
java.math.BigDecimal r6 = toBigDecimal(catalog.solve("pow", mc, b, BD2), 0);
logger.trace(
"Resolving operation b^2 with {0, number, #.##########}^{1, number, #.##########} = {2, number, #.##########}",
b, BD2, r6);
java.math.BigDecimal r2 = r3.add(r6, mc);
logger.trace(
"Resolving operation (a^2)-(a*b)+(b^2) with {0, number, #.##########}+{1, number, #.##########} = {2, number, #.##########}",
r3, r6, r2);
java.math.BigDecimal r0 = r1.multiply(r2, mc);
logger.trace(
"Resolving operation (a+b)*((a^2)-(a*b)+(b^2)) with {0, number, #.##########}*{1, number, #.##########} = {2, number, #.##########}",
r1, r2, r0);
/*
* Result
*/
logger.debug("Returning {0, number, #.#######} as the result!", r0);
return r0;
}
}
- If you configure your log subsystem to DEBUG level, you will see extra lines in the output.
[DEBUG] c.g.e.e.t.AlgebraImpl.cubesSum Resolving expression (a ^ 3) + (b ^ 3) with ( 7,3 ^ 3) + ( 3,7 ^ 3)
[DEBUG] c.g.e.e.t.AlgebraImpl.cubesSum Returning 439,67 as the result!
If you need a deeper understanding of how the formula is calculated, set the log level to TRACE.
[DEBUG] c.g.e.e.t.AlgebraImpl.cubesSum Resolving expression (a ^ 3) + (b ^ 3) with ( 7,3 ^ 3) + ( 3,7 ^ 3)
[TRACE] c.g.e.e.t.AlgebraImpl.cubesSum Resolving operation a^3 with 7,3^ 3 = 389,017
[TRACE] c.g.e.e.t.AlgebraImpl.cubesSum Resolving operation b^3 with 3,7^ 3 = 50,653
[TRACE] c.g.e.e.t.AlgebraImpl.cubesSum Resolving operation (a^3)+(b^3) with 389,017+ 50,653 = 439,67
[TRACE] c.g.e.e.t.AlgebraImpl.cubesSum Rounding the result 439,67
[DEBUG] c.g.e.e.t.AlgebraImpl.cubesSum Returning 439,67 as the result!