File size: 23,683 Bytes
b66f126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
.. _codegen_prose:

===============
Code Generation
===============

Several submodules in SymPy allow one to generate directly compilable and
executable code in a variety of different programming languages from SymPy
expressions. In addition, there are functions that generate Python importable
objects that can evaluate SymPy expressions very efficiently.

We will start with a brief introduction to the components that make up the code
generation capabilities of SymPy.

Introduction
------------

There are four main levels of abstractions::

   expression
      |
   code printers
      |
   code generators
      |
   autowrap

:mod:`sympy.utilities.autowrap` uses codegen, and codegen uses the code
printers. :mod:`sympy.utilities.autowrap` does everything: it lets you go
from SymPy expression to numerical function in the same Python process in one
step. Codegen is actual code generation, i.e., to compile and use later, or to
include in some larger project.

The code printers translate the SymPy objects into actual code, like ``abs(x)
-> fabs(x)`` (for C).

The code printers don't print optimal code in many cases. An example of this is
powers in C. ``x**2`` prints as ``pow(x, 2)`` instead of ``x*x``.  Other
optimizations (like mathematical simplifications) should happen before the code
printers.

Currently, :py:func:`sympy.simplify.cse_main.cse` is not applied automatically anywhere in this
chain. It ideally happens at the codegen level, or somewhere above it.

We will iterate through the levels below.

The following three lines will be used to setup each example::

    >>> from sympy import *
    >>> init_printing(use_unicode=True)
    >>> from sympy.abc import a, e, k, n, r, t, x, y, z, T, Z
    >>> from sympy.abc import beta, omega, tau
    >>> f, g = symbols('f, g', cls=Function)

Code printers (sympy.printing)
------------------------------

This is where the meat of code generation is; the translation of SymPy
actually more like a lightweight version of codegen for Python, and
Python (:py:func:`sympy.printing.pycode.pycode`), and
:py:func:`sympy.printing.lambdarepr.lambdarepr`, which supports many libraries
(like NumPy). The code printers are special cases of the other prints
in SymPy (str printer, pretty printer, etc.).

An important distinction is that the code printer has to deal with assignments
(using the :class:`sympy.codegen.ast.Assignment` object). This serves as
building blocks for the code printers and hence the ``codegen`` module.  An
example that shows the use of ``Assignment`` in C code::

    >>> from sympy.codegen.ast import Assignment
    >>> print(ccode(Assignment(x, y + 1)))
    x = y + 1;

Here is another simple example of printing a C version of a SymPy expression::

    >>> expr = (Rational(-1, 2) * Z * k * (e**2) / r)
    >>> expr
        2
    -Zโ‹…e โ‹…k
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
      2โ‹…r
    >>> ccode(expr)
    -1.0/2.0*Z*pow(e, 2)*k/r
    >>> from sympy.codegen.ast import real, float80
    >>> ccode(expr, assign_to="E", type_aliases={real: float80})
    E = -1.0L/2.0L*Z*powl(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
``(expr, True)`` then an error will be thrown.  This is to prevent generating
an expression that may not evaluate to anything. A use case for ``Piecewise``::

    >>> expr = Piecewise((x + 1, x > 0), (x, True))
    >>> print(fcode(expr, tau))
          if (x > 0) then
             tau = x + 1
          else
             tau = x
          end if

The various printers also tend to support ``Indexed`` objects well. With
``contract=True`` these expressions will be turned into loops, whereas
``contract=False`` will just print the assignment expression that should be
looped over::

    >>> len_y = 5
    >>> mat_1 = IndexedBase('mat_1', shape=(len_y,))
    >>> mat_2 = IndexedBase('mat_2', shape=(len_y,))
    >>> Dy = IndexedBase('Dy', shape=(len_y-1,))
    >>> i = Idx('i', len_y-1)
    >>> eq = Eq(Dy[i], (mat_1[i+1] - mat_1[i]) / (mat_2[i+1] - mat_2[i]))
    >>> print(jscode(eq.rhs, assign_to=eq.lhs, contract=False))
    Dy[i] = (mat_1[i + 1] - mat_1[i])/(mat_2[i + 1] - mat_2[i]);
    >>> Res = IndexedBase('Res', shape=(len_y,))
    >>> j = Idx('j', len_y)
    >>> eq = Eq(Res[j], mat_1[j]*mat_2[j])
    >>> print(jscode(eq.rhs, assign_to=eq.lhs, contract=True))
    for (var j=0; j<5; j++){
       Res[j] = 0;
    }
    for (var j=0; j<5; j++){
       for (var j=0; j<5; j++){
          Res[j] = Res[j] + mat_1[j]*mat_2[j];
       }
    }
    >>> print(jscode(eq.rhs, assign_to=eq.lhs, contract=False))
    Res[j] = mat_1[j]*mat_2[j];


Custom printing can be defined for certain types by passing a dictionary of
"type" : "function" to the ``user_functions`` kwarg. Alternatively, the
dictionary value can be a list of tuples i.e., ``[(argument_test,
cfunction_string)]``. This can be used to call a custom Octave function::

    >>> custom_functions = {
    ...   "f": "existing_octave_fcn",
    ...   "g": [(lambda x: x.is_Matrix, "my_mat_fcn"),
    ...         (lambda x: not x.is_Matrix, "my_fcn")]
    ... }
    >>> mat = Matrix([[1, x]])
    >>> octave_code(f(x) + g(x) + g(mat), user_functions=custom_functions)
    existing_octave_fcn(x) + my_fcn(x) + my_mat_fcn([1 x])

An example of Mathematica code printer::

    >>> x_ = Function('x')
    >>> expr = x_(n*T) * sin((t - n*T) / T)
    >>> expr = expr / ((-T*n + t) / T)
    >>> expr
                โŽ›-Tโ‹…n + tโŽž
    Tโ‹…x(Tโ‹…n)โ‹…sinโŽœโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โŽŸ
                โŽ   T    โŽ 
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
           -Tโ‹…n + t

    >>> expr = summation(expr, (n, -1, 1))
    >>> mathematica_code(expr)
    T*(x[-T]*Sin[(T + t)/T]/(T + t) + x[T]*Sin[(-T + t)/T]/(-T + t) + x[0]*Sin[t/T]/t)

We can go through a common expression in different languages we support and see
how it works::

    >>> k, g1, g2, r, I, S = symbols("k, gamma_1, gamma_2, r, I, S")
    >>> expr = k * g1 * g2 / (r**3)
    >>> expr = expr * 2 * I * S * (3 * (cos(beta))**2 - 1) / 2
    >>> expr
                โŽ›     2       โŽž
    Iโ‹…Sโ‹…ฮณโ‚โ‹…ฮณโ‚‚โ‹…kโ‹…โŽ3โ‹…cos (ฮฒ) - 1โŽ 
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                  3
                 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", 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
    >>> print(julia_code(expr, assign_to="H_is"))
    H_is = I .* S .* gamma_1 .* gamma_2 .* k .* (3 * cos(beta) .^ 2 - 1) ./ r .^ 3
    >>> print(octave_code(expr, assign_to="H_is"))
    H_is = I.*S.*gamma_1.*gamma_2.*k.*(3*cos(beta).^2 - 1)./r.^3;
    >>> print(rust_code(expr, assign_to="H_is"))
    H_is = I*S*gamma_1*gamma_2*k*(3*beta.cos().powi(2) - 1)/r.powi(3);
    >>> print(mathematica_code(expr))
    I*S*gamma_1*gamma_2*k*(3*Cos[beta]^2 - 1)/r^3

Codegen (sympy.utilities.codegen)
---------------------------------

This module deals with creating compilable code from SymPy expressions. This is
lower level than autowrap, as it doesn't actually attempt to compile the code,
but higher level than the printers, as it generates compilable files (including
header files), rather than just code snippets.

The user friendly functions, here, are ``codegen`` and ``make_routine``.
``codegen`` takes a list of ``(variable, expression)`` pairs and a language (C,
F95, and Octave/Matlab are supported). It returns, as strings, a code file and
a header file (for relevant languages). The variables are created as functions
that return the value of the expression as output.

.. note:: The ``codegen`` callable is not in the sympy namespace automatically,
   to use it you must first import ``codegen`` from ``sympy.utilities.codegen``

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), "C99", "test",
    ...         header=False, empty=False)
    >>> print(c_name)
    test.c
    >>> print(c_code)
    #include "test.h"
    #include <math.h>
    double volume(double breadth, double height, double length) {
       double volume_result;
       volume_result = breadth*height*length;
       return volume_result;
    }
    >>> print(h_name)
    test.h
    >>> print(c_header)
    #ifndef PROJECT__TEST__H
    #define PROJECT__TEST__H
    double volume(double breadth, double height, double length);
    #endif

Various flags to ``codegen`` let you modify things. The project name for
preprocessor instructions can be varied using ``project``. Variables listed as
global variables in arg ``global_vars`` will not show up as function arguments.

``language`` is a case-insensitive string that indicates the source code
language. Currently, ``C``, ``F95`` and ``Octave`` are supported. ``Octave``
generates code compatible with both Octave and Matlab.

``header`` when True, a header is written on top of each source file. ``empty``
when True, empty lines are used to structure the code. With
``argument_sequence`` a sequence of arguments for the routine can be defined in
a preferred order.

``prefix`` defines a prefix for the names of the files that contain the source
code.  If omitted, the name of the first name_expr tuple is used.

``to_files`` when True, the code will be written to one or more files with the
given prefix.

Here is an example::

    >>> [(f_name, f_code), header] = codegen(("volume", length*breadth*height),
    ...     "F95", header=False, empty=False, argument_sequence=(breadth, length),
    ...     global_vars=(height,))
    >>> print(f_code)
    REAL*8 function volume(breadth, length)
    implicit none
    REAL*8, intent(in) :: breadth
    REAL*8, intent(in) :: length
    volume = breadth*height*length
    end function

The method ``make_routine`` creates a ``Routine`` object, which represents an
evaluation routine for a set of expressions. This is only good for internal use
by the CodeGen objects, as an intermediate representation from SymPy expression
to generated code.  It is not recommended to make a ``Routine`` object
yourself. You should instead use ``make_routine`` method. ``make_routine`` in
turn calls the ``routine`` method of the CodeGen object depending upon the
language of choice. This creates the internal objects representing assignments
and so on, and creates the ``Routine`` class with them.

The various codegen objects such as ``Routine`` and ``Variable`` aren't SymPy
objects (they don't subclass from Basic).

For example::

    >>> from sympy.utilities.codegen import make_routine
    >>> from sympy.physics.hydrogen import R_nl
    >>> expr = R_nl(3, y, x, 6)
    >>> routine = make_routine('my_routine', expr)
    >>> [arg.result_var for arg in routine.results]   # doctest: +SKIP
    [resultโ‚…โ‚โ‚„โ‚‚โ‚ƒโ‚„โ‚โ‚†โ‚ˆโ‚โ‚ƒโ‚‰โ‚‡โ‚‡โ‚โ‚‰โ‚„โ‚‚โ‚ˆ]
    >>> [arg.expr for arg in routine.results]
    โŽก                __________                                          โŽค
    โŽข          y    โ•ฑ (2 - y)!   -2โ‹…x                                    โŽฅ
    โŽข4โ‹…โˆš6โ‹…(4โ‹…x) โ‹…  โ•ฑ  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ‹…โ„ฏ    โ‹…assoc_laguerre(2 - y, 2โ‹…y + 1, 4โ‹…x)โŽฅ
    โŽข            โ•ฒโ•ฑ   (y + 3)!                                           โŽฅ
    โŽขโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โŽฅ
    โŽฃ                                 3                                  โŽฆ
    >>> [arg.name for arg in routine.arguments]
    [x, y]

Another more complicated example with a mixture of specified and
automatically-assigned names.  Also has Matrix output::

    >>> routine = make_routine('fcn', [x*y, Eq(a, 1), Eq(r, x + r), Matrix([[x, 2]])])
    >>> [arg.result_var for arg in routine.results]   # doctest: +SKIP
    [result_5397460570204848505]
    >>> [arg.expr for arg in routine.results]
    [xโ‹…y]
    >>> [arg.name for arg in routine.arguments]   # doctest: +SKIP
    [x, y, a, r, out_8598435338387848786]

We can examine the various arguments more closely::

    >>> from sympy.utilities.codegen import (InputArgument, OutputArgument,
    ...                                      InOutArgument)
    >>> [a.name for a in routine.arguments if isinstance(a, InputArgument)]
    [x, y]

    >>> [a.name for a in routine.arguments if isinstance(a, OutputArgument)]  # doctest: +SKIP
    [a, out_8598435338387848786]
    >>> [a.expr for a in routine.arguments if isinstance(a, OutputArgument)]
    [1, [x  2]]

    >>> [a.name for a in routine.arguments if isinstance(a, InOutArgument)]
    [r]
    >>> [a.expr for a in routine.arguments if isinstance(a, InOutArgument)]
    [r + x]

The full API reference can be viewed :ref:`here<codegen_API>`.

Autowrap
--------

Autowrap automatically generates code, writes it to disk, compiles it, and
imports it into the current session. Main functions of this module are
``autowrap``, ``binary_function``, and ``ufuncify``.

It also automatically converts expressions containing ``Indexed`` objects into
summations. The classes IndexedBase, Indexed and Idx represent a matrix element
M[i, j]. See :ref:`tensor_module` for more on this.

.. _autowrap:

``autowrap`` creates a wrapper using f2py or Cython and creates a numerical function.

.. note:: The ``autowrap`` callable is not in the sympy namespace automatically,
   to use it you must first import ``autowrap`` from ``sympy.utilities.autowrap``

The callable returned from autowrap() is a binary Python function, not a SymPy
object. For example::

    >>> from sympy.utilities.autowrap import autowrap
    >>> expr = ((x - y + z)**(13)).expand()
    >>> binary_func = autowrap(expr)    # doctest: +SKIP
    >>> binary_func(1, 4, 2)    # doctest: +SKIP
    -1.0

The various flags available with autowrap() help to modify the services
provided by the method. The argument ``tempdir`` tells autowrap to compile the
code in a specific directory, and leave the files intact when finished. For
instance::

    >>> from sympy.utilities.autowrap import autowrap
    >>> from sympy.physics.qho_1d import psi_n
    >>> x_ = IndexedBase('x')
    >>> y_ = IndexedBase('y')
    >>> m = symbols('m', integer=True)
    >>> i = Idx('i', m)
    >>> qho = autowrap(Eq(y_[i], psi_n(0, x_[i], m, omega)), tempdir='/tmp')  # doctest: +SKIP

Checking the Fortran source code in the directory specified reveals this::

    subroutine autofunc(m, omega, x, y)
    implicit none
    INTEGER*4, intent(in) :: m
    REAL*8, intent(in) :: omega
    REAL*8, intent(in), dimension(1:m) :: x
    REAL*8, intent(out), dimension(1:m) :: y
    INTEGER*4 :: i

    REAL*8, parameter :: hbar = 1.05457162d-34
    REAL*8, parameter :: pi = 3.14159265358979d0
    do i = 1, m
       y(i) = (m*omega)**(1.0d0/4.0d0)*exp(-4.74126166983329d+33*m*omega*x(i &
             )**2)/(hbar**(1.0d0/4.0d0)*pi**(1.0d0/4.0d0))
    end do

    end subroutine

Using the argument ``args`` along with it changes argument sequence::

    >>> eq = Eq(y_[i], psi_n(0, x_[i], m, omega))
    >>> qho = autowrap(eq, tempdir='/tmp', args=[y, x, m, omega])  # doctest: +SKIP

yields::

    subroutine autofunc(y, x, m, omega)
    implicit none
    INTEGER*4, intent(in) :: m
    REAL*8, intent(in) :: omega
    REAL*8, intent(out), dimension(1:m) :: y
    REAL*8, intent(in), dimension(1:m) :: x
    INTEGER*4 :: i

    REAL*8, parameter :: hbar = 1.05457162d-34
    REAL*8, parameter :: pi = 3.14159265358979d0
    do i = 1, m
       y(i) = (m*omega)**(1.0d0/4.0d0)*exp(-4.74126166983329d+33*m*omega*x(i &
             )**2)/(hbar**(1.0d0/4.0d0)*pi**(1.0d0/4.0d0))
    end do

    end subroutine

The argument ``verbose`` is boolean, optional and if True, autowrap will not
mute the command line backends. This can be helpful for debugging.

The argument ``language`` and ``backend`` are used to change defaults:
``Fortran`` and ``f2py`` to ``C`` and ``Cython``. The argument helpers is used
to define auxiliary expressions needed for the main expression. If the main
expression needs to call a specialized function it should be put in the
``helpers`` iterable. Autowrap will then make sure that the compiled main
expression can link to the helper routine. Items should be tuples with
``(<function_name>, <sympy_expression>, <arguments>)``. It is mandatory to
supply an argument sequence to helper routines.

.. _binary_function:

Another method available at the ``autowrap`` level is ``binary_function``. It
returns a sympy function. The advantage is that we can have very fast functions
as compared to SymPy speeds. This is because we will be using compiled
functions with SymPy attributes and methods. An illustration::

    >>> from sympy.utilities.autowrap import binary_function
    >>> from sympy.physics.hydrogen import R_nl
    >>> psi_nl = R_nl(1, 0, a, r)
    >>> f = binary_function('f', psi_nl)    # doctest: +SKIP
    >>> f(a, r).evalf(3, subs={a: 1, r: 2})  # doctest: +SKIP
    0.766

.. _ufuncify_method:

While NumPy operations are very efficient for vectorized data but they
sometimes incur unnecessary costs when chained together.
Consider the following operation

    >>> x = get_numpy_array(...) # doctest: +SKIP
    >>> y = sin(x) / x

The operators ``sin`` and ``/`` call routines that execute tight for loops in
``C``. The resulting computation looks something like this

.. code:: c

    for(int i = 0; i < n; i++)
    {
        temp[i] = sin(x[i]);
    }
    for(int i = i; i < n; i++)
    {
        y[i] = temp[i] / x[i];
    }

This is slightly sub-optimal because

1.  We allocate an extra ``temp`` array
2.  We walk over ``x`` memory twice when once would have been sufficient

A better solution would fuse both element-wise operations into a single for loop

.. code:: c

    for(int i = i; i < n; i++)
    {
        y[i] = sin(x[i]) / x[i];
    }

Statically compiled projects like NumPy are unable to take advantage of such
optimizations. Fortunately, SymPy is able to generate efficient low-level C
or Fortran code. It can then depend on projects like ``Cython`` or ``f2py`` to
compile and reconnect that code back up to Python. Fortunately this process is
well automated and a SymPy user wishing to make use of this code generation
should call the ``ufuncify`` function.

``ufuncify`` is the third method available with Autowrap module. It basically
implies 'Universal functions' and follows an ideology set by NumPy. The main
point of ufuncify as compared to autowrap is that it allows arrays as arguments
and can operate in an element-by-element fashion. The core operation done
element-wise is in accordance to Numpy's array broadcasting rules. See `this
<https://numpy.org/doc/stable/reference/ufuncs.html>`_ for more.

    >>> from sympy import *
    >>> from sympy.abc import x
    >>> expr = sin(x)/x

    >>> from sympy.utilities.autowrap import ufuncify
    >>> f = ufuncify([x], expr) # doctest: +SKIP

This function ``f`` consumes and returns a NumPy array. Generally ``ufuncify``
performs at least as well as ``lambdify``. If the expression is complicated
then ``ufuncify`` often significantly outperforms the NumPy backed solution.
Jensen has a good `blog post <https://ojensen.wordpress.com/2010/08/10/fast-ufunc-ish-hydrogen-solutions/>`_ on this topic.

Let us see an example for some quantitative analysis::

    >>> from sympy.physics.hydrogen import R_nl
    >>> expr = R_nl(3, 1, x, 6)
    >>> expr
                   -2โ‹…x
    8โ‹…xโ‹…(4 - 4โ‹…x)โ‹…โ„ฏ
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
             3

The lambdify function translates SymPy expressions into Python functions,
leveraging a variety of numerical libraries. By default lambdify relies on
implementations in the ``math`` standard library. Naturally, Raw Python is
faster than SymPy. However it also supports ``mpmath`` and most notably,
``numpy``. Using the NumPy library gives the generated function access to
powerful vectorized ufuncs that are backed by compiled C code.

Let us compare the speeds::

    >>> from sympy.utilities.autowrap import ufuncify
    >>> from sympy.utilities.lambdify import lambdify
    >>> fn_numpy = lambdify(x, expr, 'numpy')   # doctest: +SKIP
    >>> fn_fortran = ufuncify([x], expr, backend='f2py')    # doctest: +SKIP
    >>> from numpy import linspace  # doctest: +SKIP
    >>> xx = linspace(0, 1, 5)  # doctest: +SKIP
    >>> fn_numpy(xx)    # doctest: +SKIP
    [ 0.          1.21306132  0.98101184  0.44626032  0.        ]
    >>> fn_fortran(xx)  # doctest: +SKIP
    [ 0.          1.21306132  0.98101184  0.44626032  0.        ]
    >>> import timeit
    >>> timeit.timeit('fn_numpy(xx)', 'from __main__ import fn_numpy, xx', number=10000)    # doctest: +SKIP
    0.18891601900395472
    >>> timeit.timeit('fn_fortran(xx)', 'from __main__ import fn_fortran, xx', number=10000)    # doctest: +SKIP
    0.004707066000264604

The options available with ufuncify are more or less the same as those
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.


Classes and functions for rewriting expressions (sympy.codegen.rewriting)
-------------------------------------------------------------------------

.. automodule:: sympy.codegen.rewriting
   :members:

.. automodule:: sympy.codegen.matrix_nodes
   :members:

Tools for simplifying expressions using approximations (sympy.codegen.approximations)
-------------------------------------------------------------------------------------

.. automodule:: sympy.codegen.approximations
   :members:

Classes for abstract syntax trees (sympy.codegen.ast)
-----------------------------------------------------

.. automodule:: sympy.codegen.ast
   :members:

Special C math functions (sympy.codegen.cfunctions)
---------------------------------------------------

.. automodule:: sympy.codegen.cfunctions
   :members:

C specific AST nodes (sympy.codegen.cnodes)
-------------------------------------------

.. automodule:: sympy.codegen.cnodes
   :members:


C++ specific AST nodes (sympy.codegen.cxxnodes)
-----------------------------------------------

.. automodule:: sympy.codegen.cxxnodes
   :members:

Fortran specific AST nodes (sympy.codegen.fnodes)
-------------------------------------------------

.. automodule:: sympy.codegen.fnodes
   :members:


Algorithms (sympy.codegen.algorithms)
-------------------------------------

.. automodule:: sympy.codegen.algorithms
   :members:


Python utilities (sympy.codegen.pyutils)
----------------------------------------

.. automodule:: sympy.codegen.pyutils
   :members:


C utilities (sympy.codegen.cutils)
----------------------------------

.. automodule:: sympy.codegen.cutils
   :members:


Fortran utilities (sympy.codegen.futils)
----------------------------------------

.. automodule:: sympy.codegen.futils
   :members: