Ejemplo n.º 1
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    s, n, m, o = symbols('s n m o', integer=True)
    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)

    x = IndexedBase('x')[j]
    A = IndexedBase('A')[i, j]
    B = IndexedBase('B')[i, j, k]

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        p = CCodePrinter()
        p._not_c = set()

        assert p._print_Indexed(x) == 'x[j]'
        assert p._print_Indexed(A) == 'A[%s]' % (m*i+j)
        assert p._print_Indexed(B) == 'B[%s]' % (i*o*m+j*o+k)
        assert p._not_c == set()

        A = IndexedBase('A', shape=(5,3))[i, j]
        assert p._print_Indexed(A) == 'A[%s]' % (3*i + j)

        A = IndexedBase('A', shape=(5,3), strides='F')[i, j]
        assert ccode(A) == 'A[%s]' % (i + 5*j)

        A = IndexedBase('A', shape=(29,29), strides=(1, s), offset=o)[i, j]
        assert ccode(A) == 'A[o + s*j + i]'

        Abase = IndexedBase('A', strides=(s, m, n), offset=o)
        assert ccode(Abase[i, j, k]) == 'A[m*j + n*k + o + s*i]'
        assert ccode(Abase[2, 3, k]) == 'A[3*m + n*k + o + 2*s]'
Ejemplo n.º 2
0
def test_CCodePrinter():
    with warnings.catch_warnings():
        warnings.filterwarnings("error", category=SymPyDeprecationWarning)
        with raises(SymPyDeprecationWarning):
            CCodePrinter()
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        assert CCodePrinter().language == 'C'
Ejemplo n.º 3
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    i, j, k, n, m, o = symbols('i j k n m o', integer=True)

    p = CCodePrinter()
    p._not_c = set()

    x = IndexedBase('x')[Idx(j, n)]
    assert p._print_Indexed(x) == 'x[j]'
    A = IndexedBase('A')[Idx(i, m), Idx(j, n)]
    assert p._print_Indexed(A) == 'A[%s]' % str(j + n*i)
    B = IndexedBase('B')[Idx(i, m), Idx(j, n), Idx(k, o)]
    assert p._print_Indexed(B) == 'B[%s]' % str(k + i*n*o + j*o)

    assert p._not_c == set()
Ejemplo n.º 4
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    n, m, o = symbols('n m o', integer=True)
    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)
    p = CCodePrinter()
    p._not_c = set()

    x = IndexedBase('x')[j]
    assert p._print_Indexed(x) == 'x[j]'
    A = IndexedBase('A')[i, j]
    assert p._print_Indexed(A) == 'A[%s]' % (m*i+j)
    B = IndexedBase('B')[i, j, k]
    assert p._print_Indexed(B) == 'B[%s]' % (i*o*m+j*o+k)

    assert p._not_c == set()
Ejemplo n.º 5
0
 def _print_Mul(self, expr):
     if len(expr.args) == 2: # log2 and log10
         a, b = expr.args
         if is_inv(a) and is_log(a.base) and is_log(b):
             log_base = a.base.args[0]
             if log_base in [2, 10]:
                 return "log%d(%s)" % (log_base, self._print(b.args[0]))
     return CCodePrinter._print_Mul(self, expr)
Ejemplo n.º 6
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    n, m, o = symbols('n m o', integer=True)
    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)

    x = IndexedBase('x')[j]
    A = IndexedBase('A')[i, j]
    B = IndexedBase('B')[i, j, k]

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        p = CCodePrinter()
        p._not_c = set()

        assert p._print_Indexed(x) == 'x[j]'
        assert p._print_Indexed(A) == 'A[%s]' % (m * i + j)
        assert p._print_Indexed(B) == 'B[%s]' % (i * o * m + j * o + k)
        assert p._not_c == set()
Ejemplo n.º 7
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    n, m, o = symbols('n m o', integer=True)
    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)

    x = IndexedBase('x')[j]
    A = IndexedBase('A')[i, j]
    B = IndexedBase('B')[i, j, k]

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        p = CCodePrinter()
        p._not_c = set()

        assert p._print_Indexed(x) == 'x[j]'
        assert p._print_Indexed(A) == 'A[%s]' % (m*i+j)
        assert p._print_Indexed(B) == 'B[%s]' % (i*o*m+j*o+k)
        assert p._not_c == set()
Ejemplo n.º 8
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    s, n, m, o = symbols('s n m o', integer=True)
    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)

    x = IndexedBase('x')[j]
    A = IndexedBase('A')[i, j]
    B = IndexedBase('B')[i, j, k]

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        p = CCodePrinter()
        p._not_c = set()

        assert p._print_Indexed(x) == 'x[j]'
        assert p._print_Indexed(A) == 'A[%s]' % (m * i + j)
        assert p._print_Indexed(B) == 'B[%s]' % (i * o * m + j * o + k)
        assert p._not_c == set()

        A = IndexedBase('A', shape=(5, 3))[i, j]
        assert p._print_Indexed(A) == 'A[%s]' % (3 * i + j)

        A = IndexedBase('A', shape=(5, 3), strides='F')[i, j]
        assert ccode(A) == 'A[%s]' % (i + 5 * j)

        A = IndexedBase('A', shape=(29, 29), strides=(1, s), offset=o)[i, j]
        assert ccode(A) == 'A[o + s*j + i]'

        Abase = IndexedBase('A', strides=(s, m, n), offset=o)
        assert ccode(Abase[i, j, k]) == 'A[m*j + n*k + o + s*i]'
        assert ccode(Abase[2, 3, k]) == 'A[3*m + n*k + o + 2*s]'
Ejemplo n.º 9
0
 def convert_to(self, language, symbols={}, namespace={}):
     '''
     Converts expression into a string for the given ``language`` using the
     given set of ``symbols``. Replaces each :class:`Symbol` appearing in the
     expression with ``sym.read()``, and if the language is C++ or GPU then
     uses ``sympy.CCodePrinter().doprint()`` to convert the syntax, e.g.
     ``x**y`` becomes ``pow(x,y)``.
     '''
     if language.name == 'python':
         return substitute_symbols(self.expr, symbols)
     elif language.name == 'c' or language.name == 'gpu':
         return substitute_symbols(CCodePrinter().doprint(self.sympy_expr),
                                   symbols)
Ejemplo n.º 10
0
 def __init__(self,
              settings={},
              tab='    ',
              level=0,
              array_format='C',
              epsilon_nan=0,
              epsilon_inf=0,
              epsilon_power=1,
              assertions=False,
              contracts=False,
              postcheck_hooks=False,
              do_cse=True,
              user_exprs=[]):
     CCodePrinter.__init__(self, settings)
     self._some_vars = SomeVars()
     self._value_type = 'double'
     self._relational_type = 'int'
     self._assignments_values = dict()
     self._decls = dict()
     self._varid = dict()
     self._declared = dict()
     self._affcts = dict()
     self._cond_affcts = dict()
     self._tab = tab
     self._level = level
     self._do_cse = do_cse
     self._array_format = array_format
     self._epsilon_nan = epsilon_nan
     self._epsilon_inf = epsilon_inf
     self._epsilon_power = epsilon_power
     self._assertions = assertions
     self._contracts = contracts
     self._postcheck_hooks = postcheck_hooks
     self._current_condition = None
     self._sign = dict()
     self._user_exprs = user_exprs
     self._var_asserts = {}
Ejemplo n.º 11
0
    def __init__(self, **kwargs):
        """Initialize a C code printer.

        The printer class, the name of the template, the line continuation
        symbol, and the statement ending will be set automatically.
        """

        super().__init__(
            CCodePrinter(),
            lambda base, indices: ''.join(
                [base] + ['[{}]'.format(i.index) for i in indices]),
            line_cont='\\',
            stmt_end=';',
            add_filters={
                'form_loop_beg': _form_c_loop_beg,
                'form_loop_end': _form_c_loop_end,
            },
            add_globals={'zero_literal': '0.0'},
            **kwargs)
Ejemplo n.º 12
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    i, j, k, n, m, o = symbols('i j k n m o', integer=True)

    p = CCodePrinter()
    p._not_c = set()

    x = IndexedBase('x')[Idx(j, n)]
    assert p._print_Indexed(x) == 'x[j]'
    A = IndexedBase('A')[Idx(i, m), Idx(j, n)]
    assert p._print_Indexed(A) == 'A[%s]' % str(j + n * i)
    B = IndexedBase('B')[Idx(i, m), Idx(j, n), Idx(k, o)]
    assert p._print_Indexed(B) == 'B[%s]' % str(k + i * n * o + j * o)

    assert p._not_c == set()
Ejemplo n.º 13
0
def test_ccode_Indexed():
    from sympy.tensor import IndexedBase, Idx
    from sympy import symbols
    n, m, o = symbols('n m o', integer=True)
    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)
    p = CCodePrinter()
    p._not_c = set()

    x = IndexedBase('x')[j]
    assert p._print_Indexed(x) == 'x[j]'
    A = IndexedBase('A')[i, j]
    assert p._print_Indexed(A) == 'A[%s]' % (m * i + j)
    B = IndexedBase('B')[i, j, k]
    assert p._print_Indexed(B) == 'B[%s]' % (i * o * m + j * o + k)

    assert p._not_c == set()
Ejemplo n.º 14
0
 def _print_Pow(self, expr):
     if expr.base == 2:
         return "exp2(%s)" % self._print(expr.exp)
     if expr.base == 10:
         return "exp10(%s)" % self._print(expr.exp)
     if expr.exp == 0.5 and isinstance(expr.base, Add):
         args = expr.base.args
         if len(args) == 2 and all(map(is_square, args)):
             if self.order != 'none':
                 args = self._as_ordered_terms(expr.base, order=None)
                 # TODO: (above) is it fine to use `order=None`?
             return "hypot(%s, %s)" % (self._print(args[0].args[0]),
                                       self._print(args[1].args[0]))
     if expr.exp == Rational(1, 3):
         return "cbrt(%s)" % self._print(expr.base)
     if expr.exp.is_integer and expr.exp != -1:
         return "pown(%s, %s)" % (
             self._print(expr.base), self._print(expr.exp))
     if expr.exp.is_positive and expr.exp != 0.5:
         return "powr(%s, %s)" % (
             self._print(expr.base), self._print(expr.exp))
     return CCodePrinter._print_Pow(self, expr)
Ejemplo n.º 15
0
def test_CCodePrinter():
    with warns_deprecated_sympy():
        CCodePrinter()
    with warns_deprecated_sympy():
        assert CCodePrinter().language == 'C'
Ejemplo n.º 16
0
 def _indent_code(self, codelines):
     p = CCodePrinter()
     return p.indent_code(codelines)
Ejemplo n.º 17
0
 def convert_to(self, language, symbols={}):
     if language.name == 'python':
         return self.substitute_symbols(self.expr, symbols)
     elif language.name == 'c':
         return self.substitute_symbols(
             CCodePrinter().doprint(self.sympy_expr), symbols)
Ejemplo n.º 18
0
 def __init__(self, settings={}):
     settings = dict(settings)
     userfuncs = settings.setdefault('user_functions', {})
     for k, v in known_functions.items():
         userfuncs.setdefault(k, v)
     CCodePrinter.__init__(self, settings)
Ejemplo n.º 19
0
 def __init__(self, settings={}):
     CCodePrinter.__init__(self, settings)
Ejemplo n.º 20
0
 def __init__(self, settings={}):
     CCodePrinter.__init__(self, settings)
     custom_functions = {'INT': '(int)', 'FLOAT': '(float)'}
     self.known_functions.update(custom_functions)
Ejemplo n.º 21
0
 def _indent_code(self, codelines):
     p = CCodePrinter()
     return p.indent_code(codelines)
Ejemplo n.º 22
0
 def __init__(self, settings={}):
     CCodePrinter.__init__(self, settings)
Ejemplo n.º 23
0
 def __init__(self, settings={}):
     CCodePrinter.__init__(self, settings)
     self.known_functions.update(self.custom_functions)