This file is indexed.

/usr/lib/python3/dist-packages/bumps/bspline.py is in python3-bumps 0.7.6-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# This program is public domain
"""
BSpline calculator.

Given a set of knots, compute the cubic B-spline interpolation.
"""
from __future__ import division, print_function

__all__ = ['bspline', 'pbs']

import numpy as np
from numpy import maximum as max, minimum as min


def pbs(x, y, t, clamp=True, parametric=True):
    """
    Evaluate the parametric B-spline px(t),py(t).

    *x* and *y* are the control points, and *t* are the points
    in [0,1] at which they are evaluated.   The *x* values are
    sorted so that the spline describes a function.

    The spline goes through the control points at the ends. If *clamp*
    is True, the derivative of the spline at both ends is zero. If *clamp*
    is False, the derivative at the ends is equal to the slope connecting
    the final pair of control points.

    If *parametric* is False, then parametric points t' are chosen such
    that x(t') = *t*.

    The B-spline knots are chosen to be equally spaced within [0,1].
    """
    x = list(sorted(x))
    knot = np.hstack((0, 0, np.linspace(0, 1, len(y)), 1, 1))
    cx = np.hstack((x[0], x[0], x[0], (2 * x[0] + x[1]) / 3,
                    x[1:-1], (2 * x[-1] + x[-2]) / 3, x[-1]))
    if clamp:
        cy = np.hstack((y[0], y[0], y[0], y, y[-1]))
    else:
        cy = np.hstack((y[0], y[0], y[0],
                        y[0] + (y[1] - y[0]) / 3,
                        y[1:-1],
                        y[-1] + (y[-2] - y[-1]) / 3,
                        y[-1]))

    if parametric:
        return _bspline3(knot, cx, t), _bspline3(knot, cy, t)

    # Find parametric t values corresponding to given z values
    # First try a few newton steps
    xt = np.interp(t, x, np.linspace(0, 1, len(x)))
    with np.errstate(all='ignore'):
        for _ in range(6):
            pt, dpt = _bspline3(knot, cx, xt, nderiv=1)
            xt -= (pt - t) / dpt
        idx = np.isnan(xt) | (abs(_bspline3(knot, cx, xt) - t) > 1e-9)

    # Use bisection when newton fails
    if idx.any():
        missing = t[idx]
        # print missing
        t_lo, t_hi = 0 * missing, 1 * missing
        for _ in range(30):  # bisection with about 1e-9 tolerance
            trial = (t_lo + t_hi) / 2
            ptrial = _bspline3(knot, cx, trial)
            tidx = ptrial < missing
            t_lo[tidx] = trial[tidx]
            t_hi[~tidx] = trial[~tidx]
        xt[idx] = (t_lo + t_hi) / 2
    # print "err",np.max(abs(_bspline3(knot,cx,t)-xt))

    # Return y evaluated at the interpolation points
    return _bspline3(knot, cx, xt), _bspline3(knot, cy, xt)


def bspline(y, xt, clamp=True):
    """
    Evaluate the B-spline with control points *y* at positions *xt* in [0,1].

    The spline goes through the control points at the ends.  If *clamp*
    is True, the derivative of the spline at both ends is zero.  If *clamp*
    is False, the derivative at the ends is equal to the slope connecting
    the final pair of control points.

    B-spline knots are chosen to be equally spaced within [0,1].
    """
    knot = np.hstack((0, 0, np.linspace(0, 1, len(y)), 1, 1))
    if clamp:
        cy = np.hstack(([y[0]] * 3, y, y[-1]))
    else:
        cy = np.hstack((y[0], y[0], y[0],
                           y[0] + (y[1] - y[0]) / 3,
                           y[1:-1],
                           y[-1] + (y[-2] - y[-1]) / 3, y[-1]))
    return _bspline3(knot, cy, xt)


