laatzen/ZeroFlowTool/Maths.cs
2021-10-01 11:12:07 +02:00

181 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
namespace GenesisZeroFlow
{
public enum InitialCrcValue { Zeros, Nonzero1 = 0xffff, Nonzero2 = 0x1D0F }
public class Crc16Ccitt
{
const ushort poly = 4129;
ushort[] table = new ushort[256];
ushort initialValue = 0;
public ushort ComputeChecksum(byte[] data)
{
#region CA1062
if (data == null)
return 0;
#endregion
ushort crc = this.initialValue;
for (int i = 0; i < data.Length; ++i)
{
crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & data[i]))]);
}
return crc;
}
public byte[] ComputeChecksumBytes(byte[] data)
{
ushort crc = ComputeChecksum(data);
return BitConverter.GetBytes(crc);
}
public Crc16Ccitt(InitialCrcValue initialValue)
{
this.initialValue = (ushort)initialValue;
ushort temp, a;
for (int i = 0; i < table.Length; ++i)
{
temp = 0;
a = (ushort)(i << 8);
for (int j = 0; j < 8; ++j)
{
if (((temp ^ a) & 0x8000) != 0)
{
temp = (ushort)((temp << 1) ^ poly);
}
else
{
temp <<= 1;
}
a <<= 1;
}
table[i] = temp;
}
}
}
public static class MyListExtensions
{
public static double Mean(this List<double> values)
{
#region CA1062
if (values == null)
return 0;
#endregion
return values.Count == 0 ? 0 : values.Mean(0, values.Count);
}
public static double Mean(this List<double> values, int start, int end)
{
#region CA1062
if (values == null)
return 0;
#endregion
double s = 0;
for (int i = start; i < end; i++)
{
s += values[i];
}
return s / (end - start);
}
public static double Variance(this List<double> values)
{
#region CA1062
if (values == null)
return 0;
#endregion
return values.Variance(values.Mean(), 0, values.Count);
}
public static double Variance(this List<double> values, double mean)
{
#region CA1062
if (values == null)
return 0;
#endregion
return values.Variance(mean, 0, values.Count);
}
public static double Variance(this List<double> values, double mean, int start, int end)
{
#region CA1062
if (values == null)
return 0;
#endregion
double variance = 0;
for (int i = start; i < end; i++)
{
variance += (double)Math.Pow((values[i] - mean), 2);
}
int n = end - start;
if (start > 0) n -= 1;
return variance / (n);
}
public static double StandardDeviation(this List<double> values)
{
#region CA1062
if (values == null)
return 0;
#endregion
return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
}
public static double StandardDeviation(this List<double> values, int start, int end)
{
double mean = values.Mean(start, end);
double variance = values.Variance(mean, start, end);
return (double)Math.Sqrt(variance);
}
}
/*
public class HexadecimalEncoding
{
public static string ToHexString(string str)
{
var sb = new StringBuilder();
var bytes = Encoding.Unicode.GetBytes(str);
foreach (var t in bytes)
{
sb.Append(t.ToString("X2"));
}
return sb.ToString(); // returns: "48656C6C6F20776F726C64" for "Hello world"
}
public static string FromHexString(string hexString)
{
var bytes = new byte[hexString.Length / 2];
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return Encoding.Unicode.GetString(bytes); // returns: "Hello world" for "48656C6C6F20776F726C64"
}
}
*/
}