Esempio n. 1
0
        public double[] measurements_  = new double[20000];             // Opretter array til målingerne med 20000 pladser

        public int Pulse()                                              //double[] measurements, double samplefrequence)
        {
            int length = measurements_.Length;                          //Finder antal målinger

            alglib.complex[] complexArray = new alglib.complex[length]; //komplekst array
            List <double>    amplitudes   = new List <double>();        //listen som skal indeholde amplituderne

            alglib.fftr1d(measurements_, out complexArray);             //fourier der danner det komplekse array

            foreach (var measurement in complexArray)                   //Hver måling i det komplekse array gennemløbes
            {
                double amplitude =
                    Math.Sqrt(Math.Pow(measurement.x, 2) +
                              Math.Pow(measurement.y, 2)); //Phytagoras for at finde højden på vektoren
                amplitudes.Add(amplitude);                 // Beregningen tilføjes til listen som indeholder amplituder
            }

            double maximum = amplitudes[1]; //Den maksimale amplitude vælges som den første i listen
            int    index   = 0;

            for (int i = 1; i < amplitudes.Count / 2; i++)
            {
                if (amplitudes[i] > maximum) //tjekker om amplituden er højere end den der allerede er valgt
                {
                    maximum = amplitudes[i]; //Hvis den er højere erstattes den med den gemte
                    index   = i;             //
                }
            }

            double frequence     = (double)samplefrequence_ / measurements_.Length;
            double calcFrequence = frequence * index;

            PulseValue = (int)(calcFrequence * 60);
            return(PulseValue); // evt slet da man nu kan hente værdien?
        } //returner pulse
Esempio n. 2
0
        public double[] Encode(Bitmap source)
        {
            Bitmap frame;

            if (source.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                frame = Grayscale.CommonAlgorithms.RMY.Apply(source);
            }
            else
            {
                frame = source;
            }

            if (!frame.Size.Equals(frameSize))
            {
                frame = resizer.Apply(frame);
            }

            double[]         samples = new double[fftSize * frameSize.Width];
            alglib.complex[] slice   = new alglib.complex[fftSize];
            double           maxSlice;
            int sampleIndex = 0;

            int colsCount = frameSize.Width;
            int startRow  = startLine;
            int endRow    = startRow + frameSize.Height;

            for (int x = 0; x < colsCount; x++)
            {
                for (int y = startRow; y < endRow; y++)
                {
                    slice[y].x = (frame.GetPixel(x, frameSize.Height - (y - startRow) - 1).R / 255.0) * short.MaxValue;
                }

                for (int y = 0; y < fftSize; y++)
                {
                    slice[y].x *= randomizerMask[y];
                }

                alglib.fftc1dinv(ref slice);

                maxSlice = double.MinValue;
                for (int y = 0; y < slice.Length; y++)
                {
                    if (Math.Abs(slice[y].x) > maxSlice)
                    {
                        maxSlice = Math.Abs(slice[y].x);
                    }
                }

                for (int i = 0; i < slice.Length; i++)
                {
                    samples[sampleIndex] = (short)Math.Round(slice[i].x * short.MaxValue / maxSlice);
                    sampleIndex++;
                }
            }

            return(samples);
        }
Esempio n. 3
0
File: ap.cs Progetto: sfcta/DaySim
 public rcommstate()
 {
     stage = -1;
     ia    = new int[0];
     ba    = new bool[0];
     ra    = new double[0];
     ca    = new alglib.complex[0];
 }
Esempio n. 4
0
        public Bitmap Decode(double[] samples)
        {
            int colCount = samples.Length / fftSize;

            if (colCount == frameSize.Width)
            {
                int    rowCount = frameSize.Height;
                Bitmap temp     = new Bitmap(colCount, rowCount);

                double[]         slice  = new double[fftSize];
                alglib.complex[] sliceC = new alglib.complex[fftSize];
                int  samplesCount       = 0;
                byte component;

                int decodeStart = startLine;
                int decodeEnd   = startLine + rowCount;

                double maxSlice;

                for (int x = 0; x < colCount; x++)
                {
                    for (int y = 0; y < fftSize; y++)
                    {
                        slice[y] = samples[samplesCount];
                        samplesCount++;
                    }

                    alglib.fftr1d(slice, out sliceC);

                    maxSlice = double.MinValue;
                    for (int y = decodeStart; y < decodeEnd; y++)
                    {
                        if (alglib.math.abscomplex(sliceC[y].x) > maxSlice)
                        {
                            maxSlice = alglib.math.abscomplex(sliceC[y].x);
                        }
                    }

                    int offset = temp.Height + decodeStart - 1;

                    for (int y = decodeStart; y < decodeEnd; y++)
                    {
                        component = (byte)(255.0 * alglib.math.abscomplex(sliceC[y].x) / maxSlice);
                        temp.SetPixel(x, offset - y, Color.FromArgb(component, component, component));
                    }
                }

                return(temp);
            }
            else
            {
                throw new ApplicationException("Specified array length error");
            }
        }
 public static alglib.complex[] chirp(double Fmin, double Fmax, double Fd, double T)
 {
     double dt = 1 / Fd;
     int samples = (int)Math.Ceiling(T *Fd);
     alglib.complex[] signal = new alglib.complex[samples];
     double df = (Fmax - Fmin) / (samples - 1) / 2;
     for (int i = 0; i < samples; i++)
     {
         signal[i] = new alglib.complex(Math.Cos(2 * Math.PI * (Fmin + i * df) * i * dt), Math.Sin(2 * Math.PI * (Fmin + i * df) * i * dt));
     }
     return signal;
 }
 public alglib.complex[] modulate(int[] message)//цифровая модуляция, создание I Q компонент для ФМ сигнала
 {
     alglib.complex[] signal = new alglib.complex[message.Length * samples_per_symbol];
     for (int i = 0; i < message.Length; i++)
     {
         for (int j = 0; j < samples_per_symbol; j++)
         {
             signal[i * samples_per_symbol + j] = alphabet[message[i]];
         }
     }
     return signal;
 }
        }//добавление АБГШ
        public static void generate_mixed_chirp_signal(int chirp_num, double duration_of_signal, double Fd, double[] Fmin, double[] Fmax, double[] init_phase, double[] A, out double[] t, out alglib.complex[] mixed_signal)//Амплитуды в децибелах
        {
            t = new double[(int)(duration_of_signal * Fd)];
            mixed_signal = new alglib.complex[(int)(duration_of_signal * Fd)];
            alglib.complex[] noise = create_AWGN(mixed_signal.Length);
            for (int i = 0; i < chirp_num; i++)
            {
                alglib.complex[] signal = chirp(Fmin[i], Fmax[i], Fd, duration_of_signal);
                double coeff = get_null_dB_mag(signal, noise, Fmin[i], Fmax[i], Fd);
                double new_coeff = Math.Sqrt(coeff * coeff * Math.Pow(10, A[i] / 10.0));
                for (int j = 0; j < signal.Length; j++)
                {
                    mixed_signal[j] += new_coeff * signal[j];
                }

               
                /* double noiseE = 0;
                 foreach (alglib.complex n in noise)
                 {
                     noiseE += Math.Pow(alglib.math.abscomplex(n), 2);
                 }
                 noiseE = noiseE * (Fmax[i] - Fmin[i]) / Fd;

                 double signalE = 0;
                 for (int j = 0; j < signal.Length; j++)
                 {
                     signalE += Math.Pow(alglib.math.abscomplex(new_coeff * signal[j]), 2);
                 }
                 double SNR =10* Math.Log10(signalE / noiseE) ;*/
            }

       /*   double E_sig = 0;
            foreach (alglib.complex val in mixed_signal)
            {
                E_sig += Math.Pow(alglib.math.abscomplex(val),2);

            }
            double E_noise = 0;
            foreach (alglib.complex val in noise)
            {
                E_noise += Math.Pow(alglib.math.abscomplex(val),2);

            }
            double SNR = 10 * Math.Log10(E_sig / E_noise);*/
            for (int j = 0; j < mixed_signal.Length; j++)
            {
                mixed_signal[j] += noise[j];
                t[j] = j / Fd;
            }

        }
        /// <summary>
        /// Filtruje sygnal z fft za pomoca zestawu filtrow z filtersBank
        /// </summary>
        /// <param name="fft"></param>
        /// <param name="filtersBank"></param>
        /// <returns></returns>
        private static double[] FilterData(Complex[] fft, double[][] filtersBank)
        {
            double[] X = new double[filtersBank.Length];

            for (int i = 0; i < X.Length; i++) {
                double sum = 0.0;
                for (int j = 0; j < fft.Length; j++) {
                    sum += alglib.math.abscomplex(fft[j]) * filtersBank[i][j];
                }
                X[i] = Math.Log10(sum);
            }

            return X;
        }
 public PSK(int _M, double _init_phase, double _T, int _samples_per_symbol)
 {
     M = _M;
     init_phase = _init_phase;
     T = _T;
     samples_per_symbol = _samples_per_symbol;
     Fd = samples_per_symbol / T;
     alphabet = new alglib.complex[M];
     for (int i = 0; i < M; i++)
     {
         double phase = init_phase + 2 * i * Math.PI / M;
         alphabet[i] = new alglib.complex(Math.Cos(phase), Math.Sin(phase));
     }
 }
 public static void create_RF_signal(alglib.complex[] baseband_signal, double Fd, double duration_of_signal, double Fc, double A, out alglib.complex[] RF_signal, out double[] t)
 {
     int samples = (int)(duration_of_signal * Fd);
     t = new double[samples];
     for (int i = 0; i < t.Length; i++)
     {
         t[i] = i / Fd;
     }
     RF_signal = new alglib.complex[t.Length];
     for (int i = 0; i < t.Length; i++)
     {
         RF_signal[i] = A * baseband_signal[i] * new alglib.complex(Math.Cos(Fc * 2 * Math.PI * t[i]), Math.Sin(Fc * 2 * Math.PI * t[i]));
     }
 }//создание радиосигнала, перенос на несущую