def _bspline3(knot, control, t, nderiv=0):
    """
    Evaluate the B-spline specified by the given *knot* sequence and
    *control* values at the parametric points *t*.  *nderiv* selects
    the function or derivative to evaluate.
    """
    knot, control, t = [np.asarray(v) for v in (knot, control, t)]

    # Deal with values outside the range
    valid = (t > knot[0]) & (t <= knot[-1])
    tv = t[valid]
    f = np.zeros(t.shape)
    f[t <= knot[0]] = control[0]
    f[t >= knot[-1]] = control[-1]

    # Find B-Spline parameters for the individual segments
    end = len(knot) - 1
    segment = knot.searchsorted(tv) - 1
    tm2 = knot[max(segment - 2, 0)]
    tm1 = knot[max(segment - 1, 0)]
    tm0 = knot[max(segment - 0, 0)]
    tp1 = knot[min(segment + 1, end)]
    tp2 = knot[min(segment + 2, end)]
    tp3 = knot[min(segment + 3, end)]

    p4 = control[min(segment + 3, end)]
    p3 = control[min(segment + 2, end)]
    p2 = control[min(segment + 1, end)]
    p1 = control[min(segment + 0, end)]

    # Compute second and third derivatives.
    if nderiv > 1:
        # Normally we require a recursion for Q, R and S to compute
        # df, d2f and d3f respectively, however Q can be computed directly
        # from intermediate values of P, S has a recursion of depth 0,
        # which leaves only the R recursion of depth 1 in the calculation
        # below.
        q4 = (p4 - p3) * 3 / (tp3 - tm0)
        q3 = (p3 - p2) * 3 / (tp2 - tm1)
        q2 = (p2 - p1) * 3 / (tp1 - tm2)
        r4 = (q4 - q3) * 2 / (tp2 - tm0)
        r3 = (q3 - q2) * 2 / (tp1 - tm1)
        if nderiv > 2:
            s4 = (r4 - r3) / (tp1 - tm0)
            d3f = np.zeros(t.shape)
            d3f[valid] = s4
        r4 = ((tv - tm0) * r4 + (tp1 - tv) * r3) / (tp1 - tm0)
        d2f = np.zeros(t.shape)
        d2f[valid] = r4

    # Compute function value and first derivative
    p4 = ((tv - tm0) * p4 + (tp3 - tv) * p3) / (tp3 - tm0)
    p3 = ((tv - tm1) * p3 + (tp2 - tv) * p2) / (tp2 - tm1)
    p2 = ((tv - tm2) * p2 + (tp1 - tv) * p1) / (tp1 - tm2)
    p4 = ((tv - tm0) * p4 + (tp2 - tv) * p3) / (tp2 - tm0)
    p3 = ((tv - tm1) * p3 + (tp1 - tv) * p2) / (tp1 - tm1)
    if nderiv >= 1:
        df = np.zeros(t.shape)
        df[valid] = (p4 - p3) * 3 / (tp1 - tm0)
    p4 = ((tv - tm0) * p4 + (tp1 - tv) * p3) / (tp1 - tm0)
    f[valid] = p4

    if nderiv == 0:
        return f
    elif nderiv == 1:
        return f, df
    elif nderiv == 2:
        return f, df, d2f
    else:
        return f, df, d2f, d3f


def bspline_control(y, clamp=True):
    return _find_control(y, clamp=clamp)


def pbs_control(x, y, clamp=True):
    return _find_control(x, clamp=clamp), _find_control(y, clamp=clamp)


def _find_control(v, clamp=True):
    raise NotImplementedError("B-spline interpolation doesn't work yet")
    from scipy.linalg import solve_banded
    n = len(v)
    udiag = np.hstack([0, 0, 0, [1 / 6] * (n - 3), 0.25, 0.3])
    ldiag = np.hstack([-0.3, 0.25, [1 / 6] * (n - 3), 0, 0, 0])
    mdiag = np.hstack([1, 0.3, 7 / 12, [2 / 3] * (n - 4), 7 / 12, -0.3, 1])
    A = np.vstack([ldiag, mdiag, udiag])
    if clamp:
        # First derivative is zero at ends
        bl, br = 0, 0
    else:
        # First derivative at ends follows line between final control points
        bl, br = (v[1] - v[0]) * n, (v[-1] - v[-2]) * n
    b = np.hstack([v[0], bl, v[1:n - 1], br, v[-1]])
    x = solve_banded((1, 1), A, b)
    return x  # x[1:-1]

# ===========================================================================
# test code

