예제 #1
0
        protected override CDoubleArray Eval(List <CDoubleArray> inList, List <string> functionNames)
        {
            if (inList == null || inList.Count < 1 || inList[0] == null)
            {
                return(null);
            }
            int count = 0;

            if (inList.Count >= 2 && inList[1] != null)
            {
                count = (int)inList[1][0].R;
            }

            for (int i = 0; i < inList[0].Length; i++)
            {
                log.Add(inList[0][i]);
            }
            if (count > 0 && count < log.Count)
            {
                log = log.GetRange(log.Count - count, count);
            }

            CDoubleArray ret = new CDoubleArray(log.ToArray());

            return(ret);
        }
예제 #2
0
        /// <summary>
        /// Evaluate.
        ///
        /// The CDoubleArray list should have 1 or 2 non-null elements.
        /// If 1 element then Eval will make sure the real and imaginary parts are of
        /// equal length, and these become X and Y for the plot.
        /// If 2 elements, then the real parts are used as X and Y.
        ///
        /// functionNames are used to determine extra pieces of information, eg, LineColour
        /// and SymbolString in that order.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="functionNames"></param>
        /// <returns></returns>
        protected override CDoubleArray Eval(List <CDoubleArray> a, List <string> functionNames)
        {
            if (a.Count < 1 || a[0] == null)
            {
                return(null);
            }

            if (a.Count == 1 || a[1] == null)
            {
                applyFunctionNames(functionNames, 1);
                if (a[0].Imag == null || a[0].Imag.Length < 1)
                {
                    double[] index = new double[a[0].Length];
                    for (int i = 0; i < index.Length; i++)
                    {
                        index[i] = (double)i;
                    }
                    return(new CDoubleArray(index, a[0].Real));
                }
                a[0].EqualiseLengths();
                return(a[0]);
            }
            applyFunctionNames(functionNames, 2);
            CDoubleArray cd = new CDoubleArray(a[0].Real, a[1].Real);

            cd.EqualiseLengths();
            return(cd);
        }
예제 #3
0
        protected virtual CDoubleArray Eval(List <CDoubleArray> inList, List <string> functionNames)
        {
            if (inList == null || inList.Count < 1 || inList[0] == null)
            {
                return(null);
            }
            CDoubleArray r = null;

            if (inList.Count == 1)
            {
                r = new CDoubleArray(inList[0].Length);
                if (inList[0] == null)
                {
                    return(null);
                }
                for (int i = 0; i < inList[0].Length; i++)
                {
                    CDouble rr = EvalDouble(inList[0][i]);
                    r[i] = rr;
                }
                return(r);
            }
            // Any function expecting more than 2 children should implement Eval(List<> directly)
            else
            {
                if (inList[1] == null)
                {
                    return(inList[0]);
                }
                // Equalise lengths.
                int length = Math.Min(inList[0].Length, inList[1].Length);
                // If one of the inputs is a single digit, then apply it to entire other array.
                if (length == 1)
                {
                    length = Math.Max(inList[0].Length, inList[1].Length);
                }
                r = new CDoubleArray(length);
                for (int i = 0; i < length; i++)
                {
                    CDouble rr = EvalDouble(inList[0][i], inList[1][i]);
                    r[i] = rr;
                }
                return(r);
            }
            //return null;
        }
예제 #4
0
        protected override CDoubleArray Eval(List <CDoubleArray> inList, List <string> functionNames)
        {
            if (inList.Count < 1 || inList[0] == null)
            {
                return(null);
            }
            CDoubleArray a        = inList[0];
            int          maxIndex = 0;
            double       max      = a[0].R;

            for (int i = 1; i < a.Length; i++)
            {
                if (a[i].R > max)
                {
                    maxIndex = i;
                    max      = a[i].R;
                }
            }
            return(new CDoubleArray(new double[] { max }, new double[] { maxIndex }));
        }
예제 #5
0
        public void PlotWaveform()
        {
            ICFunction[] cft = ComplexFunctions;
            if (cft == null)
            {
                return;
            }
            int nPlots = 0;

            foreach (ICFunction icf in cft)
            {
                if (icf.Name.Equals("Plot"))
                {
                    nPlots++;
                }
            }
            double[][]        x       = new double[nPlots][];
            double[][]        y       = new double[nPlots][];
            Color[]           colors  = new Color[nPlots];
            ZedGraph.Symbol[] symbols = new ZedGraph.Symbol[nPlots];
            string[]          labels  = new string[nPlots];
            nPlots = 0;
            for (int i = 0; i < cft.Length; i++)
            {
                CDoubleArray cd = cft[i].Eval();
                if (cft[i].GetType().Name.Equals("Plot"))
                {
                    Plot plot = cft[i] as Plot;
                    if (cd != null)
                    {
                        x[nPlots]       = cd.Real;
                        y[nPlots]       = cd.Imag;
                        labels[nPlots]  = plot.Legend;
                        symbols[nPlots] = GraphSymbol.GetSymbol(plot.SymbolString, plot.LineColor);
                        colors[nPlots]  = plot.LineColor;
                    }
                    nPlots++;
                }
            }
            base.PlotWaveform(x, y, labels.ToArray(), colors, symbols);
        }
예제 #6
0
파일: Plot.cs 프로젝트: adrianj/AdriansLib
        /// <summary>
        /// Evaluate.
        /// 
        /// The CDoubleArray list should have 1 or 2 non-null elements.
        /// If 1 element then Eval will make sure the real and imaginary parts are of
        /// equal length, and these become X and Y for the plot.
        /// If 2 elements, then the real parts are used as X and Y.
        /// 
        /// functionNames are used to determine extra pieces of information, eg, LineColour
        /// and SymbolString in that order.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="functionNames"></param>
        /// <returns></returns>
        protected override CDoubleArray Eval(List<CDoubleArray> a, List<string> functionNames)
        {
            if (a.Count < 1 || a[0] == null) return null;

            if (a.Count == 1 || a[1] == null)
            {
                applyFunctionNames(functionNames, 1);
                if (a[0].Imag == null || a[0].Imag.Length < 1)
                {
                    double[] index = new double[a[0].Length];
                    for (int i = 0; i < index.Length; i++) index[i] = (double)i;
                    return new CDoubleArray(index,a[0].Real);
                }
                a[0].EqualiseLengths();
                return a[0];
            }
            applyFunctionNames(functionNames, 2);
            CDoubleArray cd = new CDoubleArray(a[0].Real, a[1].Real);
            cd.EqualiseLengths();
            return cd;
        }
예제 #7
0
        protected override CDoubleArray Eval(List <CDoubleArray> inList, List <string> functionNames)
        {
            if (inList.Count < 2 || inList[0] == null || inList[1] == null)
            {
                return(null);
            }
            CDoubleArray a        = inList[0];
            CDoubleArray b        = inList[1];
            List <int>   iList    = new List <int>();
            List <int>   imagList = new List <int>();

            for (int i = 0; i < b.Length; i++)
            {
                int index = (int)Math.Round(b[i].R);
                if (index >= 0 && index < a.Length)
                {
                    iList.Add(index);
                }
                if (a.Imag != null && index >= 0 && index < a.Imag.Length)
                {
                    imagList.Add(index);
                }
            }
            double[] rr = new double[iList.Count];
            double[] ri = null;
            for (int i = 0; i < rr.Length; i++)
            {
                rr[i] = a.Real[iList[i]];
            }
            if (imagList.Count > 0)
            {
                ri = new double[imagList.Count];
                for (int i = 0; i < ri.Length; i++)
                {
                    ri[i] = a.Imag[imagList[i]];
                }
            }
            return(new CDoubleArray(rr, ri));
        }
예제 #8
0
 public ConstantFunction(CDoubleArray data, string name)
 {
     Data  = data;
     mName = name;
 }
예제 #9
0
        protected override CDoubleArray Eval(List <CDoubleArray> a, List <string> functionNames)
        {
            if (a.Count < 1 || a[0] == null)
            {
                return(null);
            }
            double[] real = a[0].Real;
            double[] imag = a[0].Imag;
            if (imag == null || imag.Length != real.Length)
            {
                imag = new double[real.Length];
            }
            if (Children.Count > 1)
            {
                imag = Children[1].Eval().Real;
            }
            // Find 2^x just less than data length
            long sample_rate = 1;

            while (sample_rate <= real.Length)
            {
                sample_rate <<= 1;
            }
            sample_rate >>= 1;
            //variables for the fft
            long   n, mmax, m, j, istep, i;
            double wtemp, wr, wpr, wpi, wi, theta, tempr, tempi;

            //long number_of_samples = d.Length;

            double[] vector = new double[2 * sample_rate];
            for (i = 0; i < sample_rate; i++)
            {
                vector[i * 2]     = real[i];
                vector[i * 2 + 1] = imag[i];
            }
            //binary inversion (note that the indices
            //start from 0 which means that the
            //real part of the complex is on the even-indices
            //and the complex part is on the odd-indices)
            n = sample_rate << 1;
            j = 0;
            for (i = 0; i < n / 2; i += 2)
            {
                if (j > i)
                {
                    swap(ref vector[j], ref vector[i]);
                    swap(ref vector[j + 1], ref vector[i + 1]);
                    if ((j / 2) < (n / 4))
                    {
                        swap(ref vector[(n - (i + 2))], ref vector[(n - (j + 2))]);
                        swap(ref vector[(n - (i + 2)) + 1], ref vector[(n - (j + 2)) + 1]);
                    }
                }
                m = n >> 1;
                while (m >= 2 && j >= m)
                {
                    j  -= m;
                    m >>= 1;
                }
                j += m;
            }
            //end of the bit-reversed order algorithm

            //Danielson-Lanzcos routine
            mmax = 2;
            while (n > mmax)
            {
                istep = mmax << 1;
                theta = 2 * Math.PI / mmax;
                wtemp = Math.Sin(0.5 * theta);
                wpr   = -2.0 * wtemp * wtemp;
                wpi   = Math.Sin(theta);
                wr    = 1.0;
                wi    = 0.0;
                for (m = 1; m < mmax; m += 2)
                {
                    for (i = m; i <= n; i += istep)
                    {
                        j              = i + mmax;
                        tempr          = wr * vector[j - 1] - wi * vector[j];
                        tempi          = wr * vector[j] + wi * vector[j - 1];
                        vector[j - 1]  = vector[i - 1] - tempr;
                        vector[j]      = vector[i] - tempi;
                        vector[i - 1] += tempr;
                        vector[i]     += tempi;
                    }
                    wr = (wtemp = wr) * wpr - wi * wpi + wr;
                    wi = wi * wpr + wtemp * wpi + wi;
                }
                mmax = istep;
            }

            CDoubleArray ret = new CDoubleArray(vector.Length / 4);

            real = new double[sample_rate / 2];
            imag = new double[sample_rate / 2];
            for (i = 0; i < real.Length; i++)
            {
                real[i] = vector[i * 2];
                imag[i] = vector[i * 2 + 1];
            }

            return(new CDoubleArray(real, imag));
        }
예제 #10
0
 private CDoubleArray returnResult(CDoubleArray ret)
 {
     AlreadyEvaluated = true;
     mEvalResult = ret;
     return mEvalResult;
 }
예제 #11
0
 protected virtual CDoubleArray Eval(List<CDoubleArray> inList, List<string> functionNames)
 {
     if (inList == null || inList.Count < 1 || inList[0] == null) return null;
     CDoubleArray r = null;
     if (inList.Count == 1)
     {
         r = new CDoubleArray(inList[0].Length);
         if (inList[0] == null) return null;
         for (int i = 0; i < inList[0].Length; i++)
         {
             CDouble rr = EvalDouble(inList[0][i]);
             r[i] = rr;
         }
         return r;
     }
     // Any function expecting more than 2 children should implement Eval(List<> directly)
     else
     {
         if (inList[1] == null) return inList[0];
         // Equalise lengths.
         int length = Math.Min(inList[0].Length, inList[1].Length);
         // If one of the inputs is a single digit, then apply it to entire other array.
         if (length == 1) length = Math.Max(inList[0].Length, inList[1].Length);
         r = new CDoubleArray(length);
         for (int i = 0; i < length; i++)
         {
             CDouble rr = EvalDouble(inList[0][i], inList[1][i]);
             r[i] = rr;
         }
         return r;
     }
     //return null;
 }
예제 #12
0
 public ConstantFunction(CDoubleArray data, string name)
 {
     Data = data;
     mName = name;
 }
예제 #13
0
 private CDoubleArray returnResult(CDoubleArray ret)
 {
     AlreadyEvaluated = true;
     mEvalResult      = ret;
     return(mEvalResult);
 }
예제 #14
0
        protected override CDoubleArray Eval(List<CDoubleArray> inList, List<string> functionNames)
        {
            if (inList == null || inList.Count < 1 || inList[0] == null) return null;
            int count = 0;
            if (inList.Count >= 2 && inList[1] != null)
                count = (int)inList[1][0].R;

            for(int i = 0; i < inList[0].Length; i++)
                log.Add(inList[0][i]);
            if (count > 0 && count < log.Count)
                log = log.GetRange(log.Count-count, count);

            CDoubleArray ret = new CDoubleArray(log.ToArray());
            return ret;
        }
예제 #15
0
        protected override CDoubleArray Eval(List<CDoubleArray> a, List<string> functionNames)
        {
            if (a.Count < 1 || a[0] == null) return null;
            double[] real = a[0].Real;
            double[] imag = a[0].Imag;
            if (imag == null || imag.Length != real.Length) imag = new double[real.Length];
            if (Children.Count > 1)
                imag = Children[1].Eval().Real;
            // Find 2^x just less than data length
            long sample_rate = 1;
            while (sample_rate <= real.Length) sample_rate <<= 1;
            sample_rate >>= 1;
            //variables for the fft
            long n, mmax, m, j, istep, i;
            double wtemp, wr, wpr, wpi, wi, theta, tempr, tempi;
            //long number_of_samples = d.Length;

            double[] vector = new double[2 * sample_rate];
            for (i = 0; i < sample_rate; i++)
            {
                vector[i * 2] = real[i];
                vector[i * 2 + 1] = imag[i];
            }
            //binary inversion (note that the indices
            //start from 0 which means that the
            //real part of the complex is on the even-indices
            //and the complex part is on the odd-indices)
            n = sample_rate << 1;
            j = 0;
            for (i = 0; i < n / 2; i += 2)
            {
                if (j > i)
                {
                    swap(ref vector[j], ref vector[i]);
                    swap(ref vector[j + 1], ref vector[i + 1]);
                    if ((j / 2) < (n / 4))
                    {
                        swap(ref vector[(n - (i + 2))], ref vector[(n - (j + 2))]);
                        swap(ref vector[(n - (i + 2)) + 1], ref vector[(n - (j + 2)) + 1]);
                    }
                }
                m = n >> 1;
                while (m >= 2 && j >= m)
                {
                    j -= m;
                    m >>= 1;
                }
                j += m;
            }
            //end of the bit-reversed order algorithm

            //Danielson-Lanzcos routine
            mmax = 2;
            while (n > mmax)
            {
                istep = mmax << 1;
                theta = 2 * Math.PI / mmax;
                wtemp = Math.Sin(0.5 * theta);
                wpr = -2.0 * wtemp * wtemp;
                wpi = Math.Sin(theta);
                wr = 1.0;
                wi = 0.0;
                for (m = 1; m < mmax; m += 2)
                {
                    for (i = m; i <= n; i += istep)
                    {
                        j = i + mmax;
                        tempr = wr * vector[j - 1] - wi * vector[j];
                        tempi = wr * vector[j] + wi * vector[j - 1];
                        vector[j - 1] = vector[i - 1] - tempr;
                        vector[j] = vector[i] - tempi;
                        vector[i - 1] += tempr;
                        vector[i] += tempi;
                    }
                    wr = (wtemp = wr) * wpr - wi * wpi + wr;
                    wi = wi * wpr + wtemp * wpi + wi;
                }
                mmax = istep;
            }

            CDoubleArray ret = new CDoubleArray(vector.Length / 4);
            real = new double[sample_rate / 2];
            imag = new double[sample_rate / 2];
            for (i = 0; i < real.Length; i++)
            {
                real[i] = vector[i * 2];
                imag[i] = vector[i * 2 + 1];
            }

            return new CDoubleArray(real,imag);
        }