Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ris-Bali committed Dec 3, 2023
1 parent 69fdfca commit 7fcab44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
5 changes: 2 additions & 3 deletions docs/userDocs/source/user/UsingClad.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ Few important things to note about ``clad::hessian``:
by the derived function. The hessian matrix array size should at least be as big as the size
required to store the hessian matrix. Passing an array less than the required size will result in undefined behaviour.

.. todo::

Add details for computing hessian of array ranges.
.. code-block:: cpp
Jacobian Computation
----------------------
Expand Down
44 changes: 29 additions & 15 deletions docs/userDocs/source/user/tutorials.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Tutorials
----------

Clad is an open source clang plugin which supports automatic differentiation of mathematical functions in C++.
Currently Clad supports four modes for automatic differentiation namely forward, reverse, Hessian, Jacobian.
Clad is an open source clang plugin which supports automatic differentiation of
mathematical functions in C++.Currently Clad supports four modes for automatic
differentiation namely forward, reverse, Hessian, Jacobian.

**Forward mode**

Expand All @@ -17,7 +18,8 @@ Currently Clad supports four modes for automatic differentiation namely forward,
}
int main() {
// Calling clad::differentiate to get the forward mode derivative of the given mathematical function.
/*Calling clad::differentiate to get the forward mode derivative of
the given mathematical function*/
auto d_func = clad::differentiate(func, "x");
// execute the generated derivative function.
std::cout << d_fn.execute(/*x*/=3) <<std::endl;
Expand All @@ -26,12 +28,13 @@ Currently Clad supports four modes for automatic differentiation namely forward,
Here we are differentiating a function `func` which takes an input x and
returns a scaler value `x*x`.
**Note : Currently it is necessary to define the function before `main()`.**
`.dump()` method is used to get a dump of generated derivative function to the standard output.
`.dump()` method is used to get a dump of generated derivative function to the
standard output.

**Reverse Mode**

Clad also supports reverse mode automatic differentiation, through the `clad::gradient` API call.
Clad also supports reverse mode automatic differentiation, through the
`clad::gradient` API call.


.. code-block:: cpp
Expand All @@ -50,9 +53,10 @@ Clad also supports reverse mode automatic differentiation, through the `clad::gr
std::cout<<"dx : "<< dx << "dy :"<< dy << endl;
}
In the above example we are differentiating w.r.t `x and y` we can also differentiate w.r.t to single
argument i.e. either `x` or `y` as `clad::gradient(f, "x")` not writing any argument will result in
differentiation of the function w.r.t to each input.
In the above example we are differentiating w.r.t `x and y` we can also
differentiate w.r.t to single argument i.e. either `x` or `y` as `clad::gradient(f, "x")`
not writing any argument i.e. `clad::gradient(f)` will result in differentiation
of the function w.r.t to each input.


**The Hessian Mode**
Expand Down Expand Up @@ -82,18 +86,22 @@ in case of an array type input argument.
double matrix_f[9] = {0};
clad::array_ref<double> matrix_f_ref(matrix_f, 9);
f_hess.execute(3, 4, matrix_f_ref);
std::cout << "[" << mat_f_ref[0] << ", " << mat_f_ref[1] << mat_f_ref[2] << "\n"
<< mat_f_ref[3] << ", " << mat_f_ref[4] << mat_f_ref[5] << "\n"
<< mat_f_ref[6] << ", " << mat_f_ref[7] << mat_f_ref[8] << "]";
}
When arrays are involved we need to specify the array index that needs to be differentiated.For example
if we want to differentiate w.r.t to the first two elements of the array along with `x` and `y` we will
write `clad::hessian(f_arr, z[0:1])`.
When arrays are involved we need to specify the array index that needs to be
differentiated.For example if we want to differentiate w.r.t to the first two
elements of the array along with `x` and `y` we will write `clad::hessian(f_arr, z[0:1])`
for the above example rest of the steps for execution are similar to reverse mode.
Here the `clad::array_ref` variable stores the hessian matrix.


**The Jacobian Mode**

Clad can produce Jacobian of a function using its reverse mode. It returns the jacobian matrix as a flaattened vector with
elements arranged in row-major format.
Clad can produce Jacobian of a function using its reverse mode. It returns the
jacobian matrix as a flattened vector with elements arranged in row-major format.

.. code-block:: cpp
Expand All @@ -119,6 +127,12 @@ elements arranged in row-major format.
<< jac[8]<<std::endl;
}
The jacobian matrix size should be equal to `no. of independent variables times
the number of outputs in the original function` in the above example it would be
an array of size 3x3 = 9.






Expand Down

0 comments on commit 7fcab44

Please sign in to comment.