def speed_check():
    """
    Print the time to evaluate 400 points on a 7 knot spline.
    """
    import time
    x = np.linspace(0, 1, 7)
    x[1], x[-2] = x[2], x[-3]
    y = [9, 11, 2, 3, 8, 0, 2]
    t = np.linspace(0, 1, 400)
    t0 = time.time()
    for _ in range(1000):
        bspline(y, t, clamp=True)
    print("bspline (ms)", (time.time() - t0) / 1000)


def _check(expected, got, tol):
    """
    Check that value matches expected within tolerance.

    If *expected* is never zero, use relative error for tolerance.
    """
    relative = (np.isscalar(expected) and expected != 0) \
        or (not np.isscalar(expected) and all(expected != 0))
    if relative:
        norm = np.linalg.norm((expected - got) / expected)
    else:
        norm = np.linalg.norm(expected - got)
    if norm >= tol:
        msg = [
            "expected %s"%str(expected),
            "got %s"%str(got),
            "tol %s norm %s"%(tol, norm),
        ]
        raise ValueError("\n".join(msg))


def _derivs(x, y):
    """
    Compute numerical derivative for a function evaluated on a fine grid.
    """
    # difference formula
    return (y[1] - y[0]) / (x[1] - x[0]), (y[-1] - y[-2]) / (x[-1] - x[-2])
    # 5-point difference formula
    #left = (y[0]-8*y[1]+8*y[3]-y[4]) / 12 / (x[1]-x[0])
    #right = (y[-5]-8*y[-4]+8*y[-2]-y[-1]) / 12 / (x[-1]-x[-2])
    # return left,right


def test():
    h = 1e-10
    t = np.linspace(0, 1, 100)
    dt = np.array([0, h, 2 * h, 3 * h, 4 * h,
                      1 - 4 * h, 1 - 3 * h, 1 - 2 * h, 1 - h, 1])
    y = [9, 11, 2, 3, 8, 0, 2]
    n = len(y)
    xeq = np.linspace(0, 1, n)
    x = xeq + 0
    x[0], x[-1] = (x[0] + x[1]) / 2, (x[-2] + x[-1]) / 2
    dx = np.array([x[0], x[0] + h, x[0] + 2*h, x[0] + 3*h, x[0] + 4*h,
                      x[-1] - 4*h, x[-1] - 3*h, x[-1] - 2*h, x[-1] - h, x[-1]])

    # ==== Check that bspline matches pbs with equally spaced x

    yt = bspline(y, t, clamp=True)
    xtp, ytp = pbs(xeq, y, t, clamp=True, parametric=False)
    _check(t, xtp, 1e-8)
    _check(yt, ytp, 1e-8)

    xtp, ytp = pbs(xeq, y, t, clamp=True, parametric=True)
    _check(t, xtp, 1e-8)
    _check(yt, ytp, 1e-8)

    yt = bspline(y, t, clamp=False)
    xtp, ytp = pbs(xeq, y, t, clamp=False, parametric=False)
    _check(t, xtp, 1e-8)
    _check(yt, ytp, 1e-8)

    xtp, ytp = pbs(xeq, y, t, clamp=False, parametric=True)
    _check(t, xtp, 1e-8)
    _check(yt, ytp, 1e-8)

    # ==== Check bspline f at end points

    yt = bspline(y, t, clamp=True)
    _check(y[0], yt[0], 1e-12)
    _check(y[-1], yt[-1], 1e-12)

    yt = bspline(y, t, clamp=False)
    _check(y[0], yt[0], 1e-12)
    _check(y[-1], yt[-1], 1e-12)

    xt, yt = pbs(x, y, t, clamp=True, parametric=False)
    _check(x[0], xt[0], 1e-8)
    _check(x[-1], xt[-1], 1e-8)
    _check(y[0], yt[0], 1e-8)
    _check(y[-1], yt[-1], 1e-8)

    xt, yt = pbs(x, y, t, clamp=True, parametric=True)
    _check(x[0], xt[0], 1e-8)
    _check(x[-1], xt[-1], 1e-8)
    _check(y[0], yt[0], 1e-8)
    _check(y[-1], yt[-1], 1e-8)

    xt, yt = pbs(x, y, t, clamp=False, parametric=False)
    _check(x[0], xt[0], 1e-8)
    _check(x[-1], xt[-1], 1e-8)
    _check(y[0], yt[0], 1e-8)
    _check(y[-1], yt[-1], 1e-8)

    xt, yt = pbs(x, y, t, clamp=False, parametric=True)
    _check(x[0], xt[0], 1e-8)
    _check(x[-1], xt[-1], 1e-8)
    _check(y[0], yt[0], 1e-8)
    _check(y[-1], yt[-1], 1e-8)

    # ==== Check f' at end points
    yt = bspline(y, dt, clamp=True)
    left, right = _derivs(dt, yt)
    _check(0, left, 1e-8)
    _check(0, right, 1e-8)

    xt, yt = pbs(x, y, dx, clamp=True, parametric=False)
    left, right = _derivs(xt, yt)
    _check(0, left, 1e-8)
    _check(0, right, 1e-8)

    xt, yt = pbs(x, y, dt, clamp=True, parametric=True)
    left, right = _derivs(xt, yt)
    _check(0, left, 1e-8)
    _check(0, right, 1e-8)

    yt = bspline(y, dt, clamp=False)
    left, right = _derivs(dt, yt)
    _check((y[1] - y[0]) * (n - 1), left, 5e-4)
    _check((y[-1] - y[-2]) * (n - 1), right, 5e-4)

    xt, yt = pbs(x, y, dx, clamp=False, parametric=False)
    left, right = _derivs(xt, yt)
    _check((y[1] - y[0]) / (x[1] - x[0]), left, 5e-4)
    _check((y[-1] - y[-2]) / (x[-1] - x[-2]), right, 5e-4)

    xt, yt = pbs(x, y, dt, clamp=False, parametric=True)
    left, right = _derivs(xt, yt)
    _check((y[1] - y[0]) / (x[1] - x[0]), left, 5e-4)
    _check((y[-1] - y[-2]) / (x[-1] - x[-2]), right, 5e-4)

    # ==== Check interpolator
    #yc = bspline_control(y)
    # print("y",y)
    # print("p(yc)",bspline(yc,xeq))


