public ComplexXY(ComplexLinearSpace inputvals, Func <Complex, Complex> Func)
        {
            this.inputs  = inputvals;
            this.outputs = new Complex[inputvals.Mag.N, inputvals.Phase.N];

            this.Fill(Func);
        }
        protected void PlotGammaFn(Func <ComplexLinearSpace, Complex[, ]> gammaMapFn, Color c, int N_magGammaLS = 200, int N_angleGammaLS = 200, bool reset = true)
        {
            //Sweep and calculate.
            ComplexLinearSpace gammaLS_Sweep = new ComplexLinearSpace(
                new LinearSpace(0.0, 1.0, (int)N_magGammaLS),
                new LinearSpace(0.0, 360, (int)N_angleGammaLS)
                );
            ComplexXY gammaXY = new ComplexXY(gammaLS_Sweep, gammaMapFn);

            //Reset chart.
            if (reset)
            {
                this.smithChart.PlotImpedanceChart();
            }

            //Plot.
            this.smithChart.plotGammaSpace(gammaXY, Color.Red, 1.0);

            //Invalidate.
            this.smithChart_pb.Invalidate();
        }
        private void UpdateForDesignFrequencyChange()
        {
            //Change DesignFrequency variable.
            bool UserInputFrequency = double.TryParse(setDesignFrequency_tb.Text, out DesignFrequency);

            if (!UserInputFrequency)
            {
                MessageBox.Show("Please enter valid frequency");
                return;
            }
            DesignFrequency *= MenialOperations.M;

            //Fill out table.
            BiasedBJTAtDesignFrequency = BiasedBJT.ExtractTwoPortNetwork(DesignFrequency);
            ThreadSafe.SetControlTextThreadSafe_f(this, sParamsChosen_tlp.GetControlFromPosition(0, 0), BiasedBJTAtDesignFrequency.m(1, 1).ToString());
            ThreadSafe.SetControlTextThreadSafe_f(this, sParamsChosen_tlp.GetControlFromPosition(0, 1), BiasedBJTAtDesignFrequency.m(1, 2).ToString());
            ThreadSafe.SetControlTextThreadSafe_f(this, sParamsChosen_tlp.GetControlFromPosition(1, 0), BiasedBJTAtDesignFrequency.m(2, 1).ToString());
            ThreadSafe.SetControlTextThreadSafe_f(this, sParamsChosen_tlp.GetControlFromPosition(1, 1), BiasedBJTAtDesignFrequency.m(2, 2).ToString());

            //So now we have the right two-port network. It's the one at our design-freq of course.
            //But we're interested in mag gammaIN wrt gammaL.

            //You can make this be a scalarField2D that takes in gammaL.real and gammaL.imag, then outputs gammaIN.magnitude.
            //Then you can do the Func<> and Fill() that way and get the whole .csv.
            //Alternatively you can go with the contour plot, which this is not.
            //But luckily it's not much of a change to go.

            //Sweep and calculate.
            double             maxMagnitude = 0;
            double             minMagnitude = double.MaxValue;
            ComplexLinearSpace gammaL_Sweep = new ComplexLinearSpace(
                new LinearSpace(0.0, 1.0, (int)200),
                new LinearSpace(0.0, 2 * Math.PI, (int)200)
                );

            Complex[,] gammaIN_Sweep = new Complex[gammaL_Sweep.Mag.N, gammaL_Sweep.Phase.N];
            for (int mag_ind = 0; mag_ind < gammaL_Sweep.Mag.N; mag_ind++)
            {
                for (int phase_ind = 0; phase_ind < gammaL_Sweep.Phase.N; phase_ind++)
                {
                    Complex gammaL_Current = MenialOperations.complex_magphase(gammaL_Sweep.Mag.v[mag_ind], gammaL_Sweep.Phase.v[phase_ind], false);

                    gammaIN_Sweep[mag_ind, phase_ind] = MenialOperations.gamma_IN(BiasedBJTAtDesignFrequency.p, gammaL_Current);
                    if (gammaIN_Sweep[mag_ind, phase_ind].Magnitude > maxMagnitude)
                    {
                        maxMagnitude      = gammaIN_Sweep[mag_ind, phase_ind].Magnitude;
                        maximizing_gammaL = gammaL_Current;
                        Debug.WriteLine("Max magnitude of " + maxMagnitude + " at " + gammaL_Current);
                    }
                    if (gammaIN_Sweep[mag_ind, phase_ind].Magnitude < minMagnitude)
                    {
                        minMagnitude = gammaIN_Sweep[mag_ind, phase_ind].Magnitude;
                        Debug.WriteLine("Min magnitude of " + minMagnitude + " at " + gammaL_Current);
                    }
                }
            }

            maxMagnitude = Math.Log10(maxMagnitude);
            minMagnitude = Math.Log10(minMagnitude);

            //Plot.
            for (int mag_ind = 0; mag_ind < gammaL_Sweep.Mag.N; mag_ind++)
            {
                for (int phase_ind = 0; phase_ind < gammaL_Sweep.Phase.N; phase_ind++)
                {
                    Complex gammaL_Current = MenialOperations.complex_magphase(gammaL_Sweep.Mag.v[mag_ind], gammaL_Sweep.Phase.v[phase_ind], false);

                    if (gammaIN_Sweep[mag_ind, phase_ind].Magnitude > 1)
                    {
                        int colorFactor = (int)((Math.Log10(gammaIN_Sweep[mag_ind, phase_ind].Magnitude) - minMagnitude) / (maxMagnitude - minMagnitude) * 255);
                        smithChart.plotGamma(gammaL_Current, Color.FromArgb(255, colorFactor, 255 - colorFactor, 0));
                    }
                }
            }

            smithChart_pb.Invalidate();
        }
 public ComplexXY(ComplexLinearSpace inputvals, Func <ComplexLinearSpace, Complex[, ]> Func)
 {
     this.inputs  = inputvals;
     this.outputs = Func(inputvals);
 }
 public ComplexXY(ComplexLinearSpace inputvals, Complex[,] outputvals)
 {
     this.inputs  = inputvals;
     this.outputs = outputvals;
 }
 public ComplexXY(int num_rows_mag, int num_cols_phase)
 {
     this.inputs  = new Microwave.ComplexLinearSpace(num_rows_mag, num_cols_phase);
     this.outputs = new Complex[num_rows_mag, num_cols_phase];
 }
 public ComplexXY(int square)
 {
     this.inputs  = new Microwave.ComplexLinearSpace(square, square);
     this.outputs = new Complex[square, square];
 }
 public ComplexXY()
 {
     this.inputs  = new Microwave.ComplexLinearSpace(100, 100);
     this.outputs = new Complex[100, 100];
 }
        private void Bgw_Update_DoWork(object sender, DoWorkEventArgs e)
        {
            //Update S-parameter table.
            base.UpdateForNewParams();

            //Replace everything under here with a BGW.

            //Update mu.
            //Report progress, starting calculating mu.
            //string appendTo_stabilityVsFrequency_tb = ""; //For full mu function.
            //List<string> appendTo_conditionalStability_lb = new List<string>(); //For listing of conditional stabilities.
            double prg = 0;

            for (int freq_ind = 0; freq_ind < this.Device.freq.Count; freq_ind++)
            {
                //Assign.
                double f = this.Device.freq[freq_ind];

                //New temp tpn for this freq.
                TwoPortNetworks.TwoPortNetwork tpn = this.Device.ExtractTwoPortNetwork(f);

                //Calculate mu at this freq.
                double mu = tpn.mu();

                //Full fn. versus frequency.
                //appendTo_stabilityVsFrequency_tb += Convert.ToString(f) + ", " + Convert.ToString(mu) + Environment.NewLine; //ok.

                prg = (double)freq_ind / (double)(this.Device.freq.Count) * 50;
                Bgw_Update.ReportProgress((int)prg, new object[] { 1, stabilityVsFrequency_tb, Convert.ToString(f) + ", " + Convert.ToString(mu) + Environment.NewLine });

                if (mu < 1)
                {
                    //ReportProgress in here.
                    Bgw_Update.ReportProgress(0, new object[] { 0, ConditionalStabilityListing_lb, Convert.ToString(freq_ind) + ", " + Convert.ToString(f) + ", " + Convert.ToString(mu) });
                    //appendTo_conditionalStability_lb.Add(Convert.ToString(freq_ind) + ", " + Convert.ToString(f) + ", " + Convert.ToString(mu));
                }
            }
            //ThreadSafe.ListAddRemoveListboxThreadSafe_uc(this, ConditionalStabilityListing_lb, appendTo_conditionalStability_lb);
            //ThreadSafe.SetControlTextThreadSafe_uc(this, stabilityVsFrequency_tb, appendTo_stabilityVsFrequency_tb);

            //Pre-calculate all gammaIN(gammaL) and gammaOUT(gammaS) for all frequencies, store.
            ComplexLinearSpace gammaLS_Sweep = new ComplexLinearSpace(
                new LinearSpace(0.0, 1.0, (int)100),
                new LinearSpace(0.0, 360, (int)100)
                );

            stabilityPlots_FREQ   = new double[this.Device.freq.Count];
            stabilityPlots_IN_XY  = new ComplexXY[this.Device.freq.Count];
            stabilityPlots_OUT_XY = new ComplexXY[this.Device.freq.Count];

            for (int freq_ind = 0; freq_ind < this.Device.freq.Count; freq_ind++)
            {
                //Get the current tpn.
                TwoPortNetworks.TwoPortNetwork tpn = this.Device.ExtractTwoPortNetwork((int)freq_ind);

                //Calculate.
                stabilityPlots_FREQ[freq_ind]   = this.Device.freq[freq_ind];
                stabilityPlots_IN_XY[freq_ind]  = new Microwave.ComplexXY(gammaLS_Sweep, tpn.CalculateGammaIN);
                stabilityPlots_OUT_XY[freq_ind] = new Microwave.ComplexXY(gammaLS_Sweep, tpn.CalculateGammaOUT);

                //Show GUI.
                prg = 50 + 50 * ((double)freq_ind / (double)this.Device.freq.Count);
                Bgw_Update.ReportProgress((int)prg, new object[] { 2, calculationsProgress_lbl, freq_ind + " / " + this.Device.freq.Count });
            }
        }