dace 0.2.1

Rust wrapper of DACE, the Differential Algebra Computational Toolbox.
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
use crate::dacecore::*;
use crate::*;
use auto_ops::*;
pub use ndarray::linalg::Dot;
use ndarray::prelude::*;
use ndarray::*;
pub use num_traits::{One, Pow, Zero};
use std::{
    convert::From,
    ffi::CStr,
    fmt::{Debug, Display},
    hash::{Hash, Hasher},
    mem::MaybeUninit,
    ops,
    os::raw::{c_char, c_uint, c_void},
    ptr::null_mut,
    str::FromStr,
    vec,
};

/// Basic DA class representing a single polynomial.
#[repr(C)]
#[derive(Debug)]
pub struct DA {
    pub(crate) len: c_uint,
    pub(crate) max: c_uint,
    pub(crate) dmonomial: *mut c_void,
}

/// compiledDA class representing a precomputed representation
/// of a polynomial for efficient evaluation.
#[derive(Debug)]
pub struct CompiledDA {
    pub(crate) dim: usize,
    pub(crate) ac: Vec<f64>,
    pub(crate) terms: u32,
    pub(crate) _vars: u32,
    pub(crate) ord: u32,
}

/// Compilation of an object into a compiledDA object.
pub trait Compile {
    /// Perform the compilation operation.
    fn compile(&self) -> CompiledDA;
}

macro_rules! simple_wrap {
    ($name_wrap:ident, $name_core:ident, $doc:literal) => {
        #[doc = $doc]
        pub fn $name_wrap(&self) -> DA {
            let mut out = Self::new();
            unsafe { $name_core(self, &mut out) };
            check_exception_panic();
            out
        }
    };
}

impl DA {
    /// Initialize the DACE control arrays and set the maximum order and the
    /// maximum number of variables.
    ///
    /// **MUST BE CALLED BEFORE ANY OTHER DA ROUTINE CAN BE USED**
    ///
    /// *This routine performs a mandatory version check to compare the version
    /// of the Rust interface used to compile the program to the version of the
    /// linked DACE library.*
    ///
    /// # Arguments
    ///
    /// * `ord` - order of the Taylor polynomials
    /// * `nvar` - number of variables considered
    ///
    /// # Panics
    ///
    /// Panics if the DACE-RS version is not compatible with the DACE Core version.
    pub fn init(ord: u32, nvar: u32) {
        if !Self::check_version() {
            panic!("Incompatible DACE core version!");
        }
        unsafe { daceInitialize(ord, nvar) };
        check_exception_panic();
        *INITIALIZED.write().unwrap() = true;
    }

    /// Initialize DACE for the current thread.
    pub fn initialize_thread() {
        unsafe { daceInitializeThread() };
        check_exception_panic();
    }

    /// Clean up DACE for the current thread.
    pub fn cleanup_thread() {
        unsafe { daceCleanupThread() };
        check_exception_panic();
    }

    /// Check if DACE has been initialized (true if initialized).
    pub fn is_initialized() -> bool {
        *INITIALIZED.read().unwrap()
    }

    /// Get DACE core version.
    ///
    /// Return format: (major, minor, patch)
    pub fn version() -> (i32, i32, i32) {
        let mut ver = (0, 0, 0);
        unsafe { daceGetVersion(&mut ver.0, &mut ver.1, &mut ver.2) };
        ver
    }

    /// Check DACE core version compatibility (true if compatible with Rust lib).
    ///
    /// To be compatible, these conditions must be satisfied:
    /// - core version major == Rust lib version major
    /// - core version minor == Rust lib version minor
    pub fn check_version() -> bool {
        let ver = Self::version();
        ver.0 == DACE_MAJOR_VERSION && ver.1 == DACE_MINOR_VERSION
    }

    /// Get the maximum order currently set for the computations
    /// (zero if undefined).
    pub fn max_order() -> u32 {
        unsafe { daceGetMaxOrder() }
    }

    /// Set the cutoff value eps to a new value and return the previous value
    /// (zero if undefined).
    pub fn set_eps(eps: f64) -> f64 {
        unsafe { daceSetEpsilon(eps) }
    }

    /// Get the cutoff value eps currently set for the computations
    /// (zero if undefined).
    pub fn eps() -> f64 {
        unsafe { daceGetEpsilon() }
    }

    /// Get the machine epsilon (experimentally determined).
    ///
    /// # Examples
    ///
    /// ```
    /// // Get machine epsilon and test that it is the smallest
    /// // number that added to 1.0 gives a value greater than 1.0
    /// use dace::DA;
    /// DA::init(2, 1);
    /// let eps = DA::eps_mac();
    /// assert_ne!(1.0 + eps, 1.0);
    /// assert_eq!(1.0 + (eps / 2.0), 1.0)
    /// ```
    pub fn eps_mac() -> f64 {
        unsafe { daceGetMachineEpsilon() }
    }

    /// Get the maximum number of variables set for the computations.
    pub fn max_variables() -> u32 {
        unsafe { daceGetMaxVariables() }
    }

    /// Get the maximum number of monomials available with the
    /// order and number of variables specified (zero if undefined).
    pub fn max_monomials() -> u32 {
        unsafe { daceGetMaxMonomials() }
    }

    /// Set the truncation order ot to a new value and return the previous value.
    ///
    /// All terms larger than the truncation order
    /// are discarded in subsequent operations.
    ///
    /// # Arguments
    ///
    /// * `ot` - new truncation order, use `None` to set to the maximum order
    pub fn set_truncation_order(ot: impl Into<Option<u32>>) -> u32 {
        let ot = ot.into().unwrap_or_else(Self::max_order);
        let prev_ot = unsafe { daceSetTruncationOrder(ot) };
        check_exception_panic();
        prev_ot
    }

    /// Get the truncation order currently set for the computations
    /// (default: max. order).
    ///
    /// All terms larger than the truncation order
    /// are discarded in subsequent operations.
    ///
    /// # Examples
    ///
    /// ```
    /// use dace::DA;
    /// DA::init(10, 1);
    /// assert_eq!(DA::truncation_order(), 10);
    /// ```
    pub fn truncation_order() -> u32 {
        unsafe { daceGetTruncationOrder() }
    }

    /// Set a new truncation order, saving the previous one on the truncation
    /// order stack.
    ///
    /// All terms larger than the truncation order
    /// are discarded in subsequent operations.
    ///
    /// # Arguments
    ///
    /// * `ot` - new truncation order, use `None` to set to the maximum order
    ///
    /// # Examples
    ///
    /// ```
    /// use dace::DA;
    /// // start with 10
    /// DA::init(10, 1);
    /// assert_eq!(DA::truncation_order(), 10);
    /// // change to 5
    /// DA::push_truncation_order(5);
    /// assert_eq!(DA::truncation_order(), 5);
    /// // change to 2
    /// DA::push_truncation_order(2);
    /// assert_eq!(DA::truncation_order(), 2);
    /// // back to prev (5)
    /// DA::pop_truncation_order();
    /// assert_eq!(DA::truncation_order(), 5);
    /// // back to prev (10)
    /// DA::pop_truncation_order();
    /// assert_eq!(DA::truncation_order(), 10);
    /// // back to prev (no prev available: will fail and stay on 10)
    /// DA::pop_truncation_order();
    /// assert_eq!(DA::truncation_order(), 10);
    /// ```
    pub fn push_truncation_order(ot: impl Into<Option<u32>>) {
        let ot: u32 = ot.into().unwrap_or_else(Self::max_order);
        TO_STACK
            .lock()
            .unwrap()
            .push(Self::set_truncation_order(ot));
    }

    /// Restore the previous truncation order from the truncation order stack.
    ///
    /// All terms larger than the truncation order
    /// are discarded in subsequent operations.
    ///
    /// # Examples
    ///
    /// ```
    /// use dace::DA;
    /// // start with 10
    /// DA::init(10, 1);
    /// assert_eq!(DA::truncation_order(), 10);
    /// // change to 5
    /// DA::push_truncation_order(5);
    /// assert_eq!(DA::truncation_order(), 5);
    /// // change to 2
    /// DA::push_truncation_order(2);
    /// assert_eq!(DA::truncation_order(), 2);
    /// // back to prev (5)
    /// DA::pop_truncation_order();
    /// assert_eq!(DA::truncation_order(), 5);
    /// // back to prev (10)
    /// DA::pop_truncation_order();
    /// assert_eq!(DA::truncation_order(), 10);
    /// // back to prev (no prev available: will fail and stay on 10)
    /// DA::pop_truncation_order();
    /// assert_eq!(DA::truncation_order(), 10);
    /// ```
    pub fn pop_truncation_order() -> Option<u32> {
        Some(Self::set_truncation_order(TO_STACK.lock().unwrap().pop()?))
    }

    /// Dump DACE core memory, only for debug purposes.
    pub fn memdump() {
        unsafe { daceMemoryDump() }
    }

    /// Create an empty DA object representing the constant zero function.
    pub fn new() -> Self {
        let mut da = MaybeUninit::<DA>::uninit();
        unsafe { daceAllocateDA(da.as_mut_ptr(), 0) };
        check_exception_panic();
        unsafe { da.assume_init() }
    }

    /// Create a DA object and fill it with random entries.
    ///
    /// # Arguments
    ///
    /// * `cm` - filling factor
    ///   - for `cm` < 0, the DA object is filled with random numbers
    ///   - for `cm` > 0, the DA object is filled with weighted decaying numbers
    pub fn random(cm: f64) -> Self {
        let mut out = DA::new();
        unsafe { daceCreateRandom(&mut out, cm) };
        check_exception_panic();
        out
    }

    /// Get the constant part of the DA object.
    pub fn cons(&self) -> f64 {
        let out = unsafe { daceGetConstant(self) };
        check_exception_panic();
        out
    }
    /// Get the linear part of the DA object.
    pub fn linear(&self) -> AlgebraicVector<f64> {
        let mut out = AlgebraicVector::zeros(Self::max_variables() as usize);
        unsafe { daceGetLinear(self, out.as_mut_ptr()) };
        check_exception_panic();
        out
    }

    /// Compute the gradient of a DA object.
    pub fn gradient(&self) -> AlgebraicVector<DA> {
        let nvar = Self::max_variables() as usize;
        Array::from_shape_fn((nvar,), |i| self.deriv(i as u32 + 1)).into()
    }

    /// Get a specific coefficient of a DA object.
    ///
    /// # Arguments
    ///
    /// * `jj` - vector of the exponents of the coefficient to retrieve
    ///
    /// # Example
    ///
    /// ```
    /// use dace::*;
    /// DA::init(10, 2);
    /// let (x, y) = (da!(1), da!(2));
    /// let v = (1.0 + 3.0 * &x) * (2.0 + 4.0 * &y).pow(2);
    /// let x1_y2_coeff = v.coefficient(&vec![1, 2]);
    /// assert_eq!(x1_y2_coeff, 48.0);
    /// ```
    pub fn coefficient(&self, jj: &Vec<u32>) -> f64 {
        let nvar = Self::max_variables() as usize;
        let ptr;
        let mut temp;
        if jj.len() >= nvar {
            ptr = jj.as_ptr();
        } else {
            temp = jj.clone();
            temp.resize(nvar, 0);
            ptr = temp.as_ptr();
        }
        let coeff = unsafe { daceGetCoefficient(self, ptr) };
        check_exception_panic();
        coeff
    }

    /// Set a specific coefficient into a DA object.
    ///
    /// # Arguments
    ///
    /// * `jj` - vector of the exponents of the coefficient to set
    /// * `coeff` - value to be set as coefficient
    ///
    /// # Example
    ///
    /// ```
    /// use dace::*;
    /// DA::init(10, 2);
    /// let (x, y) = (da!(1), da!(2));
    /// let mut v = DA::new();
    /// v.set_coefficient(&vec![1, 2], 12.0);
    /// assert_eq!(v, 12.0 * x * y.pow(2));
    /// ```
    pub fn set_coefficient(&mut self, jj: &Vec<u32>, coeff: f64) {
        let nvar = Self::max_variables() as usize;
        let ptr;
        let mut temp;
        if jj.len() >= nvar {
            ptr = jj.as_ptr();
        } else {
            temp = jj.clone();
            temp.resize(nvar, 0);
            ptr = temp.as_ptr();
        }
        unsafe { daceSetCoefficient(self, ptr, coeff) };
        check_exception_panic();
    }

