Esempio n. 1
0
        public static double Transitivity(Matrix m, TransitivityType type)
        {
            int total = 0, matching = 0;

            if (!m.IsSquareMatrix)
            {
                throw new ComputationException("Can only compute transitivity for square matrices.");
            }

            switch (type)
            {
            case TransitivityType.Simple:
                for (int i = 0; i < m.Rows; i++)
                {
                    for (int j = 0; j < m.Rows; j++)
                    {
                        if (m[i, j] <= 0.0 || i == j)
                        {
                            continue;
                        }

                        for (int k = 0; k < m.Rows; k++)
                        {
                            if (m[j, k] <= 0.0 || j == k || k == i)
                            {
                                continue;
                            }

                            ++total;
                            if (m[i, k] > 0)
                            {
                                ++matching;
                            }
                        }
                    }
                }
                break;

            case TransitivityType.Weak:

                for (int i = 0; i < m.Rows; i++)
                {
                    for (int j = 0; j < m.Rows; j++)
                    {
                        if (m[i, j] <= 0.0 || i == j)
                        {
                            continue;
                        }

                        for (int k = 0; k < m.Rows; k++)
                        {
                            if (m[i, k] <= 0.0 || j == k || k == i)
                            {
                                continue;
                            }

                            ++total;
                            if (m[j, k] > Math.Min(m[i, j], m[i, k]))
                            {
                                ++matching;
                            }
                        }
                    }
                }
                break;

            case TransitivityType.Strong:

                for (int i = 0; i < m.Rows; i++)
                {
                    for (int j = 0; j < m.Rows; j++)
                    {
                        if (m[i, j] <= 0.0 || i == j)
                        {
                            continue;
                        }

                        for (int k = 0; k < m.Rows; k++)
                        {
                            if (m[i, k] <= 0.0 || j == k || k == i)
                            {
                                continue;
                            }

                            ++total;
                            if (m[j, k] > Math.Max(m[i, j], m[i, k]))
                            {
                                ++matching;
                            }
                        }
                    }
                }
                break;
            }

            if (total == 0)
            {
                return(0.0);
            }

            return((double)matching / (double)total);
        }
Esempio n. 2
0
        public static double Transitivity(Matrix m, TransitivityType type)
        {
            int total = 0, matching = 0;

            if (!m.IsSquareMatrix)
                throw new ComputationException("Can only compute transitivity for square matrices.");

            switch (type)
            {
                case TransitivityType.Simple:
                    for (int i = 0; i < m.Rows; i++)
                    {
                        for (int j = 0; j < m.Rows; j++)
                        {
                            if (m[i, j] <= 0.0 || i == j)
                                continue;

                            for (int k = 0; k < m.Rows; k++)
                            {
                                if (m[j, k] <= 0.0 || j == k || k == i)
                                    continue;

                                ++total;
                                if (m[i, k] > 0)
                                    ++matching;
                            }
                        }
                    }
                    break;
                case TransitivityType.Weak:

                    for (int i = 0; i < m.Rows; i++)
                    {
                        for (int j = 0; j < m.Rows; j++)
                        {
                            if (m[i, j] <= 0.0 || i == j)
                                continue;

                            for (int k = 0; k < m.Rows; k++)
                            {
                                if (m[i, k] <= 0.0 || j == k || k == i)
                                    continue;

                                ++total;
                                if (m[j, k] > Math.Min(m[i, j], m[i, k]))
                                    ++matching;
                            }
                        }
                    }
                    break;
                case TransitivityType.Strong:

                    for (int i = 0; i < m.Rows; i++)
                    {
                        for (int j = 0; j < m.Rows; j++)
                        {
                            if (m[i, j] <= 0.0 || i == j)
                                continue;

                            for (int k = 0; k < m.Rows; k++)
                            {
                                if (m[i, k] <= 0.0 || j == k || k == i)
                                    continue;

                                ++total;
                                if (m[j, k] > Math.Max(m[i, j], m[i, k]))
                                    ++matching;
                            }
                        }
                    }
                    break;
            }

            if (total == 0)
                return 0.0;

            return (double)matching / (double)total;
        }