public (NDArray, NDArray, NDArray) svd() { int tensorLayout = this.Storage.TensorLayout; this.Storage.ChangeTensorLayout(1); double[] A = this.Storage.CloneData <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), m, n); var vtNDArr = new NDArray(typeof(double), 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.SetData(vt); sNDArr.Storage.SetData(s); this.Storage.ChangeTensorLayout(tensorLayout); return(uNDArr, sNDArr, vtNDArr); }
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, 2); Q.Storage.SetData(a); Q.Storage.ChangeTensorLayout(1); return(Q, R); }
public NDArray lstqr(NDArray nDArrayB, double rcon = 0.0001) { var AT = this.transpose(); var bT = nDArrayB.transpose(); var A = (double[])AT.Storage.GetData <double>().Clone(); var b = (double[])bT.Storage.GetData <double>().Clone(); int m = this.shape.Shapes[0]; int n = this.shape.Shapes[1]; int nrhs = nDArrayB.shape.Shapes[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.SetData(sln); return(slnArr); }