    /// Multiply the DA object with another DA object monomial by monomial.
    ///
    /// This is the equivalent of coefficient-wise multiplication (like in DA addition).
    ///
    /// # Arguments
    ///
    /// * `oth` - DA object to multiply with coefficient-wise
    pub fn multiply_monomials<T: AsRef<Self>>(&self, oth: T) -> Self {
        let mut out = Self::new();
        unsafe { daceMultiplyMonomials(self, oth.as_ref(), &mut out) };
        check_exception_panic();
        out
    }

    /// Divide by independent variable `var` raised to power `p`.
    ///
    /// # Arguments
    ///
    /// * `var` - independent variable number to divide by
    /// * `p` - power of the independent variable
    pub fn divide(&self, var: u32, p: u32) -> Self {
        let mut out = Self::new();
        unsafe { daceDivideByVariable(self, var, p, &mut out) };
        check_exception_panic();
        out
    }

    /// Compute the derivative of a DA object with respect to variable `i`.
    ///
    /// # Arguments
    ///
    /// * `i` - variable with respect to which the derivative is calculated
    pub fn deriv(&self, i: u32) -> Self {
        let mut out = Self::new();
        unsafe { daceDifferentiate(i, self, &mut out) };
        check_exception_panic();
        out
    }

    /// Compute the integral of a DA object with respect to variable `i`.
    ///
    /// # Arguments
    ///
    /// * `i` - variable with respect to which the integral is calculated
    pub fn integ(&self, i: u32) -> Self {
        let mut out = Self::new();
        unsafe { daceIntegrate(i, self, &mut out) };
        check_exception_panic();
        out
    }

    /// Returns a DA object with all monomials of order
    /// less than `min` and greater than `max` removed.
    ///
    /// # Arguments
    ///
    /// * `min` - The minimum order to keep in the DA object
    /// * `max` - The maximum order to keep in the DA object (or `None`)
    pub fn trim(&self, imin: u32, imax: impl Into<Option<u32>>) -> Self {
        let mut out = Self::new();
        let imax = imax.into().unwrap_or_else(Self::max_order);
        unsafe { daceTrim(self, imin, imax, &mut out) };
        check_exception_panic();
        out
    }

    simple_wrap!{trunc, daceTruncate, "Truncate the constant part of a DA object to an integer."}

    simple_wrap!{round, daceRound, "Round the constant part of a DA object to an integer."}

    /// Compute the p-th root of a DA object.
    ///
    /// # Arguments
    ///
    /// * `p` - root to be computed
    pub fn root(&self, p: i32) -> DA {
        let mut out = DA::new();
        unsafe { daceRoot(self, p, &mut out) };
        check_exception_panic();
        out
    }

    simple_wrap!{sqr, daceSquare, "Compute the square of a DA object."}

    /// Compute the square of a DA object (alias of `sqr`).
    #[inline(always)]
    pub fn square(&self) -> DA {
        self.sqr()
    }

    simple_wrap!{sqrt, daceSquareRoot, "Compute the square root of a DA object."}

    simple_wrap!{isrt, daceInverseSquareRoot, "Compute the inverse square root of a DA object."}

    simple_wrap!{cbrt, daceCubicRoot, "Compute the cubic root of a DA object."}

    simple_wrap!{icrt, daceInverseCubicRoot, "Compute the inverse cubic root of a DA object."}

    /// Compute the hypotenuse (`(a.sqr() + b.sqr()).sqrt()`) of a DA object and the given DA argument.
    pub fn hypot<T: AsRef<DA>>(&self, oth: T) -> DA {
        let mut out = DA::new();
        unsafe { daceHypotenuse(self, oth.as_ref(), &mut out) };
        check_exception_panic();
        out
    }

    simple_wrap!{exp, daceExponential, "Compute the exponential of a DA object."}

    simple_wrap!{log, daceLogarithm, "Compute the natural logarithm of a DA object."}

    /// Compute the logarithm of a DA object with respect to a given base.
    ///
    /// # Arguments
    ///
    /// * `b` - logarithm base
    pub fn logb(&self, b: f64) -> DA {
        let mut out = DA::new();
        unsafe { daceLogarithmBase(self, b, &mut out) };
        check_exception_panic();
        out
    }

    simple_wrap!{log10, daceLogarithm10, "Compute the 10 based logarithm of a DA object."}

    simple_wrap!{log2, daceLogarithm2, "Compute the 2 based logarithm of a DA object."}

    simple_wrap!{sin, daceSine, "Compute the sine of a DA object."}

    simple_wrap!{cos, daceCosine, "Compute the cosine of a DA object."}

    simple_wrap!{tan, daceTangent, "Compute the tangent of a DA object."}

    simple_wrap!{asin, daceArcSine, "Compute the arcsine of a DA object."}

    /// Compute the arccosine of a DA object.
    #[inline(always)]
    pub fn arcsin(&self) -> Self {
        self.asin()
    }

    simple_wrap!{acos, daceArcCosine, "Compute the arccosine of a DA object."}

    /// Compute the arccosine of a DA object.
    #[inline(always)]
    pub fn arccos(&self) -> Self {
        self.acos()
    }

    simple_wrap!{atan, daceArcTangent, "Compute the arctangent of a DA object."}

    /// Compute the arctangent of a DA object.
    #[inline(always)]
    pub fn arctan(&self) -> Self {
        self.atan()
    }