Esempio n. 11
0
        public static List <(Complex[] vec, double val)> EVD(Complex[,] input, int evNum)
        {
            var dim  = input.GetLength(0);
            var dimY = input.GetLength(1);

            if (dim != dimY)
            {
                return(new List <(Complex[] vec, double val)>());
            }

            var inputAlg = new alglib.complex[dim, dim];

            foreach (var i in Enumerable.Range(0, dim))
            {
                foreach (var j in Enumerable.Range(0, dim))
                {
                    inputAlg[i, j] = new alglib.complex(
                        input[i, j].Real,
                        input[i, j].Imaginary
                        );
                }
            }

            var w = new double[0];
            var z = new alglib.complex[0, 0];

            alglib.evd.hmatrixevdi(
                inputAlg,
                dim, 1,
                false, 0, evNum > dim ? dim - 1 : evNum - 1,
                ref w, ref z, new alglib.xparams(0)
                );

            var result = new List <(Complex[] vec, double val)>();

            foreach (var i in Enumerable.Range(0, evNum))
            {
                var vec = new Complex[dim];
                foreach (var j in Enumerable.Range(0, dim))
                {
                    vec[j] = new Complex(z[j, i].x, z[j, i].y);
                }
                result.Add((vec, w[i]));
        public static alglib.complex[] create_AWGN(int length)
        {
            alglib.complex[] noise = new alglib.complex[length];
            Random rand = new Random();
            for (int i = 0; i < length; i++)
            {
                double noise_val_I = 0;
                double noise_val_Q = 0;
                
                for (int j = 0; j < 30; j++)
                {
                    //double noise_val =(-2.0 * rand.NextDouble() + 1.0);
                    noise_val_I += (-2.0 * rand.NextDouble() + 1.0);
                    noise_val_Q += (-2.0 * rand.NextDouble() + 1.0);
                }
                noise[i] = new alglib.complex(noise_val_I / 30.0, noise_val_Q / 30.0);
            }

            return noise;
        }
Esempio n. 13
0
        /// <summary>
        /// Обратная матрица через алглиб
        /// </summary>
        /// <returns></returns>
        public CSqMatrix InvertAlg()
        {
            alglib.complex[,] res = new alglib.complex[this.ColCount, this.ColCount];
            for (int i = 0; i < this.ColCount; i++)
            {
                for (int j = 0; j < this.ColCount; j++)
                {
                    res[i, j] = new alglib.complex(this[i, j].Re, this[i, j].Im);
                }
            }

            alglib.cmatrixinverse(ref res, out int ti, out alglib.matinvreport rep);

            CSqMatrix m = new CSqMatrix(new Complex[this.ColCount, this.ColCount]);

            for (int i = 0; i < this.ColCount; i++)
            {
                for (int j = 0; j < this.ColCount; j++)
                {
                    m[i, j] = new Complex(res[i, j].x, res[i, j].y);
                }
            }
            return(m);
        }
Esempio n. 14
0
    public static void Main(string[] args)
    {
        bool _TotalResult = true;
        bool _TestResult;

        System.Console.WriteLine("x-tests. Please wait...");
        alglib.alloc_counter_activate();
        System.Console.WriteLine("Allocation counter activated...");
        try
        {
            const int max1d = 70;
            const int max2d = 40;

            System.Console.WriteLine("Basic tests:");
            {
                // deallocateimmediately()
                alglib.minlbfgsstate s;
                double[]             x = new double[100];
                long cnt0, cnt1;
                cnt0 = alglib.alloc_counter();
                alglib.minlbfgscreate(x.Length, 10, x, out s);
                alglib.deallocateimmediately(ref s);
                cnt1        = alglib.alloc_counter();
                _TestResult = cnt1 <= cnt0;
                System.Console.WriteLine("* deallocateimmediately()    " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // boolean 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int n, i, cnt;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    bool[] arr0 = new bool[n];
                    bool[] arr1 = new bool[n];
                    bool[] arr2 = new bool[n];
                    bool[] arr3 = null;
                    cnt = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randomreal() > 0.5;
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        if (arr0[i])
                        {
                            cnt++;
                        }
                    }
                    _TestResult = _TestResult && (alglib.xdebugb1count(arr0) == cnt);
                    alglib.xdebugb1not(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr1[i] == !arr0[i]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugb1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugb1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr3[i] == (i % 2 == 0));
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* boolean 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // integer 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int n, i, sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    int[] arr0 = new int[n];
                    int[] arr1 = new int[n];
                    int[] arr2 = new int[n];
                    int[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randominteger(10);
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        sum    += arr0[i];
                    }
                    _TestResult = _TestResult && (alglib.xdebugi1sum(arr0) == sum);
                    alglib.xdebugi1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr1[i] == -arr0[i]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugi1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugi1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i] == i);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* integer 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int    n, i;
                double sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    double[] arr0 = new double[n];
                    double[] arr1 = new double[n];
                    double[] arr2 = new double[n];
                    double[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randomreal() - 0.5;
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        sum    += arr0[i];
                    }
                    _TestResult = _TestResult && (Math.Abs(alglib.xdebugr1sum(arr0) - sum) < 1.0E-10);
                    alglib.xdebugr1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (Math.Abs(arr1[i] + arr0[i]) < 1.0E-10);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugr1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugr1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i] == i * 0.25);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* real 1D arrays             " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // complex 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int            n, i;
                alglib.complex sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    alglib.complex[] arr0 = new alglib.complex[n];
                    alglib.complex[] arr1 = new alglib.complex[n];
                    alglib.complex[] arr2 = new alglib.complex[n];
                    alglib.complex[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i].x = alglib.math.randomreal() - 0.5;
                        arr0[i].y = alglib.math.randomreal() - 0.5;
                        arr1[i]   = arr0[i];
                        arr2[i]   = arr0[i];
                        sum      += arr0[i];
                    }
                    _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc1sum(arr0) - sum) < 1.0E-10);
                    alglib.xdebugc1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (alglib.math.abscomplex(arr1[i] + arr0[i]) < 1.0E-10);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugc1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugc1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i].x == i * 0.250);
                                _TestResult = _TestResult && (arr3[i].y == i * 0.125);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* complex 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // boolean 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int m, n, i, j, cnt;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        bool[,] arr0 = new bool[m, n];
                        bool[,] arr1 = new bool[m, n];
                        bool[,] arr2 = new bool[m, n];
                        bool[,] arr3 = null;
                        cnt          = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randomreal() > 0.5;
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                if (arr0[i, j])
                                {
                                    cnt++;
                                }
                            }
                        }
                        _TestResult = _TestResult && (alglib.xdebugb2count(arr0) == cnt);
                        alglib.xdebugb2not(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == !arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugb2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugb2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr3[i, j] == (Math.Sin(3 * i + 5 * j) > 0));
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* boolean 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // integer 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int m, n, i, j;
                int sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        int[,] arr0 = new int[m, n];
                        int[,] arr1 = new int[m, n];
                        int[,] arr2 = new int[m, n];
                        int[,] arr3 = null;
                        sum         = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randominteger(10);
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                sum       += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (alglib.xdebugi2sum(arr0) == sum);
                        alglib.xdebugi2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugi2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugi2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr3[i, j] == System.Math.Sign(Math.Sin(3 * i + 5 * j)));
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* integer 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int    m, n, i, j;
                double sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        double[,] arr0 = new double[m, n];
                        double[,] arr1 = new double[m, n];
                        double[,] arr2 = new double[m, n];
                        double[,] arr3 = null;
                        sum            = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randomreal() - 0.5;
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                sum       += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (System.Math.Abs(alglib.xdebugr2sum(arr0) - sum) < 1.0E-10);
                        alglib.xdebugr2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugr2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugr2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j] - Math.Sin(3 * i + 5 * j)) < 1E-10);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* real 2D arrays             " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int            m, n, i, j;
                alglib.complex sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        alglib.complex[,] arr0 = new alglib.complex[m, n];
                        alglib.complex[,] arr1 = new alglib.complex[m, n];
                        alglib.complex[,] arr2 = new alglib.complex[m, n];
                        alglib.complex[,] arr3 = null;
                        sum = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j].x = alglib.math.randomreal() - 0.5;
                                arr0[i, j].y = alglib.math.randomreal() - 0.5;
                                arr1[i, j]   = arr0[i, j];
                                arr2[i, j]   = arr0[i, j];
                                sum         += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc2sum(arr0) - sum) < 1.0E-10);
                        alglib.xdebugc2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugc2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugc2outsincos(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j].x - Math.Sin(3 * i + 5 * j)) < 1E-10);
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j].y - Math.Cos(3 * i + 5 * j)) < 1E-10);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* complex 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // "biased product / sum" test
                int    m, n, i, j;
                double sum;
                _TestResult = true;
                for (n = 1; n <= max2d; n++)
                {
                    for (m = 1; m <= max2d; m++)
                    {
                        // proceed to testing
                        double[,] a = new double[m, n];
                        double[,] b = new double[m, n];
                        bool[,]   c = new bool[m, n];
                        sum         = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                a[i, j] = alglib.math.randomreal() - 0.5;
                                b[i, j] = alglib.math.randomreal() - 0.5;
                                c[i, j] = alglib.math.randomreal() > 0.5;
                                if (c[i, j])
                                {
                                    sum += a[i, j] * (1 + b[i, j]);
                                }
                            }
                        }
                        _TestResult = _TestResult && (Math.Abs(alglib.xdebugmaskedbiasedproductsum(m, n, a, b, c) - sum) < 1.0E-10);
                    }
                }
                System.Console.WriteLine("* multiple arrays            " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }


            //////////////////////////////////
            // Advanced tests
            //////
            System.Console.WriteLine("Advances tests:");

            //
            // Testing CSV functionality
            //
            {
                string csv_name = "alglib-tst-35252-ndg4sf.csv";
                _TestResult = true;
                try
                {
                    // CSV_DEFAULT must be zero
                    _TestResult = _TestResult && alglib.CSV_DEFAULT == 0;

                    // absent file - must fail
                    try
                    {
                        double[,] arr;
                        alglib.read_csv("nonexistent123foralgtestinglib", '\t', alglib.CSV_DEFAULT, out arr);
                        _TestResult = false;
                    }
                    catch
                    { }

                    // non-rectangular file - must fail
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "a,b,c\r\n1,2");
                        alglib.read_csv(csv_name, ',', alglib.CSV_SKIP_HEADERS, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = false;
                    }
                    catch
                    { }
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "a,b,c\r\n1,2,3,4");
                        alglib.read_csv(csv_name, ',', alglib.CSV_SKIP_HEADERS, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = false;
                    }
                    catch
                    { }
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "1,2,3,4\n1,2,3\n1,2,3");
                        alglib.read_csv(csv_name, ',', alglib.CSV_DEFAULT, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = false;
                    }
                    catch
                    { }

                    // empty file
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "");
                        alglib.read_csv(csv_name, '\t', alglib.CSV_DEFAULT, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = _TestResult && arr.GetLength(0) == 0 && arr.GetLength(1) == 0;
                    }
                    catch
                    { _TestResult = false; }

                    // one row with header, tab separator
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "a\tb\tc\n");
                        alglib.read_csv(csv_name, '\t', alglib.CSV_SKIP_HEADERS, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = _TestResult && arr.GetLength(0) == 0 && arr.GetLength(1) == 0;
                    }
                    catch
                    { _TestResult = false; }

                    // no header, comma-separated, full stop as decimal point
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "1.5,2,3.25\n4,5,6");
                        alglib.read_csv(csv_name, ',', alglib.CSV_DEFAULT, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = _TestResult && alglib.ap.format(arr, 2) == "{{1.50,2.00,3.25},{4.00,5.00,6.00}}";
                    }
                    catch
                    { _TestResult = false; }

                    // header, tab-separated, mixed use of comma and full stop as decimal points
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, "a\tb\tc\n1.5\t2\t3,25\n4\t5.25\t6,1\n");
                        alglib.read_csv(csv_name, '\t', alglib.CSV_SKIP_HEADERS, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = _TestResult && alglib.ap.format(arr, 2) == "{{1.50,2.00,3.25},{4.00,5.25,6.10}}";
                    }
                    catch
                    { _TestResult = false; }

                    // header, tab-separated, fixed/exponential, spaces, mixed use of comma and full stop as decimal points
                    try
                    {
                        double[,] arr;
                        System.IO.File.WriteAllText(csv_name, " a\t b \tc\n1,1\t 2.9\t -3.5  \n  1.1E1  \t 2.0E-1 \t-3E+1 \n+1  \t -2\t 3.    \n.1\t-.2\t+.3\n");
                        alglib.read_csv(csv_name, '\t', alglib.CSV_SKIP_HEADERS, out arr);
                        System.IO.File.Delete(csv_name);
                        _TestResult = _TestResult && alglib.ap.format(arr, 2) == "{{1.10,2.90,-3.50},{11.00,0.20,-30.00},{1.00,-2.00,3.00},{0.10,-0.20,0.30}}";
                    }
                    catch
                    { _TestResult = false; }
                }
                catch
                {
                    _TestResult = false;
                }

                //
                // Report
                //
                System.Console.WriteLine("* CSV support                " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }

            //////////////////////////////////
            // Test issues from Mantis
            //////
            System.Console.WriteLine("Testing issies from Mantis:");


            //
            // Task #594 (http://bugs.alglib.net/view.php?id=594) - additional
            // test for correctness of copying of objects. When we copy ALGLIB
            // object, indenendent new copy is created.
            //
            {
                //
                // First, test copying of alglib.multilayerperceptron, which
                // is an "opaque object".
                //
                // Test copy constructors:
                // * copy object with make_copy()
                // * process vector with original network
                // * randomize original network
                // * process vector with copied networks and compare
                //
                alglib.multilayerperceptron net0, net1;
                double[] x  = new double[] { 1, 2 };
                double[] y0 = new double[] { 0, 0 };
                double[] y1 = new double[] { 0, 0 };
                double[] y2 = new double[] { 0, 0 };
                _TestResult = true;
                alglib.mlpcreate0(2, 2, out net0);
                alglib.mlpprocess(net0, x, ref y0);
                net1 = (alglib.multilayerperceptron)net0.make_copy();
                alglib.mlprandomize(net0);
                alglib.mlpprocess(net1, x, ref y1);
                _TestResult = _TestResult && (Math.Abs(y0[0] - y1[0]) < 1.0E-9) && (Math.Abs(y0[1] - y1[1]) < 1.0E-9);

                //
                // Then, test correctness of copying "records", i.e.
                // objects with publicly visible fields.
                //
                alglib.xdebugrecord1 r0, r1;
                alglib.xdebuginitrecord1(out r0);
                r1          = (alglib.xdebugrecord1)r0.make_copy();
                _TestResult = _TestResult && (r1.i == r0.i);
                _TestResult = _TestResult && (r1.c == r0.c);

                _TestResult = _TestResult && (r1.a.Length == 2);
                _TestResult = _TestResult && (r0.a.Length == 2);
                _TestResult = _TestResult && (r1.a != r0.a);
                _TestResult = _TestResult && (r1.a[0] == r0.a[0]);
                _TestResult = _TestResult && (r1.a[1] == r0.a[1]);

                //
                // Test result
                //
                System.Console.WriteLine("* issue 594                  " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
        }
        catch
        {
            System.Console.WriteLine("Unhandled exception was raised!");
            System.Environment.ExitCode = 1;
            return;
        }

        //
        // Test below creates instance of MemoryLeaksTest object.
        //
        // This object is descendant of CriticalFinalizerObject class,
        // which guarantees that it will be finalized AFTER all other
        // ALGLIB objects which hold pointers to unmanaged memory.
        //
        // Tests for memory leaks are done within object's destructor.
        //
        MemoryLeaksTest _test_object = new MemoryLeaksTest();

        if (!_TotalResult)
        {
            System.Environment.ExitCode = 1;
        }
    }
        private void calculate_SPM(alglib.complex[] mixed_signal_chirp, double Fd_c, out double[] SPM_chirp_signal, out double[] f_chirp_signal)
        {
            alglib.complex[] mixed_signal_chirp_copy = new alglib.complex[mixed_signal_chirp.Length];
            System.Array.Copy(mixed_signal_chirp, mixed_signal_chirp_copy, mixed_signal_chirp.Length);
            alglib.fftc1d(ref mixed_signal_chirp_copy);
            SPM_chirp_signal = new double[mixed_signal_chirp_copy.Length];
            f_chirp_signal = new double[mixed_signal_chirp_copy.Length];


            for (int i = 0; i < f_chirp_signal.Length; i++)
            {
                f_chirp_signal[i] = Fd_c * i / (f_chirp_signal.Length - 1);
                SPM_chirp_signal[i] = alglib.math.abscomplex(mixed_signal_chirp_copy[i]);
            }
        }
Esempio n. 16
0
    public static void Main(string[] args)
    {
        bool _TotalResult = true;
        bool _TestResult;
        int  _spoil_scenario;

        System.Console.WriteLine("x-tests. Please wait...");
        alglib.alloc_counter_activate();
        System.Console.WriteLine("Allocation counter activated...");
        try
        {
            const int max1d = 70;
            const int max2d = 40;

            System.Console.WriteLine("Basic tests:");
            {
                // deallocateimmediately()
                alglib.minlbfgsstate s;
                double[]             x = new double[100];
                long cnt0, cnt1;
                cnt0 = alglib.alloc_counter();
                alglib.minlbfgscreate(x.Length, 10, x, out s);
                alglib.deallocateimmediately(ref s);
                cnt1        = alglib.alloc_counter();
                _TestResult = cnt1 <= cnt0;
                System.Console.WriteLine("* deallocateimmediately()    " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // boolean 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int n, i, cnt;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    bool[] arr0 = new bool[n];
                    bool[] arr1 = new bool[n];
                    bool[] arr2 = new bool[n];
                    bool[] arr3 = null;
                    cnt = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randomreal() > 0.5;
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        if (arr0[i])
                        {
                            cnt++;
                        }
                    }
                    _TestResult = _TestResult && (alglib.xdebugb1count(arr0) == cnt);
                    alglib.xdebugb1not(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr1[i] == !arr0[i]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugb1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugb1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr3[i] == (i % 2 == 0));
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* boolean 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // integer 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int n, i, sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    int[] arr0 = new int[n];
                    int[] arr1 = new int[n];
                    int[] arr2 = new int[n];
                    int[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randominteger(10);
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        sum    += arr0[i];
                    }
                    _TestResult = _TestResult && (alglib.xdebugi1sum(arr0) == sum);
                    alglib.xdebugi1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr1[i] == -arr0[i]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugi1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugi1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i] == i);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* integer 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int    n, i;
                double sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    double[] arr0 = new double[n];
                    double[] arr1 = new double[n];
                    double[] arr2 = new double[n];
                    double[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randomreal() - 0.5;
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        sum    += arr0[i];
                    }
                    _TestResult = _TestResult && (Math.Abs(alglib.xdebugr1sum(arr0) - sum) < 1.0E-10);
                    alglib.xdebugr1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (Math.Abs(arr1[i] + arr0[i]) < 1.0E-10);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugr1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugr1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i] == i * 0.25);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* real 1D arrays             " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // complex 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int            n, i;
                alglib.complex sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    alglib.complex[] arr0 = new alglib.complex[n];
                    alglib.complex[] arr1 = new alglib.complex[n];
                    alglib.complex[] arr2 = new alglib.complex[n];
                    alglib.complex[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i].x = alglib.math.randomreal() - 0.5;
                        arr0[i].y = alglib.math.randomreal() - 0.5;
                        arr1[i]   = arr0[i];
                        arr2[i]   = arr0[i];
                        sum      += arr0[i];
                    }
                    _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc1sum(arr0) - sum) < 1.0E-10);
                    alglib.xdebugc1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (alglib.math.abscomplex(arr1[i] + arr0[i]) < 1.0E-10);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugc1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugc1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i].x == i * 0.250);
                                _TestResult = _TestResult && (arr3[i].y == i * 0.125);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* complex 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // boolean 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int m, n, i, j, cnt;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        bool[,] arr0 = new bool[m, n];
                        bool[,] arr1 = new bool[m, n];
                        bool[,] arr2 = new bool[m, n];
                        bool[,] arr3 = null;
                        cnt          = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randomreal() > 0.5;
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                if (arr0[i, j])
                                {
                                    cnt++;
                                }
                            }
                        }
                        _TestResult = _TestResult && (alglib.xdebugb2count(arr0) == cnt);
                        alglib.xdebugb2not(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == !arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugb2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugb2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr3[i, j] == (Math.Sin(3 * i + 5 * j) > 0));
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* boolean 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // integer 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int m, n, i, j;
                int sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        int[,] arr0 = new int[m, n];
                        int[,] arr1 = new int[m, n];
                        int[,] arr2 = new int[m, n];
                        int[,] arr3 = null;
                        sum         = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randominteger(10);
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                sum       += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (alglib.xdebugi2sum(arr0) == sum);
                        alglib.xdebugi2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugi2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugi2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr3[i, j] == System.Math.Sign(Math.Sin(3 * i + 5 * j)));
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* integer 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int    m, n, i, j;
                double sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        double[,] arr0 = new double[m, n];
                        double[,] arr1 = new double[m, n];
                        double[,] arr2 = new double[m, n];
                        double[,] arr3 = null;
                        sum            = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randomreal() - 0.5;
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                sum       += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (System.Math.Abs(alglib.xdebugr2sum(arr0) - sum) < 1.0E-10);
                        alglib.xdebugr2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugr2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugr2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j] - Math.Sin(3 * i + 5 * j)) < 1E-10);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* real 2D arrays             " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int            m, n, i, j;
                alglib.complex sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        alglib.complex[,] arr0 = new alglib.complex[m, n];
                        alglib.complex[,] arr1 = new alglib.complex[m, n];
                        alglib.complex[,] arr2 = new alglib.complex[m, n];
                        alglib.complex[,] arr3 = null;
                        sum = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j].x = alglib.math.randomreal() - 0.5;
                                arr0[i, j].y = alglib.math.randomreal() - 0.5;
                                arr1[i, j]   = arr0[i, j];
                                arr2[i, j]   = arr0[i, j];
                                sum         += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc2sum(arr0) - sum) < 1.0E-10);
                        alglib.xdebugc2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugc2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugc2outsincos(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j].x - Math.Sin(3 * i + 5 * j)) < 1E-10);
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j].y - Math.Cos(3 * i + 5 * j)) < 1E-10);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* complex 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // "biased product / sum" test
                int    m, n, i, j;
                double sum;
                _TestResult = true;
                for (n = 1; n <= max2d; n++)
                {
                    for (m = 1; m <= max2d; m++)
                    {
                        // proceed to testing
                        double[,] a = new double[m, n];
                        double[,] b = new double[m, n];
                        bool[,]   c = new bool[m, n];
                        sum         = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                a[i, j] = alglib.math.randomreal() - 0.5;
                                b[i, j] = alglib.math.randomreal() - 0.5;
                                c[i, j] = alglib.math.randomreal() > 0.5;
                                if (c[i, j])
                                {
                                    sum += a[i, j] * (1 + b[i, j]);
                                }
                            }
                        }
                        _TestResult = _TestResult && (Math.Abs(alglib.xdebugmaskedbiasedproductsum(m, n, a, b, c) - sum) < 1.0E-10);
                    }
                }
                System.Console.WriteLine("* multiple arrays            " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
        }
        catch
        {
            System.Console.WriteLine("Unhandled exception was raised!");
            System.Environment.ExitCode = 1;
            return;
        }

        //
        // Test below creates instance of MemoryLeaksTest object.
        //
        // This object is descendant of CriticalFinalizerObject class,
        // which guarantees that it will be finalized AFTER all other
        // ALGLIB objects which hold pointers to unmanaged memory.
        //
        // Tests for memory leaks are done within object's destructor.
        //
        MemoryLeaksTest _test_object = new MemoryLeaksTest();

        if (!_TotalResult)
        {
            System.Environment.ExitCode = 1;
        }
    }
    public static void Main(string[] args)
    {
        bool _TotalResult = true;
        bool _TestResult;
        int  _spoil_scenario;

        System.Console.WriteLine("x-tests. Please wait...");
        alglib.alloc_counter_activate();
        System.Console.WriteLine("Allocation counter activated...");
        try
        {
            const int max1d = 70;
            const int max2d = 40;

            System.Console.WriteLine("Basic tests:");
            {
                // deallocateimmediately()
                alglib.minlbfgsstate s;
                double[]             x = new double[100];
                long cnt0, cnt1;
                cnt0 = alglib.alloc_counter();
                alglib.minlbfgscreate(x.Length, 10, x, out s);
                alglib.deallocateimmediately(ref s);
                cnt1        = alglib.alloc_counter();
                _TestResult = cnt1 <= cnt0;
                System.Console.WriteLine("* deallocateimmediately()    " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // boolean 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int n, i, cnt;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    bool[] arr0 = new bool[n];
                    bool[] arr1 = new bool[n];
                    bool[] arr2 = new bool[n];
                    bool[] arr3 = null;
                    cnt = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randomreal() > 0.5;
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        if (arr0[i])
                        {
                            cnt++;
                        }
                    }
                    _TestResult = _TestResult && (alglib.xdebugb1count(arr0) == cnt);
                    alglib.xdebugb1not(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr1[i] == !arr0[i]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugb1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugb1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr3[i] == (i % 2 == 0));
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* boolean 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // integer 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int n, i, sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    int[] arr0 = new int[n];
                    int[] arr1 = new int[n];
                    int[] arr2 = new int[n];
                    int[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randominteger(10);
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        sum    += arr0[i];
                    }
                    _TestResult = _TestResult && (alglib.xdebugi1sum(arr0) == sum);
                    alglib.xdebugi1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (arr1[i] == -arr0[i]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugi1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugi1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i] == i);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* integer 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int    n, i;
                double sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    double[] arr0 = new double[n];
                    double[] arr1 = new double[n];
                    double[] arr2 = new double[n];
                    double[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i] = alglib.math.randomreal() - 0.5;
                        arr1[i] = arr0[i];
                        arr2[i] = arr0[i];
                        sum    += arr0[i];
                    }
                    _TestResult = _TestResult && (Math.Abs(alglib.xdebugr1sum(arr0) - sum) < 1.0E-10);
                    alglib.xdebugr1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (Math.Abs(arr1[i] + arr0[i]) < 1.0E-10);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugr1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugr1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i] == i * 0.25);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* real 1D arrays             " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // complex 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int            n, i;
                alglib.complex sum;
                _TestResult = true;
                for (n = 0; n <= max1d; n++)
                {
                    alglib.complex[] arr0 = new alglib.complex[n];
                    alglib.complex[] arr1 = new alglib.complex[n];
                    alglib.complex[] arr2 = new alglib.complex[n];
                    alglib.complex[] arr3 = null;
                    sum = 0;
                    for (i = 0; i < n; i++)
                    {
                        arr0[i].x = alglib.math.randomreal() - 0.5;
                        arr0[i].y = alglib.math.randomreal() - 0.5;
                        arr1[i]   = arr0[i];
                        arr2[i]   = arr0[i];
                        sum      += arr0[i];
                    }
                    _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc1sum(arr0) - sum) < 1.0E-10);
                    alglib.xdebugc1neg(ref arr1);
                    if (alglib.ap.len(arr1) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            _TestResult = _TestResult && (alglib.math.abscomplex(arr1[i] + arr0[i]) < 1.0E-10);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugc1appendcopy(ref arr2);
                    if (alglib.ap.len(arr2) == 2 * n)
                    {
                        for (i = 0; i < 2 * n; i++)
                        {
                            _TestResult = _TestResult && (arr2[i] == arr0[i % n]);
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                    alglib.xdebugc1outeven(n, out arr3);
                    if (alglib.ap.len(arr3) == n)
                    {
                        for (i = 0; i < n; i++)
                        {
                            if (i % 2 == 0)
                            {
                                _TestResult = _TestResult && (arr3[i].x == i * 0.250);
                                _TestResult = _TestResult && (arr3[i].y == i * 0.125);
                            }
                            else
                            {
                                _TestResult = _TestResult && (arr3[i] == 0);
                            }
                        }
                    }
                    else
                    {
                        _TestResult = false;
                    }
                }
                System.Console.WriteLine("* complex 1D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // boolean 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int m, n, i, j, cnt;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        bool[,] arr0 = new bool[m, n];
                        bool[,] arr1 = new bool[m, n];
                        bool[,] arr2 = new bool[m, n];
                        bool[,] arr3 = null;
                        cnt          = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randomreal() > 0.5;
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                if (arr0[i, j])
                                {
                                    cnt++;
                                }
                            }
                        }
                        _TestResult = _TestResult && (alglib.xdebugb2count(arr0) == cnt);
                        alglib.xdebugb2not(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == !arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugb2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugb2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr3[i, j] == (Math.Sin(3 * i + 5 * j) > 0));
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* boolean 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // integer 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int m, n, i, j;
                int sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        int[,] arr0 = new int[m, n];
                        int[,] arr1 = new int[m, n];
                        int[,] arr2 = new int[m, n];
                        int[,] arr3 = null;
                        sum         = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randominteger(10);
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                sum       += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (alglib.xdebugi2sum(arr0) == sum);
                        alglib.xdebugi2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugi2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugi2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr3[i, j] == System.Math.Sign(Math.Sin(3 * i + 5 * j)));
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* integer 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int    m, n, i, j;
                double sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        double[,] arr0 = new double[m, n];
                        double[,] arr1 = new double[m, n];
                        double[,] arr2 = new double[m, n];
                        double[,] arr3 = null;
                        sum            = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j] = alglib.math.randomreal() - 0.5;
                                arr1[i, j] = arr0[i, j];
                                arr2[i, j] = arr0[i, j];
                                sum       += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (System.Math.Abs(alglib.xdebugr2sum(arr0) - sum) < 1.0E-10);
                        alglib.xdebugr2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugr2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugr2outsin(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j] - Math.Sin(3 * i + 5 * j)) < 1E-10);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* real 2D arrays             " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
                int            m, n, i, j;
                alglib.complex sum;
                _TestResult = true;
                for (n = 0; n <= max2d; n++)
                {
                    for (m = 0; m <= max2d; m++)
                    {
                        // skip situations when n*m==0, but n!=0 or m!=0
                        if (n * m == 0 && (n != 0 || m != 0))
                        {
                            continue;
                        }

                        // proceed to testing
                        alglib.complex[,] arr0 = new alglib.complex[m, n];
                        alglib.complex[,] arr1 = new alglib.complex[m, n];
                        alglib.complex[,] arr2 = new alglib.complex[m, n];
                        alglib.complex[,] arr3 = null;
                        sum = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                arr0[i, j].x = alglib.math.randomreal() - 0.5;
                                arr0[i, j].y = alglib.math.randomreal() - 0.5;
                                arr1[i, j]   = arr0[i, j];
                                arr2[i, j]   = arr0[i, j];
                                sum         += arr0[i, j];
                            }
                        }
                        _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc2sum(arr0) - sum) < 1.0E-10);
                        alglib.xdebugc2neg(ref arr1);
                        if (alglib.ap.rows(arr1) == m && alglib.ap.cols(arr1) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr1[i, j] == -arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugc2transpose(ref arr2);
                        if (alglib.ap.rows(arr2) == n && alglib.ap.cols(arr2) == m)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (arr2[j, i] == arr0[i, j]);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                        alglib.xdebugc2outsincos(m, n, out arr3);
                        if (alglib.ap.rows(arr3) == m && alglib.ap.cols(arr3) == n)
                        {
                            for (i = 0; i < m; i++)
                            {
                                for (j = 0; j < n; j++)
                                {
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j].x - Math.Sin(3 * i + 5 * j)) < 1E-10);
                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i, j].y - Math.Cos(3 * i + 5 * j)) < 1E-10);
                                }
                            }
                        }
                        else
                        {
                            _TestResult = false;
                        }
                    }
                }
                System.Console.WriteLine("* complex 2D arrays          " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
            {
                // "biased product / sum" test
                int    m, n, i, j;
                double sum;
                _TestResult = true;
                for (n = 1; n <= max2d; n++)
                {
                    for (m = 1; m <= max2d; m++)
                    {
                        // proceed to testing
                        double[,] a = new double[m, n];
                        double[,] b = new double[m, n];
                        bool[,]   c = new bool[m, n];
                        sum         = 0;
                        for (i = 0; i < m; i++)
                        {
                            for (j = 0; j < n; j++)
                            {
                                a[i, j] = alglib.math.randomreal() - 0.5;
                                b[i, j] = alglib.math.randomreal() - 0.5;
                                c[i, j] = alglib.math.randomreal() > 0.5;
                                if (c[i, j])
                                {
                                    sum += a[i, j] * (1 + b[i, j]);
                                }
                            }
                        }
                        _TestResult = _TestResult && (Math.Abs(alglib.xdebugmaskedbiasedproductsum(m, n, a, b, c) - sum) < 1.0E-10);
                    }
                }
                System.Console.WriteLine("* multiple arrays            " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }

            //////////////////////////////////
            // Test issues from Mantis
            //////
            System.Console.WriteLine("Testing issies from Mantis:");


            //
            // Task #594 (http://bugs.alglib.net/view.php?id=594) - additional
            // test for correctness of copying of objects. When we copy ALGLIB
            // object, indenendent new copy is created.
            //
            {
                //
                // First, test copying of alglib.multilayerperceptron, which
                // is an "opaque object".
                //
                // Test copy constructors:
                // * copy object with make_copy()
                // * process vector with original network
                // * randomize original network
                // * process vector with copied networks and compare
                //
                alglib.multilayerperceptron net0, net1;
                double[] x  = new double[] { 1, 2 };
                double[] y0 = new double[] { 0, 0 };
                double[] y1 = new double[] { 0, 0 };
                double[] y2 = new double[] { 0, 0 };
                _TestResult = true;
                alglib.mlpcreate0(2, 2, out net0);
                alglib.mlpprocess(net0, x, ref y0);
                net1 = (alglib.multilayerperceptron)net0.make_copy();
                alglib.mlprandomize(net0);
                alglib.mlpprocess(net1, x, ref y1);
                _TestResult = _TestResult && (Math.Abs(y0[0] - y1[0]) < 1.0E-9) && (Math.Abs(y0[1] - y1[1]) < 1.0E-9);

                //
                // Then, test correctness of copying "records", i.e.
                // objects with publicly visible fields.
                //
                alglib.xdebugrecord1 r0, r1;
                alglib.xdebuginitrecord1(out r0);
                r1          = (alglib.xdebugrecord1)r0.make_copy();
                _TestResult = _TestResult && (r1.i == r0.i);
                _TestResult = _TestResult && (r1.c == r0.c);

                _TestResult = _TestResult && (r1.a.Length == 2);
                _TestResult = _TestResult && (r0.a.Length == 2);
                _TestResult = _TestResult && (r1.a != r0.a);
                _TestResult = _TestResult && (r1.a[0] == r0.a[0]);
                _TestResult = _TestResult && (r1.a[1] == r0.a[1]);

                //
                // Test result
                //
                System.Console.WriteLine("* issue 594                  " + (_TestResult ? " OK" : " FAILED"));
                _TotalResult = _TotalResult && _TestResult;
            }
        }
        catch
        {
            System.Console.WriteLine("Unhandled exception was raised!");
            System.Environment.ExitCode = 1;
            return;
        }

        //
        // Test below creates instance of MemoryLeaksTest object.
        //
        // This object is descendant of CriticalFinalizerObject class,
        // which guarantees that it will be finalized AFTER all other
        // ALGLIB objects which hold pointers to unmanaged memory.
        //
        // Tests for memory leaks are done within object's destructor.
        //
        MemoryLeaksTest _test_object = new MemoryLeaksTest();

        if (!_TotalResult)
        {
            System.Environment.ExitCode = 1;
        }
    }
 private static double[] ExtractSignalPower(Complex[] fft)
 {
     double[] realFft = new double[fft.Length / 2];
     // Tylko 1 polowa jest znaczaca
     for (int i = 0; i < realFft.Length / 2; i++)
         realFft[i] = alglib.math.abscomplex(fft[i]);
     return realFft;
 }
Esempio n. 19
0
        public static int Main(string[] args)
        {
            //IList<double> R = getRandomVector(500, 100);
            //Читаем файлик OHLCV
            //string path = "z:\\YandexDisk\\Data\\GAZP_test_1h.txt";
            //TS.DataSource.BarList BarList = new TS.DataSource.BarList(path);

            HsaClassLibrary.Helpers.HhtCreator emdcr = new HsaClassLibrary.Helpers.HhtCreator();
            //EmdClassLibrary.EmdAbstractClass emd = emdcr.FactoryMethod(EmdClassLibrary.InterpolationEnum.ЛинейнаяИнтерполяция, EmdClassLibrary.StopCriterionEnum.КоличествоИтераций, EmdClassLibrary.StopSiftCriterionEnum.КоличествоИтераций);
            //EmdClassLibrary.EmdAbstractClass emd = emdcr.FactoryMethod(EmdClassLibrary.InterpolationEnum.ЛинейнаяИнтерполяция, EmdClassLibrary.StopCriterionEnum.КоличествоИтераций, EmdClassLibrary.StopSiftCriterionEnum.ДостигнутаТочностьОтсеивания);
            HsaClassLibrary.Transform.HilbertSpectrum Hsa = emdcr.HsaFactoryMethod(HsaClassLibrary.Transform.EnumHilbertTransform.HilbertTransform);

            //Выбираем для анализа
            IList<double> R = HsaClassLibrary.Helpers.MathHelper.getCos(256, 1, 2.0 * Math.PI / 32.0);
            //IList<double> R = HsaClassLibrary.Helpers.MathHelper.getCosAM(256, 1, 2.0 * Math.PI / 32.0, 2.0 * Math.PI / 256.0);
            //Данные для вывода в файл
            IList<IList<double>> data = new List<IList<double>>();
            data.Add(R);
            Hsa.Source = R;

            //1 ft
            double[] FT1r = new double[R.Count];
            double[] FT1i = new double[R.Count];

            AForge.Math.Complex[] FT1f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT1f[i].Re = R[i];
                FT1f[i].Im = 0;
            }
            AForge.Math.FourierTransform.DFT(FT1f, AForge.Math.FourierTransform.Direction.Forward);
            for (int i = 0; i < R.Count; i++)
            {
                FT1r[i] = FT1f[i].Re;
                FT1i[i] = FT1f[i].Im;
            }
            data.Add(FT1r);
            data.Add(FT1i);
            //2 ft
            double[] FT2r = new double[R.Count];
            double[] FT2i = new double[R.Count];

            AForge.Math.Complex[] FT2f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT2f[i].Re = R[i];
                FT2f[i].Im = 0;
            }
            AForge.Math.FourierTransform.DFT(FT2f, AForge.Math.FourierTransform.Direction.Backward);
            for (int i = 0; i < R.Count; i++)
            {
                FT2r[i] = FT2f[i].Re;
                FT2i[i] = FT2f[i].Im;
            }
            data.Add(FT2r);
            data.Add(FT2i);

            //3 ft
            double[] FT3r = new double[R.Count];
            double[] FT3i = new double[R.Count];

            AForge.Math.Complex[] FT3f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT3f[i].Re = R[i];
                FT3f[i].Im = 0;
            }
            AForge.Math.FourierTransform.FFT(FT3f, AForge.Math.FourierTransform.Direction.Forward);
            for (int i = 0; i < R.Count; i++)
            {
                FT3r[i] = FT3f[i].Re;
                FT3i[i] = FT3f[i].Im;
            }
            data.Add(FT1r);
            data.Add(FT1i);

            //4 ft
            double[] FT4r = new double[R.Count];
            double[] FT4i = new double[R.Count];

            AForge.Math.Complex[] FT4f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT4f[i].Re = R[i];
                FT4f[i].Im = 0;
            }
            AForge.Math.FourierTransform.FFT(FT4f, AForge.Math.FourierTransform.Direction.Backward);
            for (int i = 0; i < R.Count; i++)
            {
                FT4r[i] = FT4f[i].Re;
                FT4i[i] = FT4f[i].Im;
            }
            data.Add(FT4r);
            data.Add(FT4i);

            //5
            double[] FT5r = new double[R.Count];
            double[] FT5i = new double[R.Count];

            double[] FT5 = new double[R.Count];
            R.CopyTo(FT5, 0);

            alglib.complex[] FT5f;
            alglib.fftr1d(FT5, out FT5f);
            for (int i = 0; i < R.Count; i++)
            {
                FT5r[i] = FT5f[i].x;
                FT5i[i] = FT5f[i].y;
            }
            data.Add(FT5r);
            data.Add(FT5i);

            //6
            double[] FT6f = new double[R.Count];

            alglib.complex[] FT6 = new alglib.complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT6[i].x = R[i];
                FT6[i].y = 0;
            }

            alglib.fftr1dinv(FT6, out FT6f);
            data.Add(FT6f);

            //7
            /*
            double[] FT7r = new double[R.Count];
            double[] FT7i = new double[R.Count];

            List<double> FT7 = new List<double>(R);
            List<Complex> FT7f;
            FT7f = HsaClassLibrary.Transform.FourierTransform.ft(FT7);
            for (int i = 0; i < R.Count; i++)
            {
                FT7r[i] = FT7f[i].Real;
                FT7i[i] = FT7f[i].Imaginary;
            }
            data.Add(FT7r);
            data.Add(FT7i);

            data.Add(R);
             */
            //8
            List<double> FT8 = new List<double>(R);
            List<Complex> FT8f;

            FT8f = HsaClassLibrary.Transform.FourierTransform.fft(FT8).ToList();

            IList<double> FT8r = new double[R.Count];
            IList<double> FT8i = new double[R.Count];
            HsaClassLibrary.Transform.TransformHelper.convert(FT8f, out FT8r, out FT8i);
            data.Add(FT8r);
            data.Add(FT8i);

            //9
            /*
            double[] FT9r = new double[R.Count];
            double[] FT9i = new double[R.Count];

            List<Complex> FT9 = new List<Complex>(R.Count);
            for (int i = 0; i < R.Count; i++)
            {
                FT9.Add(new Complex(R[i],0));
            }
            List<Complex> FT9f;

            FT9f = HsaClassLibrary.Transform.FourierTransform.ifft(FT9);
            for (int i = 0; i < R.Count; i++)
            {
                FT9r[i] = FT9f[i].Real;
                FT9i[i] = FT9f[i].Imaginary;
            }
            data.Add(FT9r);
            data.Add(FT9i);
            */
            //10
            List<Complex> FT10 = new List<Complex>(R.Count);
            for (int i = 0; i < R.Count; i++)
            {
                FT10.Add(new Complex(R[i], 0));
            }

            IList<Complex> FT10f;
            FT10f = HsaClassLibrary.Transform.FourierTransform.ifft(FT10);

            IList<double> FT10r = new double[R.Count];
            IList<double> FT10i = new double[R.Count];
            HsaClassLibrary.Transform.TransformHelper.convert(FT10f, out FT10r, out FT10i);
            data.Add(FT10r);
            data.Add(FT10i);

            //AForge.Math.FourierTransform.FFT

            //System.Console.WriteLine("FourierTransform.FFT выполнена.");
            //System.Console.WriteLine("Console.ReadKey();");
            //Console.ReadKey();
            HsaClassLibrary.Helpers.ReadWriteHelper emdWriter = new HsaClassLibrary.Helpers.ReadWriteHelper();
            emdWriter.WriteCSV(data, "D:\\hsa8.csv");
            System.Console.WriteLine("Файл csv создан.\nD:\\hsa8.csv");
            //System.Console.WriteLine("Console.ReadKey();");
            Console.ReadKey();
            return 0;
        }
