tnorms 0.2.0

T-norm and t-conorm families
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
//! T-conorm (s-norm) families for fuzzy logic and differentiable relaxations.
//!
//! A t-conorm `S: [0, 1]^2 -> [0, 1]` generalizes logical OR. It is:
//! - Commutative: S(a, b) = S(b, a)
//! - Associative: S(S(a, b), c) = S(a, S(b, c))
//! - Monotone: a1 <= a2 => S(a1, b) <= S(a2, b)
//! - Bounded: S(a, 0) = a (0 is identity)
//!
//! Different families provide different "softness" of the OR operation,
//! useful in differentiable relaxations and probabilistic reasoning.
//!
//! ## Named families
//!
//! The constants [`GODEL`], [`PRODUCT`], and [`LUKASIEWICZ`] name the three
//! common fuzzy-logic families. [`LogicFamily`] adds the corresponding
//! residuum for implication:
//!
//! ```
//! assert_eq!(tnorms::tnorm(tnorms::GODEL, 0.4, 0.7), 0.4);
//! assert!((tnorms::tnorm(tnorms::PRODUCT, 0.4, 0.7) - 0.28).abs() < 1e-12);
//! assert!((tnorms::tnorm(tnorms::LUKASIEWICZ, 0.4, 0.7) - 0.1).abs() < 1e-12);
//! assert_eq!(tnorms::LogicFamily::Godel.residuum(0.7, 0.4), 0.4);
//! assert_eq!(tnorms::LogicFamily::Product.tnorm_gradient(0.4, 0.7).da, 0.7);
//! ```
//!
//! [`TruthAlgebra`] exposes the same three families as zero-sized marker types
//! for crates that choose the logic at the type level:
//!
//! ```
//! use tnorms::{Product, TruthAlgebra};
//!
//! assert!((Product::tnorm_f32(0.4, 0.7) - 0.28).abs() < 1e-6);
//! ```
//!
//! The generic [`TConormFamily`] catalog is an aggregation catalog.
//! [`residuum`] is available for families with a known left-continuous path;
//! [`LogicFamily`] remains the standard fuzzy-logic surface where the t-norm
//! and residuum form the adjoint pair by construction.
//!
//! ## Catalog
//!
//! | Family | Formula S(a,b) | Parameter | Behavior |
//! |--------|---------------|-----------|----------|
//! | Maximum | max(a,b) | none | Hard OR |
//! | Probabilistic | a+b-ab | none | Independent events |
//! | Bounded (Lukasiewicz) | min(1, a+b) | none | Saturating sum |
//! | Einstein | (a+b)/(1+ab) | none | Smooth OR |
//! | Hamacher | (a+b-2ab)/(1-ab) | none | Aggressive |
//! | Yager | min(1, (a^p + b^p)^(1/p)) | p >= 1 | Lp norm |
//! | Frank | 1 - log_s(1+(s^(1-a)-1)(s^(1-b)-1)/(s-1)) | s > 0 | Interpolates max<->bounded |
//! | Dombi | 1/(1+((1/a-1)^p + (1/b-1)^p)^(-1/p)) | p > 0 | Power-based |
//! | Schweizer-Sklar | dual of `(x^p+y^p-1)^(1/p)` | p | spans min/product/Łukasiewicz/drastic |
//! | Sugeno-Weber | dual of `max(0,(x+y-1+pxy)/(1+p))` | p >= -1 | spans drastic/Łukasiewicz/product |
//! | Aczel-Alsina | dual of `exp(-(((-ln x)^p+(-ln y)^p)^(1/p)))` | p > 0 | product-to-min family |
//! | Mayor-Torrens | ordinal-sum dual | `p in [0, 1]` | min-to-Łukasiewicz ordinal sum |
//! | Drastic | drastic sum | none | discontinuous limit |
//! | Nilpotent minimum | nilpotent-minimum dual | none | left-continuous threshold |
//!
//! Based on: Petersen et al., gendr -- Generalized Differentiable Rendering.
//! The t-conorm catalog provides the relaxation families used for differentiable
//! logical operations and soft aggregation.

#![forbid(unsafe_code)]
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]

use num_traits::Float;

mod formulas;
mod operators;
mod ordinal;

use formulas::*;

pub use operators::{
    implication, log_product, negation, owa, power_mean, power_mean_weighted, product_from_log,
    representable_uninorm, ImplicationFamily, MixedRegion, NegationFamily,
};
pub use ordinal::{OrdinalSegment, OrdinalSum};

/// Evaluate a t-conorm S(a, b) for the given family.
pub fn tconorm(family: TConormFamily, a: f64, b: f64) -> f64 {
    tconorm_generic(family, a, b)
}

/// Evaluate a t-conorm S(a, b) for the given family and float type.
pub fn tconorm_generic<T: Float>(family: TConormFamily, a: T, b: T) -> T {
    let a = clamp01(a);
    let b = clamp01(b);

    match family {
        TConormFamily::Maximum => a.max(b),
        TConormFamily::Probabilistic => a + b - a * b,
        TConormFamily::Bounded => (a + b).min(T::one()),
        TConormFamily::Einstein => {
            let denom = T::one() + a * b;
            if denom == T::zero() {
                T::zero()
            } else {
                (a + b) / denom
            }
        }
        TConormFamily::Hamacher => {
            let denom = T::one() - a * b;
            if denom.abs() < cast(1e-15) {
                T::one()
            } else {
                (a + b - cast::<T>(2.0) * a * b) / denom
            }
        }
        TConormFamily::Yager { p } => {
            debug_assert!(p > 0.0, "Yager p must be > 0");
            scaled_lp_norm_2(a, b, cast(p)).min(T::one())
        }
        TConormFamily::Frank { s } => {
            debug_assert!(s > 0.0, "Frank s must be > 0");
            frank_tconorm(a, b, cast(s))
        }
        TConormFamily::Dombi { p } => {
            debug_assert!(p > 0.0, "Dombi p must be > 0");
            dombi_tconorm(a, b, cast(p))
        }
        TConormFamily::Drastic => drastic_tconorm(a, b),
        TConormFamily::NilpotentMinimum => nilpotent_minimum_tconorm(a, b),
        TConormFamily::SchweizerSklar { p } => {
            T::one() - schweizer_sklar_tnorm(T::one() - a, T::one() - b, cast(p))
        }
        TConormFamily::SugenoWeber { p } => {
            T::one() - sugeno_weber_tnorm(T::one() - a, T::one() - b, cast(p))
        }
        TConormFamily::AczelAlsina { p } => {
            T::one() - aczel_alsina_tnorm(T::one() - a, T::one() - b, cast(p))
        }
        TConormFamily::MayorTorrens { p } => {
            T::one() - mayor_torrens_tnorm(T::one() - a, T::one() - b, cast(p))
        }
    }
}

/// T-conorm family selection.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TConormFamily {
    /// S(a,b) = max(a,b). Hard OR, not differentiable at a=b.
    Maximum,
    /// S(a,b) = a+b-ab. Probabilistic sum (independent events).
    Probabilistic,
    /// S(a,b) = min(1, a+b). Lukasiewicz / bounded sum.
    Bounded,
    /// S(a,b) = (a+b)/(1+ab). Smooth, well-behaved for optimization.
    Einstein,
    /// S(a,b) = (a+b-2ab)/(1-ab). More aggressive than probabilistic.
    Hamacher,
    /// S(a,b) = min(1, (a^p + b^p)^(1/p)). Lp-norm family.
    Yager {
        /// Exponent, must be >= 1. p=1 gives Bounded, p->inf gives Maximum.
        p: f64,
    },
    /// Frank t-conorm parameterized by base s.
    /// s->0 gives Maximum, s=1 gives Probabilistic (limit), s->inf gives Bounded.
    Frank {
        /// Base parameter, must be > 0. `s=1` uses the probabilistic limit.
        s: f64,
    },
    /// Dombi t-conorm. p->0 gives Maximum, p->inf gives Bounded.
    Dombi {
        /// Exponent, must be > 0.
        p: f64,
    },
    /// Drastic sum, the dual of the drastic product.
    Drastic,
    /// Nilpotent-minimum dual t-conorm.
    NilpotentMinimum,
    /// Schweizer-Sklar t-conorm, parameterized by `p`.
    SchweizerSklar {
        /// Shape parameter. `p=0` is Product, `p=1` is Lukasiewicz,
        /// `p=-inf` is Godel, and `p=inf` is Drastic.
        p: f64,
    },
    /// Sugeno-Weber t-conorm, parameterized by the t-norm parameter `p`.
    SugenoWeber {
        /// Shape parameter, with valid finite values `p >= -1`.
        /// `p=-1` is Drastic, `p=0` is Lukasiewicz, and `p=inf` is Product.
        p: f64,
    },
    /// Aczel-Alsina t-conorm.
    AczelAlsina {
        /// Shape parameter, must be > 0. `p=1` is Product and `p->inf`
        /// approaches Godel.
        p: f64,
    },
    /// Mayor-Torrens ordinal-sum t-conorm.
    MayorTorrens {
        /// Łukasiewicz segment size in `[0, 1]`.
        p: f64,
    },
}

/// Whether a t-norm family is known to satisfy the copula constraints.
///
/// `ParameterDependent` and `Unknown` deliberately avoid collapsing uncertainty
/// to `false`: they mean callers should not use the family as a copula without
/// checking the chosen parameter range against a copula-specific reference.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CopulaCompatibility {
    /// Every supported parameter value is a copula.
    Always,
    /// No supported parameter value is a copula.
    Never,
    /// Compatibility depends on the parameter value.
    ParameterDependent,
    /// Compatibility is not asserted by this crate.
    Unknown,
}

/// Short alias for selecting a t-norm/t-conorm family.
pub type Family = TConormFamily;

/// Godel family: `min(a, b)` t-norm and `max(a, b)` t-conorm.
pub const GODEL: Family = Family::Maximum;

/// Product family: `a * b` t-norm and `a + b - a * b` t-conorm.
pub const PRODUCT: Family = Family::Probabilistic;

/// Lukasiewicz family: `max(0, a + b - 1)` t-norm and `min(1, a + b)` t-conorm.
pub const LUKASIEWICZ: Family = Family::Bounded;

impl TConormFamily {
    /// Return conservative copula-compatibility metadata for this family.
    ///
    /// The three common logics (`Maximum`, `Probabilistic`, `Bounded`) are the
    /// Frechet-Hoeffding upper, independence, and lower copulas. Frank is the
    /// classical Frank copula family. Drastic and nilpotent-minimum violate the
    /// copula constraints. Other catalog families need parameter-specific
    /// treatment, so this method reports `ParameterDependent` or `Unknown`
    /// rather than pretending a t-norm law is also a copula law.
    pub fn copula_compatibility(self) -> CopulaCompatibility {
        match self {
            Self::Maximum | Self::Probabilistic | Self::Bounded | Self::Frank { .. } => {
                CopulaCompatibility::Always
            }
            Self::Drastic | Self::NilpotentMinimum => CopulaCompatibility::Never,
            Self::Dombi { .. }
            | Self::SchweizerSklar { .. }
            | Self::SugenoWeber { .. }
            | Self::AczelAlsina { .. }
            | Self::MayorTorrens { .. } => CopulaCompatibility::ParameterDependent,
            Self::Einstein | Self::Hamacher | Self::Yager { .. } => CopulaCompatibility::Unknown,
        }
    }

    /// Return `Some(true)` or `Some(false)` for families with unconditional
    /// copula status.
    ///
    /// Returns `None` when status depends on parameters or is not asserted.
    pub fn is_copula(self) -> Option<bool> {
        match self.copula_compatibility() {
            CopulaCompatibility::Always => Some(true),
            CopulaCompatibility::Never => Some(false),
            CopulaCompatibility::ParameterDependent | CopulaCompatibility::Unknown => None,
        }
    }

    /// Return this family's additive generator when it has one.
    ///
    /// Godel/minimum, drastic, nilpotent minimum, and Mayor-Torrens are not
    /// single Archimedean generator families on the whole unit interval.
    pub fn generator(self) -> Option<GeneratorFamily> {
        match self {
            Self::Maximum | Self::Drastic | Self::NilpotentMinimum | Self::MayorTorrens { .. } => {
                None
            }
            Self::Probabilistic => Some(GeneratorFamily::Product),
            Self::Bounded => Some(GeneratorFamily::Lukasiewicz),
            Self::Einstein => Some(GeneratorFamily::Hamacher { p: 2.0 }),
            Self::Hamacher => Some(GeneratorFamily::Hamacher { p: 0.0 }),
            Self::Yager { p } if p > 0.0 => Some(GeneratorFamily::Yager { p }),
            Self::Frank { s } if s > 0.0 => Some(GeneratorFamily::Frank { s }),
            Self::Dombi { p } if p > 0.0 => Some(GeneratorFamily::Dombi { p }),
            Self::SchweizerSklar { p } if p.is_finite() => {
                Some(GeneratorFamily::SchweizerSklar { p })
            }
            Self::SugenoWeber { p } if p > -1.0 && p.is_finite() => {
                Some(GeneratorFamily::SugenoWeber { p })
            }
            Self::AczelAlsina { p } if p > 0.0 && p.is_finite() => {
                Some(GeneratorFamily::AczelAlsina { p })
            }
            _ => None,
        }
    }

    /// Evaluate this family's t-conorm.
    #[inline]
    pub fn tconorm(self, a: f64, b: f64) -> f64 {
        tconorm(self, a, b)
    }

    /// Evaluate this family's t-conorm with a generic float type.
    #[inline]
    pub fn tconorm_generic<T: Float>(self, a: T, b: T) -> T {
        tconorm_generic(self, a, b)
    }

    /// Evaluate this family's standard-negation dual t-norm.
    #[inline]
    pub fn tnorm(self, a: f64, b: f64) -> f64 {
        tnorm(self, a, b)
    }

    /// Evaluate this family's standard-negation dual t-norm with a generic float type.
    #[inline]
    pub fn tnorm_generic<T: Float>(self, a: T, b: T) -> T {
        tnorm_generic(self, a, b)
    }

    /// Evaluate the residuum `a -> b` when this family has a known path.
    ///
    /// Archimedean generator families use `f^-1(max(0, f(b) - f(a)))`.
    /// Godel, nilpotent minimum, and Mayor-Torrens use direct or numeric
    /// left-continuous t-norm residuation. Drastic returns `None` because it is
    /// not left-continuous.
    pub fn residuum(self, a: f64, b: f64) -> Option<f64> {
        residuum(self, a, b)
    }

    /// Evaluate the t-norm gradient with respect to `(a, b)`.
    #[inline]
    pub fn tnorm_gradient(self, a: f64, b: f64) -> BinaryGradient<f64> {
        tnorm_gradient(self, a, b)
    }

    /// Evaluate the t-conorm gradient with respect to `(a, b)`.
    #[inline]
    pub fn tconorm_gradient(self, a: f64, b: f64) -> BinaryGradient<f64> {
        tconorm_gradient(self, a, b)
    }
}

