public IEnumerator <KeyValuePair <TColKey, TValue> > GetEnumerator() { foreach (TColKey colKey in _matrix.ColKeys) { TValue value; if (_matrix.TryGetValue(_rowKey, colKey, out value)) { yield return(new KeyValuePair <TColKey, TValue>(colKey, value)); } } }
//Similar code elsewhere, but hard to move static code to common location //Would be nice to convert UOPair<double>, etc to UOPair<char> /// <summary> /// Write a matrix to DensePairAnsi file format /// </summary> /// <param name="matrix">The matrix to write</param> /// <param name="textWriter">The TextWriter to write to</param> /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param> public static void WriteDensePairAnsi(this Matrix <string, string, UOPair <char> > matrix, TextWriter textWriter, ParallelOptions parallelOptions) { textWriter.WriteLine("var\t{0}", matrix.ColKeys.StringJoin("\t")); foreach (string rowKey in matrix.RowKeys) { textWriter.Write(rowKey); textWriter.Write("\t"); int rowIndex = matrix.IndexOfRowKey[rowKey]; List <UOPair <byte> > storeList = new List <UOPair <byte> >(matrix.ColCount); for (int colIndex = 0; colIndex < matrix.ColCount; ++colIndex) { UOPair <char> value; UOPair <byte> store; if (matrix.TryGetValue(rowIndex, colIndex, out value)) { store = DensePairAnsi.StaticValueToStore(value); } else { store = DensePairAnsi.StaticStoreMissingValue; } storeList.Add(store); } Helper.CheckCondition(storeList.Count == matrix.ColCount, Properties.Resource.ExpectedStoreListCountToEqualColCount, storeList.Count, matrix.ColCount); string s = DensePairAnsi.StoreListToString(storeList, matrix.ColCount); textWriter.WriteLine(s); } }
internal void InternalGetInstance(Matrix <string, string, TValue> inputMatrix, ParallelOptions parallelOptions) { var selectRowsAndColsView = inputMatrix as SelectRowsAndColsView <string, string, TValue>; if (null != selectRowsAndColsView && selectRowsAndColsView.ParentMatrix is DenseStructMatrix <TStore, TValue> ) //We optimize this case { var parentMatrix = (DenseStructMatrix <TStore, TValue>)selectRowsAndColsView.ParentMatrix; Parallel.ForEach(RowKeys, parallelOptions, rowKey => { List <TStore> oldStoreList = parentMatrix.RowKeyToStoreList[rowKey]; List <TStore> newStoreList = RowKeyToStoreList[rowKey]; foreach (int oldColIndex in selectRowsAndColsView.IndexOfParentColKey) { newStoreList.Add(oldStoreList[oldColIndex]); } }); } else { CounterWithMessages counterWithMessages = new CounterWithMessages("Creating new DenseStructMatrix, working on row #{0} of {1}", 1000, RowCount); Parallel.ForEach(RowKeys, parallelOptions, rowKey => { counterWithMessages.Increment(); foreach (string colKey in ColKeys) { TValue value; if (inputMatrix.TryGetValue(rowKey, colKey, out value)) { this[rowKey, colKey] = value; } } }); } }
public static void WriteDenseAnsi(this Matrix <string, string, char> matrix, TextWriter textWriter, ParallelOptions parallelOptions, bool verbose = false) { textWriter.WriteLine("var\t{0}", matrix.ColKeys.StringJoin("\t")); foreach (string rowKey in matrix.RowKeys) { textWriter.Write(rowKey); textWriter.Write("\t"); int rowIndex = matrix.IndexOfRowKey[rowKey]; List <byte> storeList = new List <byte>(matrix.ColCount); for (int colIndex = 0; colIndex < matrix.ColCount; ++colIndex) { char value; byte store; if (matrix.TryGetValue(rowIndex, colIndex, out value)) { store = DenseAnsi.StaticValueToStore(value); } else { store = DenseAnsi.StaticStoreMissingValue; } storeList.Add(store); } Helper.CheckCondition(storeList.Count == matrix.ColCount, () => string.Format(CultureInfo.InvariantCulture, Properties.Resource.ExpectedStoreListCountToEqualColCount, storeList.Count, matrix.ColCount)); string s = DenseAnsi.StoreListToString(storeList, matrix.ColCount); textWriter.WriteLine(s); } }
private static string CreateLine(Matrix <string, string, char> matrix, string rowKey, CounterWithMessages counterWithMessages) { int rowIndex = matrix.IndexOfRowKey[rowKey]; List <byte> storeList = new List <byte>(matrix.ColCount); for (int colIndex = 0; colIndex < matrix.ColCount; ++colIndex) { char value; byte store; if (matrix.TryGetValue(rowIndex, colIndex, out value)) { store = DenseAnsi.StaticValueToStore(value); } else { store = DenseAnsi.StaticStoreMissingValue; } storeList.Add(store); } Helper.CheckCondition(storeList.Count == matrix.ColCount, "Assert"); string s = rowKey + "\t" + DenseAnsi.StoreListToString(storeList, matrix.ColCount); counterWithMessages.Increment(); return(s); }
//Similar code elsewhere, but hard to move static code to common location //Would be nice if could work from, say Matrix<string,string,int> /// <summary> /// Write in PaddedDouble file format to a TextWriter /// </summary> /// <param name="matrix">The matrix to write</param> /// <param name="textWriter">The TextWriter to write to</param> /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param> public static void WritePaddedDouble(this Matrix <string, string, double> matrix, TextWriter textWriter, ParallelOptions parallelOptions) { textWriter.WriteLine("var\t{0}", matrix.ColKeys.StringJoin("\t")); foreach (string rowKey in matrix.RowKeys) { textWriter.Write(rowKey); textWriter.Write("\t"); int rowIndex = matrix.IndexOfRowKey[rowKey]; List <double> storeList = new List <double>(matrix.ColCount); for (int colIndex = 0; colIndex < matrix.ColCount; ++colIndex) { double store; if (!matrix.TryGetValue(rowIndex, colIndex, out store)) { store = PaddedDouble.StaticStoreMissingValue; } storeList.Add(store); } Helper.CheckCondition(storeList.Count == matrix.ColCount, "Assert"); string s = PaddedDouble.StoreListToString(storeList, matrix.ColCount); textWriter.WriteLine(s); } }
/// <summary> /// Do a series of unit tests in which the matrix is access by rowIndex and colIndex. /// </summary> /// <param name="matrix">The matrix to test. It should have the values of the matrix from CreateModelMatrix()</param> /// <param name="doOutOfRangeTest">If true, does a test that throws and catches an exception related to being out of bounds.</param> public static void TestByIndexes(Matrix <string, string, double> matrix, bool doOutOfRangeTest) { //Loop for index and keys //Get the missing value double missingValue = matrix.MissingValue; //TryGetValue - true double valueAX; Helper.CheckCondition(matrix.TryGetValue(0, 0, out valueAX) && valueAX == 1); //TryGetValue - false double valueCZ; Helper.CheckCondition(!matrix.TryGetValue(2, 2, out valueCZ) && matrix.IsMissing(valueCZ)); //TryGetValue that is not in range should throw some exception if (doOutOfRangeTest) { try { double valueDW; matrix.TryGetValue(3, 3, out valueDW); Helper.CheckCondition(false); } catch (Exception) { } } //SetValueOrMissing - value -> value matrix.SetValueOrMissing(0, 0, 6); Helper.CheckCondition(matrix.TryGetValue(0, 0, out valueAX) && valueAX == 6); //SetValueOrMissing - value -> missing matrix.SetValueOrMissing(0, 0, double.NaN); Helper.CheckCondition(!matrix.TryGetValue(0, 0, out valueAX)); //SetValueOrMissing - missing -> value matrix.SetValueOrMissing(0, 0, 7); Helper.CheckCondition(matrix.TryGetValue(0, 0, out valueAX) && valueAX == 7); //SetValueOrMissing - missing -> missing matrix.SetValueOrMissing(2, 2, double.NaN); Helper.CheckCondition(!matrix.TryGetValue(2, 2, out valueCZ)); //Remove - true Helper.CheckCondition(matrix.Remove(0, 0)); //Remove - false Helper.CheckCondition(!matrix.Remove(0, 0)); //this get value Helper.CheckCondition(2 == matrix[0, 1]); //this set to value matrix[0, 1] = 8; Helper.CheckCondition(8 == matrix[0, 1]); //this get missing - expect error try { double valueCX = matrix[2, 0]; Helper.CheckCondition(false); } catch (Exception) { } //this set to missing - expect error try { matrix[0, 0] = double.NaN; Helper.CheckCondition(false); } catch (Exception) { } }