Пример #1
0
 public MatrixCalc(MatrixOp op, MatrixSource provider, MatrixSource source, DrawState state)
 {
     #if DEBUG
     this.state = state;
     #endif
     this.op = op;
     this.provider = provider;
     this.source = source;
 }
Пример #2
0
        protected void EvaluateGradient(double[] x, double[] gradx, double[] lower_bounds, double[] upper_bounds, object constraints)
        {
            int m = b.Length;
            int n = A[0].Length;

            double[] c = MatrixOp.Multiply(A, x);

            double[] g      = new double[m];
            double[] gprime = new double[m];
            for (int j = 0; j < m; ++j)
            {
                g[j]      = mLinkFunc.GetInvLink(c[j]);
                gprime[j] = mLinkFunc.GetInvLinkDerivative(c[j]);
            }

            for (int i = 0; i < n; ++i)
            {
                double crossprod = 0;
                for (int j = 0; j < m; ++j)
                {
                    double cb = g[j] - b[j];
                    crossprod += cb * gprime[j] * A[j][i];
                }

                gradx[i] = crossprod / m;

                if (i != 0)
                {
                    gradx[i] += (mRegularizationLambda * x[i]) / m;
                }
            }

            /*
             * GradientEstimation.CalcGradient(x, gradx, (x2, constraints2) =>
             *  {
             *      return EvaluateCost(x2, lower_bounds, upper_bounds, constraints2);
             *  });*/
        }
        public static void RunANCOVA(double[][] X, double[] y, int[] grpCat, out ANCOVAv2 output, double significance_level = 0.05)
        {
            output = new ANCOVAv2();

            int D = X[0].Length;

            Dictionary <int, List <double> >[] groupped_x_at_dim = new Dictionary <int, List <double> > [D];
            Dictionary <int, List <double> >   groupped_y        = new Dictionary <int, List <double> >();

            for (int l = 0; l < D; ++l)
            {
                groupped_x_at_dim[l] = new Dictionary <int, List <double> >();
            }

            int N = y.Length;

            double[][] X_transpose = MatrixOp.Transpose(X);

            for (int i = 0; i < N; ++i)
            {
                int    grpId = grpCat[i];
                double yVal  = y[i];

                List <double> group_y = null;

                for (int d = 0; d < D; ++d)
                {
                    List <double> group_x = null;
                    if (groupped_x_at_dim[d].ContainsKey(grpId))
                    {
                        group_x = groupped_x_at_dim[d][grpId];
                    }
                    else
                    {
                        group_x = new List <double>();
                        groupped_x_at_dim[d][grpId] = group_x;
                    }
                    group_x.Add(X_transpose[d][i]);
                }

                if (groupped_y.ContainsKey(grpId))
                {
                    group_y = groupped_y[grpId];
                }
                else
                {
                    group_y           = new List <double>();
                    groupped_y[grpId] = group_y;
                }

                group_y.Add(yVal);
            }
            int k = groupped_x_at_dim[0].Count;

            double[] grand_mean_x = new double[D];
            double   grand_mean_y;

            output.SSTx = new double[D];

            for (int d = 0; d < D; ++d)
            {
                output.SSTx[d] = GetSST(X_transpose[d], out grand_mean_x[d]);
            }
            output.SSTy = GetSST(y, out grand_mean_y);

            output.SSBGx = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.SSBGx[d] = GetSSG(groupped_x_at_dim[d], grand_mean_x[d]);
            }

            output.SSBGy = GetSSG(groupped_y, grand_mean_y);

            output.SSWGy = output.SSTy - output.SSBGy;
            output.SSWGx = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.SSWGx[d] = output.SSTx[d] - output.SSBGx[d];
            }

            output.SCT = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.SCT[d] = GetCovariance(X_transpose[d], y);
            }
            output.SCWG = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.SCWG[d] = GetCovarianceWithinGroup(groupped_x_at_dim[d], groupped_y);
            }

            output.rT = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.rT[d] = output.SCT[d] / System.Math.Sqrt(output.SSTx[d] * output.SSTy);
            }

            output.rWG = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.rWG[d] = output.SCWG[d] / System.Math.Sqrt(output.SSWGx[d] * output.SSWGy);
            }

            output.SSTy_adj = output.SSTy;
            for (int d = 0; d < D; ++d)
            {
                output.SSTy_adj -= System.Math.Pow(output.SCT[d], 2) / output.SSTx[d];
            }
            output.SSWGy_adj = output.SSWGy;
            for (int d = 0; d < D; ++d)
            {
                output.SSWGy_adj -= System.Math.Pow(output.SCWG[d], 2) / output.SSWGx[d];
            }
            output.SSBGy_adj = output.SSTy_adj - output.SSWGy_adj;

            output.dfT  = N - 2;
            output.dfBG = k - 1;
            output.dfWG = N - k - 1;

            output.MSBGy_adj = output.SSBGy_adj / output.dfBG;
            output.MSWGy_adj = output.SSWGy_adj / output.dfWG;

            output.Slope = new double[D];
            for (int d = 0; d < D; ++d)
            {
                output.Slope[d] = output.SCWG[d] / output.SSWGx[d];
            }

            output.MeanWithinGroups_x = GetMeanWithinGroup(groupped_x_at_dim);
            output.MeanWithinGroups_y = GetMeanWithinGroup(groupped_y);

            output.Intercepts = GetIntercepts(output.MeanWithinGroups_x, output.MeanWithinGroups_y, grand_mean_x, output.Slope);

            output.F = output.MSBGy_adj / output.MSWGy_adj;
            //output.pValue = 1 - FDistribution.GetPercentile(output.F, output.dfBG, output.dfWG);
            //output.RejectH0 = output.pValue < significance_level;
        }
Пример #4
0
    /*We have 3 Main Matrix A,x and z which have [Ax = z] relation
     * Our unknown values are located at x matrix, z matrix is given values by user
     * A matrix is Relational and Conductance matrix*/
    private void CalculateValues()
    {
        //Number of voltage sources and Nodes
        int m, n;

        m = VoltageSourceList.Count;
        n = NodeList.Count - 1;                                 //-1 because we don't compute Node 0


        //Main Matrixes, Left(index 0) dimension is row and Right(index 1) dimension is column
        float[,] Amatrix = new float[n + m, n + m];
        float[,] Xmatrix = new float[n + m, 1];
        float[,] Zmatrix = new float[n + m, 1];

        /*A matrix is created from 4 sub matrixes G,B,C and D with following relation
         * A = | G  B |
         | C  D |
         */
        float[,] Gmatrix = new float[n, n];
        float[,] Bmatrix = new float[n, m];
        float[,] Cmatrix;                                               //Initialize after Bmatrix with Transpose
        float[,] Dmatrix = new float[m, m];

        //This Part creates Gmatrix which is created from passive elements and Conductance
        LinkedListNode <CircuitElement> ite = PassiveList.First;

        for (int i = 0; i < PassiveList.Count; i++)
        {
            CircuitElement ele = ite.Value;
            int            lef = ele.leftNode - 1;
            int            rig = ele.rightNode - 1;
            int            ohm = ele.temporaryResistance;



            if (lef > -1 && rig > -1)
            {
                //This part is for Conductance
                Gmatrix[lef, lef] += 1f / ohm;
                Gmatrix[rig, rig] += 1f / ohm;
                //This part is for Negative Conductance
                Gmatrix[lef, rig] -= 1f / ohm;
                Gmatrix[rig, lef] -= 1f / ohm;
            }
            else if (lef > -1)
            {
                //This part is for Conductance
                Gmatrix[lef, lef] += 1f / ohm;
            }
            else if (rig > -1)
            {
                //This part is for Conductance
                Gmatrix[rig, rig] += 1f / ohm;
            }
            else
            {
                //Short Circuit
                //abort
            }

            //Next passive element
            ite = ite.Next;
        }

        //This part Creates the Bmatrix from Voltage Sources relation to nodes
        ite = VoltageSourceList.First;
        for (int i = 0; i < VoltageSourceList.Count; i++)
        {
            CircuitElement source = ite.Value;
            int            lef    = source.leftNode - 1;
            int            rig    = source.rightNode - 1;

            if (lef > -1 && rig > -1)
            {
                //left side of the voltage is negative
                Bmatrix[lef, i] += -1f;
                Bmatrix[rig, i] += 1f;
            }
            else if (lef > -1)
            {
                Bmatrix[lef, i] += -1f;
            }
            else if (rig > -1)
            {
                Bmatrix[rig, i] += 1f;
            }
            else
            {
                //Short Circuit
                //abort
            }

            //Next voltage source
            ite = ite.Next;
        }

        //This part Creates the Cmatrix from Transpose of Bmatrix
        Cmatrix = MatrixOp.Transpose(Bmatrix);

        //Dmatrix is initialized as 0 mxm matrix which we will use as it is

        //Now we have all the sub matrixes let's compute Amatrix
        for (int i = 0; i < m + n; i++)
        {
            for (int j = 0; j < m + n; j++)
            {
                if (i >= n)
                {
                    if (j >= n)
                    {
                        Amatrix[i, j] = Dmatrix[i - n, j - n];
                    }
                    else
                    {
                        Amatrix[i, j] = Cmatrix[i - n, j];
                    }
                }
                else
                {
                    if (j >= n)
                    {
                        Amatrix[i, j] = Bmatrix[i, j - n];
                    }
                    else
                    {
                        Amatrix[i, j] = Gmatrix[i, j];
                    }
                }
            }
        }

        //Amatrix is created at this point

        /*z matrix is created from 2 sub matrixes i and e with following relation
         * z = | i |
         | e |
         */
        float[,] Imatrix = new float[n, 1];
        float[,] Ematrix = new float[m, 1];

        //This part Creates the Imatrix from Independent current sources relation to nodes
        for (int i = 0; i < n; i++)
        {
            // but for now we don't have it so set it to 0.0f
            Imatrix[i, 0] = 0.0f;
        }

        //This part Creates the Ematrix from Voltage Sources
        ite = VoltageSourceList.First;
        for (int i = 0; i < m; i++)
        {
            Ematrix[i, 0] = ite.Value.temporaryVoltage;

            //Next voltage source
            ite = ite.Next;
        }

        //This part Initialize Zmatrix from I and E matrix
        for (int i = 0; i < m + n; i++)
        {
            if (i >= n)
            {
                Zmatrix[i, 0] = Ematrix[i - n, 0];
            }
            else
            {
                Zmatrix[i, 0] = Imatrix[i, 0];
            }
        }

        //Zmatrix is created at this point
        MatrixOp.printMatrix(Zmatrix);

        //This part evaluates Xmatrix which have voltage and current values for nodes, x = (A^-1) * z
        Xmatrix = MatrixOp.MatrixMul(MatrixOp.Inverse(Amatrix), Zmatrix);

        //Write the calculated voltages to NodeList
        LinkedListNode <float> iteF = NodeList.First;

        for (int i = 0; i < n; i++)
        {
            iteF.Value = Xmatrix[i, 0];
            iteF       = iteF.Next;
        }

        CalculateAmper();
    }