/// Archimedean additive-generator family.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GeneratorFamily {
    /// Product generator `f(x) = -ln(x)`.
    Product,
    /// Lukasiewicz generator `f(x) = 1 - x`.
    Lukasiewicz,
    /// Hamacher generator with parameter `p >= 0`.
    Hamacher {
        /// Shape parameter.
        p: f64,
    },
    /// Frank generator with base `s > 0`.
    Frank {
        /// Base parameter. `s=1` uses the product limit.
        s: f64,
    },
    /// Yager generator with exponent `p > 0`.
    Yager {
        /// Shape parameter.
        p: f64,
    },
    /// Dombi generator with exponent `p > 0`.
    Dombi {
        /// Shape parameter.
        p: f64,
    },
    /// Schweizer-Sklar generator.
    SchweizerSklar {
        /// Shape parameter.
        p: f64,
    },
    /// Sugeno-Weber generator.
    SugenoWeber {
        /// Shape parameter, with valid finite values `p > -1`.
        p: f64,
    },
    /// Aczel-Alsina generator.
    AczelAlsina {
        /// Shape parameter.
        p: f64,
    },
}

impl GeneratorFamily {
    /// Evaluate the additive generator `f(x)`.
    pub fn value(self, x: f64) -> f64 {
        let x = x.clamp(0.0, 1.0);
        match self {
            Self::Product => {
                if x <= 0.0 {
                    f64::INFINITY
                } else {
                    -x.ln()
                }
            }
            Self::Lukasiewicz => 1.0 - x,
            Self::Hamacher { p } => hamacher_generator(x, p),
            Self::Frank { s } => frank_generator(x, s),
            Self::Yager { p } => (1.0 - x).powf(p),
            Self::Dombi { p } => {
                if x <= 0.0 {
                    f64::INFINITY
                } else if x >= 1.0 {
                    0.0
                } else {
                    ((1.0 - x) / x).powf(p)
                }
            }
            Self::SchweizerSklar { p } => {
                if p.abs() <= FRANK_PRODUCT_LIMIT_EPS {
                    Self::Product.value(x)
                } else if x <= 0.0 && p <= 0.0 {
                    f64::INFINITY
                } else {
                    (1.0 - x.powf(p)) / p
                }
            }
            Self::SugenoWeber { p } => {
                if p.abs() <= FRANK_PRODUCT_LIMIT_EPS {
                    1.0 - x
                } else {
                    1.0 - (p.mul_add(x, 1.0)).ln() / (1.0 + p).ln()
                }
            }
            Self::AczelAlsina { p } => {
                if x <= 0.0 {
                    f64::INFINITY
                } else {
                    (-x.ln()).powf(p)
                }
            }
        }
    }

    /// Evaluate the generator pseudo-inverse `f^-1(y)`.
    pub fn inverse(self, y: f64) -> f64 {
        let y = y.max(0.0);
        match self {
            Self::Product => (-y).exp(),
            Self::Lukasiewicz => (1.0 - y).clamp(0.0, 1.0),
            Self::Hamacher { p } => hamacher_generator_inverse(y, p),
            Self::Frank { s } => frank_generator_inverse(y, s),
            Self::Yager { p } => (1.0 - y.powf(1.0 / p)).clamp(0.0, 1.0),
            Self::Dombi { p } => 1.0 / (1.0 + y.powf(1.0 / p)),
            Self::SchweizerSklar { p } => {
                if p.abs() <= FRANK_PRODUCT_LIMIT_EPS {
                    Self::Product.inverse(y)
                } else {
                    (1.0 - p * y).max(0.0).powf(1.0 / p).clamp(0.0, 1.0)
                }
            }
            Self::SugenoWeber { p } => {
                if p.abs() <= FRANK_PRODUCT_LIMIT_EPS {
                    (1.0 - y).clamp(0.0, 1.0)
                } else {
                    (((1.0 + p).powf(1.0 - y) - 1.0) / p).clamp(0.0, 1.0)
                }
            }
            Self::AczelAlsina { p } => (-y.powf(1.0 / p)).exp(),
        }
    }

    /// Evaluate the t-norm induced by this generator.
    pub fn tnorm(self, a: f64, b: f64) -> f64 {
        self.inverse(self.value(a) + self.value(b))
    }

    /// Evaluate the generator residuum `a -> b`.
    pub fn residuum(self, a: f64, b: f64) -> f64 {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        if a <= b {
            1.0
        } else {
            self.inverse((self.value(b) - self.value(a)).max(0.0))
        }
    }
}

/// Standard t-norm fuzzy logic families with residual implication.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogicFamily {
    /// Godel logic: minimum t-norm.
    Godel,
    /// Product logic: product t-norm.
    Product,
    /// Lukasiewicz logic: bounded-sum t-norm.
    Lukasiewicz,
}

/// Partial derivatives of a binary operation.
///
/// `da` is the derivative with respect to the first argument. `db` is the
/// derivative with respect to the second argument.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct BinaryGradient<T> {
    /// Partial derivative with respect to the first argument.
    pub da: T,
    /// Partial derivative with respect to the second argument.
    pub db: T,
}

impl<T> BinaryGradient<T> {
    /// Create a binary gradient from the two partial derivatives.
    pub const fn new(da: T, db: T) -> Self {
        Self { da, db }
    }
}

impl LogicFamily {
    /// The t-conorm catalog family corresponding to this logic.
    pub const fn family(self) -> Family {
        match self {
            Self::Godel => GODEL,
            Self::Product => PRODUCT,
            Self::Lukasiewicz => LUKASIEWICZ,
        }
    }