    /// Compute the four-quadrant arctangent of Y/X.
    /// Y is the current DA object, whereas X is the given DA.
    ///
    /// # Arguments
    ///
    /// * `oth` - X
    pub fn atan2<T: AsRef<Self>>(&self, oth: T) -> Self {
        let mut out = Self::new();
        unsafe { daceArcTangent2(self, oth.as_ref(), &mut out) };
        check_exception_panic();
        out
    }

    /// Compute the four-quadrant arctangent of Y/X.
    /// Y is the current DA object, whereas X is the given DA.
    ///
    /// # Arguments
    ///
    /// * `oth` - X
    #[inline(always)]
    pub fn arctan2<T: AsRef<Self>>(&self, oth: T) -> Self {
        self.atan2(oth)
    }

    simple_wrap!{sinh, daceHyperbolicSine, "Compute the hyperbolic sine of a DA object."}

    simple_wrap!{cosh, daceHyperbolicCosine, "Compute the hyperbolic cosine of a DA object."}

    simple_wrap!{tanh, daceHyperbolicTangent, "Compute the hyperbolic tangent of a DA object."}

    simple_wrap!{asinh, daceHyperbolicArcSine, "Compute the hyperbolic arcsine of a DA object."}

    /// Compute the hyperbolic arcsine of a DA object.
    #[inline(always)]
    pub fn arcsinh(&self) -> Self {
        self.asinh()
    }

    simple_wrap!{acosh, daceHyperbolicArcCosine, "Compute the hyperbolic arccosine of a DA object."}

    /// Compute the hyperbolic arccosine of a DA object.
    #[inline(always)]
    pub fn arccosh(&self) -> Self {
        self.acosh()
    }

    simple_wrap!{atanh, daceHyperbolicArcTangent, "Compute the hyperbolic arctangent of a DA object."}

    /// Compute the hyperbolic arctangent of a DA object.
    #[inline(always)]
    pub fn arctanh(&self) -> Self {
        self.atanh()
    }

    simple_wrap!{minv, daceMultiplicativeInverse, "Compute the multiplicative inverse of a DA object."}

    simple_wrap!{erf, daceErrorFunction, "Compute the error function of a DA object."}

    simple_wrap!{erfc, daceComplementaryErrorFunction, "Compute the complementary error function of a DA object."}

    /// Compute the `n`-th Bessel function of first type `J_n` of a DA object.
    ///
    /// # Arguments
    ///
    /// * `n` - order of the Bessel function
    pub fn besselj(&self, n: i32) -> Self {
        let mut out = Self::new();
        unsafe { daceBesselJFunction(self, n, &mut out) };
        check_exception_panic();
        out
    }

    /// Compute the `n`-th Bessel function of second type `Y_n` of a DA object.
    ///
    /// # Arguments
    ///
    /// * `n` - order of the Bessel function
    pub fn bessely(&self, n: i32) -> Self {
        let mut out = Self::new();
        unsafe { daceBesselYFunction(self, n, &mut out) };
        check_exception_panic();
        out
    }

    /// Compute the `n`-th modified Bessel function of first type `I_n` of a DA object.
    ///
    /// # Arguments
    ///
    /// * `n` - order of the Bessel function
    /// * `scaled` if true, the modified Bessel function is scaled
    ///    by a factor `exp(-x)`, i.e. `exp(-x)I_n(x)` is returned.
    pub fn besseli(&self, n: i32, scaled: bool) -> Self {
        let mut out = Self::new();
        unsafe { daceBesselIFunction(self, n, scaled, &mut out) };
        check_exception_panic();
        out
    }

    /// Compute the `n`-th modified Bessel function of second type `K_n` of a DA object.
    ///
    /// # Arguments
    ///
    /// * `n` - order of the Bessel function
    /// * `scaled` if true, the modified Bessel function is scaled
    ///    by a factor `exp(-x)`, i.e. `exp(-x)K_n(x)` is returned.
    pub fn besselk(&self, n: i32, scaled: bool) -> Self {
        let mut out = Self::new();
        unsafe { daceBesselKFunction(self, n, scaled, &mut out) };
        check_exception_panic();
        out
    }

    simple_wrap!{gamma, daceGammaFunction, "Compute the Gamma function of a DA object."}

    simple_wrap!{loggamma, daceLogGammaFunction, "Compute the Logarithmic Gamma function (i.e. the natural logarithm of Gamma) of a DA object."}

    /// Compute the `n`-th order Psi function, i.e. the (`n`+1)st derivative of the Logarithmic Gamma function, of a DA object.
    pub fn psi(&self, n: u32) -> Self {
        let mut out = Self::new();
        unsafe { dacePsiFunction(self, n, &mut out) };
        check_exception_panic();
        out
    }

    /// Get the number of non-zero coefficients of a DA object.
    #[inline]
    pub fn size(&self) -> u32 {
        self.len
    }

    /// Compute the max norm of a DA object.
    pub fn abs(&self) -> f64 {
        let out = unsafe { daceAbsoluteValue(self) };
        check_exception_panic();
        out
    }

    /// Compute different types of norms for a DA object.
    ///
    /// # Arguments
    ///
    /// * `type_` type of norm to be computed. Possible norms are:
    ///   - 0: Max norm
    ///   - 1: Sum norm
    ///   - oth: Vector norm of given type
    pub fn norm(&self, type_: u32) -> f64 {
        let out = unsafe { daceNorm(self, type_) };
        check_exception_panic();
        out
    }

    /// Extract different types of order sorted norms from a DA object.
    ///
    /// # Arguments
    ///
    /// * `var` order
    ///   - 0: Terms are sorted by their order
    ///   - oth: Terms are sorted by the exponent of variable `var`
    /// * `type_` type of norm to be computed. Possible norms are:
    ///   - 0: Max norm
    ///   - 1: Sum norm
    ///   - oth: Vector norm of given type
    pub fn order_norm(&self, var: u32, type_: u32) -> AlgebraicVector<f64> {
        let mut v = AlgebraicVector::<f64>::zeros(Self::max_order() as usize + 1);
        unsafe { daceOrderedNorm(self, var, type_, v.as_mut_ptr()) };
        check_exception_panic();
        v
    }