Esempio n. 20
0
        public void Calculate_FFT_Data(double NSamples, int WaveWindowType)
        {
            //Prepearing Complex Array fo fft
            alglib.complex[] ComplexArray = new alglib.complex[DoneArray.Length];
            for (int i = 0; i < 256; i++)
            {
                ComplexArray[i].x = DoneArray[i] * GetWaveWindowCoeff(WaveWindowType, i);
                ComplexArray[i].y = 0;
            }
            // FFT Magick!
            alglib.fftc1d(ref ComplexArray);

            // Analyzing recieved data.
            int Nmax = (255 + 1) / 2;
            //harmonica ampl limit
            double limit = 0.001;
            // optimization var
            double abs2min = limit * limit * 256 * 256;

            //-------finding constant part of signal------
            if (ComplexArray[0].x >= limit)
            {
                amp[0]   = ComplexArray[0].x / 256;
                freq[0]  = 0.0;
                phase[0] = 0.0;
            }

            int harmonic_Index = 0;

            for (int i = 1; i < Nmax; ++i)
            {
                double re = ComplexArray[i].x;
                double im = ComplexArray[i].y;

                // sqr of modul
                double abs2 = re * re + im * im;

                // low harmonics no
                if (abs2 < abs2min)
                {
                    continue;
                }

                // ampl (without mirror effect)
                amp[harmonic_Index] = 2.0 * Math.Sqrt(abs2) / 256;

                // phase of COS
                phase[harmonic_Index] = Math.Atan2(im, re);

                // COS >> SIN
                phase[harmonic_Index] += 1.571;
                if (phase[harmonic_Index] > 3.142)
                {
                    phase[harmonic_Index] -= 6.283;
                }

                // get freq
                freq[harmonic_Index] = (NSamples * i) / 256;
                ++harmonic_Index;
            }
            // harmonic_Index -  Found harmonics count. Число гармоник которые нашли.
        }
        public static void generate_mixed_PSK_signal(int PSK_num, int[] symbols_in_message, double duration_of_signal, double Fd, double[] T, double[] Fc, int[] M, double[] init_phase, int[] samples_per_symbol, double[] A, out double[] t, alglib.complex[] noise, out alglib.complex[] mixed_signal_without_noise)
        {
            t = new double[0];
            mixed_signal_without_noise = new alglib.complex[(int)(duration_of_signal * Fd)];
            for (int i = 0; i < PSK_num; i++)
            {
                int[] message = new int[symbols_in_message[i]];
                Random rand = new Random();
                for (int j = 0; j < message.Length; j++)
                {
                    message[j] = rand.Next(M[i]);
                }
                PSK psk = new PSK(M[i], init_phase[0], T[i], samples_per_symbol[i]);
                alglib.complex[] signal;


                PSK.create_RF_signal(psk.modulate(message), Fd, duration_of_signal, Fc[i], 1, out signal, out t);
                psk_band_filtering(ref signal, 1.0 / T[i], Fd, Fc[i]);
                double coeff = get_null_dB_mag(signal, noise, 1.0 / T[i], Fd);
                double new_coeff = Math.Sqrt(coeff * coeff * Math.Pow(10, A[i] / 10.0));



                for (int j = 0; j < signal.Length; j++)
                {
                    mixed_signal_without_noise[j] += new_coeff * signal[j];
                    t[j] = j / Fd;
                }
            }
        }
        /// <summary>
        /// Wykonaj szybka transformate furiera na zadanym ciagu problek.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static Complex[] GetFFT(double[] data)
        {
            Debug.Assert(MFCCUtils.IsPowerOf2(data.Length));

            var fftOutput = new Complex[data.Length];
            alglib.fft.fftr1d(data, data.Length, ref fftOutput);

            return fftOutput;
        }
