Ejemplo n.º 1
0
def test_var_accepts_comma():
    v1 = var('x y z')
    v2 = var('x,y,z')
    v3 = var('x,y z')

    assert v1 == v2
    assert v1 == v3
Ejemplo n.º 2
0
def test_var():
    var("a")
    assert a == Symbol("a")

    var("b bb cc zz _x")
    assert b == Symbol("b")
    assert bb == Symbol("bb")
    assert cc == Symbol("cc")
    assert zz == Symbol("zz")
    assert _x == Symbol("_x")

    v = var(['d', 'e', 'fg'])
    assert d == Symbol('d')
    assert e == Symbol('e')
    assert fg == Symbol('fg')

    # check return value
    assert v == [d, e, fg]

    # see if var() really injects into global namespace
    pytest.raises(NameError, lambda: z1)
    _make_z1()
    assert z1 == Symbol("z1")

    pytest.raises(NameError, lambda: z2)
    _make_z2()
    assert z2 == Symbol("z2")
Ejemplo n.º 3
0
def test_var_return():
    pytest.raises(ValueError, lambda: var(''))
    v2 = var('q')
    v3 = var('q p')

    assert v2 == Symbol('q')
    assert v3 == (Symbol('q'), Symbol('p'))
Ejemplo n.º 4
0
def test_var():
    var('a')
    assert a == Symbol('a')

    var('b bb cc zz _x')
    assert b == Symbol('b')
    assert bb == Symbol('bb')
    assert cc == Symbol('cc')
    assert zz == Symbol('zz')
    assert _x == Symbol('_x')

    v = var(['d', 'e', 'fg'])
    assert d == Symbol('d')
    assert e == Symbol('e')
    assert fg == Symbol('fg')

    # check return value
    assert v == [d, e, fg]

    # pylint: disable=undefined-variable

    # see if var() really injects into global namespace
    pytest.raises(NameError, lambda: z1)  # type: ignore[name-defined]
    _make_z1()
    assert z1 == Symbol('z1')

    pytest.raises(NameError, lambda: z2)  # type: ignore[name-defined]
    _make_z2()
    assert z2 == Symbol('z2')
Ejemplo n.º 5
0
def test_var_cls():
    f = var('f', cls=Function)

    assert isinstance(f, FunctionClass)

    g, h = var('g,h', cls=Function)

    assert isinstance(g, FunctionClass)
    assert isinstance(h, FunctionClass)
Ejemplo n.º 6
0
def cholesky_solve(matlist, variable, constant, K):
    """
    Solves a system of equations using Cholesky decomposition given
    a matrix of coefficients, a vector of variables and a vector of constants.

    Examples
    ========

    >>> from diofant.matrices.densesolve import cholesky_solve
    >>> from diofant import QQ
    >>> from diofant import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> coefficients = [
    ... [QQ(25), QQ(15), QQ(-5)],
    ... [QQ(15), QQ(18), QQ(0)],
    ... [QQ(-5), QQ(0), QQ(11)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> coefficients = [
    ... [QQ(2)],
    ... [QQ(3)],
    ... [QQ(1)]]
    >>> cholesky_solve([[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], [[x], [y], [z]], [[QQ(2)], [QQ(3)], [QQ(1)]], QQ)
    [[-1/225], [23/135], [4/45]]

    See Also
    ========

    cholesky
    forward_substitution
    backward_substitution
    """
    new_matlist = copy.deepcopy(matlist)
    nrow = len(new_matlist)
    y = []
    L, Lstar = cholesky(new_matlist, K)
    for i in range(nrow):
        y.append([var('y' + str(i))])
    forward_substitution(L, y, constant, K)
    backward_substitution(Lstar, variable, y, K)
    return variable
Ejemplo n.º 7
0
def LU_solve(matlist, variable, constant, K):
    """
    Solves a system of equations using  LU decomposition given a matrix
    of coefficients, a vector of variables and a vector of constants.

    Examples
    ========

    >>> from diofant.matrices.densesolve import LU_solve
    >>> from diofant import QQ
    >>> from diofant import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> coefficients = [
    ... [QQ(2), QQ(-1), QQ(-2)],
    ... [QQ(-4), QQ(6), QQ(3)],
    ... [QQ(-4), QQ(-2), QQ(8)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> constants = [
    ... [QQ(-1)],
    ... [QQ(13)],
    ... [QQ(-6)]]
    >>> LU_solve(coefficients, variables, constants, QQ)
    [[2], [3], [1]]

    See Also
    ========

    LU
    forward_substitution
    backward_substitution
    """
    new_matlist = copy.deepcopy(matlist)
    nrow = len(new_matlist)
    y = []
    L, U = LU(new_matlist, K)
    for i in range(nrow):
        y.append([var('y' + str(i))])
    forward_substitution(L, y, constant, K)
    backward_substitution(U, variable, y, K)
    return variable
Ejemplo n.º 8
0
def test_var_nested():
    ((a, b, c, d), (x, y, z)) = var(('a:d', 'x:z'))
    assert isinstance(a, Symbol)
    assert isinstance(x, Symbol)
Ejemplo n.º 9
0
def test_var_keywords():
    var('x y', extended_real=True)
    assert x.is_extended_real and y.is_extended_real
Ejemplo n.º 10
0
def __make_z2():
    var("z2")
Ejemplo n.º 11
0
def _make_z1():
    var("z1")
Ejemplo n.º 12
0
This example illustrates the Gibbs phenomenon.

It also calculates the Wilbraham-Gibbs constant by two approaches:

1) calculating the fourier series of the step function and determining the
first maximum.
2) evaluating the integral for si(pi).

See:
 * http://en.wikipedia.org/wiki/Gibbs_phenomena
"""

from diofant import (var, sqrt, integrate, conjugate, Abs, pprint, pi, sin,
                     cos, lambdify, Integral)

x = var("x", extended_real=True)


def l2_norm(f, lim):
    """
    Calculates L2 norm of the function "f", over the domain lim=(x, a, b).

    x ...... the independent variable in f over which to integrate
    a, b ... the limits of the interval

    Examples
    ========

    >>> from diofant import Symbol
    >>> from gibbs_phenomenon import l2_norm
    >>> x = Symbol('x', extended_real=True)
Ejemplo n.º 13
0
def test_var_nested():
    ((a, *_), (x, *_)) = var(('a:d', 'x:z'))
    assert isinstance(a, Symbol)
    assert isinstance(x, Symbol)
Ejemplo n.º 14
0
def __make_z2():
    var('z2')
Ejemplo n.º 15
0
def _make_z1():
    var('z1')