示例#1
0
    def test_integral(self):
        for order in range(2, 8, 2):
            for dt in numpy.arange(0.1, 2.0, 0.1):
                # Create a spline with three segments
                aspl = bsplines.BSpline(order)
                kr = aspl.numKnotsRequired(4)
                kc = aspl.numCoefficientsRequired(4)
                # Choose a uniform knot sequence.
                knots = numpy.linspace(0.0, (kr - 1) * dt, kr)
                cp = numpy.random.random(kc)
                cpa = numpy.array([cp])

                aspl = bsplines.BSpline(order)
                aspl.setKnotVectorAndCoefficients(knots, cpa)
                fspl = (knots, cp, order - 1)

                for a in numpy.arange(aspl.t_min(),
                                      aspl.t_max() - 1e-15, 0.4 * dt):
                    for i in numpy.arange(aspl.t_min(),
                                          aspl.t_max() - 1e-15, 0.4 * dt):
                        print("Eval at %f\n" % (i))
                        f = fp.splint(a, float(i), fspl)
                        b = aspl.evalI(a, i)
                        self.assertAlmostEqual(
                            b,
                            f,
                            msg=
                            "order %d spline integral evaluated on [%f,%f] (%f != %f) was not right"
                            % (order, a, i, float(b), f))
示例#2
0
    def test_integral_non_uniform(self):
        for order in range(2, 8, 2):
            # Create a spline with three segments
            aspl = aslam_splines.OptEuclideanBSpline(order, 1)
            kr = aspl.numKnotsRequired(4)
            kc = aspl.numCoefficientsRequired(4)
            # Choose a non-uniform knot sequence.
            knots = numpy.linspace(0.0, (kr - 1), kr)
            knots = knots * knots
            cp = numpy.random.random(kc)
            cpa = numpy.array([cp])

            aspl = aslam_splines.OptEuclideanBSpline(order, 1)
            aspl.initWithKnotsAndControlVertices(knots, cpa)
            fspl = (knots, cp, order - 1)

            for a in numpy.arange(aspl.getMinTime(),
                                  aspl.getMaxTime() - 1e-15, 0.4):
                for i in numpy.arange(aspl.getMinTime(),
                                      aspl.getMaxTime() - 1e-15, 0.4):
                    #print "Eval at %f\n" % (i)
                    f = fp.splint(a, float(i), fspl)
                    b = aspl.evalI(a, i)
                    self.assertEqual(b.shape, (1, ))
                    self.assertAlmostEqual(
                        b[0],
                        f,
                        msg=
                        "order %d spline integral evaluated on [%f,%f] (%f != %f) was not right"
                        % (order, a, i, float(b), f))
示例#3
0
    def test_integral_non_uniform_repeated(self):
        for order in range(2,8,2):
            # Create a spline with three segments
            aspl = bsplines.BSpline(order)
            kr = aspl.numKnotsRequired(4)
            kc = aspl.numCoefficientsRequired(4);
            # Choose a non-uniform knot sequence.
            knots = numpy.linspace(0.0, (kr - 1), kr)
            knots = knots*knots
            for i in range(0,len(knots)):
                if i & 1 > 0:
                    knots[i] = knots[i-1]
            
            cp = numpy.random.random(kc);
            cpa = numpy.array([cp])

            aspl = bsplines.BSpline(order);
            aspl.setKnotVectorAndCoefficients(knots,cpa);
            fspl = (knots,cp,order-1)
            
            for a in numpy.arange(aspl.t_min(),aspl.t_max()-1e-15,0.4):
                for i in numpy.arange(aspl.t_min(), aspl.t_max()-1e-15, 0.4):
                    print "Eval at %f\n" % (i)
                    f = fp.splint(a,float(i),fspl)
                    b = aspl.evalI(a,i)
                    self.assertAlmostEqual(b, f, msg="order %d spline integral evaluated on [%f,%f] (%f != %f) was not right" % (order, a,i,float(b),f))
示例#4
0
    def check_2(self,
                f=f1,
                per=0,
                s=0,
                a=0,
                b=2 * pi,
                N=20,
                xb=None,
                xe=None,
                ia=0,
                ib=2 * pi,
                dx=0.2 * pi):
        if xb is None:
            xb = a
        if xe is None:
            xe = b
        x = a + (b - a) * arange(N + 1, dtype=float) / float(N)  # nodes
        v = f(x)

        def err_est(k, d):
            # Assume f has all derivatives < 1
            h = 1.0 / float(N)
            tol = 5 * h**(.75 * (k - d))
            if s > 0:
                tol += 1e5 * s
            return tol

        nk = []
        for k in range(1, 6):
            tck = splrep(x, v, s=s, per=per, k=k, xe=xe)
            nk.append([splint(ia, ib, tck), spalde(dx, tck)])
        put("\nf = %s  s=S_k(x;t,c)  x in [%s, %s] > [%s, %s]" %
            (f(None), repr(round(xb, 3)), repr(round(xe, 3)), repr(round(
                a, 3)), repr(round(b, 3))))
        put(" per=%d s=%s N=%d [a, b] = [%s, %s]  dx=%s" %
            (per, repr(s), N, repr(round(ia, 3)), repr(round(
                ib, 3)), repr(round(dx, 3))))
        put(" k :  int(s,[a,b]) Int.Error   Rel. error of s^(d)(dx) d = 0, .., k"
            )
        k = 1
        for r in nk:
            if r[0] < 0:
                sr = '-'
            else:
                sr = ' '
            put(" %d   %s%.8f   %.1e " %
                (k, sr, abs(r[0]), abs(r[0] - (f(ib, -1) - f(ia, -1)))))
            d = 0
            for dr in r[1]:
                err = abs(1 - dr / f(dx, d))
                tol = err_est(k, d)
                assert_(err < tol, (k, d))
                put(" %.1e %.1e" % (err, tol))
                d = d + 1
            put("\n")
            k = k + 1
    def test_splantider_vs_splint(self):
        # Check antiderivative vs. FITPACK
        spl2 = splantider(self.spl)

        # no extrapolation, splint assumes function is zero outside
        # range
        xx = np.linspace(0, 1, 20)

        for x1 in xx:
            for x2 in xx:
                y1 = splint(x1, x2, self.spl)
                y2 = splev(x2, spl2) - splev(x1, spl2)
                assert_allclose(y1, y2)
