/// <summary> /// Gets or sets an entry in the range. /// </summary> /// <param name="j">The 1-based row-index.</param> /// <param name="i">The 1-based column-index.</param> /// <returns>The value behind the index.</returns> public override ScalarValue this[int j, int i] { get { if (i > DimensionX || i < 1 || j > DimensionY || j < 1) { throw new ArgumentOutOfRangeException("Access in Matrix out of bounds."); } var index = new MatrixIndex(); index.Column = i; index.Row = j; if (ContainsIndex(index)) { return(GetIndex(index)); } if (i == 1) { return(GetValue(j)); } return(ScalarValue.Zero); } set { base[j, i] = value; } }
void DeleteFullRows(List<MatrixIndex> indices) { // There cannot be a full row if we have less indices than columns if (indices.Count < _dimX) { return; } // Save currently found rows var rows = new List<Int32>(); // Go over the all the indices for (var i = 0; i < indices.Count; i++) { var row = indices[i].Row; var cols = new List<Int32>(); var taken = new List<Int32>(); cols.Add(indices[i].Column); taken.Add(i); // How often do we find the same row ? for (var j = i + 1; j < indices.Count; j++) { if (indices[j].Row == row && !cols.Contains(indices[j].Column) && indices[j].Column >= 1 && indices[j].Column <= Columns) { cols.Add(indices[j].Column); taken.Add(j); // Apparently we've found a complete row - stop. if (cols.Count == Columns) { break; } } } // Check again if we found a match if (cols.Count == Columns) { // Sort the found indices taken.Sort(); // Remove the indices to improve search performance for (var k = taken.Count - 1; k >= 0; k--) { indices.RemoveAt(taken[k]); } // Add the found row to the list of found rows rows.Add(row); // Start again from the beginning, since we removed indices i = -1; } } // Just sort the rows to optimize the next itertion rows.Sort(); // Go over all found rows for (var i = rows.Count - 1; i >= 0; i--) { var count = 1; // Look how many consecutive rows we found for (var j = i - 1; j >= 0; j--) { if (rows[j] == rows[j + 1] - 1) { count++; } else { break; } } // Modify the loop iterator (for count == 1 no change is required) i -= (count - 1); // Delete the consecutive rows DeleteRows(rows[i], count); // Modify the other indices - shift them by the number of rows if required var minIndex = rows[i] + count; for (var j = 0; j < indices.Count; j++) { if (indices[j].Row >= minIndex) { indices[j] = new MatrixIndex(indices[j].Row - count, indices[j].Column); } } } }
/// <summary> /// Gets the element of the j-th row and i-th column. /// </summary> /// <param name="j">The 1-based row index.</param> /// <param name="i">The 1-based column index.</param> /// <returns>The entry of the specified row and column.</returns> public virtual ScalarValue this[int j, int i] { get { if (i > _dimX || i < 1) throw new YAMPIndexOutOfBoundException(i, 1, _dimX); else if (j > _dimY || j < 1) throw new YAMPIndexOutOfBoundException(j, 1, _dimY); var index = new MatrixIndex(); index.Column = i; index.Row = j; if(_values.ContainsKey(index)) return _values[index]; return ScalarValue.Zero; } set { if (i < 1) throw new YAMPIndexOutOfBoundException(i, 1); else if (j < 1) throw new YAMPIndexOutOfBoundException(j, 1); if (i > _dimX) _dimX = i; if (j > _dimY) _dimY = j; var index = new MatrixIndex(); index.Column = i; index.Row = j; if (value.IsZero) { if (_values.ContainsKey(index)) _values.Remove(index); } else if (_values.ContainsKey(index)) _values[index] = value; else _values.Add(index, value); } }
/// <summary> /// Gets the entry of the specified matrix-index entry /// in the dictionary. Please check if the element /// is in the dictionary anyway (ContainsIndex). /// </summary> /// <param name="index">The matrix-index (row, column).</param> /// <returns>The entry at the specified index.</returns> protected ScalarValue GetIndex(MatrixIndex index) { return _values[index]; }
/// <summary> /// Does the matrix contain this index? If not then /// the value is zero. /// </summary> /// <param name="index">The matrix-index (row, column).</param> /// <returns>A boolean.</returns> protected bool ContainsIndex(MatrixIndex index) { return _values.ContainsKey(index); }
/// <summary> /// Gets the index for the minimum entry in the matrix. /// </summary> /// <returns>The index of the minimum entry.</returns> public virtual MatrixIndex Min() { var minIndex = new MatrixIndex(); ScalarValue min = new ScalarValue(double.MaxValue); foreach (var _value in _values) { if (_value.Value < min) { minIndex = _value.Key; min = _value.Value; } } return minIndex; }
/// <summary> /// Gets the index for the maximum entry in the matrix. /// </summary> /// <returns>The index of the maximum entry.</returns> public virtual MatrixIndex Max() { var maxIndex = new MatrixIndex(); ScalarValue max = new ScalarValue(double.MinValue); foreach (var _value in _values) { if (_value.Value > max) { maxIndex = _value.Key; max = _value.Value; } } return maxIndex; }
/// <summary> /// Gets or sets an entry in the range. /// </summary> /// <param name="j">The 1-based row-index.</param> /// <param name="i">The 1-based column-index.</param> /// <returns>The value behind the index.</returns> public override ScalarValue this[int j, int i] { get { if (i > DimensionX || i < 1 || j > DimensionY || j < 1) throw new ArgumentOutOfRangeException("Access in Matrix out of bounds."); var index = new MatrixIndex(); index.Column = i; index.Row = j; if (ContainsIndex(index)) return GetIndex(index); if(i == 1) return GetValue(j); return ScalarValue.Zero; } set { base[j, i] = value; } }