title: Convert a Piecewise Polynomial Function into a Single Expression/Model
body: <h1>Briefing</h1>
  
  <p>Suppose we had a continuous piecewise polynomial, how can we express it with a single equation or formula, through any programming language (preferably Java)?</p>
  
  <p>In pure mathematics, I've found that it's possible to express the function in terms of an integral or a Heaviside step function. But the answer I couldn't find is <strong>how to do the representation in programming</strong>, if possible.</p>
  
  <h1>More...</h1>
  
  <p>It's possible to pass x to, say, a <a href="http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.html#value(org.apache.commons.math3.analysis.differentiation.DerivativeStructure)" rel="nofollow noreferrer">PolynomialSplineFunction</a> and get its y value.
  <img src="https://i.stack.imgur.com/P4sbl.png" alt="source: Wikipedia"></p>
  
  <p>But what the function really does here is binary search an array of polynomials stored in the object, substitute in the right polynomial and return the value.</p>
  
  <pre><code>public double value(double v) {
          if (v &lt; knots[0] || v &gt; knots[n]) {
              throw new OutOfRangeException(v, knots[0], knots[n]);
          }
          int i = Arrays.binarySearch(knots, v);
          if (i &lt; 0) {
              i = -i - 2;
          }
          // This will handle the case where v is the last knot value
          // There are only n-1 polynomials, so if v is the last knot
          // then we will use the last polynomial to calculate the value.
          if ( i &gt;= polynomials.length ) {
              i--;
          }
      return polynomials[i].value(v - knots[i]);
  }
  </code></pre>
  
  <p>What I'm looking for is an object that <strong>would not</strong> store arrays of values/polynomials and match to them later, <strong>but</strong> rather calculate such value in pretty much the same way of a regular mathematical formula (e.g <strong>ax³ + bx² + cx + d</strong>). That would be in code like this:</p>
  
  <pre><code>public double value(double x) {
              // the function will do more complex computation here (e.g. integration)
              return a*Math.pow(x, 3) + b*Math.pow(x, 2) + c*x + d;
  }
  </code></pre>