Пример #1
0
 private void CreateExemplaryMatrix()
 {
     //_matrix = new SparseDoubleMatrix(6, 20);
     _matrix = new SparseDoubleMatrix(1024, 1024);
     _matrix.SetRow(0);
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(0, 1.0));
     _matrix.FinishRow();
     _matrix.SetRow(1);
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(1, 2.0));
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(2, 3.0));
     _matrix.FinishRow();
     _matrix.SetRow(2);
     _matrix.FinishRow();
     _matrix.SetRow(3);
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(0, 4.0));
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(4, 5.0));
     _matrix.FinishRow();
     _matrix.SetRow(4);
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(4, 6.0));
     _matrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(3, 7.0));
     _matrix.FinishRow();
 }
Пример #2
0
        private SparseDoubleMatrix CreateDerivedMatrix(Dictionary <long, bool> exactlyOneStates, Dictionary <long, bool> exactlyZeroStates)
        {
            //Derived matrix is 0-based. Row i is equivalent to the probability distribution of state i (this is not the case for the Markov Chain).

            var derivedMatrix = new SparseDoubleMatrix(MarkovChain.States, MarkovChain.Transitions + MarkovChain.States);            //Transitions+States is a upper limit

            var enumerator = MarkovChain.GetEnumerator();

            for (var sourceState = 0; sourceState < MarkovChain.States; sourceState++)
            {
                enumerator.SelectSourceState(sourceState);
                derivedMatrix.SetRow(sourceState);
                if (exactlyOneStates.ContainsKey(sourceState) || exactlyZeroStates.ContainsKey(sourceState))
                {
                    // only add a self reference entry
                    derivedMatrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(sourceState, 1.0));
                }
                else
                {
                    // if state is neither exactlyOneStates nor exactlyZeroStates, it is a toCalculateState
                    var selfReferenceAdded = false;
                    while (enumerator.MoveNextTransition())
                    {
                        var columnValueEntry = enumerator.CurrentTransition;
                        var targetState      = columnValueEntry.Column;
                        if (targetState == sourceState)
                        {
                            //this implements the removal of the identity matrix
                            derivedMatrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(sourceState, columnValueEntry.Value - 1.0));
                            selfReferenceAdded = true;
                        }
                        else
                        {
                            derivedMatrix.AddColumnValueToCurrentRow(columnValueEntry);
                        }
                    }
                    if (!selfReferenceAdded)
                    {
                        //this implements the removal of the identity matrix (if not already done)
                        derivedMatrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(sourceState, -1.0));
                    }
                }
                derivedMatrix.FinishRow();
            }
            return(derivedMatrix);
        }