Beispiel #1
0
def test_MatrixSlice():
    n = diofant.Symbol('n', integer=True)
    X = diofant.MatrixSymbol('X', n, n)

    Y = X[1:2:3, 4:5:6]
    Yt = theano_code(Y)
    # assert tuple(Yt.owner.op.idx_list) == (slice(1,2,3), slice(4,5,6))
    assert Yt.owner.inputs[0] == theano_code(X)

    k = diofant.Symbol('k')
    kt = theano_code(k, dtypes={k: 'int32'})
    start, stop, step = 4, k, 2
    Y = X[start:stop:step]
    Yt = theano_code(Y, dtypes={n: 'int32', k: 'int32'})
Beispiel #2
0
def test_MatrixSlice_2():
    n = diofant.Symbol('n', integer=True)
    X = diofant.MatrixSymbol('X', n, n)

    Y = X[1:2:3, 4:5:6]
    Yt = theano_code(Y)
    assert tuple(Yt.owner.op.idx_list) == (slice(1, 2, 3), slice(4, 5, 6))
Beispiel #3
0
def python(expr, **settings):
    """Return Python interpretation of passed expression
    (can be passed to the exec() function without any modifications)"""

    printer = PythonPrinter(settings)
    exprp = printer.doprint(expr)

    result = ''
    # Returning found symbols and functions
    renamings = {}
    for symbolname in printer.symbols:
        newsymbolname = symbolname
        # Escape symbol names that are reserved python keywords
        if kw.iskeyword(newsymbolname):
            while True:
                newsymbolname += "_"
                if (newsymbolname not in printer.symbols
                        and newsymbolname not in printer.functions):
                    renamings[diofant.Symbol(symbolname)] = diofant.Symbol(
                        newsymbolname)
                    break
        result += newsymbolname + ' = Symbol(\'' + symbolname + '\')\n'

    for functionname in printer.functions:
        newfunctionname = functionname
        # Escape function names that are reserved python keywords
        if kw.iskeyword(newfunctionname):
            while True:
                newfunctionname += "_"
                if (newfunctionname not in printer.symbols
                        and newfunctionname not in printer.functions):
                    renamings[diofant.Function(
                        functionname)] = diofant.Function(newfunctionname)
                    break
        result += newfunctionname + ' = Function(\'' + functionname + '\')\n'

    if not len(renamings) == 0:
        exprp = expr.subs(renamings)
    result += 'e = ' + printer._str(exprp)
    return result
Beispiel #4
0
def test_BlockMatrix():
    n = diofant.Symbol('n', integer=True)
    A = diofant.MatrixSymbol('A', n, n)
    B = diofant.MatrixSymbol('B', n, n)
    C = diofant.MatrixSymbol('C', n, n)
    D = diofant.MatrixSymbol('D', n, n)
    At, Bt, Ct, Dt = map(theano_code, (A, B, C, D))
    Block = diofant.BlockMatrix([[A, B], [C, D]])
    Blockt = theano_code(Block)
    solutions = [
        tt.join(0, tt.join(1, At, Bt), tt.join(1, Ct, Dt)),
        tt.join(1, tt.join(0, At, Ct), tt.join(0, Bt, Dt))
    ]
    assert any(theq(Blockt, solution) for solution in solutions)
Beispiel #5
0
def test_function_exponentiation():
    cases = {
        'sin**2(x)': 'sin(x)**2',
        'exp^y(z)': 'exp(z)^y',
        'sin**2(E^(x))': 'sin(E^(x))**2'}
    transformations = standard_transformations + (convert_xor,)
    transformations2 = transformations + (function_exponentiation,)
    for k, v in cases.items():
        implicit = parse_expr(k, transformations=transformations2)
        normal = parse_expr(v, transformations=transformations)
        assert implicit == normal

    other_implicit = ['x y', 'x sin x', '2x', 'sin x',
                      'cos 2*x', 'sin cos x']
    for case in other_implicit:
        pytest.raises(SyntaxError,
                      lambda: parse_expr(case, transformations=transformations2))

    assert parse_expr('x**2', local_dict={'x': diofant.Symbol('x')},
                      transformations=transformations2) == parse_expr('x**2')
Beispiel #6
0
def test_factorial():
    n = diofant.Symbol('n')
    assert theano_code(diofant.factorial(n))