    /// Estimate different types of order sorted norms for terms
    /// of a DA object up to a specified order.
    ///
    /// # Arguments
    ///
    /// * `var` - order
    ///   - 0: Terms are sorted by their order
    ///   - oth: Terms are sorted by the exponent of variable `var`
    /// * `type_` - type of norm to be computed. Possible norms are:
    ///   - 0: Max norm
    ///   - 1: Sum norm
    ///   - oth: Vector norm of given type
    /// * `nc` - maximum order to be estimated
    pub fn estim_norm(&self, var: u32, type_: u32, nc: u32) -> AlgebraicVector<f64> {
        let mut v = AlgebraicVector::<f64>::zeros(nc as usize + 1);
        unsafe { daceEstimate(self, var, type_, v.as_mut_ptr(), null_mut(), nc) };
        check_exception_panic();
        v
    }

    /// Estimate different types of order sorted norms for terms
    /// of a DA object up to a specified order with error estimates.
    ///
    /// # Arguments
    ///
    /// * `var` - order
    ///   - 0: Terms are sorted by their order
    ///   - oth: Terms are sorted by the exponent of variable `var`
    /// * `type_` - type of norm to be computed. Possible norms are:
    ///   - 0: Max norm
    ///   - 1: Sum norm
    ///   - oth: Vector norm of given type
    /// * `nc` - maximum order to be estimated
    pub fn estim_norm_err(&self, var: u32, type_: u32, nc: u32) -> AlgebraicVector<f64> {
        let mut err = AlgebraicVector::<f64>::zeros(nc.min(Self::max_order()) as usize + 1);
        let mut v = AlgebraicVector::<f64>::zeros(nc as usize + 1);
        unsafe { daceEstimate(self, var, type_, v.as_mut_ptr(), err.as_mut_ptr(), nc) };
        check_exception_panic();
        v
    }

    /// Compute lower and upper bounds of a DA object.
    pub fn bound(&self) -> Interval {
        let mut out = Interval {
            m_lb: 0.0,
            m_ub: 0.0,
        };
        unsafe { daceGetBounds(self, &mut out.m_lb, &mut out.m_ub) };
        check_exception_panic();
        out
    }

    /// Estimate the convergence radius of the DA object.
    ///
    /// # Arguments
    ///
    /// * `eps` - requested tolerance
    /// * `type_` - type of norm (sum norm is used as default)
    pub fn conv_radius(&self, eps: f64, type_: u32) -> f64 {
        let ord = Self::truncation_order();
        let res = self.estim_norm(0, type_, ord + 1);
        (eps / res[ord as usize + 1]).powf(1.0 / (ord + 1) as f64)
    }

    /// Partial evaluation of a DA object.
    ///
    /// In the DA object, variable `var` is replaced by the value `val`.
    ///
    /// # Arguments
    ///
    /// * `var` - variable number to be replaced
    /// * `val` - value by which to replace the variable
    pub fn plug(&self, var: u32, val: f64) -> DA {
        let mut out = DA::new();
        unsafe { daceEvalVariable(self, var, val, &mut out) };
        check_exception_panic();
        out
    }

    /// Evaluates the DA vector using the coefficients in argument `values`
    /// as the values for each monomial.
    ///
    /// This is equivalent to a monomial-wise dot product of two DA vectors.
    ///
    /// # Arguments
    ///
    /// * `values` - DA vector containing the values of each monomial
    pub fn eval_monomials<T: AsRef<DA>>(&self, values: T) -> f64 {
        let out = unsafe { daceEvalMonomials(self, values.as_ref()) };
        check_exception_panic();
        out
    }

    /// Partial evaluation of a DA object.
    ///
    /// In the DA object, variable `from` is replaced by the value `val` times variable `to`.
    ///
    /// # Arguments
    ///
    /// * `from` - variable number to be replaced
    /// * `to` - variable number to be inserted instead
    /// * `val` - value by which to scale the inserted variable
    pub fn replace_variable(&self, from: u32, to: u32, val: f64) -> DA {
        let mut out = DA::new();
        unsafe { daceReplaceVariable(self, from, to, val, &mut out) };
        check_exception_panic();
        out
    }

    /// Scaling of an independent variable.
    ///
    /// In the DA object, variable `var` is replaced by the value `val` times `var`.
    ///
    /// # Arguments
    ///
    /// * `var` - variable number to be scaled
    /// * `val` - value by which to scale the variable
    pub fn scale_variable(&self, var: u32, val: f64) -> DA {
        let mut out = DA::new();
        unsafe { daceScaleVariable(self, var, val, &mut out) };
        check_exception_panic();
        out
    }

    /// Affine translation of an independent variable.
    ///
    /// In the DA object, variable `var` is replaced by `a * var + c`.
    ///
    /// # Arguments
    ///
    /// * `var` - variable number to be translated
    /// * `a` - value by which to scale the variable
    /// * `c` - value by which to shift the variable
    pub fn translate_variable(&self, var: u32, a: f64, c: f64) -> DA {
        let mut out = DA::new();
        unsafe { daceTranslateVariable(self, var, a, c, &mut out) };
        check_exception_panic();
        out
    }

    /// Check if the DA object has only a constant part.
    pub fn is_constant(&self) -> bool {
        *self == self.cons()
    }

    /// Print the content of the DA object.
    pub fn print(&self) {
        unsafe { dacePrint(self) };
    }

    /// Convert a DA object to bytes.
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut size = 0;
        unsafe { daceExportBlob(self, null_mut(), &mut size) };
        let mut out = vec![0; size as usize];
        unsafe { daceExportBlob(self, out.as_mut_ptr().cast(), &mut size) };
        check_exception_panic();
        out
    }
}

impl AsRef<DA> for DA {
    #[inline]
    fn as_ref(&self) -> &DA {
        self
    }
}

impl AsMut<DA> for DA {
    #[inline]
    fn as_mut(&mut self) -> &mut DA {
        self
    }
}

/// Assignment of a value to an object.
pub trait Assign<T> {
    /// Perform the assignment operation.
    fn assign(&mut self, oth: T);
}

