Ejemplo n.º 1
0
    def test_str_expr_replacement(self):
        # Signature: name(cls, frm, to, expr_string, func_ok=False)
                # replaces all occurences of name 'frm' with 'to' in expr_string
                # ('frm' may not occur as a function name on the rhs) ...
                # 'to' can be an arbitrary string so this function can also be used for
                # argument substitution.
                #
                # Returns the resulting string.
        # from nineml.abstraction_layer.component.util import MathUtil
        t = 'b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)'

        t = MathUtil.str_expr_replacement('b', 'B', t)
        self.assertEqual(t, 'B*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)')

        # 'e' is a builtin, so this function doesn't care.
        t = MathUtil.str_expr_replacement(frm='e', to='E', expr_string=t)
        self.assertEqual(t, 'B*c + d/(E*sin(f+g/E)) + b1 + e_ / exp(12*g)')
Ejemplo n.º 2
0
    def test_get_prefixed_rhs_string(self):
        # Signature: name(cls, expr_obj, prefix='', exclude=None)
                # No Docstring
        # from nineml.abstraction_layer.component.util import MathUtil

        e = StrToExpr.alias('a := b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)')

        rhs_sub = MathUtil.get_prefixed_rhs_string(e, prefix='U_', exclude=['c', 'e_'])
        self.assertEqual(
            rhs_sub,
            'U_b*c + U_d/(e*sin(U_f+U_g/e)) + U_b1 + e_ / exp(12*U_g)'
        )
Ejemplo n.º 3
0
    def test_get_rhs_substituted(self):
        # Signature: name(cls, expr_obj, namemap)
                # No Docstring
        # from nineml.abstraction_layer.component.util import MathUtil

        e = StrToExpr.alias('a := b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)')

        rhs_sub = MathUtil.get_rhs_substituted(e, {'b': 'B', 'e': 'E'})
        self.assertEqual(
            rhs_sub,
            'B*c + d/(E*sin(f+g/E)) + b1 + e_ / exp(12*g)'
        )
Ejemplo n.º 4
0
    def test_is_single_symbol(self):
        # Signature: name(cls, expr)
                # Returns ``True`` if the expression is a single symbol, possibly
                # surrounded with white-spaces
                #
                # >>> is_single_symbol('hello')
                # True
                #
                # >>> is_single_symbol('hello * world')
                # False

        self.assertTrue(MathUtil.is_single_symbol('t'))
        self.assertTrue(MathUtil.is_single_symbol('var_1'))
        self.assertTrue(MathUtil.is_single_symbol('var_long_name'))
        self.assertTrue(MathUtil.is_single_symbol('_myName'))

        self.assertFalse(MathUtil.is_single_symbol('r + y'))
        self.assertFalse(MathUtil.is_single_symbol('r+y'))
        self.assertFalse(MathUtil.is_single_symbol('sin(y)'))