Skip to content

Commit

Permalink
housekeeping updates (#7)
Browse files Browse the repository at this point in the history
* housekeeping updates

- added tests for differentiation functions
- updated Mason.toml to new format
- updated Chapel version
- import Math library explicitly

* fix typos in docs
  • Loading branch information
lucaferranti committed Oct 29, 2022
1 parent 1b27a57 commit abb8d72
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Mason.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

[brick]
authors = "Luca Ferranti"
chplVersion = "1.26.0"
chplVersion = "1.28.0"
license = "MIT"
name = "ForwardModeAD"
version = "0.1.0"
type="library"

[dependencies]

Expand Down
4 changes: 2 additions & 2 deletions docs/source/applications/newtoncircuit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Subsituting the previous values and equations we get
.. math::
g(V_D) = 10^{-9}\left(e^{40V_D}-1\right) + V_D - 5 = 0
this can be now solved with our previously developed newton method
this can be now solved with our previously developed Newton method

.. code-block:: chapel
Expand Down Expand Up @@ -160,4 +160,4 @@ using as initial guess :math:`X_0=[3, 3]`.
Iteration 1 x = 1.24792 1.01459 residual = 0.503036
Iteration 2 x = 1.33736 0.79315 residual = 0.0279132
Iteration 3 x = 1.3021 0.764332 residual = 0.000420461
Iteration 4 x = 1.30128 0.763349 residual = 2.39082e-07
Iteration 4 x = 1.30128 0.763349 residual = 2.39082e-07
4 changes: 2 additions & 2 deletions docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Finally, compile your code as
where ``prefix`` is the path where you cloned the repository.


Derivates in one dimension
Derivatives in one dimension
**************************

Suppose we have a function
Expand Down Expand Up @@ -158,4 +158,4 @@ Using the example function above
.. code-block::
2.0 1.0
3.0 5.0
3.0 5.0
2 changes: 2 additions & 0 deletions src/ForwardModeAD.chpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ForwardModeAD {

public use Math;

public use dualtype;

public use arithmetic;
Expand Down
57 changes: 57 additions & 0 deletions test/test_differentiation.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use UnitTest;
use ForwardModeAD;

type D = [0..#2] MultiDual;

proc testUnivariateFunctions(test: borrowed Test) throws {
proc f(x) {
return x ** 2 + 2 * x + 1;
}

var df = derivative(lambda(x : DualNumber) {return f(x);}, 1);
test.assertEqual(df, 4.0);

var valder = f(initdual(1));
test.assertEqual(valder.value, 4.0);
test.assertEqual(valder.derivative, 4.0);
}

proc testGradient(test: borrowed Test) throws {
proc g(x) {
return 2.0;
}

var dg = gradient(lambda(x : D) {return g(x);}, [1.0, 2.0]);
test.assertEqual(dg, [0.0, 0.0]);

proc h(x) {
return x[0] ** 2 + 3 * x[0] * x[1];
}

// TODO: debug why doesn't work with integer input
var valgradh = h(initdual([1.0, 2.0]));
test.assertEqual(valgradh.value, 7);
test.assertEqual(valgradh.derivative, [8.0, 3.0]);
}

proc testJacobian(test: borrowed Test) throws {
proc F(x) {
return [x[0] ** 2 + x[1] + 1, x[0] + x[1] ** 2 + x[0] * x[1]];
}

var valjac = F(initdual([1.0, 2.0])),
_jac: [0..1, 0..1] real = ((2.0, 1.0), (3.0, 5.0));

// TODO: revisit once API is updated
test.assertEqual(prim(valjac), [4.0, 7.0]);
test.assertEqual(dual(valjac), _jac);

proc G(x) {return [1, 2, 3];}

var Jg = jacobian(lambda(x : D) {return G(x);}, [1.0, 2.0]),
_Jg: [0..2, 0..1] real;

test.assertEqual(Jg, _Jg);
}

UnitTest.main();

0 comments on commit abb8d72

Please sign in to comment.