/// <summary> /// <para>Copying one row or column of array a[,] to b[], the row/column to copy is specified by index of row/column.</para> /// <para>Chinese Simplified: 将二维数组a[,]的指定行或列拷贝至一维数组b[],行(或列)索引值由index给出,按行索引或按列索引由indexType给出。</para> /// </summary> /// <param name="a"> /// <para>input 2D array.</para> /// <para>Chinese Simplified: 原始二维数组。</para> /// </param> /// <param name="index"> /// <para>index of the row or column to copy.</para> /// <para>Chinese Simplified: 行或列索引值,从0开始。</para> /// <param name="b"> /// <para>output sequence containing copied row or column.</para> /// <para>Chinese Simplified: 返回的指定行或列一维数组。</para> /// </param> /// </param> /// <param name="indexType"> /// <para>specifies whether to copy row or column, the default is by row.</para> /// <para>Chinese Simplified: 设定获取行还是列。</para> /// </param> public static void GetArraySubset <T>(T[,] a, int index, ref T[] b, MajorOrder indexType = MajorOrder.Row) { int actualLength; if (indexType == MajorOrder.Row) { // if index is out of range, return immediately if (index >= a.GetLength(0)) { return; } // calculate actual length and copy data actualLength = Math.Min(b.Length, a.GetLength(1)); for (int i = 0; i < actualLength; i++) { b[i] = a[index, i]; } } else { // if index is out of range, return immediately if (index >= a.GetLength(1)) { return; } // calculate actual length and copy data actualLength = Math.Min(b.Length, a.GetLength(0)); for (int i = 0; i < actualLength; i++) { b[i] = a[i, index]; } } }
/// <summary> /// 将一维数组_src1和一维数组_src2连接为列数(行数)为2,行数(列数)为_src1和_src2最小长度的二维数组 /// </summary> /// <param name="src1"></param> /// <param name="src2"></param> /// <param name="dst"></param> /// <param name="majorOrder"></param> /// <returns></returns> public static void Concatenate <T>(T[] src1, T[] src2, ref T[,] dst, MajorOrder majorOrder) { if (majorOrder == MajorOrder.Column) { if (dst.GetLength(0) != src1.Length || dst.GetLength(0) != src2.Length) { throw new InvalidOperationException("输入参数不匹配"); } for (int i = 0; i < dst.GetLength(1); i++) { if (i == 0) { for (int j = 0; j < dst.GetLength(0); j++) { dst[j, i] = src1[j]; } } else { for (int j = 0; j < dst.GetLength(0); j++) { dst[j, i] = src2[j]; } } } } else { if (dst.GetLength(1) != src1.Length || dst.GetLength(1) != src2.Length) { throw new InvalidOperationException("输入参数不匹配"); } for (int i = 0; i < dst.GetLength(0); i++) { if (i == 0) { for (int j = 0; j < dst.GetLength(1); j++) { dst[i, j] = src1[j]; } } else { for (int j = 0; j < dst.GetLength(1); j++) { dst[i, j] = src2[j]; } } } } }
/// <summary> /// 将二维数组_src1和二维数组_src2连接为列(行)数为_src1列(行)数+1,行(列)数为_src1长度和_src2行(列)数最小值的二维数组 /// </summary> /// <param name="_src1"></param> /// <param name="_src2"></param> /// <param name="_dst"></param> /// <param name="majorOrder"></param> /// <returns></returns> public static void Concatenate <T>(T[,] _src1, T[,] _src2, ref T[,] _dst, MajorOrder majorOrder) { if (majorOrder == MajorOrder.Column) { if (_dst.GetLength(0) != _src1.GetLength(0) || _dst.GetLength(0) != _src2.GetLength(0) || _dst.GetLength(1) != _src1.GetLength(1) + _src2.GetLength(1)) { throw new InvalidOperationException("输入参数不匹配"); } for (int j = 0; j < _src1.GetLength(1); j++) { for (int i = 0; i < _dst.GetLength(0); i++) { _dst[i, j] = _src1[i, j]; } } for (int j = _src1.GetLength(1); j < _dst.GetLength(1); j++) { for (int i = 0; i < _dst.GetLength(0); i++) { _dst[i, j] = _src2[i, j - _src1.GetLength(1)]; } } } else { if (_dst.GetLength(1) != _src1.GetLength(1) || _dst.GetLength(1) != _src2.GetLength(1) || _dst.GetLength(0) != _src1.GetLength(0) + _src2.GetLength(0)) { throw new InvalidOperationException("输入参数不匹配"); } for (int j = 0; j < _src1.GetLength(0); j++) { for (int i = 0; i < _src1.GetLength(1); i++) { _dst[j, i] = _src1[j, i]; } } for (int j = _src1.GetLength(0); j < _dst.GetLength(0); j++) { for (int i = 0; i < _dst.GetLength(1); i++) { _dst[j, i] = _src2[j - _src1.GetLength(0), i]; } } } }
/// <summary> /// 在csv文件中设置要读取的行列索引值,读取一维数组,需配置文件路径。 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="filePath">csv文件路径和名称</param> /// <param name="majorOrder">读取数值的方向</param> /// <param name="index">读取的行/列索引值</param> /// <param name="startIndex">要读取的行/列的起始索引位置</param> /// <param name="readCount">读取的数组长度,若小于等于0则读取全部</param> /// <param name="encoding">编码方式</param> /// <returns></returns> public static T[] Read <T>(string filePath, MajorOrder majorOrder, long index, long startIndex = 0, long readCount = 0, Encoding encoding = null) { FileUtil.CheckFilePath(filePath, FileExtName); return(StreamReadData <T>(filePath, index, startIndex, readCount, MajorOrder.Column == majorOrder, encoding)); }
/// <summary> /// 在csv文件中设置要读取的行列索引值,读取一维数组,文件路径通过文件选择窗口配置 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="majorOrder">读取数值的方向</param> /// <param name="index">读取的行/列索引值</param> /// <param name="startIndex">要读取的行/列的起始索引位置</param> /// <param name="readCount">读取的数组长度,若小于等于0则读取全部</param> /// <param name="encoding">编码方式</param> /// <returns></returns> public static T[] Read <T>(MajorOrder majorOrder, long index, long startIndex = 0, long readCount = 0, Encoding encoding = null) { string filePath = FileUtil.GetOpenFilePathFromDialog(FileExtName); return(Read <T>(filePath, majorOrder, index, startIndex, readCount, encoding)); }
private static TDataType[] GetDirectionData <TDataType>(TDataType[,] data, int index, MajorOrder majorOrder) { TDataType[] directionData = null; switch (majorOrder) { case MajorOrder.Column: directionData = new TDataType[data.GetLength(0)]; for (int i = 0; i < data.GetLength(0); i++) { directionData[index] = data[i, index]; } break; case MajorOrder.Row: directionData = new TDataType[data.GetLength(1)]; int rowSize = data.GetLength(1) * Marshal.SizeOf(typeof(TDataType)); Buffer.BlockCopy(data, index * rowSize, directionData, 0, rowSize); break; } return(directionData); }
/// <summary> /// Calculate the covariance value of two array data. Supported data type: double/float/int. /// </summary> /// <param name="data1">The first array data to calculate Covariance value.</param> /// <param name="data2">The second array data to calculate Covariance value.</param> /// <param name="index1">The column or row index of first matrix to calculate covariance value.</param> /// <param name="index2">The column or row index of second matrix to calculate covariance value.</param> /// <param name="direction1">Galculate first matrix covariance by column of row.</param> /// <param name="direction2">Galculate second matrix covariance by column of row.</param> /// <typeparam name="TDataType">Supported data type: double/float/int.</typeparam> /// <returns>The covariance value of two array data in specific place.</returns> public static double Covariance <TDataType>(TDataType[,] data1, TDataType[,] data2, int index1, int index2, MajorOrder direction1 = MajorOrder.Column, MajorOrder direction2 = MajorOrder.Column) { return(Covariance(GetDirectionData(data1, index1, direction1), GetDirectionData(data2, index2, direction2))); }
/// <summary> /// Calculate the mean value of array data. Supported data type: double/float/int. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <typeparam name="TDataType">Supported data type: double/float/int.</typeparam> /// <returns>The mean value of array data.</returns> public static double Mean <TDataType>(TDataType[,] data, int index, MajorOrder majorOrder = MajorOrder.Column) { return(Mean(GetDirectionData(data, index, majorOrder))); }
/// <summary> /// Calculate the standard deviation value of array data. Supported data type: double/float/int. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <typeparam name="TDataType">Supported data type: double/float/int.</typeparam> /// <returns>The standard deviation value of array data in specific place.</returns> public static double StandardDeviation <TDataType>(TDataType[,] data, int index, MajorOrder majorOrder = MajorOrder.Column) { return(StandardDeviation(GetDirectionData(data, index, majorOrder))); }
/// <summary> /// Get the skewness value of array data. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <returns>The skewness value of array data.</returns> public static double Skewness(double[,] data, int index, MajorOrder majorOrder = MajorOrder.Column) { return(GetDirectionData(data, index, majorOrder).Skewness()); }
/// <summary> /// Estimates the empirical cumulative distribution function (CDF) at x from the provided samples. Supported data type: double/float. /// </summary> /// <param name="data">The data sample sequence.</param> /// <param name="x">The value where to estimate the CDF at.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <typeparam name="TDataType"></typeparam> /// <returns>The empirical cumulative distribution function (CDF) at x.</returns> public static double EmpiricalCDF <TDataType>(TDataType[,] data, double x, int index, MajorOrder majorOrder = MajorOrder.Column) { return(EmpiricalCDF(GetDirectionData(data, index, majorOrder), x)); }
/// <summary> /// Get the median value of array data. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <returns>The median value of array data.</returns> public static float Median(float[,] data, int index, MajorOrder majorOrder = MajorOrder.Column) { return(Median(GetDirectionData(data, index, majorOrder))); }
/// <summary> /// Get the percentile value of array data in specific place. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="place">The place to calculate percentile. Invalid value from 0 to 100.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <returns>The percentile value of array data in specific place.</returns> public static float Percentile(float[,] data, int place, int index, MajorOrder majorOrder = MajorOrder.Column) { return(Percentile(GetDirectionData(data, index, majorOrder), place)); }
/// <summary> /// Get minimum/lower-quartile/median/upper-quartile/maximum value of array data. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <returns>{minimum, lower-quartile, median, upper-quartile, maximum}</returns> public static float[] FiveSummaryPercentile(float[,] data, int index, MajorOrder majorOrder = MajorOrder.Column) { return(FiveSummaryPercentile(GetDirectionData(data, index, majorOrder))); }
/// <summary> /// Calculate the root mean square value of array data. Supported data type: double/float/int. /// </summary> /// <param name="data">The array data to calculate statistic value.</param> /// <param name="index">The column or row index of matrix to calculate statistic value.</param> /// <param name="majorOrder">Galculate matrix statistic value by column of row.</param> /// <typeparam name="TDataType">Supported data type: double/float/int.</typeparam> /// <returns>The root mean value of array data.</returns> public static double RootMeanSqaure <TDataType>(TDataType[,] data, int index, MajorOrder majorOrder = MajorOrder.Column) { return(RootMeanSqaure(GetDirectionData(data, index, majorOrder))); }
/// <summary> /// 在csv文件中设置要读取的行列索引值,读取一维数组,需配置文件路径。 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="filePath">csv文件路径和名称</param> /// <param name="majorOrder">读取数值的方向</param> /// <param name="index">读取的行/列索引值</param> /// <param name="startPosition">要读取的行/列的起始索引位置</param> /// <param name="size">读取的数组长度,若为-1则读取全部</param> /// <param name="encoding">编码方式</param> /// <returns></returns> public static T[] Read <T>(string filePath, MajorOrder majorOrder, long index, long startPosition = 0, long size = -1, Encoding encoding = null) { FileUtil.CheckFilePath(filePath, FileExtName); return(StreamReadData <T>(filePath, index, startPosition, size, MajorOrder.Column == majorOrder, encoding)); }