Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DBC;
- public abstract class Function {
- public abstract double [] derivative(double []a) throws Exception;
- }
- public class Exponential extends Function {
- @Override
- public double[] derivative(double[] a) throws Exception {
- if (a.length != 2 )
- {
- throw new Exception ("This is not an exponential");
- }
- for (int i = 0; i < a.length; i++) {
- if (Double.isNaN(a[i]) || Double.isInfinite(a[i]))
- {
- throw new Exception("Cant compute the derivative!");
- }
- }
- return new double[]{Math.log(a[0]), a[0], a[1]};
- }
- }
- public class Logarithmic extends Function {
- @Override
- public double[] derivative(double[] a) throws Exception {
- for (int i = 0; i < a.length; i++) {
- if (Double.isNaN(a[i]) || Double.isInfinite(a[i]))
- {
- throw new Exception ("Can't compute derivative");
- }
- }
- return new double[]{ 1 / ( a[1] * Math.log(a[0]))};
- }
- }
- public class Polynomial extends Function {
- @Override
- public double[] derivative(double[] a) throws Exception{
- int power = 1;
- for (int i = 0; i < a.length; i++) {
- if (Double.isNaN(a[i]) || Double.isInfinite(a[i]))
- {
- throw new Exception("Cant compute derivative");
- }
- }
- double [] result = new double[a.length - 1];
- for (int i = 0; i < result.length; i++)
- {
- result[i] = power * a[i + 1];
- power++;
- }
- return result;
- }
- }
- public class FunctionTest
- {
- public static void main(String[] args)
- {
- double[] arrayPolynomial = new double[]{1, 2, 3, 4, 5};
- Polynomial polynomial = new Polynomial();
- try
- {
- double result[] = polynomial.derivative(arrayPolynomial);
- for (int i = 0; i < result.length; i++) {
- System.out.print(result[i] + " ");
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- System.out.println();
- double[] arrayExponential = new double[]{3, 5};
- Exponential exp = new Exponential();
- try{
- double resultExp[] = exp.derivative(arrayExponential);
- for (int i = 0; i < resultExp.length; i++) {
- System.out.print(resultExp[i] + " ");
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- System.out.println();
- double[]arrayLogarithmic = new double[]{2, 12};
- Logarithmic logarithmic = new Logarithmic();
- try{
- double []resultLog = logarithmic.derivative(arrayLogarithmic);
- for (int i = 0; i < resultLog.length; i++) {
- System.out.print(resultLog[i] + " ");
- }
- }
- catch(Exception e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement