当前位置: 首页>>代码示例>>Python>>正文


Python FCodePrinter.doprint方法代码示例

本文整理汇总了Python中sympy.printing.fcode.FCodePrinter.doprint方法的典型用法代码示例。如果您正苦于以下问题:Python FCodePrinter.doprint方法的具体用法?Python FCodePrinter.doprint怎么用?Python FCodePrinter.doprint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sympy.printing.fcode.FCodePrinter的用法示例。


在下文中一共展示了FCodePrinter.doprint方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: render_as_module

# 需要导入模块: from sympy.printing.fcode import FCodePrinter [as 别名]
# 或者: from sympy.printing.fcode.FCodePrinter import doprint [as 别名]
def render_as_module(definitions, name, declarations=(), printer_settings=None):
    """ Creates a ``Module`` instance and renders it as a string.

    This generates Fortran source code for a module with the correct ``use`` statements.

    Parameters
    ==========

    definitions : iterable
        Passed to :class:`sympy.codegen.fnodes.Module`.
    name : str
        Passed to :class:`sympy.codegen.fnodes.Module`.
    declarations : iterable
        Passed to :class:`sympy.codegen.fnodes.Module`. It will be extended with
        use statements, 'implicit none' and public list generated from ``definitions``.
    printer_settings : dict
        Passed to ``FCodePrinter`` (default: ``{'standard': 2003, 'source_format': 'free'}``).

    """
    printer_settings = printer_settings or {'standard': 2003, 'source_format': 'free'}
    printer = FCodePrinter(printer_settings)
    dummy = Dummy()
    if isinstance(definitions, Module):
        raise ValueError("This function expects to construct a module on its own.")
    mod = Module(name, chain(declarations, [dummy]), definitions)
    fstr = printer.doprint(mod)
    module_use_str = '   %s\n' % '   \n'.join(['use %s, only: %s' % (k, ', '.join(v)) for
                                                k, v in printer.module_uses.items()])
    module_use_str += '   implicit none\n'
    module_use_str += '   private\n'
    module_use_str += '   public %s\n' % ', '.join([str(node.name) for node in definitions if getattr(node, 'name', None)])
    return fstr.replace(printer.doprint(dummy), module_use_str)
    return fstr
开发者ID:Lenqth,项目名称:sympy,代码行数:35,代码来源:futils.py

示例2: test_case

# 需要导入模块: from sympy.printing.fcode import FCodePrinter [as 别名]
# 或者: from sympy.printing.fcode.FCodePrinter import doprint [as 别名]
def test_case():
    ob = FCodePrinter()
    x,x_,x__,y,X,X_,Y = symbols('x,x_,x__,y,X,X_,Y')
    assert fcode(exp(x_) + sin(x*y) + cos(X*Y)) == \
                        '      exp(x_) + sin(x*y) + cos(X__*Y_)'
    assert fcode(exp(x__) + 2*x*Y*X_**Rational(7, 2)) == \
                        '      2*X_**(7.0d0/2.0d0)*Y*x + exp(x__)'
    assert fcode(exp(x_) + sin(x*y) + cos(X*Y), name_mangling=False) == \
                        '      exp(x_) + sin(x*y) + cos(X*Y)'
    assert fcode(x - cos(X), name_mangling=False) == '      x - cos(X)'
    assert ob.doprint(X*sin(x) + x_, assign_to='me') == '      me = X*sin(x_) + x__'
    assert ob.doprint(X*sin(x), assign_to='mu') == '      mu = X*sin(x_)'
    assert ob.doprint(x_, assign_to='ad') == '      ad = x__'
    n, m = symbols('n,m', integer=True)
    A = IndexedBase('A')
    x = IndexedBase('x')
    y = IndexedBase('y')
    i = Idx('i', m)
    I = Idx('I', n)
    assert fcode(A[i, I]*x[I], assign_to=y[i], source_format='free') == (
                                            "do i = 1, m\n"
                                            "   y(i) = 0\n"
                                            "end do\n"
                                            "do i = 1, m\n"
                                            "   do I_ = 1, n\n"
                                            "      y(i) = A(i, I_)*x(I_) + y(i)\n"
                                            "   end do\n"
                                            "end do" )
开发者ID:normalhuman,项目名称:sympy,代码行数:30,代码来源:test_fcode.py

示例3: test_loops

# 需要导入模块: from sympy.printing.fcode import FCodePrinter [as 别名]
# 或者: from sympy.printing.fcode.FCodePrinter import doprint [as 别名]
def test_loops():
    from sympy import symbols
    i,j,n,m = symbols('i j n m', integer=True)
    A,x,y = symbols('A x y')
    A = Indexed(A)(Idx(i, m), Idx(j, n))
    x = Indexed(x)(Idx(j, n))
    y = Indexed(y)(Idx(i, m))

    # human = False
    printer = FCodePrinter({ 'source_format': 'free', 'assign_to':y, 'human':0})
    expected = ([], set([A, x, y, Idx(j, n), Idx(i, m)]), 'do i = 1, m\n   do j = 1, n\n      y(i) = A(i, j)*x(j)\n   end do\nend do')
    code = printer.doprint(A*x)
    # assert expected == code

    # human = True
    printer = FCodePrinter({ 'source_format': 'free', 'assign_to':y, 'human':1})

    expected = (
            '! Not Fortran:\n'
            '! A(i, j)\n'
            '! i\n'
            '! j\n'
            '! x(j)\n'
            '! y(i)\n'
            'do i = 1, m\n'
            '   do j = 1, n\n'
            '      y(i) = A(i, j)*x(j)\n'
            '   end do\n'
            'end do'
            )
    code = printer.doprint(A*x)
    assert expected == code
开发者ID:goriccardo,项目名称:sympy,代码行数:34,代码来源:test_fcode.py


注:本文中的sympy.printing.fcode.FCodePrinter.doprint方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。