Exemplo n.º 1
0
def elements_of(space, names):
    """ Create multiple elements of same space (possibly a ProductSpace).

    Parameters
    ----------
    space : ScalarFunctionSpace | VectorFunctionSpace | ProductSpace

    names : str | iterable
        Pattern or list of patterns from which a list of function names is
        produced.

    Results
    -------
    res : iterable
        Multiple elements taken from the given space. If space is ProductSpace,
        each element is a list of functions; otherwise, each element is a
        single function.

    """
    if not isinstance(space, BasicFunctionSpace):
        raise TypeError(space)
    names = expand_name_patterns(names, seq=True)
    return _recursive_elements_of(space, names)
Exemplo n.º 2
0
def element_of(space, name):
    """ Create a single element of a given space (possibly a ProductSpace).

    Parameters
    ----------
    space : ScalarFunctionSpace | VectorFunctionSpace | ProductSpace
        Function space from which a single element should be created.

    name : str | iterable
        If space is ProductSpace, 'name' must either be an explicit list of
        function names, or a single pattern string that will be expanded into
        such a list. Otherwise, 'name' must be a simple string.

    Results
    -------
    res : ScalarTestFunction | VectorTestFunction | iterable
        Single element taken from the given space. If space is ProductSpace,
        an element is a list of functions; otherwise, it is a single function.

    """
    if not isinstance(space, BasicFunctionSpace):
        raise TypeError(space)
    names = expand_name_patterns(name)
    return _recursive_element_of(space, names)
Exemplo n.º 3
0
def test_expand_name_patterns():

    assert expand_name_patterns('x,y,z') == ('x', 'y', 'z')
    assert expand_name_patterns('x y z') == ('x', 'y', 'z')

    assert expand_name_patterns('x,') == ('x',)
    assert expand_name_patterns('x', seq=True) == ('x',)

    assert expand_name_patterns(('a', 'b', 'c')) == ('a', 'b', 'c')
    assert expand_name_patterns(['a', 'b', 'c']) == ['a', 'b', 'c']
    assert expand_name_patterns({'a', 'b', 'c'}) == {'a', 'b', 'c'}

    assert expand_name_patterns('x:4') == ('x0', 'x1', 'x2', 'x3')
    assert expand_name_patterns('x5:10') == ('x5', 'x6', 'x7', 'x8', 'x9')
    assert expand_name_patterns('x7(1:3)') == ('x71', 'x72')
    assert expand_name_patterns('x2:5, y:2') == ('x2', 'x3', 'x4', 'y0', 'y1')
    assert expand_name_patterns(('x2:5', 'y:2')) == (('x2', 'x3', 'x4'), ('y0', 'y1'))

    assert expand_name_patterns(':c') == ('a', 'b', 'c')
    assert expand_name_patterns('x:c') == ()
    assert expand_name_patterns('x:z') == ('x', 'y', 'z')
    assert expand_name_patterns('x(:c)') == ('xa', 'xb', 'xc')
    assert expand_name_patterns('a:b, x:z') == ('a', 'b', 'x', 'y', 'z')
    assert expand_name_patterns(('a:b', 'x:z')) == (('a', 'b'), ('x', 'y', 'z'))

    assert expand_name_patterns('x:2(1:3)') == ('x01', 'x02', 'x11', 'x12')
    assert expand_name_patterns(':3:2') == ('00', '01', '10', '11', '20', '21')

    assert expand_name_patterns('x((a:b))') == ('x(a)', 'x(b)')
    assert expand_name_patterns(r'x(:1\,:2)') == ('x(0,0)', 'x(0,1)')