### Navigation

- index
- modules |
- next |
- previous |
- Symbolic Calculus »

# Real Interpolation using GSL

Real Interpolation using GSL

class `sage.gsl.interpolation.``Spline`

  Bases: `object`

  Create a spline interpolation object.

  Given a list \(v\) of pairs, `s = spline(v)` is an object `s` such that \(s(x)\) is the value of the spline interpolation through the points in \(v\) at the point \(x\).

  The values in \(v\) do not have to be sorted. Moreover, one can append values to \(v\), delete values from \(v\), or change values in \(v\), and the spline is recomputed.

  EXAMPLES:

      sage: S = spline([(0, 1), (1, 2), (4, 5), (5, 3)]); S
      [(0, 1), (1, 2), (4, 5), (5, 3)]
      sage: S(1.5)
      2.76136363636...

  Changing the points of the spline causes the spline to be recomputed:

      sage: S[0] = (0, 2); S
      [(0, 2), (1, 2), (4, 5), (5, 3)]
      sage: S(1.5)
      2.507575757575...

  We may delete interpolation points of the spline:

      sage: del S[2]; S
      [(0, 2), (1, 2), (5, 3)]
      sage: S(1.5)
      2.04296875

  We may append to the list of interpolation points:

      sage: S.append((4, 5)); S
      [(0, 2), (1, 2), (5, 3), (4, 5)]
      sage: S(1.5)
      2.507575757575...

  If we set the \(n\)-th interpolation point, where \(n\) is larger than `len(S)`, then points \((0, 0)\) will be inserted between the interpolation points and the point to be added:

      sage: S[6] = (6, 3); S
      [(0, 2), (1, 2), (5, 3), (4, 5), (0, 0), (0, 0), (6, 3)]

  This example is in the GSL documentation:

      sage: v = [(i + sin(i)/2, i+cos(i^2)) for i in range(10)]
      sage: s = spline(v)
      sage: show(point(v) + plot(s,0,9, hue=.8))

  We compute the area underneath the spline:

      sage: s.definite_integral(0, 9)
      41.196516041067...

  The definite integral is additive:

      sage: s.definite_integral(0, 4) + s.definite_integral(4, 9)
      41.196516041067...

  Switching the order of the bounds changes the sign of the integral:

      sage: s.definite_integral(9, 0)
      -41.196516041067...

  We compute the first and second-order derivatives at a few points:

      sage: s.derivative(5)
      -0.16230085261803...
      sage: s.derivative(6)
      0.20997986285714...
      sage: s.derivative(5, order=2)
      -3.08747074561380...
      sage: s.derivative(6, order=2)
      2.61876848274853...

  Only the first two derivatives are supported:

      sage: s.derivative(4, order=3)
      Traceback (most recent call last):
      ...
      ValueError: Order of derivative must be 1 or 2.

  `append`(xy)

    EXAMPLES:

        sage: S = spline([(1,1), (2,3), (4,5)]); S.append((5,7)); S
        [(1, 1), (2, 3), (4, 5), (5, 7)]

    The spline is recomputed when points are appended (trac ticket #13519):

        sage: S = spline([(1,1), (2,3), (4,5)]); S
        [(1, 1), (2, 3), (4, 5)]
        sage: S(3)
        4.25
        sage: S.append((5, 5)); S
        [(1, 1), (2, 3), (4, 5), (5, 5)]
        sage: S(3)
        4.375

  `definite_integral`(a, b)

    Value of the definite integral between \(a\) and \(b\).

    INPUT:

    - `a` – Lower bound for the integral.
    - `b` – Upper bound for the integral.

    EXAMPLES:

    We draw a cubic spline through three points and compute the area underneath the curve:

        sage: s = spline([(0, 0), (1, 3), (2, 0)])
        sage: s.definite_integral(0, 2)
        3.75
        sage: s.definite_integral(0, 1)
        1.875
        sage: s.definite_integral(0, 1) + s.definite_integral(1, 2)
        3.75
        sage: s.definite_integral(2, 0)
        -3.75

  `derivative`(x, order=1)

    Value of the first or second derivative of the spline at \(x\).

    INPUT:

    - `x` – value at which to evaluate the derivative.
    - `order` (default: 1) – order of the derivative. Must be 1 or 2.

    EXAMPLES:

    We draw a cubic spline through three points and compute the derivatives:

        sage: s = spline([(0, 0), (2, 3), (4, 0)])
        sage: s.derivative(0)
        2.25
        sage: s.derivative(2)
        0.0
        sage: s.derivative(4)
        -2.25
        sage: s.derivative(1, order=2)
        -1.125
        sage: s.derivative(3, order=2)
        -1.125

  `list`()

    Underlying list of points that this spline goes through.

    EXAMPLES:

        sage: S = spline([(1,1), (2,3), (4,5)]); S.list()
        [(1, 1), (2, 3), (4, 5)]

    This is a copy of the list, not a reference (trac ticket #13530):

        sage: S = spline([(1,1), (2,3), (4,5)])
        sage: L = S.list(); L
        [(1, 1), (2, 3), (4, 5)]
        sage: L[2] = (3, 2)
        sage: L
        [(1, 1), (2, 3), (3, 2)]
        sage: S.list()
        [(1, 1), (2, 3), (4, 5)]

`sage.gsl.interpolation.``spline`

  alias of `Spline`

### Navigation

- index
- modules |
- next |
- previous |
- Symbolic Calculus »