Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-387

Duplicate code

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.1
    • 2.2
    • None
    • None

    Description

      In package optimization:

      SimpleRealPointChecker.java
      public boolean converged(final int iteration, final RealPointValuePair previous, final RealPointValuePair current) {
          final double[] p        = previous.getPoint();
          final double[] c        = current.getPoint();
          for (int i = 0; i < p.length; ++i) {
              final double difference = Math.abs(p[i] - c[i]);
              final double size       = Math.max(Math.abs(p[i]), Math.abs(c[i]));
              if ((difference > (size * relativeThreshold)) && (difference > absoluteThreshold)) {
                  return false;
              }
          }
          return true;
      }
      
      SimpleVectorialPointChecker.java
      public boolean converged(final int iteration, final VectorialPointValuePair previous, final VectorialPointValuePair current) {
          final double[] p = previous.getPointRef();
          final double[] c = current.getPointRef();
          for (int i = 0; i < p.length; ++i) {
              final double pi         = p[i];
              final double ci         = c[i];
              final double difference = Math.abs(pi - ci);
              final double size       = Math.max(Math.abs(pi), Math.abs(ci));
              if ((difference > (size * relativeThreshold)) &&
                  (difference > absoluteThreshold)) {
                  return false;
              }
          }
          return true;
      }
      

      Do they do the same thing or am I missing something?

      Also in

      SimpleScalarValueChecker.java
      public boolean converged(final int iteration, final RealPointValuePair previous, final RealPointValuePair current) {
          final double p          = previous.getValue();
          final double c          = current.getValue();
          final double difference = Math.abs(p - c);
          final double size       = Math.max(Math.abs(p), Math.abs(c));
          return (difference <= (size * relativeThreshold)) || (difference <= absoluteThreshold);
      }
      

      it seems overkill that one must create two RealPointValuePair objects when one just wants to compare two double. Shouldn't this class contain a method like

      public boolean converged(int iteration, double previous, double current) {
          final double difference = Math.abs(previous - current);
          final double size       = Math.max(Math.abs(previous), Math.abs(current));
          return (difference <= (size * relativeThreshold)) || (difference <= absoluteThreshold);
      

      ?

      Also none of these methods seem to need an iteration parameter.

      Attachments

        1. RealPointValuePair.java.diff
          1 kB
          Gilles Sadowski

        Activity

          People

            Unassigned Unassigned
            erans Gilles Sadowski
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: