void svdWriteDenseMatrix(DMat D, char *filename, int format) { SMat S = NULL; FILE *file = svd_writeFile(filename, FALSE); if (!file) { svd_error("svdWriteDenseMatrix: failed to write file %s\n", filename); return; } switch (format) { case SVD_F_STH: S = svdConvertDtoS(D); svdWriteSparseTextHBFile(S, file); break; case SVD_F_ST: S = svdConvertDtoS(D); svdWriteSparseTextFile(S, file); break; case SVD_F_SB: S = svdConvertDtoS(D); svdWriteSparseBinaryFile(S, file); break; case SVD_F_DT: svdWriteDenseTextFile(D, file); break; case SVD_F_DB: svdWriteDenseBinaryFile(D, file); break; default: svd_error("svdLoadSparseMatrix: unknown format %d", format); } svd_closeFile(file); if (S) svdFreeSMat(S); }
SMat svdLoadSparseMatrix(char *filename, int format) { SMat S = NULL; DMat D = NULL; FILE *file = svd_fatalReadFile(filename); switch (format) { case SVD_F_STH: S = svdLoadSparseTextHBFile(file); break; case SVD_F_ST: S = svdLoadSparseTextFile(file); break; case SVD_F_SB: S = svdLoadSparseBinaryFile(file); break; case SVD_F_DT: D = svdLoadDenseTextFile(file); break; case SVD_F_DB: D = svdLoadDenseBinaryFile(file); break; default: svd_error("svdLoadSparseMatrix: unknown format %d", format); } svd_closeFile(file); if (D) { S = svdConvertDtoS(D); svdFreeDMat(D); } return S; }
JNIEXPORT jint JNICALL Java_utils_SVD_svd___3DII_3D_3D_3D (JNIEnv *env, jclass obj, jdoubleArray _A, jint _N, jint _M, jdoubleArray _S, jdoubleArray _Ut, jdoubleArray _Vt) { //double svd_end[2] = { -1e-10, 1e-10 }; SVDVerbosity = 0; double* At = env->GetDoubleArrayElements(_A, 0); DMat dAt = svdNewDMat(_N, _M); for (int i = 0; i < _N; ++i) for (int j = 0; j < _M; ++j) dAt->value[i][j] = At[i * _M + j]; SMat sAt = svdConvertDtoS(dAt); svdFreeDMat(dAt); env->ReleaseDoubleArrayElements(_A, At, 0); jsize maxRank = env->GetArrayLength(_S); //SVDRec Arec = svdLAS2(sAt, 0, SVD_MAX_ITER, svd_end, SVD_KAPPA); SVDRec Arec = svdLAS2A(sAt, maxRank); svdFreeSMat(sAt); if (Arec == NULL) { svdFreeSVDRec(Arec); printf("WARNING: SVDLIBC las2a returns NULL \n\n"); return 0; } int rank = Arec->d; double* S = env->GetDoubleArrayElements(_S, 0); double* Ut = env->GetDoubleArrayElements(_Ut, 0); double* Vt = env->GetDoubleArrayElements(_Vt, 0); for (int i = 0; i < rank; ++i) S[i] = Arec->S[i]; for (int i = 0; i < rank; ++i) for (int j = 0; j < _N; ++j) { int p = i * _N + j; Ut[p] = Arec->Ut->value[i][j]; } for (int i = 0; i < rank; ++i) for (int j = 0; j < _M; ++j) { int p = i * _M + j; Vt[p] += Arec->Vt->value[i][j]; } env->ReleaseDoubleArrayElements(_S, S, 0); env->ReleaseDoubleArrayElements(_Ut, Ut, 0); env->ReleaseDoubleArrayElements(_Vt, Vt, 0); svdFreeSVDRec(Arec); return rank; }
JNIEXPORT jint JNICALL Java_utils_SVD_lowRankSvd (JNIEnv *env, jclass obj, jdoubleArray _At, jdoubleArray _Bt, jint _N, jint _M, jint _R, jdoubleArray _S, jdoubleArray _Ut, jdoubleArray _Vt) { //double svd_end[2] = { -1e-10, 1e-10 }; SVDVerbosity = 0; double* At = env->GetDoubleArrayElements(_At, 0); double* Bt = env->GetDoubleArrayElements(_Bt, 0); DMat dAt = svdNewDMat(_R, _N); for (int i = 0; i < _R; ++i) for (int j = 0; j < _N; ++j) dAt->value[i][j] = At[i * _N + j]; SMat sAt = svdConvertDtoS(dAt); svdFreeDMat(dAt); DMat dBt = svdNewDMat(_R, _M); for (int i = 0; i < _R; ++i) for (int j = 0; j < _M; ++j) dBt->value[i][j] = Bt[i * _M + j]; SMat sBt = svdConvertDtoS(dBt); svdFreeDMat(dBt); env->ReleaseDoubleArrayElements(_At, At, 0); env->ReleaseDoubleArrayElements(_Bt, Bt, 0); //SVDRec Arec = svdLAS2(sAt, 0, SVD_MAX_ITER, svd_end, SVD_KAPPA); //SVDRec Brec = svdLAS2(sBt, 0, SVD_MAX_ITER, svd_end, SVD_KAPPA); SVDRec Arec = svdLAS2A(sAt, 0); SVDRec Brec = svdLAS2A(sBt, 0); svdFreeSMat(sAt); svdFreeSMat(sBt); if (Arec == NULL || Brec == NULL) { svdFreeSVDRec(Arec); svdFreeSVDRec(Brec); printf("WARNING: SVDLIBC las2a returns NULL \n\n"); return 0; } int ranka = Arec->d, rankb = Brec->d; if (ranka == 0 || rankb == 0) { printf("WARNING: one matrix has 0 rank. %d %d\n\n", ranka, rankb); return 0; } DMat dM = svdNewDMat(ranka, rankb); for (int i = 0; i < ranka; ++i) for (int j = 0; j < rankb; ++j) { double va = 0; for (int k = 0; k < _R; ++k) va += Arec->Ut->value[i][k] * Brec->Ut->value[j][k]; dM->value[i][j] = va * Arec->S[i] * Brec->S[j]; } SMat sM = svdConvertDtoS(dM); svdFreeDMat(dM); //SVDRec Mrec = svdLAS2(sM, 0, SVD_MAX_ITER, svd_end, SVD_KAPPA); SVDRec Mrec = svdLAS2A(sM, 0); svdFreeSMat(sM); int rank = 0; if (Mrec != NULL && Mrec->d > 0) { double* S = env->GetDoubleArrayElements(_S, 0); double* Ut = env->GetDoubleArrayElements(_Ut, 0); double* Vt = env->GetDoubleArrayElements(_Vt, 0); rank = Mrec->d; for (int i = 0; i < rank; ++i) S[i] = Mrec->S[i]; for (int i = 0; i < rank; ++i) for (int j = 0; j < _N; ++j) { int p = i * _N + j; for (int k = 0; k < ranka; ++k) Ut[p] += Mrec->Ut->value[i][k] * Arec->Vt->value[k][j]; } for (int i = 0; i < rank; ++i) for (int j = 0; j < _M; ++j) { int p = i * _M + j; for (int k = 0; k < rankb; ++k) Vt[p] += Mrec->Vt->value[i][k] * Brec->Vt->value[k][j]; } env->ReleaseDoubleArrayElements(_S, S, 0); env->ReleaseDoubleArrayElements(_Ut, Ut, 0); env->ReleaseDoubleArrayElements(_Vt, Vt, 0); } else { printf("WARNING: matrix M has 0 rank or las2a returns NULL \n\n"); } svdFreeSVDRec(Arec); svdFreeSVDRec(Brec); svdFreeSVDRec(Mrec); return rank; }