impl Assign<(u32, f64)> for DA {
    /// Assign a value to the DA variable, as `c` times the `i`-th variable.
    ///
    /// # Arguments
    ///
    /// * `i` - variable number
    /// * `c` - multiplicative constant
    ///
    /// # Example
    ///
    /// ```
    /// use dace::*;
    /// DA::init(3, 2);
    /// // create a var using the `assign` method
    /// let mut var_1 = DA::new();
    /// var_1.assign((2, 3.0));
    /// // create the same value in another way
    /// let var_2 = da!(2) * 3.0;
    /// // check that they are equal
    /// assert_eq!(var_1, var_2);
    /// ```
    fn assign(&mut self, oth: (u32, f64)) {
        unsafe { daceCreateVariable(self, oth.0, oth.1) };
        check_exception_panic();
    }
}

impl Assign<u32> for DA {
    /// Assign the value of the n-th variable to this DA variable.
    ///
    /// # Arguments
    ///
    /// * `oth` - variable number to be assigned
    fn assign(&mut self, oth: u32) {
        unsafe { daceCreateVariable(self, oth, 1.0) };
        check_exception_panic();
    }
}

impl Assign<f64> for DA {
    /// Assign the value of a f64 constant to this DA variable.
    ///
    /// # Arguments
    ///
    /// * `oth` - f64 to be assigned
    fn assign(&mut self, oth: f64) {
        unsafe { daceCreateConstant(self, oth) };
        check_exception_panic();
    }
}

impl Assign<&DA> for DA {
    /// Assign the value of another DA variable to this DA variable.
    ///
    /// # Arguments
    ///
    /// * `oth` - DA variable to copy from
    ///
    /// # Example
    ///
    /// ```
    /// use dace::*;
    /// DA::init(3, 2);
    /// // create a value in another way
    /// let var_1 = da!(2) * 3.0;
    /// // create a var using the `assign` method
    /// let mut var_2 = DA::new();
    /// var_2.assign(&var_1);
    /// // check that they are equal
    /// assert_eq!(var_1, var_2);
    /// ```
    fn assign(&mut self, oth: &DA) {
        unsafe { daceCopy(oth, self) };
        check_exception_panic();
    }
}

impl Assign<DA> for DA {
    /// Assign the value of another DA variable to this DA variable.
    ///
    /// # Arguments
    ///
    /// * `val` - DA variable to copy from
    ///
    /// # Example
    ///
    /// ```
    /// use dace::*;
    /// DA::init(3, 2);
    /// // create a value in another way
    /// let var_1 = da!(2) * 3.0;
    /// let var_1_clone = var_1.clone();
    /// // create a var using the `assign` method
    /// let mut var_2 = DA::new();
    /// var_2.assign(var_1);
    /// // check that they are equal
    /// assert_eq!(var_1_clone, var_2);
    /// ```
    fn assign(&mut self, mut oth: DA) {
        *self = Self {
            len: oth.len,
            max: oth.max,
            dmonomial: oth.dmonomial,
        };
        unsafe { daceInvalidateDA(&mut oth) };
        check_exception_panic();
    }
}

impl PartialEq<f64> for DA {
    fn eq(&self, other: &f64) -> bool {
        (self - *other).size() == 0
    }
}

impl PartialEq for DA {
    fn eq(&self, other: &Self) -> bool {
        (self - other).size() == 0
    }
}

impl Eq for DA {}

impl Display for DA {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut nstr = unsafe { daceGetMaxMonomials() } + 2;
        let mut vec = vec![0 as c_char; nstr as usize * DACE_STRLEN];
        let vec_ptr = vec.as_mut_ptr();
        unsafe { daceWrite(self, vec_ptr, &mut nstr) };
        for i in 0..nstr {
            let ptr = unsafe { vec_ptr.offset((i as usize * DACE_STRLEN) as isize) };
            let s = unsafe { CStr::from_ptr(ptr) };
            let s = s.to_str().expect("Error decoding string");
            writeln!(f, "{s}")?;
        }
        check_exception_panic();
        Ok(())
    }
}

impl ScalarOperand for DA {}
impl ScalarOperand for &'static DA {}

// Add

impl_op_ex!(+ |a: &DA, b: &DA| -> DA {
    let mut c = DA::new();
    unsafe { daceAdd(a, b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(+= |a: &mut DA, b: &DA| {
    unsafe { daceAdd(a, b, a) };
    check_exception_panic();
});

impl_op_ex_commutative!(+ |a: &DA, b: &f64| -> DA {
    let mut c = DA::new();
    unsafe { daceAddDouble(a, *b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(+= |a: &mut DA, b: &f64| {
    unsafe { daceAddDouble(a, *b, a) };
    check_exception_panic();
});

// Sub

impl_op_ex!(-|a: &DA, b: &DA| -> DA {
    let mut c = DA::new();
    unsafe { daceSubtract(a, b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(-= |a: &mut DA, b: &DA| {
    unsafe { daceSubtract(a, b, a) };
    check_exception_panic();
});

impl_op_ex!(-|a: &DA, b: &f64| -> DA {
    let mut c = DA::new();
    unsafe { daceSubtractDouble(a, *b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(-|a: &f64, b: &DA| -> DA {
    let mut c = DA::new();
    unsafe { daceDoubleSubtract(b, *a, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(-= |a: &mut DA, b: &f64| {
    unsafe { daceSubtractDouble(a, *b, a) };
    check_exception_panic();
});

// Mul

impl_op_ex!(*|a: &DA, b: &DA| -> DA {
    let mut c = DA::new();
    unsafe { daceMultiply(a, b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(*= |a: &mut DA, b: &DA| {
    unsafe { daceMultiply(a, b, a) };
    check_exception_panic();
});

impl_op_ex_commutative!(*|a: &DA, b: &f64| -> DA {
    let mut c = DA::new();
    unsafe { daceMultiplyDouble(a, *b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(*= |a: &mut DA, b: &f64| {
    unsafe { daceMultiplyDouble(a, *b, a) };
    check_exception_panic();
});

// Div

impl_op_ex!(/ |a: &DA, b: &DA| -> DA {
    let mut c = DA::new();
    unsafe { daceDivide(a, b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(/= |a: &mut DA, b: &DA| {
    unsafe { daceDivide(a, b, a) };
    check_exception_panic();
});

impl_op_ex!(/ |a: &DA, b: &f64| -> DA {
    let mut c = DA::new();
    unsafe { daceDivideDouble(a, *b, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(/ |a: f64, b: &DA| -> DA {
    let mut c = DA::new();
    unsafe { daceDoubleDivide(b, a, &mut c) };
    check_exception_panic();
    c
});

impl_op_ex!(/= |a: &mut DA, b: &f64| {
    unsafe { daceDivideDouble(a, *b, a) };
    check_exception_panic();
});

// Neg

impl ops::Neg for DA {
    type Output = DA;
    fn neg(self) -> Self::Output {
        self * -1.0
    }
}

impl ops::Neg for &DA {
    type Output = DA;
    fn neg(self) -> Self::Output {
        self * -1.0
    }
}

// Mod

impl ops::Rem<f64> for DA {
    type Output = DA;
    /// Compute the floating-point remainder of `c`/`p` (`c` modulo `p`),
    /// where `c` is the constant part of the current DA object.
    ///
    /// # Arguments
    ///
    /// * `p` - costant with respect to which the modulo function is computed.
    fn rem(self, rhs: f64) -> Self::Output {
        &self % rhs
    }
}

impl ops::Rem<f64> for &DA {
    type Output = DA;
    /// Compute the floating-point remainder of `c`/`p` (`c` modulo `p`),
    /// where `c` is the constant part of the current DA object.
    ///
    /// # Arguments
    ///
    /// * `p` - costant with respect to which the modulo function is computed.
    fn rem(self, rhs: f64) -> Self::Output {
        let mut out = DA::new();
        unsafe { daceModulo(self, rhs, &mut out) };
        check_exception_panic();
        out
    }
}

impl ops::RemAssign<f64> for DA {
    /// Compute the floating-point remainder of `c`/`p` (`c` modulo `p`),
    /// where `c` is the constant part of the current DA object.
    ///
    /// # Arguments
    ///
    /// * `p` - costant with respect to which the modulo function is computed.
    fn rem_assign(&mut self, rhs: f64) {
        unsafe { daceModulo(self, rhs, self) };
        check_exception_panic();
    }
}

impl Pow<i32> for &DA {
    type Output = DA;
    /// Elevate a DA object to a given integer power.
    ///
    /// # Arguments
    ///
    /// * `p` - power to which the DA object is raised
    fn pow(self: Self, p: i32) -> DA {
        let mut out = DA::new();
        unsafe { dacePower(self, p, &mut out) };
        check_exception_panic();
        out
    }
}

impl Pow<f64> for &DA {
    type Output = DA;
    /// Elevate a DA object to a given real power.
    ///
    /// # Arguments
    ///
    /// * `p` - power to which the DA object is raised
    fn pow(self: Self, p: f64) -> DA {
        let mut out = DA::new();
        unsafe { dacePowerDouble(self, p, &mut out) };
        check_exception_panic();
        out
    }
}

impl<T: AsRef<DA>> Pow<T> for &DA {
    type Output = DA;
    /// Elevate a DA object to a given DA power.
    ///
    /// # Arguments
    ///
    /// * `p` - power to which the DA object is raised
    fn pow(self: Self, p: T) -> DA {
        let p_ref = p.as_ref();
        // avoid using log formula if p is constant
        if p_ref.is_constant() {
            return self.pow(p_ref.cons());
        }
        let mut out = DA::new();
        unsafe {
            daceLogarithm(self, &mut out);
            daceMultiply(p_ref, &out, &mut out);
            daceExponential(&out, &mut out);
        }
        check_exception_panic();
        out
    }
}

impl From<(u32, f64)> for DA {
    fn from(ic: (u32, f64)) -> Self {
        let mut out = Self::new();
        out.assign(ic);
        out
    }
}

impl From<u32> for DA {
    fn from(i: u32) -> Self {
        let mut out = Self::new();
        out.assign(i);
        out
    }
}

impl From<f64> for DA {
    fn from(c: f64) -> Self {
        let mut out = Self::new();
        out.assign(c);
        out
    }
}

impl From<&DA> for DA {
    fn from(oth: &DA) -> Self {
        oth.to_owned()
    }
}

impl TryFrom<&Vec<u8>> for DA {
    type Error = DACEException;

    /// Convert bytes to DA objects.
    ///
    /// # Example
    ///
    /// ```
    /// use dace::*;
    /// DA::init(10, 2);
    /// let x = da!(1) * 2.0 + da!(2).pow(2);
    /// let bytes = x.to_bytes();
    /// let x_decoded = DA::try_from(&bytes).unwrap();
    /// assert_eq!(x, x_decoded);
    /// ```
    fn try_from(blob: &Vec<u8>) -> Result<Self, DACEException> {
        let mut out = Self::new();
        unsafe { daceImportBlob(blob.as_ptr().cast(), &mut out) };
        check_exception()?;
        Ok(out)
    }
}

impl FromStr for DA {
    type Err = DACEException;

    /// Convert a string to a DA object.
    fn from_str(s: &str) -> Result<Self, DACEException> {
        let mut out = Self::new();
        let nstr = s.lines().count();
        let mut vec = vec![0 as c_char; nstr * DACE_STRLEN];
        let mut vec_ptr = vec.as_mut_ptr();
        unsafe {
            for line in s.lines() {
                vec_ptr.copy_from(line.as_ptr() as *const c_char, line.len());
                vec_ptr = vec_ptr.offset(DACE_STRLEN as isize);
            }
            daceRead(&mut out, vec.as_ptr(), nstr as c_uint);
        }
        check_exception()?;
        Ok(out)
    }
}

impl Clone for DA {
    fn clone(&self) -> Self {
        let mut out = Self::new();
        unsafe { daceCopy(self, &mut out) };
        check_exception_panic();
        out
    }
}

impl Hash for DA {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.to_bytes().hash(state);
    }
}

impl Drop for DA {
    /// Destroy a DA object and free the associated object in the DACE core.
    fn drop(&mut self) {
        unsafe {
            daceFreeDA(self);
            if daceGetError() != 0 {
                daceClearError();
            }
        }
    }
}

impl Compile for DA {
    // Compile current DA object and create a compiledDA object.
    fn compile(&self) -> CompiledDA {
        darray![self.to_owned()].compile()
    }
}

impl Default for DA {
    /// Get a zero-valued constant DA object.
    fn default() -> Self {
        Self::new()
    }
}

impl Zero for DA {
    fn zero() -> Self {
        DA::new()
    }
    fn is_zero(&self) -> bool {
        self.size() == 0
    }
    fn set_zero(&mut self) {
        self.assign(0.0)
    }
}

impl One for DA {
    fn one() -> Self {
        da!(1.0)
    }
    fn is_one(&self) -> bool
    where
        Self: PartialEq,
    {
        *self == 1.0
    }
    fn set_one(&mut self) {
        self.assign(1.0);
    }
}

/// Evaluation of a DA object with another object.
pub trait Eval<Args> {
    /// The resulting type after applying the evaluation operation.
    type Output;
    /// Perform the evaluation operation.
    fn eval(&self, args: Args) -> Self::Output;
}

impl Eval<&AlgebraicVector<f64>> for CompiledDA {
    type Output = AlgebraicVector<f64>;
    fn eval(&self, args: &AlgebraicVector<f64>) -> Self::Output {
        let mut res = AlgebraicVector::<f64>::zeros(self.dim);
        let narg = args.len();
        let mut xm = AlgebraicVector::<f64>::ones(self.ord as usize + 1);

        // constant part
        for i in 0..self.dim {
            res[i] = self.ac[i + 2];
        }

        // higher order terms
        let mut p = 2 + self.dim as usize;
        for _ in 1..self.terms {
            let jl = self.ac[p] as usize;
            p += 1;
            let jv = self.ac[p] as usize - 1;
            p += 1;
            if jv < narg {
                xm[jl] = xm[jl - 1] * args[jv];
            } else {
                xm[jl] = 0.0;
            }
            for j in 0..self.dim {
                res[j] += xm[jl] * self.ac[p];
                p += 1;
            }
        }
        res
    }
}

impl Eval<&AlgebraicVector<DA>> for CompiledDA {
    type Output = AlgebraicVector<DA>;
    fn eval(&self, args: &AlgebraicVector<DA>) -> Self::Output {
        let mut res = AlgebraicVector::<DA>::zeros(self.dim);
        let narg = args.len();
        let mut jlskip = self.ord as usize + 1;
        let mut p: usize = 2;
        let mut xm = AlgebraicVector::<DA>::ones(self.ord as usize + 1);
        let mut tmp = DA::new();

        // constant part
        for i in 0..self.dim {
            res[i].assign(self.ac[p]);
            p += 1;
        }

        // higher order terms
        for _ in 1..self.terms {
            let jl = self.ac[p] as usize;
            p += 1;
            let jv = self.ac[p] as usize - 1;
            p += 1;
            if jl > jlskip {
                p += self.dim;
                continue;
            }
            if jv >= narg {
                jlskip = jl;
                p += self.dim;
                continue;
            }
            jlskip = self.ord as usize + 1;
            unsafe { daceMultiply(&xm[jl - 1], &args[jv], &mut xm[jl]) };
            for j in 0..self.dim {
                if self.ac[p] != 0.0 {
                    unsafe { daceMultiplyDouble(&xm[jl], self.ac[p], &mut tmp) };
                    res[j] += &tmp;
                }
                p += 1;
            }
        }

        check_exception_panic();
        res
    }
}

impl Eval<&AlgebraicVector<f64>> for DA {
    type Output = f64;
    fn eval(&self, args: &AlgebraicVector<f64>) -> Self::Output {
        self.compile().eval(args)[0]
    }
}

impl Eval<&AlgebraicVector<f64>> for AlgebraicVector<DA> {
    type Output = AlgebraicVector<f64>;
    fn eval(&self, args: &AlgebraicVector<f64>) -> Self::Output {
        self.compile().eval(args)
    }
}

impl Eval<&AlgebraicVector<DA>> for AlgebraicVector<DA> {
    type Output = AlgebraicVector<DA>;
    fn eval(&self, args: &AlgebraicVector<DA>) -> Self::Output {
        self.compile().eval(args)
    }
}

impl Eval<&AlgebraicVector<DA>> for DA {
    type Output = DA;
    fn eval(&self, args: &AlgebraicVector<DA>) -> DA {
        let x = self.compile();
        CompiledDA::eval(&x, args)[0].to_owned()
    }
}

impl Eval<f64> for CompiledDA {
    type Output = AlgebraicVector<f64>;
    fn eval(&self, arg: f64) -> Self::Output {
        self.eval(&darray![arg]).into()
    }
}

impl<T: AsRef<DA>> Eval<T> for CompiledDA {
    type Output = AlgebraicVector<DA>;
    fn eval(&self, arg: T) -> Self::Output {
        self.eval(&darray![arg.as_ref().to_owned()]).into()
    }
}

impl<T: AsRef<DA>> Eval<T> for AlgebraicVector<DA> {
    type Output = AlgebraicVector<DA>;
    fn eval(&self, arg: T) -> Self::Output {
        self.compile().eval(arg.as_ref())
    }
}

impl<T: AsRef<DA>> Eval<T> for DA {
    type Output = DA;
    fn eval(&self, arg: T) -> Self::Output {
        self.compile().eval(arg.as_ref())[0].to_owned()
    }
}

impl Eval<f64> for DA {
    type Output = f64;
    fn eval(&self, arg: f64) -> Self::Output {
        self.compile().eval(arg)[0].to_owned()
    }
}