示例#1
0
        /// <summary>
        /// Performs iteration
        /// </summary>
        /// <returns>Residue</returns>
        public double Iterate()
        {
            PrepareIteration();
            double           sigma = 0;
            List <IIterator> iterators;

            if (ownIterators.Count != 0)
            {
                iterators = ownIterators;
            }
            else
            {
                iterators = this.iterators;
            }
            if (iterators.Count == 0)
            {
                return(1);
            }
            foreach (IIterator it in iterators)
            {
                it.Reset();
            }
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    a[i, j] = d[i, j];
                }
            }
            for (int i = 0; i < z.Length; i++)
            {
                z[i] = 0;
            }
            while (true)
            {
                consumer.Reset();
                try
                {
                    consumer.UpdateChildrenData();
                }
                catch (Exception ex)
                {
                    ex.ShowError(10);
                    goto cycle;
                }
                for (int i = 0; i < y.Length; i++)
                {
                    object o = left[i].Parameter();
                    if (o == null)
                    {
                        goto cycle;
                    }
                    y[i] = (double)o;
                    o    = right[i].Parameter();
                    if (o == null | o is DBNull)
                    {
                        goto cycle;
                    }
                    double res = (double)o - y[i];
                    yr[i]  = res;
                    sigma += res * res;
                }
                for (int i = 0; i < aliases.Length; i++)
                {
                    IAliasName a     = aliases[i];
                    double     delta = dx[i];
                    SetDelta(a, delta);
                    consumer.Reset();
                    consumer.UpdateChildrenData();
                    for (int j = 0; j < y.Length; j++)
                    {
                        object obj = left[j].Parameter();
                        if (obj == null)
                        {
                            SetDelta(a, -delta);
                            goto cycle;
                        }
                        ht[i, j] = ((double)obj - y[j]) / delta;
                    }
                    SetDelta(a, -delta);
                }
                for (int i = 0; i < y.Length; i++)
                {
                    for (int j = 0; j <= i; j++)
                    {
                        mr[i, j] = (double)r[i, j].Parameter();
                        mr[j, i] = mr[i, j];
                    }
                }
                RealMatrix.Invert(mr, mr1);
                RealMatrix.Multiply(ht, mr1, htr);
                for (int i = 0; i < a.GetLength(0); i++)
                {
                    for (int k = 0; k < htr.GetLength(1); k++)
                    {
                        z[i] += htr[i, k] * yr[k];
                        for (int j = 0; j < a.GetLength(1); j++)
                        {
                            a[i, j] += htr[i, k] * ht[j, k];
                        }
                    }
                }
cycle:
                foreach (IIterator it in iterators)
                {
                    if (!it.Next())
                    {
                        goto m;
                    }
                }
            }
m:
            RealMatrix.Solve(a, z, indxa);
            for (int i = 0; i < z.Length; i++)
            {
                SetDelta(aliases[i], z[i]);
            }
            return(sigma);
        }
示例#2
0
        /// <summary>
        /// Performs iteration step
        /// </summary>
        /// <param name="x">Input/Output parameters</param>
        /// <param name="dx">Delta</param>
        /// <param name="d">Weights of Input/Output</param>
        /// <param name="y">Auxiliary variable</param>
        /// <param name="y1">Auxiliary variable</param>
        /// <param name="h">Numercial derivations</param>
        /// <returns>Sigma0</returns>
        public double Iterate(double[] x, double[] dx, double[] d, double?[] y, double?[] y1, double?[,] h)
        {
            double s = 0;
            int    l = DataDimension;

            for (int im = 0; im < l; im++)
            {
                for (int i = 0; i < n; i++)
                {
                    h[i, im] = 1;
                }
            }

            for (int i = 0; i < n; i++)
            {
                z[i] = 0;
                for (int j = 0; j < n; j++)
                {
                    a[i, j] = 0;
                }
            }
            if (residuals == null)
            {
                residuals = new double?[l];
            }
            else if (residuals.Length != l)
            {
                residuals = new double?[l];
            }
            Calculate(x, selection, y);
            for (int i = 0; i < n; i++)
            {
                x[i] += dx[i];
                Calculate(x, selection, y1);
                for (int im = 0; im < l; im++)
                {
                    double?der = (y1[im] - y[im]) / dx[i];
                    double?res = this[im] - y[im];
                    residuals[im] = -res;
                    double r = GetWeight(im);
                    if (res != null)
                    {
                        z[i] += (double)(res * der * r);
                    }
                    h[i, im] = der;
                    if (res != null)
                    {
                        s += (double)(res * res * r);
                    }
                }
                x[i] -= dx[i];
            }
            for (int im = 0; im < l; im++)
            {
                for (int i = 0; i < n; i++)
                {
                    if (h[i, im] == null)
                    {
                        continue;
                    }
                    for (int j = 0; j < n; j++)
                    {
                        if (h[j, im] == null)
                        {
                            continue;
                        }
                        double r = GetWeight(im);
                        a[i, j] += (double)(r * h[i, im] * h[j, im]);
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                a[i, i] += d[i];
            }
            RealMatrix.Solve(a, z, indx);
            for (int i = 0; i < n; i++)
            {
                x[i] += z[i];
            }
            return(s);
        }
示例#3
0
        /// <summary>
        /// Performs full iteration
        /// </summary>
        /// <returns>Residue</returns>
        public double FullIterate()
        {
            PrepareIteration();
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    a[i, j] = d[i, j];
                }
            }
            for (int i = 0; i < z.Length; i++)
            {
                z[i] = 0;
            }
            currentSigma = 0;
            List <double[]> c = Calculate();
            double          s = currentSigma;

            List <double[]>[] ll = new List <double[]> [aliases.Length];
            double[,] h = new double[aliases.Length, c.Count];
            for (int i = 0; i < aliases.Length; i++)
            {
                IAliasName alias = aliases[i];
                double     delta = dx[i];
                SetDelta(alias, delta);
                ll[i] = Calculate();
                SetDelta(alias, -delta);
            }
            for (int i = 0; i < y.Length; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    mr[i, j] = (double)r[i, j].Parameter();
                    mr[j, i] = mr[i, j];
                }
            }
            RealMatrix.Invert(mr, mr1);
            for (int im = 0; im < c.Count; im++)
            {
                double[] y0 = c[im];
                for (int i = 0; i < aliases.Length; i++)
                {
                    double[] y = ll[i][im];
                    for (int j = 0; j < y.Length; j++)
                    {
                        ht[i, j] = (y[j] - y0[j]) / dx[i];
                    }
                }
                RealMatrix.Multiply(ht, mr1, htr);
                for (int i = 0; i < a.GetLength(0); i++)
                {
                    for (int k = 0; k < htr.GetLength(1); k++)
                    {
                        z[i] -= htr[i, k] * y0[k];
                        for (int j = 0; j < a.GetLength(1); j++)
                        {
                            a[i, j] += htr[i, k] * ht[j, k];
                        }
                    }
                }
            }
            RealMatrix.Solve(a, z, indxa);
            for (int i = 0; i < z.Length; i++)
            {
                SetDelta(aliases[i], z[i]);
            }
            return(s);
        }