Beispiel #1
0
    def test_sBasis(self):
        S = SBasis()
        T = SBasis(2)
        U = SBasis(1, 7)
        V = SBasis.from_linear( Linear(2, 8) )
        
        self.assertEqual(V[0], Linear(2, 8))
        self.assertEqual(V.back(), Linear(2, 8))
        
        #~ self.assertTrue(S.empty())
        self.assertFalse(T.empty())
        
        T.pop_back()
        self.assertTrue(T.empty())
        
        self.assertEqual(S.size(), 0)
        self.assertEqual(U.size(), 1)
        self.assertEqual((U*V).size(), 2)
        
        T.resize(1, Linear(2, 3))
        self.assertEqual(T[0], Linear(2, 3))
        T.clear()
        self.assertTrue(T.empty())
        #TODO
        #~ T.reserve(5)
        #~ print T.size()
        self.assertEqual(V.at(0), V[0])
        self.assertEqual(V, U+1)
        self.assertNotEqual(V, U)
        self.assertTrue(T.is_zero())
        self.assertTrue(SBasis(1).is_constant())
        def f(A, B):
            return (-A)*(A+B*2.2)*(A*B-B*B/3)
        W = f(U, V)
        self.assertAlmostEqual(W(0), W.at0())
        self.assertAlmostEqual(W(1), W.at1())
        
        for i in range(11):
            t = i/10.0
            self.assertAlmostEqual(W(t), W.value_at(t))
            self.assertAlmostEqual(W(t), f(U(t), V(t)))
            
            vd_UV = (U*V).value_and_derivatives(t, 1)
            vd_U = U.value_and_derivatives(t, 1)
            vd_V = V.value_and_derivatives(t, 1)
            self.assertAlmostEqual( vd_UV[1], vd_U[1]*V(t)+U(t)*vd_V[1] )
            
            self.assertAlmostEqual( U(V)(t), U(V(t)) )
        self.assertEqual(T.degrees_of_freedom(), 0)
        self.assertEqual(U.degrees_of_freedom(), 2)
        
        self.assertEqual(T, T.to_SBasis())
        
        U2 = SBasis(U(0), U(1))
        U2.resize(10)
        self.assertNotEqual(U2, U)
        U2.truncate(U.size())
        self.assertEqual(U2, U)
        #TODO: normalize()
        sL = Linear.sin(Linear(0, 1), 3)
        cL = Linear.cos(Linear(0, 1), 3)
        sqrtU = SBasis.sqrt( U, 3 )
        rL = Linear.reciprocal(Linear(1,2), 3)
        # cy2geom.inverse seems to return nans for degrees > 1
        #~ asin = cy2geom.inverse( cy2geom.sqrt( SBasis(Linear(0, 1)), 3 ), 1)
        for i in range(11):
            t = i/10.0
            self.assertAlmostEqual(sL(t), math.sin(t))
            self.assertAlmostEqual(cL(t), math.cos(t))
            #cy2geom.sqrt is not that precise 
            self.assertAlmostEqual(sqrtU(t), math.sqrt(U(t)), places = 1)
            self.assertAlmostEqual(rL(t), 1/(1+t), places = 1 )
            #~ self.assertAlmostEqual( asin(t), math.asin(t) )
            self.assertAlmostEqual( SBasis.compose(U, V)(t), U(V)(t) )
            self.assertAlmostEqual( SBasis.divide(U, V, 3)(t), U(t)/V(t), places = 1)
            
            self.assertAlmostEqual( SBasis.derivative(SBasis.integral(W))(t), W(t))
            self.assertAlmostEqual( cy2geom.reverse(W)(t), W(1-t) )
            self.assertAlmostEqual( SBasis.multiply(U, V)(t), (U*V)(t))
            #TODO looks like bug in 2geom
            #~ print cy2geom.multiply_add(U, V, W)(t), (U*V+W)(t)
            self.assertAlmostEqual( SBasis.multiply_add(U, W, V)(t), (U*W+V)(t))
            
            self.assertTrue( SBasis.bounds_exact(U).Interval.contains(U(t)) )
            self.assertTrue( SBasis.bounds_fast(U).Interval.contains(U(t)) )
            self.assertTrue( SBasis.bounds_local(U, OptInterval(t-0.05, t+0.05)).Interval.contains(U(t)) )
        
        
        for r in SBasis.roots(W):
            self.assertAlmostEqual(W(r), 0)
        for r in SBasis.roots(W, Interval(0, 0.7)):
            self.assertAlmostEqual(W(r), 0)
            self.assertTrue(Interval(0, 0.7).contains(r))

        levels = [0, 3, 22, -21]
        for i, roots in enumerate( SBasis.multi_roots(W, levels) ):
            level = levels[i]
            for r in roots:
                self.assertAlmostEqual(W(r), level)

        self.assertEqual(SBasis.valuation(W), 0)
        #TODO: why is this still 0?
        #~ print cy2geom.valuation(cy2geom.shift(W, 6))
        self.assertEqual( U[0], SBasis.shift(U, 2)[2] )
        
        for I in SBasis.level_set(W, 2, tol = 1e-7):
            self.assertAlmostEqual( W(I.mid()), 2 )
        for I in SBasis.level_set(W, Interval(0, 1), tol = 1e-7, vtol = 1e-7):
            self.assertTrue( 0 <= W(I.begin()) <= 1 )
            self.assertTrue( 0 <= W(I.mid()) <= 1 )
            self.assertTrue( 0 <= W(I.end()) <= 1 )
Beispiel #2
0
 def test_linear(self):
     L = Linear(0, 1)
     M = Linear(2)
     N = Linear()
     self.assertEqual( (L+M), L+2 )
     self.assertEqual( (L-M), L-2 )
     self.assertAlmostEqual(L(0.5), lerp(.5, 0, 1))
     #~ self.assertTrue(N.is_zero())
     self.assertTrue(M.is_constant())
     self.assertTrue(L.is_finite())
     self.assertAlmostEqual(L(0), L.at0())
     self.assertAlmostEqual(L(1), L.at1())
     self.assertAlmostEqual(L.value_at(0.3), L(0.3))
     self.assertTrue( isinstance(M.to_SBasis(), SBasis ))
     
     self.assertAlmostEqual(L.tri(), L(1) - L(0))
     self.assertAlmostEqual(L.hat(), (L(1) + L(0))/2)
     
     for i in range(11):
         t = i/10.0
         self.assertTrue(L.bounds_exact().Interval.contains(L(t)))
         self.assertTrue(L.bounds_fast().Interval.contains(L(t)))
         self.assertTrue(L.bounds_local(t-0.05, t+0.05).Interval.contains(L(t)))
         self.assertAlmostEqual(lerp(t, 0, 4), t*4)
         self.assertAlmostEqual(L(t), cy2geom.reverse(L)(1-t))
         self.assertAlmostEqual( L(t)*t, (L*t)(t) )
         self.assertAlmostEqual( L(t)+t, (L+t)(t) )
         self.assertAlmostEqual( L(t)-t, (L-t)(t) )
         self.assertAlmostEqual( -( L(t) ), (-L)(t) )
         self.assertAlmostEqual( (L/2)(t), L(t)/2 )