ad_trait 0.3.1

Easy to use, efficient, and highly flexible automatic differentiation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>ad_trait: Automatic Differentiation for Rust</title>
        <meta name="robots" content="noindex">


        <!-- Custom HTML head -->

        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff">

        <link rel="icon" href="favicon-de23e50b.svg">
        <link rel="shortcut icon" href="favicon-8114d1fc.png">
        <link rel="stylesheet" href="css/variables-8adf115d.css">
        <link rel="stylesheet" href="css/general-2459343d.css">
        <link rel="stylesheet" href="css/chrome-ae938929.css">
        <link rel="stylesheet" href="css/print-9e4910d8.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="fonts/fonts-9644e21d.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
        <link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
        <link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">

        <!-- Custom theme stylesheets -->


        <!-- Provide site root and default themes to javascript -->
        <script>
            const path_to_root = "";
            const default_light_theme = "light";
            const default_dark_theme = "navy";
            window.path_to_searchindex_js = "searchindex-bcd09740.js";
        </script>
        <!-- Start loading toc.js asap -->
        <script src="toc-b4c2bab5.js"></script>
    </head>
    <body>
    <div id="mdbook-help-container">
        <div id="mdbook-help-popup">
            <h2 class="mdbook-help-title">Keyboard shortcuts</h2>
            <div>
                <p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
                <p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
                <p>Press <kbd>?</kbd> to show this help</p>
                <p>Press <kbd>Esc</kbd> to hide this help</p>
            </div>
        </div>
    </div>
    <div id="mdbook-body-container">
        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script>
            try {
                let theme = localStorage.getItem('mdbook-theme');
                let sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script>
            const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
            let theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            const html = document.documentElement;
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add("js");
        </script>

        <input type="checkbox" id="mdbook-sidebar-toggle-anchor" class="hidden">

        <!-- Hide / unhide sidebar before it is displayed -->
        <script>
            let sidebar = null;
            const sidebar_toggle = document.getElementById("mdbook-sidebar-toggle-anchor");
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            } else {
                sidebar = 'hidden';
                sidebar_toggle.checked = false;
            }
            if (sidebar === 'visible') {
                sidebar_toggle.checked = true;
            } else {
                html.classList.remove('sidebar-visible');
            }
        </script>

        <nav id="mdbook-sidebar" class="sidebar" aria-label="Table of contents">
            <!-- populated by js -->
            <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
            <noscript>
                <iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
            </noscript>
            <div id="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
                <div class="sidebar-resize-indicator"></div>
            </div>
        </nav>

        <div id="mdbook-page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="mdbook-menu-bar-hover-placeholder"></div>
                <div id="mdbook-menu-bar" class="menu-bar sticky">
                    <div class="left-buttons">
                        <label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
                            <span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
                        </label>
                        <button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
                            <span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
                        </button>
                        <ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
                        </ul>
                        <button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
                            <span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
                        </button>
                    </div>

                    <h1 class="menu-title">ad_trait: Automatic Differentiation for Rust</h1>

                    <div class="right-buttons">
                        <a href="print.html" title="Print this book" aria-label="Print this book">
                            <span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
                        </a>
                        <a href="https://github.com/djrakita/ad_trait" title="Git repository" aria-label="Git repository">
                            <span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
                        </a>

                    </div>
                </div>

                <div id="mdbook-search-wrapper" class="hidden">
                    <form id="mdbook-searchbar-outer" class="searchbar-outer">
                        <div class="search-wrapper">
                            <input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
                            <div class="spinner-wrapper">
                                <span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
                            </div>
                        </div>
                    </form>
                    <div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
                        <div id="mdbook-searchresults-header" class="searchresults-header"></div>
                        <ul id="mdbook-searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script>
                    document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="mdbook-content" class="content">
                    <main>
                        <h1 id="introduction"><a class="header" href="#introduction">Introduction</a></h1>
