Skip to content

Commit e4cae9c

Browse files
committed
added vector Hessian functions and the Differentiator class
1 parent c1c7e63 commit e4cae9c

File tree

689 files changed

+359
-489
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

689 files changed

+359
-489
lines changed
Binary file not shown.

README.md

+26-19
Binary file not shown.

docs/Differentiator_doc.html

+47-41
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,9 @@
178178
<tr>
179179
<td style="text-align:center"><TT>method</TT></td>
180180
<td style="text-align:center">-</td>
181-
<td>numerical differentiation method</td>
181+
<td>numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td>
182182
<td style="text-align:center">char</td>
183183
</tr>
184-
<tr>
185-
<td style="text-align:center"><TT>hi</TT></td>
186-
<td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td>
187-
<td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td>
188-
<td style="text-align:center">1×1<BR>double</td>
189-
</tr>
190184
<tr>
191185
<td style="text-align:center"><TT>hc</TT></td>
192186
<td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{c}" title="" /></td>
@@ -202,12 +196,18 @@
202196
<tr>
203197
<td style="text-align:center"><TT>hf2</TT></td>
204198
<td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{f_{2}}" title="" /></td>
205-
<td>elative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td>
199+
<td>relative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td>
200+
<td style="text-align:center">1×1<BR>double</td>
201+
</tr>
202+
<tr>
203+
<td style="text-align:center"><TT>hi</TT></td>
204+
<td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td>
205+
<td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td>
206206
<td style="text-align:center">1×1<BR>double</td>
207207
</tr>
208208
</table>
209-
</p><h2 id="2">Constructor</h2><pre class="language-matlab">differentiator = Differentiator
210-
differentiator = Differentiator(method)
209+
</p><h2 id="2">Constructor</h2><pre class="language-matlab">d = Differentiator
210+
d = Differentiator(method)
211211
</pre><p>
212212
<table border=1>
213213
<tr>
@@ -219,7 +219,7 @@
219219
<tr>
220220
<td style="text-align:center"><TT>method</TT></td>
221221
<td style="text-align:center">-</td>
222-
<td>(OPTIONAL) numerical differentiation method (defaults to <TT>'central difference'</TT>)</td>
222+
<td>(OPTIONAL) numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td>
223223
<td style="text-align:center">char</td>
224224
</tr>
225225
</table>
@@ -239,9 +239,9 @@
239239
<td style="text-align:center">1×1<BR>double</td>
240240
</tr>
241241
</table>
242-
</p><h2 id="4">Note</h2><div><ul><li>The <tt>Differentiator</tt> class uses the central difference approximation by default as a compromise between accuracy and compatibility. The central difference approximation, like the forward difference approximation, can be used with any function, but it is also of a higher order than the forward difference approximation. The complex-step approximation is more accurate than the central difference approximation, but it is <i>not</i> compatible with all functions (in some cases, functions have to be replaced with their "complexified" versions to be compatible with the complex-step approximation).</li><li>The complex-step approximation is still the most accurate approximation by far, and should be used if all functions that are incompatible with the complex-step approximation have been replaced with their complexified equivalent (i.e. replacing <tt>atan2</tt> with <tt>iatan2</tt>).</li></ul></div><h2 id="5">Example #1: Default <tt>Differentiator</tt> object.</h2><p>Let's define a default <tt>Differentiator</tt> object.</p><pre class="codeinput">D = Differentiator
242+
</p><h2 id="4">Note</h2><div><ul><li>The <tt>Differentiator</tt> class uses the central difference approximation by default as a compromise between accuracy and compatibility. The central difference approximation, like the forward difference approximation, can be used with any function, but it is also of a higher order than the forward difference approximation. The complex-step approximation is more accurate than the central difference approximation, but it is <i>not</i> compatible with all functions (in some cases, functions have to be replaced with their "complexified" versions to be compatible with the complex-step approximation).</li><li>The complex-step approximation is still the most accurate approximation by far, and should be used if all functions that are incompatible with the complex-step approximation have been replaced with their complexified equivalent (i.e. replacing <tt>atan2</tt> with <tt>iatan2</tt>).</li><li>The reason that the differentiation functions are defined as properties rather than as methods is for speed. If they were defined as methods, each time they were called they would have to do checking of which differentiation method to use and call then call the appropriate functions. Defining them as properties avoids this repeated logical checking.</li></ul></div><h2 id="5">Example #1: Default <tt>Differentiator</tt> object.</h2><p>Let's define a default <tt>Differentiator</tt> object.</p><pre class="codeinput">d = Differentiator
243243
</pre><pre class="codeoutput">
244-
D =
244+
d =
245245

246246
Differentiator with properties:
247247

@@ -253,13 +253,13 @@
253253
hessian: @(f,x)chessian(f,x,obj.hc)
254254
vechessian: @(f,x)cvechessian(f,x,obj.hc)
255255
method: 'central difference'
256-
hi: 1.0000e-200
257256
hc: 6.0555e-06
258257
hf: 1.4901e-08
259258
hf2: 6.0555e-06
259+
hi: 1.0000e-200
260260

261261
</pre><p>Note that the <tt>Differentiator</tt> class uses the central difference approximation by default. Evaluating the derivative of <img src="Differentiator_doc_eq17084161653528035876.png" alt="$f(x)=x^{2}$" style="width:46px;height:12px;"> at <img src="Differentiator_doc_eq04851200360885152548.png" alt="$x=2$" style="width:26px;height:8px;">,</p><pre class="codeinput">f = @(x) x^2;
262-
df = D.derivative(f,2)
262+
df = d.derivative(f,2)
263263
</pre><pre class="codeoutput">
264264
df =
265265

@@ -271,9 +271,9 @@
271271

272272
3.5696e-12
273273

274-
</pre><h2 id="8">Example #2: Specificying a differentiation method.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the forward difference approximation.</p><pre class="codeinput">D = Differentiator(<span class="string">'forward difference'</span>);
274+
</pre><h2 id="8">Example #2: Specificying a differentiation method.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the forward difference approximation.</p><pre class="codeinput">d = Differentiator(<span class="string">'forward difference'</span>);
275275
</pre><p>Evaluating the derivative of <img src="Differentiator_doc_eq07863013981449083213.png" alt="$f(x)=x^{3}$" style="width:46px;height:12px;"> at <img src="Differentiator_doc_eq14752139412850437817.png" alt="$x=3$" style="width:26px;height:8px;">,</p><pre class="codeinput">f = @(x) x^3;
276-
df = D.derivative(f,3)
276+
df = d.derivative(f,3)
277277
</pre><pre class="codeoutput">
278278
df =
279279

@@ -285,9 +285,9 @@
285285

286286
5.3644e-07
287287

288-
</pre><h2 id="11">Example #3: Adjusting the step size.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the complex-step approximation.</p><pre class="codeinput">D = Differentiator(<span class="string">'complex-step'</span>);
288+
</pre><h2 id="11">Example #3: Adjusting the step size.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the complex-step approximation.</p><pre class="codeinput">d = Differentiator(<span class="string">'complex-step'</span>);
289289
</pre><p>Evaluating the derivative of <img src="Differentiator_doc_eq07501950415006138384.png" alt="$f(x)=\sin{(x)}$" style="width:65px;height:11px;"> at <img src="Differentiator_doc_eq15469668660187898548.png" alt="$x=\pi$" style="width:27px;height:6px;">,</p><pre class="codeinput">f = @(x) sin(x);
290-
df = D.derivative(f,pi)
290+
df = d.derivative(f,pi)
291291
</pre><pre class="codeoutput">
292292
df =
293293

@@ -299,7 +299,7 @@
299299

300300
0
301301

302-
</pre><p>Now, let's increase the step-size from <img src="Differentiator_doc_eq09646552950603438366.png" alt="$10^{-200}$" style="width:28px;height:10px;"> (the default step size that is used with the complex-step approximation) to <img src="Differentiator_doc_eq00842521489666112223.png" alt="$h=0.001$" style="width:45px;height:8px;">.</p><pre class="codeinput">D.set_step_size(0.001)
302+
</pre><p>Now, let's increase the step-size from <img src="Differentiator_doc_eq09646552950603438366.png" alt="$10^{-200}$" style="width:28px;height:10px;"> (the default step size that is used with the complex-step approximation) to <img src="Differentiator_doc_eq00842521489666112223.png" alt="$h=0.001$" style="width:45px;height:8px;">.</p><pre class="codeinput">d.set_step_size(0.001)
303303
</pre><pre class="codeoutput">
304304
ans =
305305

@@ -313,12 +313,12 @@
313313
hessian: @(f,x)ihessian(f,x,obj.hi,obj.hc)
314314
vechessian: @(f,x)ivechessian(f,x,obj.hi,obj.hc)
315315
method: 'complex-step'
316-
hi: 1.0000e-03
317316
hc: 6.0555e-06
318317
hf: 1.4901e-08
319318
hf2: 6.0555e-06
319+
hi: 1.0000e-03
320320

321-
</pre><p>We can see that <tt>hi</tt> has been updated to <tt>1.0000e-03</tt>. Now, evaluating the same derivative as before,</p><pre class="codeinput">df = D.derivative(f,pi)
321+
</pre><p>We can see that <tt>hi</tt> has been updated to <tt>1.0000e-03</tt>. Now, evaluating the same derivative as before,</p><pre class="codeinput">df = d.derivative(f,pi)
322322
</pre><pre class="codeoutput">
323323
df =
324324

@@ -448,16 +448,10 @@
448448
% <tr>
449449
% <td style="text-align:center"><TT>method</TT></td>
450450
% <td style="text-align:center">-</td>
451-
% <td>numerical differentiation method</td>
451+
% <td>numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td>
452452
% <td style="text-align:center">char</td>
453453
% </tr>
454454
% <tr>
455-
% <td style="text-align:center"><TT>hi</TT></td>
456-
% <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td>
457-
% <td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td>
458-
% <td style="text-align:center">1×1<BR>double</td>
459-
% </tr>
460-
% <tr>
461455
% <td style="text-align:center"><TT>hc</TT></td>
462456
% <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{c}" title="" /></td>
463457
% <td>relative step size for central difference approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td>
@@ -472,14 +466,20 @@
472466
% <tr>
473467
% <td style="text-align:center"><TT>hf2</TT></td>
474468
% <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{f_{2}}" title="" /></td>
475-
% <td>elative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td>
469+
% <td>relative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td>
470+
% <td style="text-align:center">1×1<BR>double</td>
471+
% </tr>
472+
% <tr>
473+
% <td style="text-align:center"><TT>hi</TT></td>
474+
% <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td>
475+
% <td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td>
476476
% <td style="text-align:center">1×1<BR>double</td>
477477
% </tr>
478478
% </table>
479479
% </html>
480480
%% Constructor
481-
% differentiator = Differentiator
482-
% differentiator = Differentiator(method)
481+
% d = Differentiator
482+
% d = Differentiator(method)
483483
%
484484
% <html>
485485
% <table border=1>
@@ -492,7 +492,7 @@
492492
% <tr>
493493
% <td style="text-align:center"><TT>method</TT></td>
494494
% <td style="text-align:center">-</td>
495-
% <td>(OPTIONAL) numerical differentiation method (defaults to <TT>'central difference'</TT>)</td>
495+
% <td>(OPTIONAL) numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td>
496496
% <td style="text-align:center">char</td>
497497
% </tr>
498498
% </table>
@@ -532,48 +532,54 @@
532532
% by far, and should be used if all functions that are incompatible with
533533
% the complex-step approximation have been replaced with their complexified
534534
% equivalent (i.e. replacing |atan2| with |iatan2|).
535+
% * The reason that the differentiation functions are defined as properties
536+
% rather than as methods is for speed. If they were defined as methods,
537+
% each time they were called they would have to do checking of which
538+
% differentiation method to use and call then call the appropriate
539+
% functions. Defining them as properties avoids this repeated logical
540+
% checking.
535541
%% Example #1: Default |Differentiator| object.
536542
% Let's define a default |Differentiator| object.
537-
D = Differentiator
543+
d = Differentiator
538544
%%
539545
% Note that the |Differentiator| class uses the central difference
540546
% approximation by default. Evaluating the derivative of $f(x)=x^{2}$ at
541547
% $x=2$,
542548
f = @(x) x^2;
543-
df = D.derivative(f,2)
549+
df = d.derivative(f,2)
544550
%%
545551
% Taking a look at the error (the expected result is 4),
546552
err = abs(df-4)
547553
%% Example #2: Specificying a differentiation method.
548554
% Let's define a |Differentiation| object that uses the forward difference
549555
% approximation.
550-
D = Differentiator('forward difference');
556+
d = Differentiator('forward difference');
551557
%%
552558
% Evaluating the derivative of $f(x)=x^{3}$ at $x=3$,
553559
f = @(x) x^3;
554-
df = D.derivative(f,3)
560+
df = d.derivative(f,3)
555561
%%
556562
% Taking a look at the error (the expected result is 27),
557563
err = abs(df-27)
558564
%% Example #3: Adjusting the step size.
559565
% Let's define a |Differentiation| object that uses the complex-step
560566
% approximation.
561-
D = Differentiator('complex-step');
567+
d = Differentiator('complex-step');
562568
%%
563569
% Evaluating the derivative of $f(x)=\sin{(x)}$ at $x=\pi$,
564570
f = @(x) sin(x);
565-
df = D.derivative(f,pi)
571+
df = d.derivative(f,pi)
566572
%%
567573
% Taking a look at the error (the expected result is -1),
568574
err = abs(df-(-1))
569575
%%
570576
% Now, let's increase the step-size from $10^{-200}$ (the default step size
571577
% that is used with the complex-step approximation) to $h=0.001$.
572-
D.set_step_size(0.001)
578+
d.set_step_size(0.001)
573579
%%
574580
% We can see that |hi| has been updated to |1.0000e-03|. Now, evaluating
575581
% the same derivative as before,
576-
df = D.derivative(f,pi)
582+
df = d.derivative(f,pi)
577583
%%
578584
% Taking a look at the error once more,
579585
err = abs(df-(-1))
Binary file not shown.
Binary file not shown.
-762 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

docs/cderivative_doc.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!--
77
This HTML was auto-generated from MATLAB code.
88
To make changes, update the MATLAB code and republish this document.
9-
--><title>cderivative</title><meta name="generator" content="MATLAB 9.12"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2022-07-06"><meta name="DC.source" content="cderivative_doc.m"><style type="text/css">
9+
--><title>cderivative</title><meta name="generator" content="MATLAB 9.12"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2022-09-10"><meta name="DC.source" content="cderivative_doc.m"><style type="text/css">
1010
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
1111

1212
html { min-height:100%; margin-bottom:1px; }
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes

docs/cdirectional_doc.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!--
77
This HTML was auto-generated from MATLAB code.
88
To make changes, update the MATLAB code and republish this document.
9-
--><title>cdirectional</title><meta name="generator" content="MATLAB 9.12"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2022-07-06"><meta name="DC.source" content="cdirectional_doc.m"><style type="text/css">
9+
--><title>cdirectional</title><meta name="generator" content="MATLAB 9.12"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2022-09-10"><meta name="DC.source" content="cdirectional_doc.m"><style type="text/css">
1010
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
1111

1212
html { min-height:100%; margin-bottom:1px; }
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes

0 commit comments

Comments
 (0)