    /// Evaluate the t-norm conjunction.
    pub fn tnorm(self, a: f64, b: f64) -> f64 {
        tnorm(self.family(), a, b)
    }

    /// Evaluate the dual t-conorm disjunction.
    pub fn tconorm(self, a: f64, b: f64) -> f64 {
        tconorm(self.family(), a, b)
    }

    /// Evaluate the residual implication `a -> b`.
    pub fn residuum(self, a: f64, b: f64) -> f64 {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => {
                if a <= b {
                    1.0
                } else {
                    b
                }
            }
            Self::Product => {
                if a <= b {
                    1.0
                } else {
                    b / a
                }
            }
            Self::Lukasiewicz => (1.0 - a + b).min(1.0),
        }
    }

    /// Evaluate the residual negation `a -> 0`.
    pub fn neg(self, a: f64) -> f64 {
        self.residuum(a, 0.0)
    }

    /// Evaluate the t-norm gradient with respect to `(a, b)`.
    ///
    /// The inputs are clamped to `[0, 1]` before selecting the local gradient.
    /// At non-differentiable points, min/max split ties evenly and
    /// Lukasiewicz hinges use the zero subgradient.
    pub fn tnorm_gradient(self, a: f64, b: f64) -> BinaryGradient<f64> {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => min_gradient(a, b),
            Self::Product => BinaryGradient::new(b, a),
            Self::Lukasiewicz => {
                if a + b > 1.0 {
                    BinaryGradient::new(1.0, 1.0)
                } else {
                    BinaryGradient::new(0.0, 0.0)
                }
            }
        }
    }

    /// Evaluate the t-conorm gradient with respect to `(a, b)`.
    ///
    /// The inputs are clamped to `[0, 1]` before selecting the local gradient.
    /// At non-differentiable points, min/max split ties evenly and
    /// Lukasiewicz hinges use the zero subgradient.
    pub fn tconorm_gradient(self, a: f64, b: f64) -> BinaryGradient<f64> {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => max_gradient(a, b),
            Self::Product => BinaryGradient::new(1.0 - b, 1.0 - a),
            Self::Lukasiewicz => {
                if a + b < 1.0 {
                    BinaryGradient::new(1.0, 1.0)
                } else {
                    BinaryGradient::new(0.0, 0.0)
                }
            }
        }
    }

    /// Evaluate the residual-implication gradient with respect to `(a, b)`.
    ///
    /// The inputs are clamped to `[0, 1]` before selecting the local gradient.
    /// On branch boundaries where the residuum saturates to `1`, this uses the
    /// saturated branch's zero subgradient.
    pub fn residuum_gradient(self, a: f64, b: f64) -> BinaryGradient<f64> {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => {
                if a > b {
                    BinaryGradient::new(0.0, 1.0)
                } else {
                    BinaryGradient::new(0.0, 0.0)
                }
            }
            Self::Product => {
                if a > b {
                    BinaryGradient::new(-b / (a * a), 1.0 / a)
                } else {
                    BinaryGradient::new(0.0, 0.0)
                }
            }
            Self::Lukasiewicz => {
                if a > b {
                    BinaryGradient::new(-1.0, 1.0)
                } else {
                    BinaryGradient::new(0.0, 0.0)
                }
            }
        }
    }

    /// Evaluate the residual-negation gradient with respect to `a`.
    pub fn neg_gradient(self, _a: f64) -> f64 {
        match self {
            Self::Godel | Self::Product => 0.0,
            Self::Lukasiewicz => -1.0,
        }
    }

    /// Evaluate the t-norm conjunction using `f32`.
    pub fn tnorm_f32(self, a: f32, b: f32) -> f32 {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => a.min(b),
            Self::Product => a * b,
            Self::Lukasiewicz => (a + b - 1.0).max(0.0),
        }
    }

    /// Evaluate the dual t-conorm disjunction using `f32`.
    pub fn tconorm_f32(self, a: f32, b: f32) -> f32 {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => a.max(b),
            Self::Product => a + b - a * b,
            Self::Lukasiewicz => (a + b).min(1.0),
        }
    }

    /// Evaluate the residual implication using `f32`.
    pub fn residuum_f32(self, a: f32, b: f32) -> f32 {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => {
                if a <= b {
                    1.0
                } else {
                    b
                }
            }
            Self::Product => {
                if a <= b {
                    1.0
                } else {
                    b / a
                }
            }
            Self::Lukasiewicz => (1.0 - a + b).min(1.0),
        }
    }

    /// Evaluate the residual negation using `f32`.
    pub fn neg_f32(self, a: f32) -> f32 {
        self.residuum_f32(a, 0.0)
    }

    /// Evaluate the t-norm gradient with respect to `(a, b)` using `f32`.
    pub fn tnorm_gradient_f32(self, a: f32, b: f32) -> BinaryGradient<f32> {
        let gradient = self.tnorm_gradient(f64::from(a), f64::from(b));
        BinaryGradient::new(gradient.da as f32, gradient.db as f32)
    }

    /// Evaluate the t-conorm gradient with respect to `(a, b)` using `f32`.
    pub fn tconorm_gradient_f32(self, a: f32, b: f32) -> BinaryGradient<f32> {
        let gradient = self.tconorm_gradient(f64::from(a), f64::from(b));
        BinaryGradient::new(gradient.da as f32, gradient.db as f32)
    }

    /// Evaluate the residual-implication gradient with respect to `(a, b)` using `f32`.
    pub fn residuum_gradient_f32(self, a: f32, b: f32) -> BinaryGradient<f32> {
        let gradient = self.residuum_gradient(f64::from(a), f64::from(b));
        BinaryGradient::new(gradient.da as f32, gradient.db as f32)
    }

    /// Evaluate the residual-negation gradient with respect to `a` using `f32`.
    pub fn neg_gradient_f32(self, a: f32) -> f32 {
        self.neg_gradient(f64::from(a)) as f32
    }

    /// Evaluate the t-norm conjunction for Burn tensors.
    ///
    /// This method is available with the `burn` feature. Inputs are clamped to
    /// `[0, 1]` before applying the operation.
    #[cfg(feature = "burn")]
    pub fn tnorm_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        self,
        a: burn::tensor::Tensor<B, D>,
        b: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => a.min_pair(b),
            Self::Product => a * b,
            Self::Lukasiewicz => (a + b - 1.0).clamp_min(0.0),
        }
    }

    /// Evaluate the dual t-conorm disjunction for Burn tensors.
    ///
    /// This method is available with the `burn` feature. Inputs are clamped to
    /// `[0, 1]` before applying the operation.
    #[cfg(feature = "burn")]
    pub fn tconorm_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        self,
        a: burn::tensor::Tensor<B, D>,
        b: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => a.max_pair(b),
            Self::Product => {
                let product = a.clone() * b.clone();
                a + b - product
            }
            Self::Lukasiewicz => (a + b).clamp_max(1.0),
        }
    }

    /// Evaluate the residual implication `a -> b` for Burn tensors.
    ///
    /// This method is available with the `burn` feature. Inputs are clamped to
    /// `[0, 1]` before applying the operation.
    #[cfg(feature = "burn")]
    pub fn residuum_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        self,
        a: burn::tensor::Tensor<B, D>,
        b: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        let a = a.clamp(0.0, 1.0);
        let b = b.clamp(0.0, 1.0);
        match self {
            Self::Godel => {
                let mask = a.lower_equal(b.clone());
                let one = b.ones_like();
                b.mask_where(mask, one)
            }
            Self::Product => {
                let mask = a.clone().lower_equal(b.clone());
                let one = a.ones_like();
                let denominator = a.clamp_min(1e-12);
                let quotient = b / denominator;
                quotient.mask_where(mask, one)
            }
            Self::Lukasiewicz => (a.neg().add_scalar(1.0) + b).clamp_max(1.0),
        }
    }

    /// Evaluate the residual negation `a -> 0` for Burn tensors.
    ///
    /// This method is available with the `burn` feature. Inputs are clamped to
    /// `[0, 1]` before applying the operation.
    #[cfg(feature = "burn")]
    pub fn neg_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        self,
        a: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        let a = a.clamp(0.0, 1.0);
        match self {
            Self::Godel | Self::Product => {
                let zeros = a.zeros_like();
                let ones = a.ones_like();
                zeros.mask_where(a.lower_equal_elem(0.0), ones)
            }
            Self::Lukasiewicz => a.neg().add_scalar(1.0),
        }
    }
}

/// A standard residuated truth algebra on degrees in `[0, 1]`.
///
/// This trait is for type-level selection of the common fuzzy-logic families.
/// It names the t-norm conjunction, dual t-conorm disjunction, residual
/// implication, and residual negation. The methods are static because the
/// implementors are zero-sized marker types.
pub trait TruthAlgebra {
    /// The runtime logic family represented by this marker type.
    const FAMILY: LogicFamily;

    /// The fully true degree, `1.0`.
    #[inline]
    fn top() -> f64 {
        1.0
    }

    /// The fully false degree, `0.0`.
    #[inline]
    fn bottom() -> f64 {
        0.0
    }

    /// Evaluate the t-norm conjunction.
    #[inline]
    fn tnorm(a: f64, b: f64) -> f64 {
        Self::FAMILY.tnorm(a, b)
    }

    /// Evaluate the dual t-conorm disjunction.
    #[inline]
    fn tconorm(a: f64, b: f64) -> f64 {
        Self::FAMILY.tconorm(a, b)
    }

    /// Evaluate the residual implication `a -> b`.
    #[inline]
    fn residuum(a: f64, b: f64) -> f64 {
        Self::FAMILY.residuum(a, b)
    }

    /// Evaluate the residual negation `a -> 0`.
    #[inline]
    fn neg(a: f64) -> f64 {
        Self::FAMILY.neg(a)
    }

    /// Evaluate the t-norm gradient with respect to `(a, b)`.
    #[inline]
    fn tnorm_gradient(a: f64, b: f64) -> BinaryGradient<f64> {
        Self::FAMILY.tnorm_gradient(a, b)
    }

    /// Evaluate the dual t-conorm gradient with respect to `(a, b)`.
    #[inline]
    fn tconorm_gradient(a: f64, b: f64) -> BinaryGradient<f64> {
        Self::FAMILY.tconorm_gradient(a, b)
    }