<p><code>ad_trait</code> is a powerful, flexible, and easy-to-use Automatic Differentiation (AD) library for Rust.</p>
<h2 id="what-is-automatic-differentiation"><a class="header" href="#what-is-automatic-differentiation">What is Automatic Differentiation?</a></h2>
<p>Automatic Differentiation is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. Unlike symbolic differentiation, which produces a mathematical expression for the derivative, or finite differencing, which estimates derivatives from function evaluations, AD computes derivatives exactly (up to machine precision) by applying the chain rule to the program’s elementary operations.</p>
<h2 id="why-ad_trait"><a class="header" href="#why-ad_trait">Why ad_trait?</a></h2>
<ul>
<li><strong>Unified Interface</strong>: Use the same code for forward-mode, reverse-mode, and finite differencing.</li>
<li><strong>Integration</strong>: Seamlessly works with <code>nalgebra</code> and <code>ndarray</code>.</li>
<li><strong>Flexibility</strong>: Define your own differentiable functions using a simple trait.</li>
<li><strong>Performance</strong>: High-performance implementations including multi-tangent forward AD and SIMD acceleration.</li>
<li><strong>Second-Order AD</strong>: Native support for Hessians via recursive dual types.</li>
</ul>
<h2 id="goals"><a class="header" href="#goals">Goals</a></h2>
<p>The primary goal of <code>ad_trait</code> is to make sophisticated automatic differentiation accessible to the Rust ecosystem with a focus on robotics, optimization, and machine learning.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="getting-started"><a class="header" href="#getting-started">Getting Started</a></h1>
<p>Adding <code>ad_trait</code> to your project is straightforward.</p>
<h2 id="installation"><a class="header" href="#installation">Installation</a></h2>
<p>Add the following to your <code>Cargo.toml</code>:</p>
<pre><code class="language-toml">[dependencies]
ad_trait = "0.2.0"
</code></pre>
<h2 id="basic-usage"><a class="header" href="#basic-usage">Basic Usage</a></h2>
<p>The core workflow of <code>ad_trait</code> involves three steps:</p>
<ol>
<li><strong>Implement <code>DifferentiableFunctionTrait</code></strong>: Define your function.</li>
<li><strong>Implement <code>Reparameterize</code></strong>: Allow your function to work with different AD types.</li>
<li><strong>Use <code>FunctionEngine</code></strong>: Wrap your function with a differentiation method.</li>
</ol>
<h3 id="a-simple-example"><a class="header" href="#a-simple-example">A Simple Example</a></h3>
<p>Here’s how to compute the derivative of $f(x) = x^2$:</p>
<pre class="playground"><code class="language-rust">use ad_trait::{AD, DifferentiableFunctionTrait, Reparameterize, FunctionEngine, ForwardAD};

#[derive(Clone)]
struct Square;

impl&lt;T: AD&gt; DifferentiableFunctionTrait&lt;T&gt; for Square {
    const NAME: &amp;'static str = "Square";
    fn call(&amp;self, inputs: &amp;[T], _freeze: bool) -&gt; Vec&lt;T&gt; {
        vec![inputs[0] * inputs[0]]
    }
    fn num_inputs(&amp;self) -&gt; usize { 1 }
    fn num_outputs(&amp;self) -&gt; usize { 1 }
}

impl Reparameterize for Square {
    type SelfType&lt;T2: AD&gt; = Square;
}

fn main() {
    let func = Square;
    let engine = FunctionEngine::new(func.clone(), func, ForwardAD::new());
    
    let x = 3.0;
    let (val, grad) = engine.derivative(&amp;[x]);
    
    println!("f(3) = {}", val[0]); // Output: 9
    println!("f'(3) = {}", grad[(0, 0)]); // Output: 6
}</code></pre>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="core-concepts"><a class="header" href="#core-concepts">Core Concepts</a></h1>
<p><code>ad_trait</code> is built around a few central abstractions that make it highly extensible.</p>
<h2 id="the-ad-type-system"><a class="header" href="#the-ad-type-system">The AD Type System</a></h2>
<p>The cornerstone of the library is the <code>AD</code> trait. Any type that implements <code>AD</code> can be used in a differentiable computation. The library provides several built-in implementations:</p>
<ul>
<li><code>f64</code> and <code>f32</code>: For standard computations without derivative tracking.</li>
<li><code>adfn&lt;N&gt;</code>: For forward-mode AD with $N$ tangents.</li>
<li><code>adr</code>: For reverse-mode AD using a global computation graph.</li>
<li><code>f64xn&lt;N&gt;</code>: For SIMD-accelerated numerical computations.</li>
</ul>
<h2 id="trait-hierarchy"><a class="header" href="#trait-hierarchy">Trait Hierarchy</a></h2>
<ol>
<li><strong><code>AD</code></strong>: The base numerical trait for differentiation.</li>
<li><strong><code>DifferentiableFunctionTrait&lt;T&gt;</code></strong>: Defines how a function is evaluated for a given AD type <code>T</code>.</li>
<li><strong><code>Reparameterize</code></strong>: Bridges the gap between different AD types, allowing a function to be automatically adapted for different differentiation modes.</li>
<li><strong><code>DerivativeMethodTrait</code></strong>: Defines how a derivative is calculated (e.g., Forward, Reverse).</li>
</ol>
<h2 id="the-function-engine"><a class="header" href="#the-function-engine">The Function Engine</a></h2>
<p>The <code>FunctionEngine</code> is the primary interface for users. It wraps a differentiable function and a derivative method, providing a simple way to call the function and get its Jacobian.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="the-ad-trait"><a class="header" href="#the-ad-trait">The AD Trait</a></h1>
<p>The <code>AD</code> trait is the fundamental building block of <code>ad_trait</code>. It defines the arithmetic and mathematical operations required for automatic differentiation.</p>
<h2 id="why-use-a-trait"><a class="header" href="#why-use-a-trait">Why use a Trait?</a></h2>
<p>By using a trait instead of a concrete type, <code>ad_trait</code> allows you to write generic algorithms that can be used for:</p>
<ul>
<li>Standard evaluation (using <code>f64</code>).</li>
<li>First-order derivatives (using <code>adfn&lt;1&gt;</code>).</li>
<li>Gradients for many inputs (using <code>adr</code>).</li>
<li>Accelerated vector math (using <code>f64xn</code>).</li>
</ul>
<h2 id="key-methods"><a class="header" href="#key-methods">Key Methods</a></h2>
<ul>
<li><code>constant(f64) -&gt; Self</code>: Creates a new AD value from a constant.</li>
<li><code>to_constant(&amp;self) -&gt; f64</code>: Retrieves the underlying value.</li>
<li><code>ad_num_mode()</code>: Returns the current mode (Float, ForwardAD, etc.).</li>
<li><code>to_other_ad_type&lt;T2: AD&gt;(&amp;self) -&gt; T2</code>: Converts to a different AD type.</li>
</ul>
<h2 id="numerical-operations"><a class="header" href="#numerical-operations">Numerical Operations</a></h2>
<p><code>AD</code> requires many standard numerical traits, including:</p>
<ul>
<li><code>RealField</code> and <code>ComplexField</code> from <code>simba</code>.</li>
<li><code>num_traits::Signed</code>.</li>
<li>Standard operator overloads (<code>Add</code>, <code>Mul</code>, etc.).</li>
</ul>
<p>This ensures that any type implementing <code>AD</code> behaves like a sophisticated number.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="automatic-differentiation-modes"><a class="header" href="#automatic-differentiation-modes">Automatic Differentiation Modes</a></h1>
<p><code>ad_trait</code> supports three primary modes of differentiation, each with its own strengths and weaknesses.</p>
<h2 id="summary-table"><a class="header" href="#summary-table">Summary Table</a></h2>
<div class="table-wrapper">
<table>
<thead>
<tr><th style="text-align: left">Mode</th><th style="text-align: left">Type</th><th style="text-align: left">Speed (Few Inputs)</th><th style="text-align: left">Speed (Many Inputs)</th><th style="text-align: left">Precision</th></tr>
</thead>
<tbody>
<tr><td style="text-align: left"><strong>Forward-Mode</strong></td><td style="text-align: left">Exact</td><td style="text-align: left">Very Fast</td><td style="text-align: left">Slow</td><td style="text-align: left">Exact</td></tr>
<tr><td style="text-align: left"><strong>Reverse-Mode</strong></td><td style="text-align: left">Exact</td><td style="text-align: left">Moderate</td><td style="text-align: left">Very Fast</td><td style="text-align: left">Exact</td></tr>
<tr><td style="text-align: left"><strong>Hessian AD</strong></td><td style="text-align: left">Exact</td><td style="text-align: left">Moderate</td><td style="text-align: left">Slow</td><td style="text-align: left">Exact</td></tr>
<tr><td style="text-align: left"><strong>Finite Differencing</strong></td><td style="text-align: left">Approx</td><td style="text-align: left">Fast</td><td style="text-align: left">Very Slow</td><td style="text-align: left">Low</td></tr>
</tbody>
</table>
</div>
<p>In the following chapters, we will explore each of these modes in detail.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="forward-mode-ad"><a class="header" href="#forward-mode-ad">Forward-Mode AD</a></h1>
<p>Forward-mode automatic differentiation propagates derivatives along with the function evaluation. In <code>ad_trait</code>, this is implemented using the <code>adfn&lt;N&gt;</code> type, where <code>N</code> is a <strong>const generic</strong> representing the number of “tangent lanes” (the number of derivatives tracked simultaneously).</p>
<h2 id="how-it-works"><a class="header" href="#how-it-works">How it Works</a></h2>
<p>A forward-mode AD variable can be thought of as a pair $(v, \dot{v})$, where $v$ is the current value and $\dot{v}$ is its tangent (the derivative with respect to some input). Every operation on these variables updates both the value and the tangent using the rules of calculus.</p>
<h2 id="single-tangent-forward-ad"><a class="header" href="#single-tangent-forward-ad">Single-Tangent Forward AD</a></h2>
<p>When using <code>ForwardAD</code>, the library calculates the derivative with respect to one input at a time. To compute a full Jacobian for $M$ inputs, the function is evaluated $M$ times.</p>
<h2 id="multi-tangent-forward-ad"><a class="header" href="#multi-tangent-forward-ad">Multi-Tangent Forward AD</a></h2>
<p>One of the unique features of <code>ad_trait</code> is its support for multiple tangents. By using <code>adfn&lt;N&gt;</code>, you can compute up to $N$ columns of the Jacobian in a single forward pass. This is extremely efficient for functions where most work is shared across different input variables.</p>
<h2 id="usage-example"><a class="header" href="#usage-example">Usage Example</a></h2>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use ad_trait::{ForwardAD, adfn};

// evaluate with ForwardAD
// this will compute derivatives by calling the function once 
// per input dimension.
let engine = FunctionEngine::new(func.clone(), func, ForwardAD::new());
<span class="boring">}</span></code></pre>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="reverse-mode-ad"><a class="header" href="#reverse-mode-ad">Reverse-Mode AD</a></h1>
<p>Reverse-mode automatic differentiation is the most efficient way to compute gradients of functions with a very large number of inputs (e.g., neural networks). In <code>ad_trait</code>, this is implemented using the <code>adr</code> type.</p>
<h2 id="how-it-works-1"><a class="header" href="#how-it-works-1">How it Works</a></h2>
<p>Unlike forward-mode, reverse-mode AD works in two phases:</p>
<ol>
<li><strong>Forward Pass</strong>: The function is evaluated, and all operations are recorded in a <strong>Global Computation Graph</strong>.</li>
<li><strong>Backward Pass</strong>: The library traverses the graph in reverse, applying the chain rule to compute the gradient with respect to every input variable.</li>
</ol>
<h2 id="the-global-computation-graph"><a class="header" href="#the-global-computation-graph">The Global Computation Graph</a></h2>
<p>Because <code>adr</code> relies on a global graph, certain care must be taken:</p>
<ul>
<li>The graph must be reset between independent differentiation calls (handled automatically by <code>FunctionEngine</code>).</li>
<li>In multi-threaded environments, access to the graph must be synchronized.</li>
</ul>
<h2 id="usage-example-1"><a class="header" href="#usage-example-1">Usage Example</a></h2>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use ad_trait::ReverseAD;

// evaluate with ReverseAD
// this is best for functions with many inputs and few outputs.
let engine = FunctionEngine::new(func.clone(), func, ReverseAD::new());
<span class="boring">}</span></code></pre>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="finite-differencing"><a class="header" href="#finite-differencing">Finite Differencing</a></h1>
<p>Finite Differencing is a classical numerical method for estimating derivatives. It is provided in <code>ad_trait</code> as a baseline and for functions where AD types might be impractical.</p>
<h2 id="how-it-works-2"><a class="header" href="#how-it-works-2">How it Works</a></h2>
<p>The derivative is approximated using the formula:
$$f’(x) \approx \frac{f(x + h) - f(x)}{h}$$
where $h$ is a very small value.</p>
<h2 id="accuracy-vs-precision"><a class="header" href="#accuracy-vs-precision">Accuracy vs. Precision</a></h2>
<p>Finite differencing is an approximation and is subject to both truncation error (making $h$ too large) and round-off error (making $h$ too small). It is generally much less precise than true automatic differentiation.</p>
<h2 id="usage-example-2"><a class="header" href="#usage-example-2">Usage Example</a></h2>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use ad_trait::FiniteDifferencing;

// evaluate with FiniteDifferencing
let engine = FunctionEngine::new(func.clone(), func, FiniteDifferencing::new());
<span class="boring">}</span></code></pre>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="hessian--second-order-ad"><a class="header" href="#hessian--second-order-ad">Hessian / Second-Order AD</a></h1>
<p><code>ad_trait</code> supports computing second-order derivatives (Hessians) by nesting automatic differentiation types. This is achieved through the <code>HyperAD</code> family of types.</p>
<blockquote class="blockquote-tag blockquote-tag-note">
<p class="blockquote-tag-title"><svg viewbox="0 0 16 16" width="18" height="18"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>Note</p>
<p>The bracketed value <code>&lt;N&gt;</code> (e.g., <code>adfn&lt;N&gt;</code>, <code>HessianAD&lt;N&gt;</code>) is a <strong>const generic</strong> that specifies the number of <strong>tangent lanes</strong>. For Hessian computation, this typically should match the number of input variables you are differentiating with respect to.</p>
</blockquote>
<div class="table-wrapper">
<table>
<thead>
<tr><th>Mode</th><th>Type</th><th>Best For</th><th>Scaling</th></tr>
</thead>
<tbody>
<tr><td><strong>Forward-over-Forward</strong></td><td><code>HyperAD_ADFN&lt;N&gt;</code></td><td>Few inputs ($N &lt; 20$), low memory.</td><td>$O(N^2)$</td></tr>
<tr><td><strong>Forward-over-Reverse</strong></td><td><code>HyperAD_ADR&lt;N&gt;</code></td><td>Many inputs, few outputs (e.g., loss functions).</td><td>$O(N)$</td></tr>
</tbody>
</table>
</div>
<h2 id="choosing-the-right-mode"><a class="header" href="#choosing-the-right-mode">Choosing the Right Mode</a></h2>
<p>Selecting the optimal mode depends primarily on the number of input variables ($N$) and the memory constraints of your application.</p>
<h3 id="forward-over-forward-fof"><a class="header" href="#forward-over-forward-fof">Forward-over-Forward (FoF)</a></h3>
<ul>
<li><strong>When to use</strong>: Use this when you have a small number of inputs. It is the most robust mode and has the lowest memory overhead because it does not require building a computation graph.</li>
<li><strong>Efficiency</strong>: The computational cost scales quadratically with the number of inputs ($O(N^2)$). For a function with 10 inputs, it is very fast; for 1000 inputs, it becomes prohibitively slow.</li>
<li><strong>Implementation</strong>: Uses <code>HessianAD&lt;N&gt;</code>.</li>
</ul>
<h3 id="forward-over-reverse-for"><a class="header" href="#forward-over-reverse-for">Forward-over-Reverse (FoR)</a></h3>
<ul>
<li><strong>When to use</strong>: Use this for functions with many inputs and a single (or few) outputs, such as a neural network loss function or a complex physics simulation.</li>
<li><strong>Efficiency</strong>: This mode is significantly more efficient for large $N$. Because the inner layer is Reverse-mode AD, a single backpropagation through a tangent value can recover an entire row of the Hessian. This allows the total cost to scale linearly with the number of inputs ($O(N)$) for scalar-valued functions.</li>
<li><strong>Memory</strong>: Higher memory usage than FoF because it must maintain the reverse-mode computation graph.</li>
<li><strong>Implementation</strong>: Uses <code>HessianAD_FOR&lt;N&gt;</code>.</li>
</ul>
<h2 id="forward-over-forward-hessian"><a class="header" href="#forward-over-forward-hessian">Forward-over-Forward Hessian</a></h2>
<p>This mode uses <code>HyperAD_ADFN</code>, which is essentially an <code>adfn</code> type where the primary value and tangents are themselves <code>adfn</code> types.</p>
<h3 id="example"><a class="header" href="#example">Example</a></h3>
<pre class="playground"><code class="language-rust">use ad_trait::AD;
use ad_trait::function_engine::FunctionEngine;
use ad_trait::differentiable_function::{DifferentiableFunctionTrait, HessianAD, ToOtherADType};
use ad_trait::hyper_ad::hyper::HyperAD_ADFN;

#[derive(Clone)]
struct MyFunc;
impl&lt;T: AD&gt; DifferentiableFunctionTrait&lt;T&gt; for MyFunc {
    const NAME: &amp;'static str = "MyFunc";
    fn call(&amp;self, inputs: &amp;[T], _freeze: bool) -&gt; Vec&lt;T&gt; {
        let x = inputs[0];
        vec![ x * x * x ] // f(x) = x^3
    }
    fn num_inputs(&amp;self) -&gt; usize { 1 }
    fn num_outputs(&amp;self) -&gt; usize { 1 }
}

fn main() {
    let inputs = [2.0];
    let func = MyFunc;
    let engine = FunctionEngine::new(
        func.clone(), 
        func.to_other_ad_type::&lt;HyperAD_ADFN&lt;1&gt;&gt;(), 
        HessianAD::&lt;1&gt;::new()
    );

    let (f_res, jacobian_res, hessian_res) = engine.hessian(&amp;inputs);
    
    println!("f(2) = {}", f_res[0]);             // 8.0
    println!("f'(2) = {}", jacobian_res[(0,0)]);  // 12.0
    println!("f''(2) = {}", hessian_res[0][(0,0)]); // 12.0
}</code></pre>
<h2 id="forward-over-reverse-hessian"><a class="header" href="#forward-over-reverse-hessian">Forward-over-Reverse Hessian</a></h2>
<p>This mode uses <code>HyperAD_ADR</code>, which uses <code>adr</code> as the inner type. This is useful when you want to combine the benefits of forward and reverse mode.</p>
<h3 id="example-1"><a class="header" href="#example-1">Example</a></h3>
<pre class="playground"><code class="language-rust">use ad_trait::AD;
use ad_trait::function_engine::FunctionEngine;
use ad_trait::differentiable_function::{DifferentiableFunctionTrait, HessianAD_FOR, ToOtherADType};
use ad_trait::hyper_ad::hyper_adr::HyperAD_ADR;

#[derive(Clone)]
struct MyFunc;
impl&lt;T: AD&gt; DifferentiableFunctionTrait&lt;T&gt; for MyFunc {
    const NAME: &amp;'static str = "MyFunc";
    fn call(&amp;self, inputs: &amp;[T], _freeze: bool) -&gt; Vec&lt;T&gt; {
        let x = inputs[0];
        vec![ x * x * x ]
    }
    fn num_inputs(&amp;self) -&gt; usize { 1 }
    fn num_outputs(&amp;self) -&gt; usize { 1 }
}

fn main() {
    let inputs = [2.0];
    let func = MyFunc;
    let engine = FunctionEngine::new(
        func.clone(), 
        func.to_other_ad_type::&lt;HyperAD_ADR&lt;1&gt;&gt;(), 
        HessianAD_FOR::&lt;1&gt;::new()
    );

    let (f_res, jacobian_res, hessian_res) = engine.hessian(&amp;inputs);
    
    println!("f'(2) = {}", jacobian_res[(0,0)]);    // 12.0
    println!("f''(2) = {}", hessian_res[0][(0,0)]); // 12.0
}</code></pre>
<h2 id="first-order-derivatives-from-hessian-engines"><a class="header" href="#first-order-derivatives-from-hessian-engines">First-Order Derivatives from Hessian Engines</a></h2>
<p>It is important to note that a <code>FunctionEngine</code> initialized for Hessian computation can still be used for standard first-order derivatives.</p>
<p>Calling <code>engine.derivative(&amp;inputs)</code> on a Hessian-enabled block will return the Jacobian matrix as usual. This is possible because the hyper-dual types used for second-order differentiation internally track the first-order gradients as their primal “tangent” values.</p>
<p>This allows you to maintain a single <code>FunctionEngine</code> instance for both standard gradient-based optimization and second-order methods.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="advanced-topics"><a class="header" href="#advanced-topics">Advanced Topics</a></h1>
<p>Explore the high-performance features and deep technical details of <code>ad_trait</code>.</p>
<ul>
<li><strong>Multi-Tangents</strong>: Scaling forward-mode AD.</li>
<li><strong>SIMD Acceleration</strong>: Leveraging hardware for faster differentiation.</li>
<li><strong>Matrix Operations</strong>: Efficiently handling linear algebra.</li>
</ul>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="multi-tangents"><a class="header" href="#multi-tangents">Multi-Tangents</a></h1>
<p>Multi-tangent forward AD is a powerful optimization technique that allows <code>ad_trait</code> to compute multiple partial derivatives in a single pass of the function.</p>
<h2 id="the-problem"><a class="header" href="#the-problem">The Problem</a></h2>
<p>Standard forward AD computes one column of the Jacobian per pass. If a function has 100 inputs, you need 100 passes. For complex functions, this overhead can be significant.</p>
<h2 id="the-solution-adfnn"><a class="header" href="#the-solution-adfnn">The Solution: <code>adfn&lt;N&gt;</code></a></h2>
<p>By setting $N &gt; 1$, the tangent component of the AD type becomes a vector of length $N$. Each multiplication or operation now operates on this vector simultaneously.</p>
<h2 id="when-to-use"><a class="header" href="#when-to-use">When to Use</a></h2>
<ul>
<li>When the input dimension is moderately large (e.g., 2-32).</li>
<li>When the function evaluation is computationally expensive relative to the number of inputs.</li>
<li>When you want to minimize the number of times the function logic is executed.</li>
</ul>
<h2 id="integration-with-simd"><a class="header" href="#integration-with-simd">Integration with SIMD</a></h2>
<p>Multi-tangents pair perfectly with SIMD. By propagating multiple tangents, the underlying hardware can often execute these operations in parallel, providing a “free” speedup.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="simd-acceleration"><a class="header" href="#simd-acceleration">SIMD Acceleration</a></h1>
<p><code>ad_trait</code> provides first-class support for Single Instruction, Multiple Data (SIMD) acceleration through the <code>f64xn&lt;N&gt;</code> type.</p>
<h2 id="what-is-simd"><a class="header" href="#what-is-simd">What is SIMD?</a></h2>
<p>SIMD allows a single CPU instruction to operate on multiple pieces of data (usually vectors) at once. This can lead to 4x-8x speedups for arithmetic operations on modern processors.</p>
<h2 id="using-f64xn"><a class="header" href="#using-f64xn">Using <code>f64xn</code></a></h2>
<p>The <code>f64xn&lt;N&gt;</code> type implements the <code>AD</code> trait (in <code>SIMDNum</code> mode). It stores $N$ floats and performs element-wise operations using SIMD intrinsics (where available).</p>
<h2 id="requirements"><a class="header" href="#requirements">Requirements</a></h2>
<p>Currently, <code>f64xn</code> requires the <strong>nightly</strong> version of Rust to access the <code>portable_simd</code> feature.</p>
<h2 id="example-2"><a class="header" href="#example-2">Example</a></h2>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>// Requires nightly and #![feature(portable_simd)]
use ad_trait::simd::f64xn;

