Advertisement
ttaaa

Reimann Sum Counter

Mar 9th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. import exception.NotAllowedScopeException;
  2. import exception.NotImplementedMethodException;
  3. import exception.NotImplementedSolutionException;
  4. import exception.UnknownReimannSumRuleException;
  5.  
  6. public class ReimannSum {
  7.     public double getReimannSum(
  8.             Function function,
  9.             Bounds bounds,
  10.             double accuracy,
  11.             ReimannSumRule rule,
  12.             TypeOfSolution solutionType
  13.     ) throws
  14.             NotImplementedMethodException,
  15.             UnknownReimannSumRuleException,
  16.             NotAllowedScopeException {
  17.         checkAllowedScope(function, bounds);
  18.  
  19.         switch (solutionType) {
  20.             case SOLUTION_BY_FORMULAS:
  21.                 return getSumByFormulasSolution(function, bounds, accuracy, rule);
  22.             case SOLUTION_BY_RUNGE:
  23.                 return getSumByRungeSolution(function, bounds, accuracy, rule);
  24.             default:
  25.                 throw new NotImplementedSolutionException();
  26.         }
  27.     }
  28.  
  29.     public double getReimannSum(
  30.             Function function,
  31.             Bounds bounds,
  32.             double accuracy
  33.     ) throws
  34.             NotImplementedMethodException,
  35.             UnknownReimannSumRuleException,
  36.             NotAllowedScopeException {
  37.         return getReimannSum(
  38.                 function,
  39.                 bounds,
  40.                 accuracy,
  41.                 ReimannSumRule.TRAPEZOIDAL_RULE,
  42.                 TypeOfSolution.SOLUTION_BY_FORMULAS
  43.         );
  44.     }
  45.  
  46.     private void checkAllowedScope(Function function, Bounds bounds)
  47.             throws NotAllowedScopeException {
  48.         for (Interval notAllowedScope : function.getAllowedScope()) {
  49.             if (notAllowedScope.isIntersect(bounds)) {
  50.                 throw new NotAllowedScopeException();
  51.             }
  52.         }
  53.     }
  54.  
  55.     private double getSumByFormulasSolution(
  56.             Function function,
  57.             Bounds bounds,
  58.             double accuracy,
  59.             ReimannSumRule rule
  60.     ) throws NotImplementedMethodException, UnknownReimannSumRuleException {
  61.         int n = getCountOfSections(function, bounds, accuracy, rule);
  62.  
  63.         return getSumByRuleByN(function, bounds, rule, n);
  64.     }
  65.  
  66.     private double getSumByRungeSolution(
  67.             Function function,
  68.             Bounds bounds,
  69.             double accuracy,
  70.             ReimannSumRule rule
  71.     ) throws NotImplementedSolutionException {
  72.         int n = 5;
  73.         double curValue = getSumByRuleByN(function, bounds, rule, n);
  74.         double prevValue;
  75.  
  76.         do {
  77.             n <<= 1;
  78.             prevValue = curValue;
  79.             curValue = getSumByRuleByN(function, bounds, rule, n);
  80.  
  81.         } while (!(Math.abs(curValue - prevValue) < accuracy));
  82.  
  83.         return curValue;
  84.     }
  85.  
  86.     private double getSumByRuleByN(Function function, Bounds bounds, ReimannSumRule rule, int n)
  87.             throws NotImplementedSolutionException {
  88.         double sum = 0d;
  89.         double step = bounds.getLength() / n;
  90.  
  91.         for (Bounds cur: bounds.separate(n)) {
  92.             switch (rule) {
  93.                 case LEFT_RULE:
  94.                     sum += function.getValue(cur.getLeftBound()) * step;
  95.                     break;
  96.                 case RIGHT_RULE:
  97.                     sum += function.getValue(cur.getRightBound()) * step;
  98.                     break;
  99.                 case MIDPOINT_RULE:
  100.                     sum += function.getValue(
  101.                             (cur.getLeftBound() + cur.getRightBound()) / 2
  102.                     ) * step;
  103.                     break;
  104.                 case TRAPEZOIDAL_RULE:
  105.                     sum += (
  106.                             function.getValue(cur.getLeftBound()) +
  107.                             function.getValue(cur.getRightBound())
  108.                     ) * step / 2;
  109.                     break;
  110.                 case SIMPSONS_RULE:
  111.                     //TODO: solve
  112.                 default:
  113.                     throw new NotImplementedSolutionException();
  114.             }
  115.         }
  116.  
  117.         return sum;
  118.     }
  119.  
  120.     private int getCountOfSections(
  121.             Function function,
  122.             Bounds bounds,
  123.             double accuracy,
  124.             ReimannSumRule rule
  125.     ) throws UnknownReimannSumRuleException, NotImplementedMethodException {
  126.         double res;
  127.  
  128.         switch (rule) {
  129.             case LEFT_RULE:
  130.             case RIGHT_RULE:
  131.             case MIDPOINT_RULE:
  132.                 res = Math.sqrt(
  133.                         Math.pow(bounds.getLength(), 3) *
  134.                         function.get2Derivative().getMaxValue(bounds) / 24 / accuracy
  135.                 );
  136.                 return (int) (res + 1.0d);
  137.             case SIMPSONS_RULE:
  138.                 res = Math.sqrt(
  139.                         Math.sqrt(
  140.                                 Math.pow(bounds.getLength(), 5) *
  141.                                 function.get4Derivative().getMaxValue(bounds) / 180 / accuracy
  142.                         )
  143.                 );
  144.                 return (int) (res + 1.0d);
  145.             case TRAPEZOIDAL_RULE:
  146.                 res = Math.sqrt(
  147.                         Math.pow(bounds.getLength(), 3) *
  148.                         function.get2Derivative().getMaxValue(bounds) / 12 / accuracy
  149.                 );
  150.                 return (int) (res + 1.0d);
  151.             default:
  152.                 throw new UnknownReimannSumRuleException();
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement