/// <summary> /// Creates a DenseAnsi from file(s) in sparse format. /// </summary> /// <param name="inputSparsePattern">The name of a file (or a pattern matching several files). The file(s) are in sparse format.</param> /// <param name="denseAnsi">The new DenseAnsi (or null, if the method fails).</param> /// <returns>true if the file(s) is read and the DenseAnsi is created; otherwise, false </returns> public static bool TryGetInstanceFromSparse(string inputSparsePattern, out DenseAnsi denseAnsi) { try { denseAnsi = GetInstanceFromSparse(inputSparsePattern); return(true); } catch { denseAnsi = null; return(false); } }
/// <summary> /// Converts matrix to a DenseAnsi. Even if the matrix is already an denseAnsi, a new one is created.. /// </summary> /// <param name="matrix">The matrix to convert from</param> /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param> /// <returns>A denseAnsi version of the matrix</returns> public static DenseAnsi ToDenseAnsi(this Matrix <string, string, char> matrix, ParallelOptions parallelOptions) { var denseAnsi = DenseAnsi.CreateEmptyInstance(matrix.RowKeys, matrix.ColKeys, DenseAnsi.StaticMissingValue); //Console.WriteLine("Convert no more than {0} values", matrix.RowCount * matrix.ColCount); //CounterWithMessages counterWithMessages = new CounterWithMessages("adding value #{0}", 100000, null); Parallel.ForEach(matrix.RowKeyColKeyValues, parallelOptions, triple => { //counterWithMessages.Increment(); denseAnsi[triple.RowKey, triple.ColKey] = triple.Value; } ); return(denseAnsi); }
/// <summary> /// Tries to read a file in DenseAnsi format and then creates a DenseAnsi class with values of the desired type. /// </summary> /// <typeparam name="TValue">The type of values wanted, for example, double</typeparam> /// <param name="filename">The dense ansi formatted file to read from.</param> /// <param name="missingValue">The special value that represents missing</param> /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param> /// <param name="matrix">A matrix that internally stores values in a DenseAnsi object.</param> /// <returns>true, if the file can be parsed; false, otherwise.</returns> public static bool TryParseDenseAnsiFormatAsGenericMatrix <TValue>(string filename, TValue missingValue, ParallelOptions parallelOptions, out Matrix <string, string, TValue> matrix) { matrix = null; Matrix <string, string, char> sscMatrix; if (!DenseAnsi.TryGetInstance(filename, '?', parallelOptions, out sscMatrix)) { return(false); } matrix = sscMatrix.ConvertValueView(new CharToGenericConverter <TValue>(), missingValue); //matrix = sscMatrix.ConvertValueView(c => Parser.Parse<TValue>(c.ToString()), val => SpecialFunctions.FirstAndOnly<char>(val.ToString()), missingValue); return(true); }