Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package DBC;
  2.  
  3. public abstract class Function {
  4. public abstract double [] derivative(double []a) throws Exception;
  5. }
  6.  
  7. public class Exponential extends Function {
  8.  
  9. @Override
  10. public double[] derivative(double[] a) throws Exception {
  11. if (a.length != 2 )
  12. {
  13. throw new Exception ("This is not an exponential");
  14. }
  15. for (int i = 0; i < a.length; i++) {
  16. if (Double.isNaN(a[i]) || Double.isInfinite(a[i]))
  17. {
  18. throw new Exception("Cant compute the derivative!");
  19. }
  20. }
  21. return new double[]{Math.log(a[0]), a[0], a[1]};
  22. }
  23. }
  24.  
  25. public class Logarithmic extends Function {
  26.  
  27. @Override
  28. public double[] derivative(double[] a) throws Exception {
  29. for (int i = 0; i < a.length; i++) {
  30. if (Double.isNaN(a[i]) || Double.isInfinite(a[i]))
  31. {
  32. throw new Exception ("Can't compute derivative");
  33. }
  34. }
  35. return new double[]{ 1 / ( a[1] * Math.log(a[0]))};
  36. }
  37. }
  38. public class Polynomial extends Function {
  39. @Override
  40. public double[] derivative(double[] a) throws Exception{
  41.  
  42. int power = 1;
  43. for (int i = 0; i < a.length; i++) {
  44. if (Double.isNaN(a[i]) || Double.isInfinite(a[i]))
  45. {
  46. throw new Exception("Cant compute derivative");
  47. }
  48. }
  49. double [] result = new double[a.length - 1];
  50. for (int i = 0; i < result.length; i++)
  51. {
  52. result[i] = power * a[i + 1];
  53. power++;
  54. }
  55.  
  56. return result;
  57. }
  58.  
  59. }
  60.  
  61. public class FunctionTest
  62. {
  63. public static void main(String[] args)
  64. {
  65. double[] arrayPolynomial = new double[]{1, 2, 3, 4, 5};
  66. Polynomial polynomial = new Polynomial();
  67. try
  68. {
  69. double result[] = polynomial.derivative(arrayPolynomial);
  70. for (int i = 0; i < result.length; i++) {
  71. System.out.print(result[i] + " ");
  72. }
  73. }
  74. catch(Exception e)
  75. {
  76. e.printStackTrace();
  77. }
  78.  
  79. System.out.println();
  80.  
  81. double[] arrayExponential = new double[]{3, 5};
  82. Exponential exp = new Exponential();
  83. try{
  84. double resultExp[] = exp.derivative(arrayExponential);
  85. for (int i = 0; i < resultExp.length; i++) {
  86. System.out.print(resultExp[i] + " ");
  87. }
  88. }
  89. catch (Exception e)
  90. {
  91. e.printStackTrace();
  92. }
  93.  
  94. System.out.println();
  95.  
  96. double[]arrayLogarithmic = new double[]{2, 12};
  97. Logarithmic logarithmic = new Logarithmic();
  98. try{
  99. double []resultLog = logarithmic.derivative(arrayLogarithmic);
  100. for (int i = 0; i < resultLog.length; i++) {
  101. System.out.print(resultLog[i] + " ");
  102. }
  103. }
  104. catch(Exception e)
  105. {
  106.  
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement