Example #1
0
        public (NDArray, NDArray) qr()
        {
            var a = (double[])this.Storage.GetData <double>().Clone();

            int m = this.Storage.Shape.Dimensions[0];
            int n = this.Storage.Shape.Dimensions[1];

            int lda = m;

            double[] tau  = new double[Math.Min(m, n)];
            double[] work = new double[Math.Max(m, n)];

            int lwork = m;

            int info = 0;

            LAPACK.dgeqrf_(ref m, ref n, a, ref lda, tau, work, ref lwork, ref info);

            double[] RDouble = new double[n * n];

            for (int idx = 0; idx < n; idx++)
            {
                for (int jdx = idx; jdx < n; jdx++)
                {
                    RDouble[idx + jdx * n] = a[idx + jdx * n];
                }
            }


            var R = new NDArray(typeof(double), new Shape(n, n));

            R.Storage.SetData(RDouble);

            int k = tau.Length;

            LAPACK.dorgqr_(ref m, ref n, ref k, a, ref lda, tau, work, ref lwork, ref info);

            var Q = new NDArray(typeof(double), new Shape(tau.Length, tau.Length));

            Q.Storage.Allocate(Q.Storage.DType, Q.Storage.Shape);
            Q.Storage.SetData(a);

            return(Q, R);
        }
Example #2
0
        public (NDArray, NDArray, NDArray) svd()
        {
            double[] A = Data <double>();

            int m     = this.shape[0];
            int n     = this.shape[1];
            int lda   = m;
            int ldu   = m;
            int ldvt  = n;
            int info  = 0;
            int lwork = -1;

            double[] work = new double[Math.Max(m, n)];

            double[] s  = new double[n];
            double[] u  = new double[m * m];
            double[] vt = new double[n * n];

            LAPACK.dgesvd_("ALL".ToCharArray(), "All".ToCharArray(), ref m, ref n, A, ref lda, s, u, ref ldu, vt, ref ldvt, work, ref lwork, ref info);

            lwork = (int)work[0];

            work = new double[lwork];

            LAPACK.dgesvd_("ALL".ToCharArray(), "All".ToCharArray(), ref m, ref n, A, ref lda, s, u, ref ldu, vt, ref ldvt, work, ref lwork, ref info);

            var uNDArr  = new NDArray(typeof(double), new Shape(m, n));
            var vtNDArr = new NDArray(typeof(double), new Shape(n, n));
            var sNDArr  = new NDArray(typeof(double), n);

            // set variables
            double[] uDouble = uNDArr.Storage.GetData <double>();

            for (int idx = 0; idx < uNDArr.size; idx++)
            {
                uDouble[idx] = u[idx];
            }

            vtNDArr.Storage.ReplaceData(vt);
            sNDArr.Storage.ReplaceData(s);

            return(uNDArr, sNDArr, vtNDArr);
        }
Example #3
0
        /// <summary>
        /// Least Square method
        ///
        /// Determines NDArray X which reduces least square error of Linear System A * X = B.
        /// This NDArray is equal to A.
        /// </summary>
        /// <param name="nDArrayB">Result NDArray B</param>
        /// <param name="rcon"></param>
        /// <returns>NArray X</returns>
        public NDArray lstqr(NDArray nDArrayB, double rcon = 0.0001)
        {
            var A = (double[])Data <double>();
            var b = (double[])nDArrayB.Data <double>();

            int m = this.shape[0];
            int n = this.shape[1];

            int nrhs = (nDArrayB.ndim > 1) ? nDArrayB.shape[1] : 1;

            int lda = m;
            int ldb = m;

            int rank = 0;

            double[] work  = new double[1];
            int      lwork = -1;
            int      info  = 0;

            double[] singVal = new double[m];

            LAPACK.dgelss_(ref m, ref n, ref nrhs, A, ref lda, b, ref ldb, singVal, ref rcon, ref rank, work, ref lwork, ref info);

            lwork = (int)work[0];
            work  = new double[lwork];

            LAPACK.dgelss_(ref m, ref n, ref nrhs, A, ref lda, b, ref ldb, singVal, ref rcon, ref rank, work, ref lwork, ref info);

            double[] sln = new double[n * nrhs];

            for (int idx = 0; idx < sln.Length; idx++)
            {
                sln[idx] = b[m * (idx % nrhs) + idx / nrhs];
            }

            var slnArr = new NDArray(typeof(double), new Shape(n, nrhs));

            slnArr.Storage.ReplaceData(sln);

            return(slnArr);
        }