TValue IList <TValue> .this[int colIndex]
 {
     get
     {
         return(Matrix.GetValueOrMissing(RowIndex, colIndex));
     }
     set
     {
         Matrix.SetValueOrMissing(RowIndex, colIndex, value);
     }
 }
Example #2
0
        /// <summary>
        /// Converts matrix to a new DenseMatrix. Even if the inputMatrix is a DenseMatrix, a new DenseMatrix is created. The copy is, thus,
        /// always deep.
        /// </summary>
        /// <typeparam name="TRowKey">The type of the row key. Usually "String"</typeparam>
        /// <typeparam name="TColKey">The type of the col key. Usually "String"</typeparam>
        /// <typeparam name="TValue">The type of the value, for example, double, int, char, etc.</typeparam>
        /// <param name="inputMatrix">The matrix to convert.</param>
        /// <returns>A new DenseMatrix with same rowKeys, colKeys, missing and nonmissing values, and special missing value.</returns>
        public static DenseMatrix <TRowKey, TColKey, TValue> ToDenseMatrix <TRowKey, TColKey, TValue>(this Matrix <TRowKey, TColKey, TValue> inputMatrix)
        {
            //Special code for materializing views

            //SelectRowsAndColsView
            var selectRowsAndColsView = inputMatrix as SelectRowsAndColsView <TRowKey, TColKey, TValue>;

            if (null != selectRowsAndColsView && selectRowsAndColsView.ParentMatrix is DenseMatrix <TRowKey, TColKey, TValue> )
            {
                return(MaterializeSelectRowsColsViewToDenseMatrix(selectRowsAndColsView));
            }
            //Transpose
            var transposeView = inputMatrix as TransposeView <TRowKey, TColKey, TValue>;

            if (transposeView != null && transposeView.ParentMatrix is DenseMatrix <TColKey, TRowKey, TValue> )
            {
                return(MaterializeTransposeViewToDenseMatrix(transposeView));
            }

            var denseMatrix = DenseMatrix <TRowKey, TColKey, TValue> .CreateDefaultInstance(inputMatrix.RowKeys, inputMatrix.ColKeys, inputMatrix.MissingValue);

            foreach (TRowKey rowKey in inputMatrix.RowKeys)
            {
                foreach (TColKey colKey in inputMatrix.ColKeys)
                {
                    denseMatrix.SetValueOrMissing(rowKey, colKey, inputMatrix.GetValueOrMissing(rowKey, colKey));
                }
            }
            return(denseMatrix);
        }
Example #3
0
        /// <summary>
        /// Determines whether two Matrix&lt;TRowKey,TColKey,TValue&gt; are equal. They are equal if they
        ///   0. The 2nd one is not null
        ///   3. Have the same missing values
        ///   1. have the same rowKeys, in the same order
        ///   2. have the same colKeys, in the same order
        ///   4. Have the same nonmissing values
        /// May be as slow as O(rowCount * colCount)
        /// </summary>
        /// <param name="otherMatrix">The matrix to compare to</param>
        /// <returns>true, if they are equal in terms of rowKeys, colKeys, missing and nonMissing values. Otherwise, false.</returns>
        public bool MatrixEquals(Matrix <TRowKey, TColKey, TValue> otherMatrix)
        {
            if (otherMatrix == null)
            {
                return(false);
            }

            if (!IsMissing(otherMatrix.MissingValue))
            {
                return(false);
            }

            if (!RowKeys.SequenceEqual(otherMatrix.RowKeys))
            {
                return(false);
            }

            if (!ColKeys.SequenceEqual(otherMatrix.ColKeys))
            {
                return(false);
            }


            for (int rIdx = 0; rIdx < RowCount; rIdx++)
            {
                for (int cIdx = 0; cIdx < ColCount; cIdx++)
                {
                    TValue value1 = GetValueOrMissing(rIdx, cIdx);
                    TValue value2 = otherMatrix.GetValueOrMissing(rIdx, cIdx);
                    if (value1 == null)
                    {
                        if (value2 != null)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (!value1.Equals(value2))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Example #4
0
 public bool Contains(KeyValuePair <TRowKey, TValue> pair)
 {
     Helper.CheckCondition(!_matrix.IsMissing(pair.Value), Properties.Resource.MatrixSpecialValueUseError);
     return(_matrix.GetValueOrMissing(pair.Key, _colKey).Equals(pair.Value));
 }
Example #5
0
 public bool Contains(KeyValuePair <TRowKey, TValue> pair)
 {
     Helper.CheckCondition(!_matrix.IsMissing(pair.Value), "The value in the pair should not be the special missing value.");
     return(_matrix.GetValueOrMissing(pair.Key, _colKey).Equals(pair.Value));
 }