-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from HamletTanyavong/dev
Add second-order, forward-mode automatic differentiation
- Loading branch information
Showing
19 changed files
with
799 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Second-Order, Forward Mode Automatic Differentiation | ||
|
||
Support for first-order, forward-mode autodiff is provided by the `HyperDual<T>` type. Because this type is used in a similar manner to `Dual<T>`, please refer to that section for help. | ||
|
||
### Second-Order Derivatives | ||
|
||
Suppose we wanted to find the second derivative of the complex function | ||
$$ | ||
f(z,w) = \sin(\tan{z}*\log{w}) | ||
$$ | ||
with respect to $ z $, at the points $ z=1.23+i0.66 $ and $ w=2.34-i0.25 $. We can do so by writing | ||
```csharp | ||
using Mathematics.NET.AutoDiff; | ||
using Mathematics.NET.Core; | ||
using static Mathematics.NET.AutoDiff.HyperDual<Mathematics.NET.Core.Complex>; | ||
|
||
var z = CreateVariable(new(1.23, 0.66), 1.0, 1.0); | ||
var w = CreateVariable(new(2.34, -0.25)); | ||
|
||
var result = Sin(Tan(z) * Ln(w)); | ||
|
||
Console.WriteLine(result.D3); | ||
``` | ||
which will give us `(-6.158582087985498, 6.391603674636932)`. Notice that we now have to provide two seed values. Each one, very loosely speaking, "represents" a first-order derivative with respect to the variable in which it appears. Since there are two seeds present with the variable $ z $, it means we want to take its derivative twice. Similarly, we can write the following if we wanted the second derivative of our function with respect to $ w $: | ||
```csharp | ||
var z = CreateVariable(new(1.23, 0.66)); | ||
var w = CreateVariable(new(2.34, -0.25), 1.0, 1.0); | ||
``` | ||
This will give us `(0.30998196902728725, -0.11498565892578178)`. To get our mixed derivative, $ \partial f/\partial{z}\partial{w} $, we must indicate with we want one of each derivative | ||
```csharp | ||
var z = CreateVariable(new(1.23, 0.66), 1.0, 0.0); | ||
var w = CreateVariable(new(2.34, -0.25), 0.0, 1.0); | ||
``` | ||
keeping in mind that the seeds must not occupy the same "slot." This, for example, will not give us the correct answer: | ||
```csharp | ||
// Incorrect, seeds must not occupy the same "slot" | ||
var z = CreateVariable(new(1.23, 0.66), 1.0, 0.0); | ||
var w = CreateVariable(new(2.34, -0.25), 1.0, 0.0); | ||
``` | ||
This will print `(0.6670456012622978, 2.2955143408553718)` to the console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.