# HG changeset patch
# User Golam Mortuza Hossain <gmhossain@gmail.com>
# Date 1245096292 10800
# Node ID a810ab7f1442ee2a3a198e506f30f68f9683fd1b
# Parent 58abaa4df2e26c9d272e262845af6e025a4d8fa0
Allow keyword arguments such as latex_name=LaTeX while defining symbolic functions
diff r 58abaa4df2e2 r a810ab7f1442 sage/calculus/calculus.py
a

b


1303  1303  """ 
1304  1304  return cmp(repr(x), repr(y)) 
1305  1305  
1306   def function(s, *args): 
 1306  def function(s, *args, **kwds): 
1307  1307  """ 
1308  1308  Create a formal symbolic function with the name *s*. 
1309  1309  
… 
… 

1344  1344  return s 
1345  1345  
1346  1346  if len(args) > 0: 
1347   return function(s)(*args) 
 1347  return function(s, **kwds)(*args) 
1348  1348  
1349  1349  s = str(s) 
1350  1350  if ',' in s: 
1351   return tuple([function(x.strip()) for x in s.split(',')]) 
 1351  return tuple([function(x.strip(), **kwds) for x in s.split(',')]) 
1352  1352  elif ' ' in s: 
1353   return tuple([function(x.strip()) for x in s.split()]) 
 1353  return tuple([function(x.strip(), **kwds) for x in s.split()]) 
1354  1354  try: 
1355  1355  return symbol_table['functions'][s] 
1356  1356  except KeyError: 
1357  1357  pass 
1358   f = SFunction(s) 
 1358  f = SFunction(s, **kwds) 
1359  1359  symbol_table['functions'][s] = f 
1360  1360  return f 
1361  1361  
diff r 58abaa4df2e2 r a810ab7f1442 sage/calculus/var.pyx
a

b


72  72  G[repr(v)] = v 
73  73  return v 
74  74  
75   def function(s, *args): 
76   """ 
 75  def function(s, *args, **kwds): 
 76  r""" 
77  77  Create a formal symbolic function with the name *s*. 
78  78  
79  79  INPUT: 
… 
… 

81  81   ``s``  a string, either a single variable name, or a space or 
82  82  comma separated list of variable names. 
83  83  
 84   ``**kwds``  keyword arguments. Either one of the following two 
 85  keywords can be used to customize latex representation of 
 86  symbolic functions: 
 87  
 88  (1) latex_name=LaTeX 
 89  where ``LaTeX`` is any valid latex expression. 
 90  Ex: f = function('f', x, latex_name="\\mathcal{F}") 
 91  See EXAMPLES for more. 
 92  
 93  (2) print_latex_func=my_latex_print 
 94  where ``my_latex_print`` is any callable function 
 95  that returns a valid latex expression. 
 96  Ex: f = function('f', x, print_latex_func=my_latex_print) 
 97  See EXAMPLES for an explicit usage. 
 98  
84  99  .. note:: 
85  100  
86  101  The new function is both returned and automatically injected 
… 
… 

112  127  sage: k = g.diff(x); k 
113  128  (x, y) > 2*supersin(x)*D[0](supersin)(x) 
114  129  
 130  Custom typesetting of symbolic functions in LaTeX:: 
 131  
 132  (1) Either using latex_name keyword:: 
 133  
 134  sage: riemann(x) = function('riemann', x, latex_name="\\mathcal{R}") 
 135  sage: latex(riemann(x)) 
 136  \mathcal{R}\left(x\right) 
 137  
 138  (2) Or passing a custom callable function that returns a 
 139  latex expression:: 
 140  
 141  sage: mu,nu = var('mu,nu') 
 142  sage: def my_latex_print(*args): return "\\psi_{%s}"%(', '.join(map(latex, args))) 
 143  sage: psi(mu,nu) = function('psi', mu, nu, print_latex_func=my_latex_print) 
 144  sage: latex(psi(mu,nu)) 
 145  \psi_{\mu, \nu} 
 146  
115  147  In Sage 4.0, you must now use the :meth:`substitute_function` 
116  148  method to replace functions:: 
117  149  
… 
… 

119  151  2*sin(x)*cos(x) 
120  152  """ 
121  153  if len(args) > 0: 
122   return function(s)(*args) 
 154  return function(s, **kwds)(*args) 
123  155  
124  156  G = globals() # this is the reason the code must be in Cython. 
125   v = calculus.function(s) 
 157  v = calculus.function(s, **kwds) 
126  158  if isinstance(v, tuple): 
127  159  for x in v: 
128  160  G[repr(x)] = x 