mirror of
https://github.com/triqs/dft_tools
synced 2024-12-27 06:43:40 +01:00
3fe400d34c
- examples split from the rst file using a python script (split_code). - Final result for the doc is unchanged. - examples are compiled and tested with the other tests. - examples' code have been clang-formatted, with triqs style. - doc compiles much faster, and with the same options as the rest of the test. - examples are added as tests, so they are run by make test, as simple C tests. - done for the tutorials and the reference. - autocompile removed (changed into triqs_example directive). - add triqs_example : - make a literal include of the source code. - runs the compiled example - add, as before, the result to the source code in the doc. - added the script split_code, used to make the changes automatically, maybe for later reuse. (in _tools)
78 lines
2.2 KiB
ReStructuredText
78 lines
2.2 KiB
ReStructuredText
|
|
.. highlight:: c
|
|
|
|
Transform CLEF expressions into functions
|
|
===============================================
|
|
|
|
Clef expressions are **NOT** functions. In short,
|
|
|
|
* clef expressions are *evaluated* (the order of argument *does not* matter) ::
|
|
|
|
eval( expr, x_=1, y_=2, ...);
|
|
|
|
* while functions are *called*, as usual (the order of argument *does* matter !) ::
|
|
|
|
f(1,2)
|
|
|
|
It is however possible to transform expressions into functions, *as soon as you specify the order of the placeholders*.
|
|
(the opposite is true, if the function accept lazy arguments, cf :ref:`overload_function`).
|
|
|
|
|
|
make_function
|
|
---------------
|
|
|
|
Given any expression with placeholder `x_`, `y_`, `z_`, ..., `make_function`
|
|
transform them into a regular function. If we say ::
|
|
|
|
auto f = make_function( clef_expression, placeholder_1, placeholder_2, placeholder_3, ...)
|
|
|
|
then f is ::
|
|
|
|
a function (x1,x2,x3) --> RESULT
|
|
|
|
where RESULT is :
|
|
|
|
* the result of the complete evaluation of the expression if the list of placeholder exhausts the placeholders of the expression.
|
|
* otherwise a clef_expression of the remaining placeholders, returning a **function**.
|
|
|
|
|
|
Short notation with >> operator
|
|
.....................................
|
|
|
|
For function of *one* variable, the make_function notation can be simplified into ::
|
|
|
|
// same
|
|
auto f = make_function( 2*x_ + y_ + 1, x_);
|
|
auto f = x_ >> 2*x_ + y_ + 1;
|
|
|
|
// same
|
|
auto f = make_function ( make_function( 2*x_ + y_ + 1, y_), x_);
|
|
auto f = x_ >> (y_ >> 2*x_ + y_ + 1) ;
|
|
|
|
.. warning::
|
|
The notation ::
|
|
|
|
`x_` >> `y_` >> expression
|
|
|
|
is banned because it conflicts with the standard priority of >>.
|
|
Use parenthesis.
|
|
|
|
clef::function
|
|
--------------------------
|
|
|
|
The class triqs::clef::function stored a function of a given signature
|
|
It is similar to std::function but
|
|
it can be constructed from an expression and an ordered list of placeholders.
|
|
|
|
clef::function can be assigned with the = operator, Cf example below.
|
|
|
|
.. note::
|
|
Like std::function, it stores the expression polymorphically, by erasing its type.
|
|
This might lead to some performance penalty in some case, even though tests do not show that at present...
|
|
|
|
|
|
Examples
|
|
---------
|
|
|
|
.. triqs_example:: ./function_0.cpp
|