    /// Evaluate the residual-implication gradient with respect to `(a, b)`.
    #[inline]
    fn residuum_gradient(a: f64, b: f64) -> BinaryGradient<f64> {
        Self::FAMILY.residuum_gradient(a, b)
    }

    /// Evaluate the residual-negation gradient with respect to `a`.
    #[inline]
    fn neg_gradient(a: f64) -> f64 {
        Self::FAMILY.neg_gradient(a)
    }

    /// The fully true `f32` degree, `1.0`.
    #[inline]
    fn top_f32() -> f32 {
        1.0
    }

    /// The fully false `f32` degree, `0.0`.
    #[inline]
    fn bottom_f32() -> f32 {
        0.0
    }

    /// Evaluate the t-norm conjunction using `f32`.
    #[inline]
    fn tnorm_f32(a: f32, b: f32) -> f32 {
        Self::FAMILY.tnorm_f32(a, b)
    }

    /// Evaluate the dual t-conorm disjunction using `f32`.
    #[inline]
    fn tconorm_f32(a: f32, b: f32) -> f32 {
        Self::FAMILY.tconorm_f32(a, b)
    }

    /// Evaluate the residual implication using `f32`.
    #[inline]
    fn residuum_f32(a: f32, b: f32) -> f32 {
        Self::FAMILY.residuum_f32(a, b)
    }

    /// Evaluate the residual negation using `f32`.
    #[inline]
    fn neg_f32(a: f32) -> f32 {
        Self::FAMILY.neg_f32(a)
    }

    /// Evaluate the t-norm gradient with respect to `(a, b)` using `f32`.
    #[inline]
    fn tnorm_gradient_f32(a: f32, b: f32) -> BinaryGradient<f32> {
        Self::FAMILY.tnorm_gradient_f32(a, b)
    }

    /// Evaluate the dual t-conorm gradient with respect to `(a, b)` using `f32`.
    #[inline]
    fn tconorm_gradient_f32(a: f32, b: f32) -> BinaryGradient<f32> {
        Self::FAMILY.tconorm_gradient_f32(a, b)
    }

    /// Evaluate the residual-implication gradient with respect to `(a, b)` using `f32`.
    #[inline]
    fn residuum_gradient_f32(a: f32, b: f32) -> BinaryGradient<f32> {
        Self::FAMILY.residuum_gradient_f32(a, b)
    }

    /// Evaluate the residual-negation gradient with respect to `a` using `f32`.
    #[inline]
    fn neg_gradient_f32(a: f32) -> f32 {
        Self::FAMILY.neg_gradient_f32(a)
    }

    /// Evaluate the t-norm conjunction for Burn tensors.
    #[cfg(feature = "burn")]
    #[inline]
    fn tnorm_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        a: burn::tensor::Tensor<B, D>,
        b: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        Self::FAMILY.tnorm_tensor(a, b)
    }

    /// Evaluate the dual t-conorm disjunction for Burn tensors.
    #[cfg(feature = "burn")]
    #[inline]
    fn tconorm_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        a: burn::tensor::Tensor<B, D>,
        b: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        Self::FAMILY.tconorm_tensor(a, b)
    }

    /// Evaluate the residual implication `a -> b` for Burn tensors.
    #[cfg(feature = "burn")]
    #[inline]
    fn residuum_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        a: burn::tensor::Tensor<B, D>,
        b: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        Self::FAMILY.residuum_tensor(a, b)
    }

    /// Evaluate the residual negation `a -> 0` for Burn tensors.
    #[cfg(feature = "burn")]
    #[inline]
    fn neg_tensor<B: burn::tensor::backend::Backend, const D: usize>(
        a: burn::tensor::Tensor<B, D>,
    ) -> burn::tensor::Tensor<B, D> {
        Self::FAMILY.neg_tensor(a)
    }
}

/// Godel truth algebra: minimum t-norm, maximum t-conorm, and Godel residuum.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Godel;

impl TruthAlgebra for Godel {
    const FAMILY: LogicFamily = LogicFamily::Godel;
}

/// Product truth algebra: product t-norm, probabilistic t-conorm, and Goguen residuum.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Product;

impl TruthAlgebra for Product {
    const FAMILY: LogicFamily = LogicFamily::Product;
}

/// Lukasiewicz truth algebra: bounded-sum t-norm, bounded t-conorm, and Lukasiewicz residuum.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Lukasiewicz;

impl TruthAlgebra for Lukasiewicz {
    const FAMILY: LogicFamily = LogicFamily::Lukasiewicz;
}

/// The dual t-norm T(a,b) = 1 - S(1-a, 1-b) for a given t-conorm family.
///
/// T-norms generalize logical AND.
pub fn tnorm(family: TConormFamily, a: f64, b: f64) -> f64 {
    tnorm_generic(family, a, b)
}