Esempio n. 23
0
 public static void Main(string[] args)
 {
     bool _TotalResult = true;
     bool _TestResult;
     int _spoil_scenario;
     System.Console.WriteLine("x-tests. Please wait...");
     alglib.alloc_counter_activate();
     System.Console.WriteLine("Allocation counter activated...");
     try
     {
         const int max1d = 70;
         const int max2d = 40;
         
         System.Console.WriteLine("Basic tests:");
         {
             // deallocateimmediately()
             alglib.minlbfgsstate s;
             double[] x = new double[100];
             long cnt0, cnt1;
             cnt0 = alglib.alloc_counter();
             alglib.minlbfgscreate(x.Length, 10, x, out s);
             alglib.deallocateimmediately(ref s);
             cnt1 = alglib.alloc_counter();
             _TestResult = cnt1<=cnt0;
             System.Console.WriteLine("* deallocateimmediately()    "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // boolean 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int n, i, cnt;
             _TestResult = true;
             for(n=0; n<=max1d; n++)
             {
                 bool[] arr0 = new bool[n];
                 bool[] arr1 = new bool[n];
                 bool[] arr2 = new bool[n];
                 bool[] arr3 = null;
                 cnt = 0;
                 for(i=0; i<n; i++)
                 {
                     arr0[i] = alglib.math.randomreal()>0.5;
                     arr1[i] = arr0[i];
                     arr2[i] = arr0[i];
                     if( arr0[i] )
                         cnt++;
                 }
                 _TestResult = _TestResult && (alglib.xdebugb1count(arr0)==cnt);
                 alglib.xdebugb1not(ref arr1);
                 if( alglib.ap.len(arr1)==n )
                 {
                     for(i=0; i<n; i++)
                         _TestResult = _TestResult && (arr1[i]==!arr0[i]);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugb1appendcopy(ref arr2);
                 if( alglib.ap.len(arr2)==2*n )
                 {
                     for(i=0; i<2*n; i++)
                         _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugb1outeven(n, out arr3);
                 if( alglib.ap.len(arr3)==n )
                 {
                     for(i=0; i<n; i++)
                         _TestResult = _TestResult && (arr3[i]==(i%2==0));
                 }
                 else
                     _TestResult = false;
             }
             System.Console.WriteLine("* boolean 1D arrays          "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // integer 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int n, i, sum;
             _TestResult = true;
             for(n=0; n<=max1d; n++)
             {
                 int[] arr0 = new int[n];
                 int[] arr1 = new int[n];
                 int[] arr2 = new int[n];
                 int[] arr3 = null;
                 sum = 0;
                 for(i=0; i<n; i++)
                 {
                     arr0[i] = alglib.math.randominteger(10);
                     arr1[i] = arr0[i];
                     arr2[i] = arr0[i];
                     sum+=arr0[i];
                 }
                 _TestResult = _TestResult && (alglib.xdebugi1sum(arr0)==sum);
                 alglib.xdebugi1neg(ref arr1);
                 if( alglib.ap.len(arr1)==n )
                 {
                     for(i=0; i<n; i++)
                         _TestResult = _TestResult && (arr1[i]==-arr0[i]);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugi1appendcopy(ref arr2);
                 if( alglib.ap.len(arr2)==2*n )
                 {
                     for(i=0; i<2*n; i++)
                         _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugi1outeven(n,out arr3);
                 if( alglib.ap.len(arr3)==n )
                 {
                     for(i=0; i<n; i++)
                         if( i%2==0 )
                             _TestResult = _TestResult && (arr3[i]==i);
                         else
                             _TestResult = _TestResult && (arr3[i]==0);
                 }
                 else
                     _TestResult = false;
             }
             System.Console.WriteLine("* integer 1D arrays          "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // real 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int n, i;
             double sum;
             _TestResult = true;
             for(n=0; n<=max1d; n++)
             {
                 double[] arr0 = new double[n];
                 double[] arr1 = new double[n];
                 double[] arr2 = new double[n];
                 double[] arr3 = null;
                 sum = 0;
                 for(i=0; i<n; i++)
                 {
                     arr0[i] = alglib.math.randomreal()-0.5;
                     arr1[i] = arr0[i];
                     arr2[i] = arr0[i];
                     sum+=arr0[i];
                 }
                 _TestResult = _TestResult && (Math.Abs(alglib.xdebugr1sum(arr0)-sum)<1.0E-10);
                 alglib.xdebugr1neg(ref arr1);
                 if( alglib.ap.len(arr1)==n )
                 {
                     for(i=0; i<n; i++)
                         _TestResult = _TestResult && (Math.Abs(arr1[i]+arr0[i])<1.0E-10);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugr1appendcopy(ref arr2);
                 if( alglib.ap.len(arr2)==2*n )
                 {
                     for(i=0; i<2*n; i++)
                         _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugr1outeven(n,out arr3);
                 if( alglib.ap.len(arr3)==n )
                 {
                     for(i=0; i<n; i++)
                         if( i%2==0 )
                             _TestResult = _TestResult && (arr3[i]==i*0.25);
                         else
                             _TestResult = _TestResult && (arr3[i]==0);
                 }
                 else
                     _TestResult = false;
             }
             System.Console.WriteLine("* real 1D arrays             "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // complex 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int n, i;
             alglib.complex sum;
             _TestResult = true;
             for(n=0; n<=max1d; n++)
             {
                 alglib.complex[] arr0 = new alglib.complex[n];
                 alglib.complex[] arr1 = new alglib.complex[n];
                 alglib.complex[] arr2 = new alglib.complex[n];
                 alglib.complex[] arr3 = null;
                 sum = 0;
                 for(i=0; i<n; i++)
                 {
                     arr0[i].x = alglib.math.randomreal()-0.5;
                     arr0[i].y = alglib.math.randomreal()-0.5;
                     arr1[i] = arr0[i];
                     arr2[i] = arr0[i];
                     sum+=arr0[i];
                 }
                 _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc1sum(arr0)-sum)<1.0E-10);
                 alglib.xdebugc1neg(ref arr1);
                 if( alglib.ap.len(arr1)==n )
                 {
                     for(i=0; i<n; i++)
                         _TestResult = _TestResult && (alglib.math.abscomplex(arr1[i]+arr0[i])<1.0E-10);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugc1appendcopy(ref arr2);
                 if( alglib.ap.len(arr2)==2*n )
                 {
                     for(i=0; i<2*n; i++)
                         _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
                 }
                 else
                     _TestResult = false;
                 alglib.xdebugc1outeven(n,out arr3);
                 if( alglib.ap.len(arr3)==n )
                 {
                     for(i=0; i<n; i++)
                         if( i%2==0 )
                         {
                             _TestResult = _TestResult && (arr3[i].x==i*0.250);
                             _TestResult = _TestResult && (arr3[i].y==i*0.125);
                         }
                         else
                             _TestResult = _TestResult && (arr3[i]==0);
                 }
                 else
                     _TestResult = false;
             }
             System.Console.WriteLine("* complex 1D arrays          "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // boolean 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int m, n, i, j, cnt;
             _TestResult = true;
             for(n=0; n<=max2d; n++)
                 for(m=0; m<=max2d; m++)
                 {
                     // skip situations when n*m==0, but n!=0 or m!=0
                     if( n*m==0 && (n!=0 || m!=0) )
                         continue;
                     
                     // proceed to testing
                     bool[,] arr0 = new bool[m,n];
                     bool[,] arr1 = new bool[m,n];
                     bool[,] arr2 = new bool[m,n];
                     bool[,] arr3 = null;
                     cnt = 0;
                     for(i=0; i<m; i++)
                         for(j=0; j<n; j++)
                         {
                             arr0[i,j] = alglib.math.randomreal()>0.5;
                             arr1[i,j] = arr0[i,j];
                             arr2[i,j] = arr0[i,j];
                             if( arr0[i,j] )
                                 cnt++;
                         }
                     _TestResult = _TestResult && (alglib.xdebugb2count(arr0)==cnt);
                     alglib.xdebugb2not(ref arr1);
                     if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr1[i,j]==!arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugb2transpose(ref arr2);
                     if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugb2outsin(m, n, out arr3);
                     if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr3[i,j]==(Math.Sin(3*i+5*j)>0));
                     }
                     else
                         _TestResult = false;
                 }
             System.Console.WriteLine("* boolean 2D arrays          "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // integer 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int m, n, i, j;
             int sum;
             _TestResult = true;
             for(n=0; n<=max2d; n++)
                 for(m=0; m<=max2d; m++)
                 {
                     // skip situations when n*m==0, but n!=0 or m!=0
                     if( n*m==0 && (n!=0 || m!=0) )
                         continue;
                     
                     // proceed to testing
                     int[,] arr0 = new int[m,n];
                     int[,] arr1 = new int[m,n];
                     int[,] arr2 = new int[m,n];
                     int[,] arr3 = null;
                     sum = 0;
                     for(i=0; i<m; i++)
                         for(j=0; j<n; j++)
                         {
                             arr0[i,j] = alglib.math.randominteger(10);
                             arr1[i,j] = arr0[i,j];
                             arr2[i,j] = arr0[i,j];
                             sum += arr0[i,j];
                         }
                     _TestResult = _TestResult && (alglib.xdebugi2sum(arr0)==sum);
                     alglib.xdebugi2neg(ref arr1);
                     if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr1[i,j]==-arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugi2transpose(ref arr2);
                     if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugi2outsin(m, n, out arr3);
                     if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr3[i,j]==System.Math.Sign(Math.Sin(3*i+5*j)));
                     }
                     else
                         _TestResult = false;
                 }
             System.Console.WriteLine("* integer 2D arrays          "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int m, n, i, j;
             double sum;
             _TestResult = true;
             for(n=0; n<=max2d; n++)
                 for(m=0; m<=max2d; m++)
                 {
                     // skip situations when n*m==0, but n!=0 or m!=0
                     if( n*m==0 && (n!=0 || m!=0) )
                         continue;
                     
                     // proceed to testing
                     double[,] arr0 = new double[m,n];
                     double[,] arr1 = new double[m,n];
                     double[,] arr2 = new double[m,n];
                     double[,] arr3 = null;
                     sum = 0;
                     for(i=0; i<m; i++)
                         for(j=0; j<n; j++)
                         {
                             arr0[i,j] = alglib.math.randomreal()-0.5;
                             arr1[i,j] = arr0[i,j];
                             arr2[i,j] = arr0[i,j];
                             sum += arr0[i,j];
                         }
                     _TestResult = _TestResult && (System.Math.Abs(alglib.xdebugr2sum(arr0)-sum)<1.0E-10);
                     alglib.xdebugr2neg(ref arr1);
                     if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr1[i,j]==-arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugr2transpose(ref arr2);
                     if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugr2outsin(m, n, out arr3);
                     if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (System.Math.Abs(arr3[i,j]-Math.Sin(3*i+5*j))<1E-10);
                     }
                     else
                         _TestResult = false;
                 }
             System.Console.WriteLine("* real 2D arrays             "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
             int m, n, i, j;
             alglib.complex sum;
             _TestResult = true;
             for(n=0; n<=max2d; n++)
                 for(m=0; m<=max2d; m++)
                 {
                     // skip situations when n*m==0, but n!=0 or m!=0
                     if( n*m==0 && (n!=0 || m!=0) )
                         continue;
                     
                     // proceed to testing
                     alglib.complex[,] arr0 = new alglib.complex[m,n];
                     alglib.complex[,] arr1 = new alglib.complex[m,n];
                     alglib.complex[,] arr2 = new alglib.complex[m,n];
                     alglib.complex[,] arr3 = null;
                     sum = 0;
                     for(i=0; i<m; i++)
                         for(j=0; j<n; j++)
                         {
                             arr0[i,j].x = alglib.math.randomreal()-0.5;
                             arr0[i,j].y = alglib.math.randomreal()-0.5;
                             arr1[i,j] = arr0[i,j];
                             arr2[i,j] = arr0[i,j];
                             sum += arr0[i,j];
                         }
                     _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc2sum(arr0)-sum)<1.0E-10);
                     alglib.xdebugc2neg(ref arr1);
                     if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr1[i,j]==-arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugc2transpose(ref arr2);
                     if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                                 _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
                     }
                     else
                         _TestResult = false;
                     alglib.xdebugc2outsincos(m, n, out arr3);
                     if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
                     {
                         for(i=0; i<m; i++)
                             for(j=0; j<n; j++)
                             {
                                 _TestResult = _TestResult && (System.Math.Abs(arr3[i,j].x-Math.Sin(3*i+5*j))<1E-10);
                                 _TestResult = _TestResult && (System.Math.Abs(arr3[i,j].y-Math.Cos(3*i+5*j))<1E-10);
                             }
                     }
                     else
                         _TestResult = false;
                 }
             System.Console.WriteLine("* complex 2D arrays          "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
         {
             // "biased product / sum" test
             int m, n, i, j;
             double sum;
             _TestResult = true;
             for(n=1; n<=max2d; n++)
                 for(m=1; m<=max2d; m++)
                 {
                     // proceed to testing
                     double[,] a = new double[m,n];
                     double[,] b = new double[m,n];
                     bool[,]   c = new bool[m,n];
                     sum = 0;
                     for(i=0; i<m; i++)
                         for(j=0; j<n; j++)
                         {
                             a[i,j] = alglib.math.randomreal()-0.5;
                             b[i,j] = alglib.math.randomreal()-0.5;
                             c[i,j] = alglib.math.randomreal()>0.5;
                             if( c[i,j] )
                                 sum += a[i,j]*(1+b[i,j]);
                         }
                     _TestResult = _TestResult && (Math.Abs(alglib.xdebugmaskedbiasedproductsum(m,n,a,b,c)-sum)<1.0E-10);
                 }
             System.Console.WriteLine("* multiple arrays            "+(_TestResult ? " OK" : " FAILED"));
             _TotalResult = _TotalResult && _TestResult;
         }
     }
     catch
     {
         System.Console.WriteLine("Unhandled exception was raised!");
         System.Environment.ExitCode = 1;
         return;
     }
     
     //
     // Test below creates instance of MemoryLeaksTest object.
     //
     // This object is descendant of CriticalFinalizerObject class,
     // which guarantees that it will be finalized AFTER all other
     // ALGLIB objects which hold pointers to unmanaged memory.
     //
     // Tests for memory leaks are done within object's destructor.
     //
     MemoryLeaksTest _test_object = new MemoryLeaksTest();
     if( !_TotalResult )
         System.Environment.ExitCode = 1;
 }
        public static void generate_mixed_chirp_signal(int chirp_num, double duration_of_signal, double Fd, double[] Fmin, double[] Fmax, double[] init_phase, double[] A, out double[] t, alglib.complex[] noise,  out alglib.complex[] mixed_signal_without_noise)//Амплитуды в децибелах
        {
            t = new double[(int)(duration_of_signal * Fd)];
            mixed_signal_without_noise = new alglib.complex[(int)(duration_of_signal * Fd)];
            for (int i = 0; i < chirp_num; i++)
            {
                alglib.complex[] signal = chirp(Fmin[i], Fmax[i], Fd, duration_of_signal);
                double coeff = get_null_dB_mag(signal, noise, Fmin[i], Fmax[i], Fd);
                double new_coeff = Math.Sqrt(coeff * coeff * Math.Pow(10, A[i] / 10.0));
                for (int j = 0; j < signal.Length; j++)
                {
                    mixed_signal_without_noise[j] += new_coeff * signal[j];
                }              
            }


            for (int j = 0; j < mixed_signal_without_noise.Length; j++)
            {
                t[j] = j / Fd;
            }

        }
        }//добавление АБГШ
        public static void generate_mixed_PSK_signal(int PSK_num, int[] symbols_in_message, double duration_of_signal, double Fd, double[] T, double[] Fc, int[] M, double[] init_phase, int[] samples_per_symbol, double[] A, out double[] t, out alglib.complex[] mixed_signal)//Амплитуды в децибелах
        {
            t = new double[0];
            mixed_signal = new alglib.complex[(int)(duration_of_signal * Fd)];
            alglib.complex[] noise = create_AWGN(mixed_signal.Length);

            for (int i = 0; i < PSK_num; i++)
            {
                int[] message = new int[symbols_in_message[i]];
                Random rand = new Random();
                for (int j = 0; j < message.Length; j++)
                {
                    message[j] = rand.Next(M[i]);
                }
                PSK psk = new PSK(M[i], init_phase[0], T[i], samples_per_symbol[i]);
                alglib.complex[] signal;


                PSK.create_RF_signal(psk.modulate(message), Fd, duration_of_signal, Fc[i], 1, out signal, out t);
                psk_band_filtering(ref signal, 1.0 / T[i], Fd, Fc[i]);
                double coeff = get_null_dB_mag(signal, noise, 1.0 / T[i], Fd);
                double new_coeff = Math.Sqrt(coeff * coeff * Math.Pow(10, A[i] / 10.0));



                for (int j = 0; j < signal.Length; j++)
                {

                    mixed_signal[j] += new_coeff * signal[j];
                }

                /*double signal_energ = 0;
                double noise_energ = 0;
                for (int j = 0; j < signal.Length; j++)
                {
                    signal_energ += Math.Pow(alglib.math.abscomplex(new_coeff * signal[j]), 2);
                }
                for (int j = 0; j < noise.Length; j++)
                {
                    noise_energ += Math.Pow(alglib.math.abscomplex(noise[j]), 2);
                }
                noise_energ = noise_energ * 2 * (1 / T[i]) / Fd;
                double SNR = 10 * Math.Log10(signal_energ / noise_energ);*/

            }
            for (int j = 0; j < mixed_signal.Length; j++)
            {
                mixed_signal[j] += noise[j];
                t[j] = j / Fd;
            }

        }