using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; namespace Esiur.Analysis.Fuzzy { public class DiscreteSet : INumericalSet, IEnumerable> { Dictionary vector = new Dictionary(); public double this[double index] { get => vector.ContainsKey(index) ? vector[index] : 0; set { if (vector.ContainsKey(index)) vector[index] = value; else vector.Add(index, value); } } public double AlphaCut { get; set; } public INumericalSet Intersection(INumericalSet with) { return new OperationSet(Operation.Intersection, new INumericalSet[] { this, with }); } public INumericalSet Union(INumericalSet with) { return new OperationSet(Operation.Union, new INumericalSet[] { this, with }); } IEnumerator IEnumerable.GetEnumerator() { return vector.GetEnumerator(); } IEnumerator> IEnumerable>.GetEnumerator() { return vector.GetEnumerator(); } public KeyValuePair[] Maximas { get { var max = vector.Values.Max(); return vector.Where(x => x.Value == max).ToArray(); } } public double Integral(double from, double to) { return vector.Where(x => x.Key >= from && x.Key <= to).Sum(x => x.Value); } public double Centroid(double from, double to) { var r = vector.Where(x => x.Key >= from && x.Key <= to).ToArray(); var total = r.Sum(x => x.Value); if (total == 0) return 0; else return r.Sum(x => x.Key * x.Value ) / total; } public KeyValuePair[] Minimas { get { var min = vector.Values.Min(); return vector.Where(x => x.Value == min).ToArray(); } } public double[] ToArray() => vector.Values.ToArray(); } }