示例#1
0
文件: test_ast.py 项目: Lenqth/sympy
def test_Attribute():
    noexcept = Attribute('noexcept')
    assert noexcept == Attribute('noexcept')
    alignas16 = Attribute('alignas', [16])
    alignas32 = Attribute('alignas', [32])
    assert alignas16 != alignas32
    assert alignas16.func(*alignas16.args) == alignas16
示例#2
0
def bind_C(name=None):
    """ Creates an Attribute ``bind_C`` with a name

    Parameters
    ==========

    name : str

    Examples
    ========

    >>> from sympy import Symbol
    >>> from sympy.printing import fcode
    >>> from sympy.codegen.ast import FunctionDefinition, real, Return, Variable
    >>> from sympy.codegen.fnodes import array, sum_, size, bind_C
    >>> a = Symbol('a', real=True)
    >>> s = Symbol('s', integer=True)
    >>> arr = array(a, dim=[s], intent='in')
    >>> body = [Return((sum_(a**2)/s)**.5)]
    >>> fd = FunctionDefinition(real, 'rms', [arr, s], body, attrs=[bind_C('rms')])
    >>> print(fcode(fd, source_format='free', standard=2003))
    real*8 function rms(a, s) bind(C, name="rms")
    real*8, dimension(s), intent(in) :: a
    integer*4 :: s
    rms = sqrt(sum(a**2)/s)
    end function
    """
    return Attribute('bind_C', [String(name)] if name else [])
示例#3
0
def dimension(*args):
    """ Creates a 'dimension' Attribute with (up to 7) extents.

    Examples
    ========

    >>> from sympy.printing import fcode
    >>> from sympy.codegen.fnodes import dimension, intent_in
    >>> dim = dimension('2', ':')  # 2 rows, runtime determined number of columns
    >>> from sympy.codegen.ast import Variable, integer
    >>> arr = Variable('a', integer, attrs=[dim, intent_in])
    >>> fcode(arr.as_Declaration(), source_format='free', standard=2003)
    'integer*4, dimension(2, :), intent(in) :: a'

    """
    if len(args) > 7:
        raise ValueError("Fortran only supports up to 7 dimensional arrays")
    parameters = []
    for arg in args:
        if isinstance(arg, Extent):
            parameters.append(arg)
        elif isinstance(arg, str):
            if arg == ':':
                parameters.append(Extent())
            else:
                parameters.append(String(arg))
        elif iterable(arg):
            parameters.append(Extent(*arg))
        else:
            parameters.append(sympify(arg))
    if len(args) == 0:
        raise ValueError("Need at least one dimension")
    return Attribute('dimension', parameters)
示例#4
0
def test_Attribute():
    noexcept = Attribute('noexcept')
    assert noexcept == Attribute('noexcept')
    alignas16 = Attribute('alignas', [16])
    alignas32 = Attribute('alignas', [32])
    assert alignas16 != alignas32
    assert alignas16.func(*alignas16.args) == alignas16
示例#5
0
The functions defined in this module allows the user to express functions such as ``dsign``
as a SymPy function for symbolic manipulation.
"""

from sympy.codegen.ast import (Attribute, CodeBlock, FunctionCall, Node, none,
                               String, Token, _mk_Tuple, Variable)
from sympy.core.basic import Basic
from sympy.core.containers import Tuple
from sympy.core.expr import Expr
from sympy.core.function import Function
from sympy.core.numbers import Float, Integer
from sympy.core.sympify import sympify
from sympy.logic import true, false
from sympy.utilities.iterables import iterable

pure = Attribute('pure')
elemental = Attribute('elemental')  # (all elemental procedures are also pure)

intent_in = Attribute('intent_in')
intent_out = Attribute('intent_out')
intent_inout = Attribute('intent_inout')

allocatable = Attribute('allocatable')


class Program(Token):
    """ Represents a 'program' block in Fortran

    Examples
    ========
示例#6
0
    Attribute,
    Declaration,
    Node,
    String,
    Token,
    Type,
    none,
    FunctionCall,
)
from sympy.core.basic import Basic
from sympy.core.containers import Tuple
from sympy.core.sympify import sympify

void = Type("void")

restrict = Attribute("restrict")  # guarantees no pointer aliasing
volatile = Attribute("volatile")
static = Attribute("static")


def alignof(arg):
    """ Generate of FunctionCall instance for calling 'alignof' """
    return FunctionCall("alignof",
                        [String(arg) if isinstance(arg, str) else arg])


def sizeof(arg):
    """ Generate of FunctionCall instance for calling 'sizeof'

    Examples
    ========
"""
AST nodes specific to the C family of languages
"""

from sympy.core.basic import Basic
from sympy.core.compatibility import string_types
from sympy.core.containers import Tuple
from sympy.core.sympify import sympify
from sympy.codegen.ast import Attribute, Declaration, Node, String, Token, Type, none, FunctionCall

void = Type('void')

restrict = Attribute('restrict')  # guarantees no pointer aliasing
volatile = Attribute('volatile')
static = Attribute('static')


def alignof(arg):
    """ Generate of FunctionCall instance for calling 'alignof' """
    return FunctionCall(
        'alignof', [String(arg) if isinstance(arg, string_types) else arg])


def sizeof(arg):
    """ Generate of FunctionCall instance for calling 'sizeof'

    Examples
    ========

    >>> from sympy.codegen.ast import real
    >>> from sympy.codegen.cnodes import sizeof
示例#8
0
"""
AST nodes specific to C++.
"""

from sympy.codegen.ast import Attribute, String, Token, Type, none


class using(Token):
    """ Represents a 'using' statement in C++ """
    __slots__ = ['type', 'alias']
    defaults = {'alias': none}
    _construct_type = Type
    _construct_alias = String


constexpr = Attribute('constexpr')
示例#9
0
文件: cxxnodes.py 项目: msgoff/sympy
"""
AST nodes specific to C++.
"""

from sympy.codegen.ast import Attribute, String, Token, Type, none


class using(Token):
    """ Represents a 'using' statement in C++ """

    __slots__ = ("type", "alias")
    defaults = {"alias": none}
    _construct_type = Type
    _construct_alias = String


constexpr = Attribute("constexpr")
示例#10
0
文件: fnodes.py 项目: msgoff/sympy
    String,
    Token,
    _mk_Tuple,
    Variable,
)
from sympy.core.basic import Basic
from sympy.core.containers import Tuple
from sympy.core.expr import Expr
from sympy.core.function import Function
from sympy.core.numbers import Float, Integer
from sympy.core.sympify import sympify
from sympy.logic import true, false
from sympy.utilities.iterables import iterable


pure = Attribute("pure")
elemental = Attribute("elemental")  # (all elemental procedures are also pure)

intent_in = Attribute("intent_in")
intent_out = Attribute("intent_out")
intent_inout = Attribute("intent_inout")

allocatable = Attribute("allocatable")


class Program(Token):
    """ Represents a 'program' block in Fortran

    Examples
    ========