示例#6
0
    def test_splantider_vs_splint(self):
        # Check antiderivative vs. FITPACK
        spl2 = splantider(self.spl)

        # no extrapolation, splint assumes function is zero outside
        # range
        xx = np.linspace(0, 1, 20)

        for x1 in xx:
            for x2 in xx:
                y1 = splint(x1, x2, self.spl)
                y2 = splev(x2, spl2) - splev(x1, spl2)
                assert_allclose(y1, y2)
示例#7
0
    def check_2(self, f=f1, per=0, s=0, a=0, b=2 * pi, N=20, xb=None, xe=None, ia=0, ib=2 * pi, dx=0.2 * pi):
        if xb is None:
            xb = a
        if xe is None:
            xe = b
        x = a + (b - a) * arange(N + 1, dtype=float) / float(N)  # nodes
        v = f(x)

        def err_est(k, d):
            # Assume f has all derivatives < 1
            h = 1.0 / float(N)
            tol = 5 * h ** (0.75 * (k - d))
            if s > 0:
                tol += 1e5 * s
            return tol

        nk = []
        for k in range(1, 6):
            tck = splrep(x, v, s=s, per=per, k=k, xe=xe)
            nk.append([splint(ia, ib, tck), spalde(dx, tck)])
        put(
            "\nf = %s  s=S_k(x;t,c)  x in [%s, %s] > [%s, %s]"
            % (f(None), repr(round(xb, 3)), repr(round(xe, 3)), repr(round(a, 3)), repr(round(b, 3)))
        )
        put(
            " per=%d s=%s N=%d [a, b] = [%s, %s]  dx=%s"
            % (per, repr(s), N, repr(round(ia, 3)), repr(round(ib, 3)), repr(round(dx, 3)))
        )
        put(" k :  int(s,[a,b]) Int.Error   Rel. error of s^(d)(dx) d = 0, .., k")
        k = 1
        for r in nk:
            if r[0] < 0:
                sr = "-"
            else:
                sr = " "
            put(" %d   %s%.8f   %.1e " % (k, sr, abs(r[0]), abs(r[0] - (f(ib, -1) - f(ia, -1)))))
            d = 0
            for dr in r[1]:
                err = abs(1 - dr / f(dx, d))
                tol = err_est(k, d)
                assert_(err < tol, (k, d))
                put(" %.1e %.1e" % (err, tol))
                d = d + 1
            put("\n")
            k = k + 1
示例#8
0
    def test_new_uniform_cubicIntegral(self):
        # create the knot and control point vectors
        cardinalknots = numpy.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
        cp    = numpy.array([0.0, 10.0, 2.0, 30.0, 4.0, 50.0])
        cpa    = numpy.array([cp])
        
        for dt in numpy.arange(0.1,2.0,0.1): 
            knots = cardinalknots * dt;

        
            aspl = asrl_splines.UniformCubicBSpline(cpa,dt,knots[3]);
            fspl = (knots,cp,3)
        
            for a in numpy.arange(2.0*dt,3.9*dt,0.1*dt):
                for i in numpy.arange(2.1*dt,4.0*dt,0.1*dt):
                    print "Eval at %f\n" % (i)
                    f = fp.splint(a,float(i),fspl)
                    b = aspl.evalI(a,i)
                    assert abs(b - f) < 1e-10, "spline integral evaluated on [%f,%f] (%f != %f) was not right" % (a,i,float(b),f)
示例#9
0
    def test_integral(self):
        for order in range(2,8,2):
            for dt in numpy.arange(0.1,2.0,0.1): 
                # Create a spline with three segments
                aspl = aslam_splines.OptEuclideanBSpline(order, 1)
                kr = aspl.numKnotsRequired(4)
                kc = aspl.numCoefficientsRequired(4);
                # Choose a uniform knot sequence.
                knots = numpy.linspace(0.0, (kr - 1)*dt, kr)
                cp = numpy.random.random(kc);
                cpa = numpy.array([cp])

                aspl.initWithKnotsAndControlVertices(knots, cpa);
                fspl = (knots,cp,order-1)
                for a in numpy.arange(aspl.getMinTime(),aspl.getMaxTime()-1e-15,0.4*dt):
                    for i in numpy.arange(aspl.getMinTime(), aspl.getMaxTime()-1e-15, 0.4*dt):
                        #print "Eval at %f\n" % (i)
                        f = fp.splint(a,float(i),fspl)
                        b = aspl.evalI(a,i)
                        self.assertAlmostEqual(b, f, msg="order %d spline integral evaluated on [%f,%f] (%f != %f) was not right" % (order, a,i,float(b),f))