def demo():
    from pylab import hold, linspace, subplot, plot, legend, show
    hold(True)
    #y = [9,6,1,3,8,4,2]
    #y = [9,11,13,3,-2,0,2]
    y = [9, 11, 2, 3, 8, 0]
    #y = [9,9,1,3,8,2,2]
    x = linspace(0, 1, len(y))
    t = linspace(x[0], x[-1], 400)
    subplot(211)
    plot(t, bspline(y, t, clamp=False), '-.y',
         label="unclamped bspline")  # bspline
    # bspline
    plot(t, bspline(y, t, clamp=True), '-y', label="clamped bspline")
    plot(sorted(x), y, ':oy', label="control points")
    legend()
    #left, right = _derivs(t, bspline(y, t, clamp=False))
    #print(left, (y[1] - y[0]) / (x[1] - x[0]))

    subplot(212)
    xt, yt = pbs(x, y, t, clamp=False)
    plot(xt, yt, '-.b', label="unclamped pbs")  # pbs
    xt, yt = pbs(x, y, t, clamp=True)
    plot(xt, yt, '-b', label="clamped pbs")  # pbs
    #xt,yt = pbs(x,y,t,clamp=True, parametric=True)
    # plot(xt,yt,'-g') # pbs
    plot(sorted(x), y, ':ob', label="control points")
    legend()
    show()


def demo_interp():
    # B-Spline control point inverse function is not yet implemented
    from pylab import hold, linspace, plot, show
    hold(True)
    x = linspace(0, 1, 7)
    y = [9, 11, 2, 3, 8, 0, 2]
    t = linspace(0, 1, 400)
    yc = bspline_control(y, clamp=True)
    xc = linspace(x[0], x[-1], 9)
    plot(xc, yc, ':oy', x, y, 'xg')
    #knot = np.hstack((0, np.linspace(0,1,len(y)), 1))
    #fy = _bspline3(knot,yc,t)
    fy = bspline(yc, t, clamp=True)
    plot(t, fy, '-.y')
    show()

if __name__ == "__main__":
    # test()
    demo()
    # demo_interp()
    # speed_check()