// *** Wrapper for external DoLSqr *** public static double[] DoLSqr(int num_cols, LSqrSparseMatrix mat, LSqrSparseMatrix mat_transp, double[] rhs, int max_iter) { IntPtr sol_ptr = DoLSqr(mat.Id, mat_transp.Id, rhs, max_iter); double[] sol = new double[num_cols]; Marshal.Copy(sol_ptr, sol, 0, sol.Length); Marshal.FreeHGlobal(sol_ptr); return(sol); }
public static double[] DoLSqr(int numCols, LSqrSparseMatrix mat, LSqrSparseMatrix matTransp, double[] initSol, double[] rhs, int maxIter) { IntPtr solPtr = DoLSqr(mat.Id, matTransp.Id, initSol, rhs, maxIter); GC.KeepAlive(mat); // avoid premature garbage collection GC.KeepAlive(matTransp); double[] sol = new double[numCols]; Marshal.Copy(solPtr, sol, 0, sol.Length); Marshal.FreeHGlobal(solPtr); return sol; }
public static double[] DoLSqr(int numCols, LSqrSparseMatrix mat, LSqrSparseMatrix matTransp, double[] initSol, double[] rhs, int maxIter) { IntPtr solPtr = DoLSqr(mat.Id, matTransp.Id, initSol, rhs, maxIter); GC.KeepAlive(mat); // avoid premature garbage collection GC.KeepAlive(matTransp); double[] sol = new double[numCols]; Marshal.Copy(solPtr, sol, 0, sol.Length); Marshal.FreeHGlobal(solPtr); return(sol); }
public static LSqrSparseMatrix FromDenseMatrix(double[,] mat) { LSqrSparseMatrix lsqrMat = new LSqrSparseMatrix(mat.GetLength(0)); for (int row = 0; row < mat.GetLength(0); row++) { for (int col = 0; col < mat.GetLength(1); col++) { if (mat[row, col] != 0) { lsqrMat.InsertValue(row, col, mat[row, col]); } } } return(lsqrMat); }
public void Train(ILabeledExampleCollection <double, SparseVector <double> > dataset) { Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null); Utils.ThrowException(dataset.Count == 0 ? new ArgumentValueException("dataset") : null); LSqrSparseMatrix mat = new LSqrSparseMatrix(dataset.Count); double[] rhs = new double[dataset.Count]; int solSize = -1; int i = 0; foreach (LabeledExample <double, SparseVector <double> > labeledExample in dataset) { if (labeledExample.Example.LastNonEmptyIndex + 1 > solSize) { solSize = labeledExample.Example.LastNonEmptyIndex + 1; } foreach (IdxDat <double> item in labeledExample.Example) { mat.InsertValue(i, item.Idx, item.Dat); } rhs[i++] = labeledExample.Label; } Utils.ThrowException((mInitSol != null && mInitSol.Length != solSize) ? new ArgumentValueException("InitialSolution") : null); LSqrSparseMatrix matT = new LSqrSparseMatrix(solSize); i = 0; foreach (LabeledExample <double, SparseVector <double> > labeledExample in dataset) { foreach (IdxDat <double> item in labeledExample.Example) { matT.InsertValue(item.Idx, i, item.Dat); } i++; } int numIter = mNumIter < 0 ? solSize + dataset.Count + 50 : mNumIter; mSol = new ArrayList <double>(LSqrDll.DoLSqr(solSize, mat, matT, mInitSol, rhs, numIter)); mat.Dispose(); matT.Dispose(); }
public void Train(IExampleCollection <double, SparseVector <double> .ReadOnly> dataset) { Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null); Utils.ThrowException(dataset.Count == 0 ? new ArgumentValueException("dataset") : null); LSqrSparseMatrix mat = new LSqrSparseMatrix(dataset.Count); double[] rhs = new double[dataset.Count]; int sol_size = -1; int i = 0; foreach (LabeledExample <double, SparseVector <double> .ReadOnly> labeled_example in dataset) { if (labeled_example.Example.LastNonEmptyIndex + 1 > sol_size) { sol_size = labeled_example.Example.LastNonEmptyIndex + 1; } foreach (IdxDat <double> item in labeled_example.Example) { mat.InsertValue(i, item.Idx, item.Dat); } rhs[i++] = labeled_example.Label; } LSqrSparseMatrix mat_t = new LSqrSparseMatrix(sol_size); i = 0; foreach (LabeledExample <double, SparseVector <double> .ReadOnly> labeled_example in dataset) { foreach (IdxDat <double> item in labeled_example.Example) { mat_t.InsertValue(item.Idx, i, item.Dat); } i++; } int num_iter = m_num_iter < 0 ? sol_size + dataset.Count + 50 : m_num_iter; m_sol = new ArrayList <double>(LSqrDll.DoLSqr(sol_size, mat, mat_t, rhs, num_iter)); mat.Dispose(); mat_t.Dispose(); }
// *** Wrappers for external DoLSqr *** public static double[] DoLSqr(int numCols, LSqrSparseMatrix mat, LSqrSparseMatrix matTransp, double[] rhs, int maxIter) { return(DoLSqr(numCols, mat, matTransp, /*initSol=*/ null, rhs, maxIter)); }
// *** Wrappers for external DoLSqr *** public static double[] DoLSqr(int numCols, LSqrSparseMatrix mat, LSqrSparseMatrix matTransp, double[] rhs, int maxIter) { return DoLSqr(numCols, mat, matTransp, /*initSol=*/null, rhs, maxIter); }
public static LSqrSparseMatrix TransposeFromDenseMatrix(double[,] mat) { LSqrSparseMatrix lsqrMat = new LSqrSparseMatrix(mat.GetLength(1)); for (int col = 0; col < mat.GetLength(1); col++) { for (int row = 0; row < mat.GetLength(0); row++) { if (mat[row, col] != 0) { lsqrMat.InsertValue(col, row, mat[row, col]); } } } return lsqrMat; }