Skip to content

Commit

Permalink
Merge branch 'master' into gosper-disp
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Jan 30, 2016
2 parents 7fb1dae + 97db939 commit adf60b5
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 72 deletions.
4 changes: 2 additions & 2 deletions docs/modules/vector/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ The scalar field :math:`f` and the measure numbers of the vector field
coordinate system in general.
Hence, define SymPy functions that way.

>>> from sympy import symbols
>>> v1, v2, v3, f = symbols('v1 v2 v3 f', type="Function")
>>> from sympy import symbols, Function
>>> v1, v2, v3, f = symbols('v1 v2 v3 f', cls=Function)

``v1``, ``v2`` and ``v3`` are the :math:`X`, :math:`Y` and :math:`Z`
components of the vector field respectively.
Expand Down
8 changes: 4 additions & 4 deletions sympy/concrete/summations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sympy.concrete.expr_with_limits import AddWithLimits
from sympy.concrete.expr_with_intlimits import ExprWithIntLimits
from sympy.core.function import Derivative
from sympy.core.function import Derivative, Function
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.core.symbol import (Dummy, Wild)
Expand Down Expand Up @@ -415,7 +415,7 @@ def reverse_order(self, *indices):

return Sum(e * self.function, *limits)

def findrecur(self, F=Dummy('F'), n=None):
def findrecur(self, F=Function('F'), n=None):
"""
Find a recurrence formula for the summand of the sum.
Expand All @@ -429,7 +429,7 @@ def findrecur(self, F=Dummy('F'), n=None):
Examples
========
>>> from sympy import symbols, Function, factorial, oo
>>> from sympy import symbols, factorial, oo
>>> from sympy.concrete import Sum
>>> n, k = symbols('n, k', integer=True)
>>> s = Sum(factorial(n)/(factorial(k)*factorial(n - k)), (k, 0, oo))
Expand All @@ -447,7 +447,7 @@ def findrecur(self, F=Dummy('F'), n=None):
.. [1] M. Petkovšek, H. S. Wilf, D. Zeilberger, A = B, 1996, Ch. 4.
"""
from sympy import Function, expand_func, gamma, factor, Mul
from sympy import expand_func, gamma, factor, Mul
from sympy.polys import together
from sympy.simplify import collect

Expand Down
3 changes: 2 additions & 1 deletion sympy/concrete/tests/test_sums_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ def test_function_subs():
pytest.raises(ValueError, lambda: S.subs(f(y), x + y))
S = Sum(x*log(y), (x, 0, oo), (y, 0, oo))
assert S.subs(log(y), y) == S
f = Symbol('f')
S = Sum(x*f(y), (x, 0, oo), (y, 0, oo))
assert S.subs(f(y), y) == Sum(x*y, (x, 0, oo), (y, 0, oo))

Expand Down Expand Up @@ -517,6 +516,7 @@ def test_equality():


def test_Sum_doit():
f = Function('f')
assert Sum(n*Integral(a**2), (n, 0, 2)).doit() == a**3
assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \
3*Integral(a**2)
Expand Down Expand Up @@ -826,6 +826,7 @@ def test_factor_expand_subs():


def test_distribution_over_equality():
f = Function('f')
assert Product(Eq(x*2, f(x)), (x, 1, 3)).doit() == Eq(48, f(1)*f(2)*f(3))
assert Sum(Eq(f(x), x**2), (x, 0, y)) == \
Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
Expand Down
7 changes: 0 additions & 7 deletions sympy/core/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ def as_dummy(self):
"""Return a Dummy having the same name and same assumptions as self."""
return Dummy(self.name, **self._assumptions.generator)

def __call__(self, *args):
from .function import Function
return Function(self.name)(*args)

def as_real_imag(self, deep=True, **hints):
from sympy import im, re
if hints.get('ignore') == self:
Expand Down Expand Up @@ -344,9 +340,6 @@ def matches(self, expr, repl_dict={}):
repl_dict[self] = expr
return repl_dict

def __call__(self, *args, **kwargs):
raise TypeError("'%s' object is not callable" % type(self).__name__)


_range = _re.compile('([0-9]*:[0-9]+|[a-zA-Z]?:[a-zA-Z])')

Expand Down
4 changes: 2 additions & 2 deletions sympy/core/tests/test_count_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_count_ops_visual():
ADD, MUL, POW, SIN, COS, EXP, AND, D, G = symbols(
'Add Mul Pow sin cos exp And Derivative Integral'.upper())
DIV, SUB, NEG = symbols('DIV SUB NEG')
NOT, OR, AND, XOR, IMPLIES, EQUIVALENT, ITE, BASIC, TUPLE = symbols(
NOT, OR, AND, XOR, IMPLIES, EQUIVALENT, _ITE, BASIC, TUPLE = symbols(
'Not Or And Xor Implies Equivalent ITE Basic Tuple'.upper())

def count(val):
Expand Down Expand Up @@ -106,7 +106,7 @@ def count(val):
assert count(Xor(x, y)) == XOR
assert count(Implies(x, y)) == IMPLIES
assert count(Equivalent(x, y)) == EQUIVALENT
assert count(ITE(x, y, z)) == ITE
assert count(ITE(x, y, z)) == _ITE
assert count([Or(x, y), And(x, y), Basic(x + y)]) == ADD + AND + BASIC + OR

assert count(Basic(Tuple(x))) == BASIC + TUPLE
Expand Down
4 changes: 2 additions & 2 deletions sympy/core/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_symbol_expand():


def test_function():
f = Function('f')
l, x = map(Symbol, 'lx')
f, l = map(Function, 'fl')
x = Symbol('x')
assert exp(l(x))*l(x)/exp(l(x)) == l(x)
assert exp(f(x))*f(x)/exp(f(x)) == f(x)
8 changes: 2 additions & 6 deletions sympy/core/tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,6 @@ def _eval_derivative(self, s):
assert diff(expr, f(x), x) == Derivative(expr, f(x), x)


def test_issue_4711():
x = Symbol("x")
assert Symbol('f')(x) == f(x)


def test_nfloat():
from sympy.core.basic import _aresame
from sympy.polys.rootoftools import RootOf
Expand Down Expand Up @@ -635,7 +630,8 @@ def test_nfloat():


def test_issue_7068():
from sympy.abc import a, b, f
from sympy.abc import a, b
f = Function('f')
y1 = Dummy('y')
y2 = Dummy('y')
func1 = f(a + y1 * b)
Expand Down
6 changes: 0 additions & 6 deletions sympy/core/tests/test_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,3 @@ def sym(s):
pytest.raises(ValueError, lambda: symbols('a::'))
pytest.raises(ValueError, lambda: symbols(':a:'))
pytest.raises(ValueError, lambda: symbols('::a'))


def test_call():
f = Symbol('f')
assert f(2)
pytest.raises(TypeError, lambda: Wild('x')(1))
5 changes: 3 additions & 2 deletions sympy/matrices/tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sympy import (Abs, E, Float, I, Integer, Max, Min, N, Poly, Pow,
PurePoly, Rational, S, Dummy, Symbol, cos, exp, oo, pi,
signsimp, simplify, sin, sqrt, symbols, sympify,
trigsimp, sstr)
trigsimp, sstr, Function)
from sympy.matrices.matrices import (ShapeError, MatrixError,
NonSquareMatrixError, DeferredVector)
from sympy.matrices import (
Expand Down Expand Up @@ -883,7 +883,8 @@ def test_subs():


def test_simplify():
f, n = symbols('f, n')
n = Symbol('n')
f = Function('f')

m = Matrix([[1, x], [x + 1/x, x - 1]])
m = m.row_join(eye(m.cols))
Expand Down
43 changes: 29 additions & 14 deletions sympy/parsing/sympy_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def _implicit_multiplication(tokens, local_dict, global_dict):
elif (isinstance(tok, AppliedFunction) and
nextTok[0] == OP and nextTok[1] == '('):
# Applied function followed by an open parenthesis
if tok.function[1] == "Function":
result[-1].function = (result[-1].function[0], 'Symbol')
result.append((OP, '*'))
elif (tok[0] == OP and tok[1] == ')' and
isinstance(nextTok, AppliedFunction)):
Expand Down Expand Up @@ -295,6 +297,8 @@ def function_exponentiation(tokens, local_dict, global_dict):
if _token_callable(tok, local_dict, global_dict):
consuming_exponent = True
elif consuming_exponent:
if tok[0] == NAME and tok[1] == 'Function':
tok = (NAME, 'Symbol')
exponent.append(tok)

# only want to stop after hitting )
Expand Down Expand Up @@ -356,25 +360,28 @@ def _split_symbols(tokens, local_dict, global_dict):
split_previous = False
continue
split_previous = False
if tok[0] == NAME and tok[1] == 'Symbol':
if tok[0] == NAME and tok[1] in ['Symbol', 'Function']:
split = True
elif split and tok[0] == NAME:
symbol = tok[1][1:-1]
if predicate(symbol):
for char in symbol:
tok_type = result[-2][1] # Symbol or Function
del result[-2:] # Get rid of the call to Symbol
for char in symbol[:-1]:
if char in local_dict or char in global_dict:
# Get rid of the call to Symbol
del result[-2:]
result.extend([(NAME, "%s" % char),
(NAME, 'Symbol'), (OP, '(')])
result.extend([(NAME, "%s" % char)])
else:
result.extend([(NAME, "'%s'" % char), (OP, ')'),
(NAME, 'Symbol'), (OP, '(')])
# Delete the last two tokens: get rid of the extraneous
# Symbol( we just added
# Also, set split_previous=True so will skip
result.extend([(NAME, 'Symbol'), (OP, '('),
(NAME, "'%s'" % char), (OP, ')')])
char = symbol[-1]
if char in local_dict or char in global_dict:
result.extend([(NAME, "%s" % char)])
else:
result.extend([(NAME, tok_type), (OP, '('),
(NAME, "'%s'" % char), (OP, ')')])

# Set split_previous=True so will skip
# the closing parenthesis of the original Symbol
del result[-2:]
split = False
split_previous = True
continue
Expand Down Expand Up @@ -488,22 +495,30 @@ def auto_symbol(tokens, local_dict, global_dict):

if (name in ['True', 'False', 'None']
or iskeyword(name)
or name in local_dict
# Don't convert attribute access
or (prevTok[0] == OP and prevTok[1] == '.')
# Don't convert keyword arguments
or (prevTok[0] == OP and prevTok[1] in ('(', ',')
and nextTokNum == OP and nextTokVal == '=')):
result.append((NAME, name))
continue
elif name in local_dict:
if isinstance(local_dict[name], Symbol) and nextTokVal == '(':
result.extend([(NAME, 'Function'),
(OP, '('),
(NAME, repr(str(local_dict[name]))),
(OP, ')')])
else:
result.append((NAME, name))
continue
elif name in global_dict:
obj = global_dict[name]
if isinstance(obj, (Basic, type)) or callable(obj):
result.append((NAME, name))
continue

result.extend([
(NAME, 'Symbol'),
(NAME, 'Symbol' if nextTokVal != '(' else 'Function'),
(OP, '('),
(NAME, repr(str(name))),
(OP, ')'),
Expand Down
4 changes: 3 additions & 1 deletion sympy/printing/pretty/tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4930,6 +4930,7 @@ def test_issue_8292():


def test_issue_4335():
y = Function('y')
expr = -y(x).diff(x)
ucode_str = \
"""\
Expand Down Expand Up @@ -5000,7 +5001,8 @@ def test_issue_7927():


def test_issue_6134():
from sympy.abc import lamda, phi, t
from sympy.abc import lamda, t
phi = Function('phi')

e = lamda*x*Integral(phi(t)*pi*sin(pi*t), (t, 0, 1)) + lamda*x**2*Integral(phi(t)*2*pi*sin(2*pi*t), (t, 0, 1))
ucode_str = \
Expand Down
4 changes: 3 additions & 1 deletion sympy/simplify/tests/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_factorial_simplify():


def test_simplify_expr():
x, y, z, k, n, m, w, f, s, A = symbols('x,y,z,k,n,m,w,f,s,A')
x, y, z, k, n, m, w, s, A = symbols('x,y,z,k,n,m,w,s,A')
f = Function('f')

assert all(simplify(tmp) == tmp for tmp in [I, E, oo, x, -x, -oo, -E, -I])

Expand Down Expand Up @@ -66,6 +67,7 @@ def test_simplify_expr():
e = integrate(x/(x**2 + 3*x + 1), x).diff(x)
assert simplify(e) == x/(x**2 + 3*x + 1)

f = Symbol('f')
A = Matrix([[2*k - m*w**2, -k], [-k, k - m*w**2]]).inv()
assert simplify((A*Matrix([0, f]))[1]) == \
-f*(2*k - m*w**2)/(k**2 - (k - m*w**2)*(2*k - m*w**2))
Expand Down
Loading

0 comments on commit adf60b5

Please sign in to comment.