Skip to content

Commit

Permalink
Merge pull request #11637 from bjodah/expm1-log1p
Browse files Browse the repository at this point in the history
Expose C99/C++11 functions in printers
  • Loading branch information
asmeurer committed Mar 17, 2017
2 parents e287b31 + 3fd2dd4 commit 50b5818
Show file tree
Hide file tree
Showing 22 changed files with 1,516 additions and 239 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -69,3 +69,5 @@ sample.tex
# IPython Notebook Checkpoints
.ipynb_checkpoints/

# pytest cache folder
.cache
22 changes: 22 additions & 0 deletions conftest.py
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-

from __future__ import print_function, division, absolute_import

import pytest


def pytest_addoption(parser):
parser.addoption("--slow", dest="runslow", action="store_true",
help="allow slow tests to run")


def pytest_configure(config):
# register an additional marker
config.addinivalue_line("markers", "slow: slow test")


def pytest_runtest_setup(item):
if not isinstance(item, pytest.Function):
return
if not item.config.getvalue("runslow") and hasattr(item.obj, 'slow'):
pytest.skip("slow test: pass --slow to run")
21 changes: 17 additions & 4 deletions doc/src/modules/codegen.rst
Expand Up @@ -93,11 +93,18 @@ Here is a simple example of printing a C version of a SymPy expression::
-Z⋅e ⋅k
────────
2⋅r
>>> ccode(expr)
>>> ccode(expr, standard='C99')
-1.0L/2.0L*Z*pow(e, 2)*k/r
>>> ccode(expr, assign_to="E")
>>> ccode(expr, assign_to="E", standard='C99')
E = -1.0L/2.0L*Z*pow(e, 2)*k/r;

To generate code with some math functions provided by e.g. the C99 standard we need
to import functions from :mod:`sympy.codegen.cfunctions`::

>>> from sympy.codegen.cfunctions import expm1
>>> ccode(expm1(x), standard='C99')
expm1(x)

``Piecewise`` expressions are converted into conditionals. If an ``assign_to``
variable is provided an if statement is created, otherwise the ternary operator
is used. Note that if the ``Piecewise`` lacks a default term, represented by
Expand Down Expand Up @@ -186,7 +193,7 @@ how it works::
r
>>> print(jscode(expr, assign_to="H_is"))
H_is = I*S*gamma_1*gamma_2*k*(3*Math.pow(Math.cos(beta), 2) - 1)/Math.pow(r, 3);
>>> print(ccode(expr, assign_to="H_is"))
>>> print(ccode(expr, assign_to="H_is", standard='C89'))
H_is = I*S*gamma_1*gamma_2*k*(3*pow(cos(beta), 2) - 1)/pow(r, 3);
>>> print(fcode(expr, assign_to="H_is"))
H_is = I*S*gamma_1*gamma_2*k*(3*cos(beta)**2 - 1)/r**3
Expand Down Expand Up @@ -219,7 +226,7 @@ For instance::
>>> from sympy.utilities.codegen import codegen
>>> length, breadth, height = symbols('length, breadth, height')
>>> [(c_name, c_code), (h_name, c_header)] = \
... codegen(('volume', length*breadth*height), "C", "test",
... codegen(('volume', length*breadth*height), "C99", "test",
... header=False, empty=False)
>>> print(c_name)
test.c
Expand Down Expand Up @@ -543,3 +550,9 @@ available with ``autowrap``.

There are other facilities available with Sympy to do efficient numeric
computation. See :ref:`this<numeric_computation>` page for a comparison among them.

Special (finite precision arithmetic) math functions
----------------------------------------------------

.. automodule:: sympy.codegen.cfunctions
:members:
62 changes: 51 additions & 11 deletions doc/src/modules/printing.rst
Expand Up @@ -49,38 +49,78 @@ when possible.
.. autofunction:: pretty
.. autofunction:: pretty_print

CCodePrinter
------------
C code printers
---------------

.. module:: sympy.printing.ccode

This class implements C code printing (i.e. it converts Python expressions
to strings of C code).
This class implements C code printing, i.e. it converts Python expressions
to strings of C code (see also ``C89CodePrinter``).

Usage::

>>> from sympy.printing import print_ccode
>>> from sympy.functions import sin, cos, Abs
>>> from sympy.functions import sin, cos, Abs, gamma
>>> from sympy.abc import x
>>> print_ccode(sin(x)**2 + cos(x)**2)
>>> print_ccode(sin(x)**2 + cos(x)**2, standard='C89')
pow(sin(x), 2) + pow(cos(x), 2)
>>> print_ccode(2*x + cos(x), assign_to="result")
>>> print_ccode(2*x + cos(x), assign_to="result", standard='C89')
result = 2*x + cos(x);
>>> print_ccode(Abs(x**2))
>>> print_ccode(Abs(x**2), standard='C89')
fabs(pow(x, 2))
>>> print_ccode(gamma(x**2), standard='C99')
tgamma(pow(x, 2))

.. autodata:: sympy.printing.ccode.known_functions_C89
.. autodata:: sympy.printing.ccode.known_functions_C99

.. autodata:: sympy.printing.ccode.known_functions
.. autoclass:: sympy.printing.ccode.C89CodePrinter
:members:

.. autoattribute:: C89CodePrinter.printmethod

.. autoclass:: sympy.printing.ccode.CCodePrinter
.. autoclass:: sympy.printing.ccode.C99CodePrinter
:members:

.. autoattribute:: CCodePrinter.printmethod
.. autoattribute:: C99CodePrinter.printmethod


.. autofunction:: sympy.printing.ccode.ccode

.. autofunction:: sympy.printing.ccode.print_ccode

C++ code printers
-----------------

.. module:: sympy.printing.cxxcode

This module contains printers for C++ code, i.e. functions to convert
SymPy expressions to strings of C++ code.

Usage::

>>> from sympy.printing.cxxcode import cxxcode
>>> from sympy.functions import Min, gamma
>>> from sympy.abc import x
>>> print(cxxcode(Min(gamma(x) - 1, x), standard='C++11'))
std::min(x, std::tgamma(x) - 1)

.. autoclass:: sympy.printing.cxxcode.CXX98CodePrinter
:members:

.. autoattribute:: CXX98CodePrinter.printmethod


.. autoclass:: sympy.printing.cxxcode.CXX11CodePrinter
:members:

.. autoattribute:: CXX11CodePrinter.printmethod


.. autofunction:: sympy.printing.cxxcode.cxxcode



RCodePrinter
------------

Expand Down

0 comments on commit 50b5818

Please sign in to comment.