Ejemplo n.º 1
0
                public PNormMinClass(floatLinalg.floatMatrix A, floatLinalg.floatVector b, floatLinalg.floatVector w,
                    floatLinalg.floatVector lambda, floatLinalg.floatVector CLp, floatLinalg.floatVector CLq)
                {
                    this.A = A;
                    this.b = b;
                    this.w = w;
                    this.lambda = lambda;
                    this.CLp = CLp;
                    this.CLq = CLq;

                    //Temporary buffers
                    tempX = new floatLinalg.floatVector(new float[A.Cols]);
                    tempdX = new floatLinalg.floatVector(new float[A.Cols]);

                    tempAxmb = new floatLinalg.floatVector(new float[A.Rows]);
                    Axmb = new floatLinalg.floatVector(new float[A.Rows]);

                    Diagw = new floatLinalg.floatDiag(w);
                    Diaglambda = new floatLinalg.floatDiag(lambda);

                    tempdX = new floatLinalg.floatVector(new float[lambda.Length]);
                    tempd2x = new floatLinalg.floatVector(new float[lambda.Length]);
                    tempAtz = new floatLinalg.floatVector(new float[lambda.Length]);

                    dtempAtz = new floatLinalg.floatVector(new float[w.Length]);
                    d2tempAtz = new floatLinalg.floatVector(new float[w.Length]);
                    Diagd2Atz = new floatLinalg.floatDiag(d2tempAtz);

                    float[] id = new float[A.Rows];
                    for (int i = 0; i < A.Rows; i++) id[i] = 1;
                    Identity = new floatLinalg.floatDiag(id);
                }
Ejemplo n.º 2
0
            /// <summary>Computes least squares fitting of Ax = b weighted by W and returns x</summary>
            /// <param name="A">Dependent variables measurements</param>
            /// <param name="b">Independent variables measurements</param>
            /// <param name="W">Weights</param>
            /// <param name="lambda">Regularization term</param>
            public static float[] LeastSquares(float[,] A, float[] b, float[] W, float lambda)
            {
                floatLinalg.floatMatrix CLA = new floatLinalg.floatMatrix(A);
                floatLinalg.floatVector CLb = new floatLinalg.floatVector(b);

                if (W != null && W.Length != CLA.Rows) throw new Exception("Incompatible Weight dimensions");

                floatLinalg.floatDiag CLW;
                if (W == null)
                {
                    float[] ww = new float[CLA.Rows];
                    for (int i = 0; i < ww.Length; i++) ww[i] = 1;
                    CLW = new floatLinalg.floatDiag(ww);
                }
                else CLW = new floatLinalg.floatDiag(W);

                float[] lambdas = new float[CLA.Cols];
                for (int i = 0; i < lambdas.Length; i++) lambdas[i] = lambda;
                floatLinalg.floatVector CLlambda = new floatLinalg.floatVector(lambdas);

                floatLinalg.floatSymPosDefMatrix AtA = null;
                AtA = floatLinalg.BLAS.MatrTranspMatrProd(CLA, CLW, CLlambda, ref AtA);

                //CLA.CLValues.ReadFromDeviceTo(CLA.Values);
                //AtA.CLValues.ReadFromDeviceTo(AtA.Values);

                floatLinalg.floatVector Atb = null;
                Atb = floatLinalg.BLAS.MatrTraspVecMult(CLA, CLW, CLb, ref Atb);

                //Atb.CLValues.WriteToDevice(Atb.Values);

                GC.Collect();
                floatLinalg.floatVector resp = null;
                AtA.LinearSolve(Atb, true, ref resp);

                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL) resp.CLValues.ReadFromDeviceTo(resp.Values);
                return resp.Values;
            }