Пример #1
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
Пример #2
0
 def test_new_uniform_cubic1(self):
     # create the knot and control point vectors
     knots = 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])
     
     aspl = asrl_splines.UniformCubicBSpline(cpa,1.0,knots[3]);
     fspl = (knots,cp,3)
     
     for i in numpy.arange(2.0,4.0,0.1):
         print "Eval at %f\n" % (i)
         f = fp.spalde(float(i),fspl)
         for j in range(0,3):
             a = aspl.evalD(i,j)
             assert abs(a - f[j]) < 1e-10, "spline (D%d) evaluated at %f (%f != %f) was not right" % (j,float(i),a,f[j])
Пример #3
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
Пример #4
0
    def test_random(self):
        numpy.random.seed(3)

        for order in range(2, 10):
            aspl = aslam_splines.OptEuclideanBSpline(order, 1)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3)
            knots = numpy.random.random([kr]) * 10
            knots.sort()
            cp = numpy.random.random([kc])
            cpa = numpy.array([cp])

            aspl.initWithKnotsAndControlVertices(knots, cpa)
            fspl = (knots, cp, order - 1)
            for i in numpy.linspace(aspl.getMinTime(), aspl.getMaxTime(), 10):
                f = fp.spalde(float(i), fspl)
                a = aspl.eval(i)
                for j in range(0, f.shape[0]):
                    a = aspl.evalD(i, j)
                    self.assertAlmostEqual(a, f[j])
Пример #5
0
    def test_random(self):
        numpy.random.seed(3)
        
        for order in range(2,10):
            aspl = bsplines.EuclideanBSpline(order, 1)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3);
            knots = numpy.random.random([kr]) * 10
            knots.sort()
            cp    = numpy.random.random([kc])
            cpa    = numpy.array([cp])
            
            aspl.initWithKnotsAndControlVertices(knots, cpa)
            fspl = (knots,cp,order-1)
            for i in numpy.linspace(aspl.getMinTime(),aspl.getMaxTime(),10):
                f = fp.spalde(float(i),fspl)
                a = aspl.eval(i)
                for j in range(0,f.shape[0]):
                    a = aspl.evalD(i,j)
                    self.assertAlmostEqual(a, f[j])
Пример #6
0
    def test_random(self):
        numpy.random.seed(3)

        for order in range(2, 10):
            aspl = bsplines.BSpline(order)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3)
            knots = numpy.random.random([kr]) * 10
            knots.sort()
            cp = numpy.random.random([kc])
            cpa = numpy.array([cp])

            aspl.setKnotVectorAndCoefficients(knots, cpa)
            fspl = (knots, cp, order - 1)
            for i in numpy.linspace(aspl.t_min(), aspl.t_max(), 10):
                f = fp.spalde(float(i), fspl)
                a = aspl.eval(i)
                for j in range(0, f.shape[0]):
                    a = aspl.evalD(i, j)
                    self.assertAlmostEqual(a, f[j])
Пример #7
0
    def test_uniform(self):
        numpy.random.seed(1)
        for order in range(2,10):
            aspl = aslam_splines.OptEuclideanBSpline(order, 1)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3);
            # Choose a uniform knot sequence.
            knots = numpy.linspace(0.0,kr*1.0, kr)

            cp    = numpy.random.random([kc])
            cpa    = numpy.array([cp])
            
            aspl.initWithKnotsAndControlVertices(knots, cpa)
            fspl = (knots,cp,order-1)
            for i in numpy.linspace(aspl.getMinTime(),aspl.getMaxTime()-1e-15,10):
                f = fp.spalde(float(i),fspl)
                a = aspl.eval(i)
                for j in range(0,f.shape[0]):
                    a = aspl.evalD(i,j)
                    self.assertAlmostEqual(a, f[j])
Пример #8
0
    def test_random(self):
        numpy.random.seed(3)
        
        for order in range(2,10):
            aspl = bsplines.BSpline(order)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3);
            knots = numpy.random.random([kr]) * 10
            knots.sort()
            cp    = numpy.random.random([kc])
            cpa    = numpy.array([cp])
            
            aspl.setKnotVectorAndCoefficients(knots, cpa)
            fspl = (knots,cp,order-1)
            for i in numpy.linspace(aspl.t_min(),aspl.t_max(),10):
                f = fp.spalde(float(i),fspl)
                a = aspl.eval(i)
                for j in range(0,f.shape[0]):
                    a = aspl.evalD(i,j)
                    self.assertAlmostEqual(a, f[j])
Пример #9
0
    def test_uniform(self):
        numpy.random.seed(1)
        for order in range(2, 10):
            aspl = bsplines.EuclideanBSpline(order, 1)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3)
            # Choose a uniform knot sequence.
            knots = numpy.linspace(0.0, kr * 1.0, kr)

            cp = numpy.random.random([kc])
            cpa = numpy.array([cp])

            aspl.initWithKnotsAndControlVertices(knots, cpa)
            fspl = (knots, cp, order - 1)
            for i in numpy.linspace(aspl.getMinTime(),
                                    aspl.getMaxTime() - 1e-15, 10):
                f = fp.spalde(float(i), fspl)
                a = aspl.eval(i)
                for j in range(0, f.shape[0]):
                    a = aspl.evalD(i, j)
                    self.assertAlmostEqual(a, f[j])
Пример #10
0
    def test_repeated(self):
        numpy.random.seed(2)
        for order in range(2, 10):
            aspl = bsplines.BSpline(order)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3)
            # Make a knot sequence that is all zeros at one end and all ones at the other.
            knots = numpy.zeros(kr)
            for i in range(0, knots.size):
                if i >= knots.size * 0.5:
                    knots[i] = 1.0

            cp = numpy.random.random([kc])
            cpa = numpy.array([cp])

            aspl.setKnotVectorAndCoefficients(knots, cpa)
            fspl = (knots, cp, order - 1)
            for i in numpy.linspace(aspl.t_min(), aspl.t_max() - 1e-15, 10):
                f = fp.spalde(float(i), fspl)
                a = aspl.eval(i)
                for j in range(0, f.shape[0]):
                    a = aspl.evalD(i, j)
                    self.assertAlmostEqual(a, f[j])
Пример #11
0
    def test_repeated(self):
        numpy.random.seed(2)
        for order in range(2,10):
            aspl = bsplines.BSpline(order)

            kr = aspl.numKnotsRequired(3)
            kc = aspl.numCoefficientsRequired(3);
            # Make a knot sequence that is all zeros at one end and all ones at the other.
            knots = numpy.zeros(kr)
            for i in range(0,knots.size):
                if i >= knots.size * 0.5:
                    knots[i] = 1.0

            cp    = numpy.random.random([kc])
            cpa    = numpy.array([cp])
            
            aspl.setKnotVectorAndCoefficients(knots, cpa)
            fspl = (knots,cp,order-1)
            for i in numpy.linspace(aspl.t_min(),aspl.t_max()-1e-15,10):
                f = fp.spalde(float(i),fspl)
                a = aspl.eval(i)
                for j in range(0,f.shape[0]):
                    a = aspl.evalD(i,j)
                    self.assertAlmostEqual(a, f[j])