let a = f64xn::&lt;4&gt;::new([1.0, 2.0, 3.0, 4.0]);
let b = f64xn::&lt;4&gt;::new([5.0, 6.0, 7.0, 8.0]);
let c = a + b; // Computed using a single CPU instruction if possible
<span class="boring">}</span></code></pre>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="matrix-operations"><a class="header" href="#matrix-operations">Matrix Operations</a></h1>
<p><code>ad_trait</code> is designed to handle linear algebra efficiently. The <code>AD</code> trait includes requirements for matrix-scalar multiplication, which is the foundation for differentiating through linear systems.</p>
<h2 id="scalar-matrix-multiplication"><a class="header" href="#scalar-matrix-multiplication">Scalar-Matrix Multiplication</a></h2>
<p>Types implementing <code>AD</code> must provide:</p>
<ul>
<li><code>mul_by_nalgebra_matrix</code>: Multiplies an AD scalar by a <code>nalgebra</code> matrix.</li>
<li><code>mul_by_ndarray_matrix_ref</code>: Multiplies an AD scalar by an <code>ndarray</code> array.</li>
</ul>
<h2 id="why-this-is-necessary"><a class="header" href="#why-this-is-necessary">Why this is necessary</a></h2>
<p>By providing these specialized methods at the trait level, <code>ad_trait</code> can ensure that matrix operations are handled correctly for each AD mode. For example, in <code>ForwardAD</code>, the matrix multiplication also propagates the tangent through every element of the matrix.</p>
<h2 id="performance-considerations"><a class="header" href="#performance-considerations">Performance Considerations</a></h2>
<p>When performing large matrix multiplications, it is often more efficient to use a differentiation mode that minimizes the number of passes (like <code>ReverseAD</code> or <code>ForwardADMulti</code>).</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="integration"><a class="header" href="#integration">Integration</a></h1>
<p><code>ad_trait</code> doesn’t exist in a vacuum. It is designed to play well with the rest of the Rust numerical ecosystem.</p>
<ul>
<li><strong>nalgebra</strong>: The standard for linear algebra in Rust.</li>
<li><strong>ndarray</strong>: Flexible multi-dimensional arrays.</li>
</ul>
<p>In this section, we’ll see how to use <code>ad_trait</code> types within these libraries.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="integration-with-nalgebra"><a class="header" href="#integration-with-nalgebra">Integration with nalgebra</a></h1>
<p><code>ad_trait</code> types are fully compatible with <code>nalgebra</code>’s generic matrix types.</p>
<h2 id="example-ad-matrix"><a class="header" href="#example-ad-matrix">Example: AD Matrix</a></h2>
<p>You can create a <code>nalgebra</code> matrix using any <code>AD</code> type:</p>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use nalgebra::SMatrix;
use ad_trait::adfn;

// A 2x2 matrix of Forward-Mode AD variables
let m = SMatrix::&lt;adfn&lt;1&gt;, 2, 2&gt;::zeros();
<span class="boring">}</span></code></pre>
<h2 id="differentiating-through-nalgebra"><a class="header" href="#differentiating-through-nalgebra">Differentiating through nalgebra</a></h2>
<p>Because <code>adfn</code> and <code>adr</code> implement the relevant traits required by <code>nalgebra</code> (like <code>RealField</code> and <code>ComplexField</code>), you can use standard <code>nalgebra</code> functions (determinant, inverse, multiplication) in your differentiable code.</p>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn my_func&lt;T: AD&gt;(inputs: &amp;[T]) -&gt; Vec&lt;T&gt; {
    let m = SMatrix::&lt;T, 2, 2&gt;::from_vec(inputs.to_vec());
    let inv = m.try_inverse().unwrap();
    vec![inv[(0, 0)]]
}
<span class="boring">}</span></code></pre>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="integration-with-ndarray"><a class="header" href="#integration-with-ndarray">Integration with ndarray</a></h1>
<p><code>ad_trait</code> also supports the <code>ndarray</code> crate, which is commonly used for data science and machine learning.</p>
<h2 id="example-ad-array"><a class="header" href="#example-ad-array">Example: AD Array</a></h2>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use ndarray::Array2;
use ad_trait::adfn;

let a = Array2::&lt;adfn&lt;1&gt;&gt;::zeros((10, 10));
<span class="boring">}</span></code></pre>
<h2 id="scalar-operations"><a class="header" href="#scalar-operations">Scalar Operations</a></h2>
<p>The <code>AD</code> trait includes <code>mul_by_ndarray_matrix_ref</code>, allowing you to perform scalar-array multiplication efficiently across different AD modes.</p>
<h2 id="generic-algorithms"><a class="header" href="#generic-algorithms">Generic Algorithms</a></h2>
<p>Similar to <code>nalgebra</code>, you can write generic algorithms using <code>ndarray</code> that work with any <code>AD</code> type. This is particularly useful for implementing complex mathematical models that require gradients for optimization.</p>
<div style="break-before: page; page-break-before: always;"></div>
<h1 id="examples"><a class="header" href="#examples">Examples</a></h1>
<p>The <code>ad_trait</code> repository contains several examples that demonstrate the library in action.</p>
<h2 id="built-in-examples"><a class="header" href="#built-in-examples">Built-in Examples</a></h2>
<p>Check the <code>examples/</code> directory in the crate for standalone programs:</p>
<ul>
<li><code>test.rs</code>: A general demonstration of forward and reverse AD.</li>
</ul>
<h2 id="regression-tests"><a class="header" href="#regression-tests">Regression Tests</a></h2>
<p>The <code>tests/regression_tests.rs</code> file contains many examples of differentiating complex functions, including:</p>
<ul>
<li>Multi-variate polynomials.</li>
<li>Matrix-vector multiplication.</li>
<li>Jacobian calculations for multiple outputs.</li>
</ul>
<p>These tests serve as an excellent reference for how to structure your own differentiable functions.</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->


                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">

            </nav>

        </div>

        <template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
        <template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
        <template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
        <template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
        <template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>



        <script>
            window.playground_copyable = true;
        </script>


        <script src="elasticlunr-ef4e11c1.min.js"></script>
        <script src="mark-09e88c2c.min.js"></script>
        <script src="searcher-c2a407aa.js"></script>

        <script src="clipboard-1626706a.min.js"></script>
        <script src="highlight-abc7f01d.js"></script>
        <script src="book-a0b12cfe.js"></script>

        <!-- Custom JS scripts -->

        <script>
        window.addEventListener('load', function() {
            window.setTimeout(window.print, 100);
        });
        </script>


    </div>
    </body>
</html>