Advertisement
MagnusArias

SBO | Odchylenie std C#

Nov 30th, 2019
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using Microsoft.SqlServer.Server;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. [Serializable]
  6. [SqlUserDefinedAggregate(Format.UserDefined,
  7. IsInvariantToDuplicates = false,
  8. IsInvariantToOrder = false,
  9. MaxByteSize = -1, Name = "OdchylenieStandardowe")]
  10. public struct OdchStd : IBinarySerialize
  11. {
  12.     public List<double> posr;
  13.     private int licznik; // zmienna zliczająca liczbę wierszy nie mających wartości NULL
  14.     private double suma; // zmienna przechowująca sumę pól
  15.     private double temp; // zmienna pomocnicza
  16.  
  17.     public void Init()
  18.     {
  19.         posr = new List<double>();
  20.         licznik = 0;
  21.         suma = 0;
  22.     }
  23.     public void Accumulate(double? value)
  24.     {
  25.         if (value != null) // wyznaczanie sumy dla pól nie mających wartości NULL
  26.         {
  27.             licznik++;
  28.             temp = (double)value;
  29.             suma = suma + temp;
  30.             posr.Add(temp);
  31.         }
  32.     }
  33.     public void Merge(OdchStd Group)
  34.     {
  35.         this.suma += Group.suma;
  36.         this.licznik += Group.licznik;
  37.         this.temp += Group.temp;
  38.         // kiedy obliczenia równoległe suma staje się sumą z wszystkich procesów
  39.     }
  40.     public double? Terminate() //SqString
  41.     {
  42.         if (licznik <= 1)
  43.         {
  44.             return null;
  45.         }
  46.         else
  47.         {
  48.             return (double?)(this.temp); //zwrócenie ostatecznej wartości obliczeń
  49.         }
  50.     }
  51.     public void Write(System.IO.BinaryWriter w)
  52.     {
  53.         temp = 0;
  54.         double sr = suma / licznik;
  55.         foreach (double d in posr)
  56.         {
  57.             temp = temp + Math.Pow((d - sr), 2);
  58.         }
  59.         temp = temp / (licznik - 1);
  60.         temp = Math.Pow(temp, 0.5);
  61.         w.Write(temp);
  62.         //w.Write(suma);
  63.         w.Write(licznik);
  64.     }
  65.     public void Read(System.IO.BinaryReader r)
  66.     {
  67.         temp = r.ReadDouble();
  68.         //suma = r.ReadDouble();
  69.         licznik = r.ReadInt32();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement