Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.SqlServer.Server;
- using System;
- [Serializable]
- [SqlUserDefinedAggregate(Format.Native,
- IsInvariantToDuplicates = false,
- IsInvariantToNulls = true,
- IsInvariantToOrder = true,
- IsNullIfEmpty = false,
- Name = "WskKoncentracji")]
- public struct WskKoncentracji
- {
- private double sumaX;
- private double sumaX_Pow2;
- private double sumaX_Mul2;
- private double sumaX_Pow4;
- private double sumaX_Mul4_Pow3;
- private double sumaX_Mul6_Pow2;
- private double sumaX_Mul4;
- private int licznik;
- public void Init()
- {
- sumaX = 0;
- sumaX_Pow2 = 0;
- sumaX_Mul2 = 0;
- sumaX_Pow4 = 0;
- sumaX_Mul4_Pow3 = 0;
- sumaX_Mul6_Pow2 = 0;
- sumaX_Mul4 = 0;
- licznik = 0;
- }
- public void Accumulate(double? value)
- {
- if (value == null) return;
- sumaX += value.Value;
- sumaX_Pow2 += Math.Pow(value.Value, 2);
- sumaX_Mul2 += 2 * value.Value;
- sumaX_Pow4 += Math.Pow(value.Value, 4);
- sumaX_Mul4_Pow3 += 4 * Math.Pow(value.Value, 3);
- sumaX_Mul6_Pow2 += 6 * Math.Pow(value.Value, 2);
- sumaX_Mul4 += 4 * value.Value;
- licznik++;
- }
- public void Merge(WskKoncentracji Group)
- {
- this.sumaX += Group.sumaX;
- this.sumaX_Pow2 += Group.sumaX_Pow2;
- this.sumaX_Mul2 += Group.sumaX_Mul2;
- this.sumaX_Pow4 += Group.sumaX_Pow4;
- this.sumaX_Mul4_Pow3 += Group.sumaX_Mul4_Pow3;
- this.sumaX_Mul6_Pow2 += Group.sumaX_Mul6_Pow2;
- this.sumaX_Mul4 += Group.sumaX_Mul4;
- this.licznik += Group.licznik;
- }
- public double? Terminate()
- {
- if (licznik == 0) return null;
- else
- {
- double srednia = sumaX / licznik;
- if (licznik - 1 == 0) return null;
- else
- {
- double odchylenie = Math.Sqrt((sumaX_Pow2 - sumaX_Mul2 * srednia + Math.Pow(srednia, 2)) / (licznik - 1));
- if (licznik - 3 == 0) return null;
- else
- {
- double up = sumaX_Pow4 -
- sumaX_Mul4_Pow3 * srednia +
- sumaX_Mul6_Pow2 * Math.Pow(srednia, 2) -
- sumaX_Mul4 * Math.Pow(srednia, 3) +
- Math.Pow(srednia, 4);
- double down = licznik * Math.Pow(odchylenie, 4);
- if (down == 0.0) return null;
- double wsk = up / down;
- return (double?)wsk;
- }
- }
- }
- }
- public void Read(System.IO.BinaryReader r)
- {
- sumaX = r.ReadDouble();
- sumaX_Pow2 = r.ReadDouble();
- sumaX_Mul2 = r.ReadDouble();
- sumaX_Pow4 = r.ReadDouble();
- sumaX_Mul4_Pow3 = r.ReadDouble();
- sumaX_Mul6_Pow2 = r.ReadDouble();
- sumaX_Mul4 = r.ReadDouble();
- licznik = r.ReadInt32();
- }
- public void Write(System.IO.BinaryWriter w)
- {
- w.Write(sumaX);
- w.Write(sumaX_Pow2);
- w.Write(sumaX_Mul2);
- w.Write(sumaX_Pow4);
- w.Write(sumaX_Mul4_Pow3);
- w.Write(sumaX_Mul6_Pow2);
- w.Write(sumaX_Mul4);
- w.Write(licznik);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement