Exemplo n.º 1
0
        public double Determinant()
        {
            if (Width != Height)
            {
                return(0.0);
            }

            DMatrix result = DMatrixUtility.UnitVector2d(Height);
            DMatrix clone  = Clone();

            int    max;
            double tmp = 0.0;
            int    i, j, k;

            for (k = 0; k < Height; k++)
            {
                max = k;
                for (j = k + 1; j < Height; j++)
                {
                    if (Math.Abs(clone[j, k]) > Math.Abs(clone[max, k]))
                    {
                        max = j;
                    }
                }

                if (max != k)
                {
                    for (i = 0; i < Width; i++)
                    {
                        (clone[max, i], clone[k, i])   = (clone[k, i], clone[max, i]);
                        (result[max, i], result[k, i]) = (result[k, i], result[max, i]);
                    }
                }

                tmp = clone[k, k];
            }
            return(tmp);
        }
Exemplo n.º 2
0
        public double Median()
        {
            List <double> list = new List <double>();
            int           length, i, j;
            DMatrix       cloneFlattened = Clone().Flatten();

            for (i = 0; i < Width * Height; i++)
            {
                list.Add(Mat[0, i]);
            }

            list.Sort();
            length = list.Count();

            if (length % 2 == 0)
            {
                return((list[length / 2 - 1] + list[length / 2]) / 2);
            }
            else
            {
                return(list[length / 2]);
            }
        }
Exemplo n.º 3
0
        public DMatrix Pow(double coef)
        {
            DMatrix clone = Clone();

            return(clone.ApplyFunction(delegate(double item) { return Math.Pow(item, coef); }));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 逆行列を計算する。
        /// 実装は以下のページを参考(https://qiita.com/sekky0816/items/8c73a7ec32fd9b040127)
        /// TODO https://docs.microsoft.com/ja-jp/archive/msdn-magazine/2012/december/csharp-matrix-decomposition#%E8%A1%8C%E5%88%97%E5%BC%8F も参考にして組み込む
        /// </summary>
        /// <returns></returns>
        public DMatrix Inv()
        {
            if (Width != Height)
            {
                return(null);
            }

            DMatrix result = DMatrixUtility.UnitVector2d(Height);
            DMatrix clone  = Clone();

            int    max;
            double tmp;
            int    i, j, k;

            for (k = 0; k < Height; k++)
            {
                max = k;
                for (j = k + 1; j < Height; j++)
                {
                    if (Math.Abs(clone[j, k]) > Math.Abs(clone[max, k]))
                    {
                        max = j;
                    }
                }

                if (max != k)
                {
                    for (i = 0; i < Width; i++)
                    {
                        (clone[max, i], clone[k, i])   = (clone[k, i], clone[max, i]);
                        (result[max, i], result[k, i]) = (result[k, i], result[max, i]);
                    }
                }

                tmp = clone[k, k];

                for (i = 0; i < Width; i++)
                {
                    clone[k, i]  /= tmp;
                    result[k, i] /= tmp;
                }

                for (j = 0; j < Height; j++)
                {
                    if (j != k)
                    {
                        tmp = clone[j, k] / clone[k, k];
                        for (i = 0; i < Width; i++)
                        {
                            clone[j, i]  -= clone[k, i] * tmp;
                            result[j, i] -= result[k, i] * tmp;
                        }
                    }
                }
            }

            for (j = 0; j < Height; j++)
            {
                for (i = 0; i < Width; i++)
                {
                    if (double.IsNaN(result[j, i]))
                    {
                        return(null);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 二つの行列が同じ形か確認する
 /// </summary>
 /// <param name="otherDMatrix"></param>
 /// <returns></returns>
 public bool IsSameShape(DMatrix otherDMatrix)
 {
     return((this.Width == otherDMatrix.Width) && (this.Height == otherDMatrix.Height) ? true : false);
 }
Exemplo n.º 6
0
 /// <summary>
 /// ドット積が計算可能か確認する
 /// </summary>
 /// <param name="otherDMatrix"></param>
 /// <returns></returns>
 private bool IsProductEnable(DMatrix otherDMatrix)
 {
     return(this.Width == otherDMatrix.Height ? true : false);
 }