Пример #1
0
        public double TestSpeedManaged_wrapped()
        {
            d_managed a = new d_managed(1), b = new d_managed(2), c = new d_managed(3);

            var sum = new d_managed(0);

            for (var i = 0; i < N; i++)
            {
                sum += a * b + c;
                a    = b + c;
                b    = c + a;
            }

            return(sum.val);
        }
        public void Managed_Wrapped()
        {
            var m = new Matrix <d_managed>(N);
            var b = new d_managed[N];
            var x = new d_managed[N];

            var size = N;
            var zero = new d_managed(0);
            var one  = new d_managed(1);

            d_managed Abs(d_managed val)
            {
                return(val.val > 0 ? val : new d_managed(-val.val));
            }

            for (var i = 0; i < size - 1; i++)
            {
                // Search for maximum in this column
                var maxEl  = Abs(m[i, i]);
                var maxRow = i;
                for (var k = i + 1; k < size; k++)
                {
                    if (Abs(m[k, i]) > maxEl)
                    {
                        maxEl  = Abs(m[k, i]);
                        maxRow = k;
                    }
                }

                // Swap maximum row with current row (column by column)
                for (var k = i; k < size; k++)
                {
                    var tmp = m[maxRow, k];
                    m[maxRow, k] = m[i, k];
                    m[i, k]      = tmp;
                }

                // swap in b vector
                {
                    var tmp = b[maxRow];
                    b[maxRow] = b[i];
                    b[i]      = tmp;
                }

                // eliminate current variable in all columns
                for (var k = i + 1; k < size; k++)
                {
                    var c = zero - m[k, i] / m[i, i];
                    for (var j = i; j < size; j++)
                    {
                        if (i == j)
                        {
                            m[k, j] = zero;
                        }
                        else
                        {
                            m[k, j] += c * m[i, j];
                        }
                    }
                    // b vector
                    b[k] += c * b[i];
                }
            }


            // Solve equation Ax=b for an upper triangular matrix A
            for (var i = size - 1; i >= 0; i--)
            {
                if (b[i] == zero)
                {
                    continue;
                }
                // normalize
                b[i]   /= m[i, i];
                m[i, i] = one;
                // backward elimination
                for (var k = i - 1; k >= 0; k--)
                {
                    b[k]   -= m[k, i] * b[i];
                    m[k, i] = zero;
                }
            }

            b.CopyTo(x, 0);
        }