rcode.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. """
  2. R code printer
  3. The RCodePrinter converts single SymPy expressions into single R expressions,
  4. using the functions defined in math.h where possible.
  5. """
  6. from __future__ import annotations
  7. from typing import Any
  8. from sympy.core.numbers import equal_valued
  9. from sympy.printing.codeprinter import CodePrinter
  10. from sympy.printing.precedence import precedence, PRECEDENCE
  11. from sympy.sets.fancysets import Range
  12. # dictionary mapping SymPy function to (argument_conditions, C_function).
  13. # Used in RCodePrinter._print_Function(self)
  14. known_functions = {
  15. #"Abs": [(lambda x: not x.is_integer, "fabs")],
  16. "Abs": "abs",
  17. "sin": "sin",
  18. "cos": "cos",
  19. "tan": "tan",
  20. "asin": "asin",
  21. "acos": "acos",
  22. "atan": "atan",
  23. "atan2": "atan2",
  24. "exp": "exp",
  25. "log": "log",
  26. "erf": "erf",
  27. "sinh": "sinh",
  28. "cosh": "cosh",
  29. "tanh": "tanh",
  30. "asinh": "asinh",
  31. "acosh": "acosh",
  32. "atanh": "atanh",
  33. "floor": "floor",
  34. "ceiling": "ceiling",
  35. "sign": "sign",
  36. "Max": "max",
  37. "Min": "min",
  38. "factorial": "factorial",
  39. "gamma": "gamma",
  40. "digamma": "digamma",
  41. "trigamma": "trigamma",
  42. "beta": "beta",
  43. "sqrt": "sqrt", # To enable automatic rewrite
  44. }
  45. # These are the core reserved words in the R language. Taken from:
  46. # https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Reserved-words
  47. reserved_words = ['if',
  48. 'else',
  49. 'repeat',
  50. 'while',
  51. 'function',
  52. 'for',
  53. 'in',
  54. 'next',
  55. 'break',
  56. 'TRUE',
  57. 'FALSE',
  58. 'NULL',
  59. 'Inf',
  60. 'NaN',
  61. 'NA',
  62. 'NA_integer_',
  63. 'NA_real_',
  64. 'NA_complex_',
  65. 'NA_character_',
  66. 'volatile']
  67. class RCodePrinter(CodePrinter):
  68. """A printer to convert SymPy expressions to strings of R code"""
  69. printmethod = "_rcode"
  70. language = "R"
  71. _default_settings: dict[str, Any] = dict(CodePrinter._default_settings, **{
  72. 'precision': 15,
  73. 'user_functions': {},
  74. 'contract': True,
  75. 'dereference': set(),
  76. })
  77. _operators = {
  78. 'and': '&',
  79. 'or': '|',
  80. 'not': '!',
  81. }
  82. _relationals: dict[str, str] = {}
  83. def __init__(self, settings={}):
  84. CodePrinter.__init__(self, settings)
  85. self.known_functions = dict(known_functions)
  86. userfuncs = settings.get('user_functions', {})
  87. self.known_functions.update(userfuncs)
  88. self._dereference = set(settings.get('dereference', []))
  89. self.reserved_words = set(reserved_words)
  90. def _rate_index_position(self, p):
  91. return p*5
  92. def _get_statement(self, codestring):
  93. return "%s;" % codestring
  94. def _get_comment(self, text):
  95. return "// {}".format(text)
  96. def _declare_number_const(self, name, value):
  97. return "{} = {};".format(name, value)
  98. def _format_code(self, lines):
  99. return self.indent_code(lines)
  100. def _traverse_matrix_indices(self, mat):
  101. rows, cols = mat.shape
  102. return ((i, j) for i in range(rows) for j in range(cols))
  103. def _get_loop_opening_ending(self, indices):
  104. """Returns a tuple (open_lines, close_lines) containing lists of codelines
  105. """
  106. open_lines = []
  107. close_lines = []
  108. loopstart = "for (%(var)s in %(start)s:%(end)s){"
  109. for i in indices:
  110. # R arrays start at 1 and end at dimension
  111. open_lines.append(loopstart % {
  112. 'var': self._print(i.label),
  113. 'start': self._print(i.lower+1),
  114. 'end': self._print(i.upper + 1)})
  115. close_lines.append("}")
  116. return open_lines, close_lines
  117. def _print_Pow(self, expr):
  118. if "Pow" in self.known_functions:
  119. return self._print_Function(expr)
  120. PREC = precedence(expr)
  121. if equal_valued(expr.exp, -1):
  122. return '1.0/%s' % (self.parenthesize(expr.base, PREC))
  123. elif equal_valued(expr.exp, 0.5):
  124. return 'sqrt(%s)' % self._print(expr.base)
  125. else:
  126. return '%s^%s' % (self.parenthesize(expr.base, PREC),
  127. self.parenthesize(expr.exp, PREC))
  128. def _print_Rational(self, expr):
  129. p, q = int(expr.p), int(expr.q)
  130. return '%d.0/%d.0' % (p, q)
  131. def _print_Indexed(self, expr):
  132. inds = [ self._print(i) for i in expr.indices ]
  133. return "%s[%s]" % (self._print(expr.base.label), ", ".join(inds))
  134. def _print_Exp1(self, expr):
  135. return "exp(1)"
  136. def _print_Pi(self, expr):
  137. return 'pi'
  138. def _print_Infinity(self, expr):
  139. return 'Inf'
  140. def _print_NegativeInfinity(self, expr):
  141. return '-Inf'
  142. def _print_Assignment(self, expr):
  143. from sympy.codegen.ast import Assignment
  144. from sympy.matrices.expressions.matexpr import MatrixSymbol
  145. from sympy.tensor.indexed import IndexedBase
  146. lhs = expr.lhs
  147. rhs = expr.rhs
  148. # We special case assignments that take multiple lines
  149. #if isinstance(expr.rhs, Piecewise):
  150. # from sympy.functions.elementary.piecewise import Piecewise
  151. # # Here we modify Piecewise so each expression is now
  152. # # an Assignment, and then continue on the print.
  153. # expressions = []
  154. # conditions = []
  155. # for (e, c) in rhs.args:
  156. # expressions.append(Assignment(lhs, e))
  157. # conditions.append(c)
  158. # temp = Piecewise(*zip(expressions, conditions))
  159. # return self._print(temp)
  160. #elif isinstance(lhs, MatrixSymbol):
  161. if isinstance(lhs, MatrixSymbol):
  162. # Here we form an Assignment for each element in the array,
  163. # printing each one.
  164. lines = []
  165. for (i, j) in self._traverse_matrix_indices(lhs):
  166. temp = Assignment(lhs[i, j], rhs[i, j])
  167. code0 = self._print(temp)
  168. lines.append(code0)
  169. return "\n".join(lines)
  170. elif self._settings["contract"] and (lhs.has(IndexedBase) or
  171. rhs.has(IndexedBase)):
  172. # Here we check if there is looping to be done, and if so
  173. # print the required loops.
  174. return self._doprint_loops(rhs, lhs)
  175. else:
  176. lhs_code = self._print(lhs)
  177. rhs_code = self._print(rhs)
  178. return self._get_statement("%s = %s" % (lhs_code, rhs_code))
  179. def _print_Piecewise(self, expr):
  180. # This method is called only for inline if constructs
  181. # Top level piecewise is handled in doprint()
  182. if expr.args[-1].cond == True:
  183. last_line = "%s" % self._print(expr.args[-1].expr)
  184. else:
  185. last_line = "ifelse(%s,%s,NA)" % (self._print(expr.args[-1].cond), self._print(expr.args[-1].expr))
  186. code=last_line
  187. for e, c in reversed(expr.args[:-1]):
  188. code= "ifelse(%s,%s," % (self._print(c), self._print(e))+code+")"
  189. return(code)
  190. def _print_ITE(self, expr):
  191. from sympy.functions import Piecewise
  192. return self._print(expr.rewrite(Piecewise))
  193. def _print_MatrixElement(self, expr):
  194. return "{}[{}]".format(self.parenthesize(expr.parent, PRECEDENCE["Atom"],
  195. strict=True), expr.j + expr.i*expr.parent.shape[1])
  196. def _print_Symbol(self, expr):
  197. name = super()._print_Symbol(expr)
  198. if expr in self._dereference:
  199. return '(*{})'.format(name)
  200. else:
  201. return name
  202. def _print_Relational(self, expr):
  203. lhs_code = self._print(expr.lhs)
  204. rhs_code = self._print(expr.rhs)
  205. op = expr.rel_op
  206. return "{} {} {}".format(lhs_code, op, rhs_code)
  207. def _print_AugmentedAssignment(self, expr):
  208. lhs_code = self._print(expr.lhs)
  209. op = expr.op
  210. rhs_code = self._print(expr.rhs)
  211. return "{} {} {};".format(lhs_code, op, rhs_code)
  212. def _print_For(self, expr):
  213. target = self._print(expr.target)
  214. if isinstance(expr.iterable, Range):
  215. start, stop, step = expr.iterable.args
  216. else:
  217. raise NotImplementedError("Only iterable currently supported is Range")
  218. body = self._print(expr.body)
  219. return 'for({target} in seq(from={start}, to={stop}, by={step}){{\n{body}\n}}'.format(target=target, start=start,
  220. stop=stop-1, step=step, body=body)
  221. def indent_code(self, code):
  222. """Accepts a string of code or a list of code lines"""
  223. if isinstance(code, str):
  224. code_lines = self.indent_code(code.splitlines(True))
  225. return ''.join(code_lines)
  226. tab = " "
  227. inc_token = ('{', '(', '{\n', '(\n')
  228. dec_token = ('}', ')')
  229. code = [ line.lstrip(' \t') for line in code ]
  230. increase = [ int(any(map(line.endswith, inc_token))) for line in code ]
  231. decrease = [ int(any(map(line.startswith, dec_token)))
  232. for line in code ]
  233. pretty = []
  234. level = 0
  235. for n, line in enumerate(code):
  236. if line in ('', '\n'):
  237. pretty.append(line)
  238. continue
  239. level -= decrease[n]
  240. pretty.append("%s%s" % (tab*level, line))
  241. level += increase[n]
  242. return pretty
  243. def rcode(expr, assign_to=None, **settings):
  244. """Converts an expr to a string of r code
  245. Parameters
  246. ==========
  247. expr : Expr
  248. A SymPy expression to be converted.
  249. assign_to : optional
  250. When given, the argument is used as the name of the variable to which
  251. the expression is assigned. Can be a string, ``Symbol``,
  252. ``MatrixSymbol``, or ``Indexed`` type. This is helpful in case of
  253. line-wrapping, or for expressions that generate multi-line statements.
  254. precision : integer, optional
  255. The precision for numbers such as pi [default=15].
  256. user_functions : dict, optional
  257. A dictionary where the keys are string representations of either
  258. ``FunctionClass`` or ``UndefinedFunction`` instances and the values
  259. are their desired R string representations. Alternatively, the
  260. dictionary value can be a list of tuples i.e. [(argument_test,
  261. rfunction_string)] or [(argument_test, rfunction_formater)]. See below
  262. for examples.
  263. human : bool, optional
  264. If True, the result is a single string that may contain some constant
  265. declarations for the number symbols. If False, the same information is
  266. returned in a tuple of (symbols_to_declare, not_supported_functions,
  267. code_text). [default=True].
  268. contract: bool, optional
  269. If True, ``Indexed`` instances are assumed to obey tensor contraction
  270. rules and the corresponding nested loops over indices are generated.
  271. Setting contract=False will not generate loops, instead the user is
  272. responsible to provide values for the indices in the code.
  273. [default=True].
  274. Examples
  275. ========
  276. >>> from sympy import rcode, symbols, Rational, sin, ceiling, Abs, Function
  277. >>> x, tau = symbols("x, tau")
  278. >>> rcode((2*tau)**Rational(7, 2))
  279. '8*sqrt(2)*tau^(7.0/2.0)'
  280. >>> rcode(sin(x), assign_to="s")
  281. 's = sin(x);'
  282. Simple custom printing can be defined for certain types by passing a
  283. dictionary of {"type" : "function"} to the ``user_functions`` kwarg.
  284. Alternatively, the dictionary value can be a list of tuples i.e.
  285. [(argument_test, cfunction_string)].
  286. >>> custom_functions = {
  287. ... "ceiling": "CEIL",
  288. ... "Abs": [(lambda x: not x.is_integer, "fabs"),
  289. ... (lambda x: x.is_integer, "ABS")],
  290. ... "func": "f"
  291. ... }
  292. >>> func = Function('func')
  293. >>> rcode(func(Abs(x) + ceiling(x)), user_functions=custom_functions)
  294. 'f(fabs(x) + CEIL(x))'
  295. or if the R-function takes a subset of the original arguments:
  296. >>> rcode(2**x + 3**x, user_functions={'Pow': [
  297. ... (lambda b, e: b == 2, lambda b, e: 'exp2(%s)' % e),
  298. ... (lambda b, e: b != 2, 'pow')]})
  299. 'exp2(x) + pow(3, x)'
  300. ``Piecewise`` expressions are converted into conditionals. If an
  301. ``assign_to`` variable is provided an if statement is created, otherwise
  302. the ternary operator is used. Note that if the ``Piecewise`` lacks a
  303. default term, represented by ``(expr, True)`` then an error will be thrown.
  304. This is to prevent generating an expression that may not evaluate to
  305. anything.
  306. >>> from sympy import Piecewise
  307. >>> expr = Piecewise((x + 1, x > 0), (x, True))
  308. >>> print(rcode(expr, assign_to=tau))
  309. tau = ifelse(x > 0,x + 1,x);
  310. Support for loops is provided through ``Indexed`` types. With
  311. ``contract=True`` these expressions will be turned into loops, whereas
  312. ``contract=False`` will just print the assignment expression that should be
  313. looped over:
  314. >>> from sympy import Eq, IndexedBase, Idx
  315. >>> len_y = 5
  316. >>> y = IndexedBase('y', shape=(len_y,))
  317. >>> t = IndexedBase('t', shape=(len_y,))
  318. >>> Dy = IndexedBase('Dy', shape=(len_y-1,))
  319. >>> i = Idx('i', len_y-1)
  320. >>> e=Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
  321. >>> rcode(e.rhs, assign_to=e.lhs, contract=False)
  322. 'Dy[i] = (y[i + 1] - y[i])/(t[i + 1] - t[i]);'
  323. Matrices are also supported, but a ``MatrixSymbol`` of the same dimensions
  324. must be provided to ``assign_to``. Note that any expression that can be
  325. generated normally can also exist inside a Matrix:
  326. >>> from sympy import Matrix, MatrixSymbol
  327. >>> mat = Matrix([x**2, Piecewise((x + 1, x > 0), (x, True)), sin(x)])
  328. >>> A = MatrixSymbol('A', 3, 1)
  329. >>> print(rcode(mat, A))
  330. A[0] = x^2;
  331. A[1] = ifelse(x > 0,x + 1,x);
  332. A[2] = sin(x);
  333. """
  334. return RCodePrinter(settings).doprint(expr, assign_to)
  335. def print_rcode(expr, **settings):
  336. """Prints R representation of the given expression."""
  337. print(rcode(expr, **settings))