Пример #5
0
        public void Jet2()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = ImageTypes.RgbPixel, ExpectResult = false, Max = 255, Min = 0 },
                new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Max = 255, Min = 0 },
                new { Type = ImageTypes.UInt8, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.UInt16, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.UInt32, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.Int8, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.Int16, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.Int32, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.HsiPixel, ExpectResult = false, Max = 255, Min = 0 },
                new { Type = ImageTypes.Float, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.Double, ExpectResult = true, Max = 255, Min = 0 },
                new { Type = ImageTypes.RgbPixel, ExpectResult = false, Max = 75, Min = 50 },
                new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Max = 75, Min = 50 },
                new { Type = ImageTypes.UInt8, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.UInt16, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.UInt32, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.Int8, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.Int16, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.Int32, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.HsiPixel, ExpectResult = false, Max = 75, Min = 50 },
                new { Type = ImageTypes.Float, ExpectResult = true, Max = 75, Min = 50 },
                new { Type = ImageTypes.Double, ExpectResult = true, Max = 75, Min = 50 }
            };

            var type = this.GetType().Name;

            foreach (var test in tests)
            {
                Array2DBase imageObj       = null;
                Array2DBase horzObj        = null;
                Array2DBase vertObj        = null;
                Array2DBase outputImageObj = null;
                MatrixOp    matrix         = null;
                DlibObject  windowObj      = null;

                try
                {
                    const ImageTypes inputType = ImageTypes.Float;

                    var image = DlibTest.LoadImage(test.Type, path);
                    imageObj = image;
                    var horz = Array2DTest.CreateArray2D(inputType);
                    horzObj = horz;
                    var vert = Array2DTest.CreateArray2D(inputType);
                    vertObj = vert;
                    var outputImage = Array2DTest.CreateArray2D(test.Type);
                    outputImageObj = outputImage;

                    Dlib.SobelEdgeDetector(image, horz, vert);
                    Dlib.SuppressNonMaximumEdges(horz, vert, outputImage);

                    try
                    {
                        matrix = Jet(test.Type, outputImage, test.Max, test.Min);

                        if (test.ExpectResult)
                        {
                            if (this.CanGuiDebug)
                            {
                                var window = new ImageWindow(matrix, $"{test.Type} - Max: {test.Max}, Min : {test.Min}");
                                windowObj = window;
                            }

                            Dlib.SaveBmp(image, $"{Path.Combine(this.GetOutDir(type, "Jet2"), $"{LoadTarget}_{test.Type}_{test.Max}_{test.Min}.bmp")}");
                        }
                        else
                        {
                            Assert.Fail($"Failed to execute Jet2 to Type: {test.Type}");
                        }
                    }
                    catch (Exception)
                    {
                        if (!test.ExpectResult)
                        {
                            Console.WriteLine("OK");
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine($"Failed to execute Jet2 to Type: {test.Type}");
                    throw;
                }
                finally
                {
                    if (outputImageObj != null)
                    {
                        this.DisposeAndCheckDisposedState(outputImageObj);
                    }
                    if (vertObj != null)
                    {
                        this.DisposeAndCheckDisposedState(vertObj);
                    }
                    if (horzObj != null)
                    {
                        this.DisposeAndCheckDisposedState(horzObj);
                    }
                    if (windowObj != null)
                    {
                        this.DisposeAndCheckDisposedState(windowObj);
                    }
                    if (matrix != null)
                    {
                        this.DisposeAndCheckDisposedState(matrix);
                    }
                    if (imageObj != null)
                    {
                        this.DisposeAndCheckDisposedState(imageObj);
                    }
                }
            }
        }
Пример #6
0
 public static SharedParameter CreateCustom(String varName, Effect effect, MatrixOp matrixOp)
 {
     EffectMatrixVariable eV = effect.GetVariableByName(varName).AsMatrix();
     UpdateSharedParameter update = ((fxParam, renderer) => MatrixUpdate(fxParam.EffectVariable, matrixOp()));
     return new SharedParameter(varName, SceneVariable.MatrixOp, effect, eV, update);
 }