/// Evaluate the standard-negation dual t-norm for a generic float type.
pub fn tnorm_generic<T: Float>(family: TConormFamily, a: T, b: T) -> T {
    let a = clamp01(a);
    let b = clamp01(b);
    match family {
        TConormFamily::Maximum => a.min(b),
        TConormFamily::Probabilistic => a * b,
        TConormFamily::Bounded => (a + b - T::one()).max(T::zero()),
        TConormFamily::Drastic => drastic_tnorm(a, b),
        TConormFamily::NilpotentMinimum => nilpotent_minimum_tnorm(a, b),
        TConormFamily::SchweizerSklar { p } => schweizer_sklar_tnorm(a, b, cast(p)),
        TConormFamily::SugenoWeber { p } => sugeno_weber_tnorm(a, b, cast(p)),
        TConormFamily::AczelAlsina { p } => aczel_alsina_tnorm(a, b, cast(p)),
        TConormFamily::MayorTorrens { p } => mayor_torrens_tnorm(a, b, cast(p)),
        _ => T::one() - tconorm_generic(family, T::one() - a, T::one() - b),
    }
}

/// Evaluate the residuum `a -> b` for a t-norm family when available.
///
/// This returns `None` for the drastic product because it is not
/// left-continuous. Families without a closed generator path use a bounded
/// numeric supremum over `c` such that `T(a, c) <= b`.
pub fn residuum(family: TConormFamily, a: f64, b: f64) -> Option<f64> {
    let a = a.clamp(0.0, 1.0);
    let b = b.clamp(0.0, 1.0);
    if a <= b {
        return Some(1.0);
    }

    match family {
        TConormFamily::Maximum => Some(b),
        TConormFamily::NilpotentMinimum => Some((1.0 - a).max(b)),
        TConormFamily::Drastic => None,
        TConormFamily::MayorTorrens { .. } => Some(residuum_numeric(family, a, b, 64)),
        _ => family.generator().map(|generator| generator.residuum(a, b)),
    }
}

/// Numerically approximate the residuum as `sup { c : T(a, c) <= b }`.
pub fn residuum_numeric(family: TConormFamily, a: f64, b: f64, iterations: usize) -> f64 {
    let a = a.clamp(0.0, 1.0);
    let b = b.clamp(0.0, 1.0);
    if a <= b {
        return 1.0;
    }

    let mut lo = 0.0;
    let mut hi = 1.0;
    for _ in 0..iterations {
        let mid = (lo + hi) * 0.5;
        if tnorm(family, a, mid) <= b {
            lo = mid;
        } else {
            hi = mid;
        }
    }
    lo
}

/// Evaluate the t-norm gradient with respect to `(a, b)`.
pub fn tnorm_gradient(family: TConormFamily, a: f64, b: f64) -> BinaryGradient<f64> {
    let a = a.clamp(0.0, 1.0);
    let b = b.clamp(0.0, 1.0);
    match family {
        TConormFamily::Maximum => min_gradient(a, b),
        TConormFamily::Probabilistic => BinaryGradient::new(b, a),
        TConormFamily::Bounded => {
            if a + b > 1.0 {
                BinaryGradient::new(1.0, 1.0)
            } else {
                BinaryGradient::new(0.0, 0.0)
            }
        }
        TConormFamily::Einstein => hamacher_tnorm_gradient(a, b, 2.0),
        TConormFamily::Hamacher => hamacher_tnorm_gradient(a, b, 0.0),
        TConormFamily::Yager { p } => yager_tnorm_gradient(a, b, p),
        TConormFamily::Frank { s } => frank_tnorm_gradient(a, b, s),
        TConormFamily::Dombi { p } => dombi_tnorm_gradient(a, b, p),
        TConormFamily::Drastic => drastic_tnorm_gradient(a, b),
        TConormFamily::NilpotentMinimum => nilpotent_minimum_tnorm_gradient(a, b),
        TConormFamily::SchweizerSklar { p } => schweizer_sklar_tnorm_gradient(a, b, p),
        TConormFamily::SugenoWeber { p } => sugeno_weber_tnorm_gradient(a, b, p),
        TConormFamily::AczelAlsina { p } => aczel_alsina_tnorm_gradient(a, b, p),
        TConormFamily::MayorTorrens { p } => mayor_torrens_tnorm_gradient(a, b, p),
    }
}

/// Evaluate the t-conorm gradient with respect to `(a, b)`.
pub fn tconorm_gradient(family: TConormFamily, a: f64, b: f64) -> BinaryGradient<f64> {
    let a = a.clamp(0.0, 1.0);
    let b = b.clamp(0.0, 1.0);
    let dual = tnorm_gradient(family, 1.0 - a, 1.0 - b);
    BinaryGradient::new(dual.da, dual.db)
}

/// Aggregate multiple values using a t-conorm (generalized multi-way OR).
///
/// Folds left-to-right using associativity: S(S(S(a, b), c), d)...
pub fn tconorm_fold(family: TConormFamily, values: &[f64]) -> f64 {
    match values.len() {
        0 => 0.0, // identity element for t-conorms
        1 => values[0].clamp(0.0, 1.0),
        _ => values
            .iter()
            .skip(1)
            .fold(values[0].clamp(0.0, 1.0), |acc, &v| tconorm(family, acc, v)),
    }
}

/// Aggregate multiple values using a t-norm (generalized multi-way AND).
pub fn tnorm_fold(family: TConormFamily, values: &[f64]) -> f64 {
    match values.len() {
        0 => 1.0, // identity element for t-norms
        1 => values[0].clamp(0.0, 1.0),
        _ => values
            .iter()
            .skip(1)
            .fold(values[0].clamp(0.0, 1.0), |acc, &v| tnorm(family, acc, v)),
    }
}

#[cfg(test)]
mod tests;