numl.Math.LinearAlgebra.Matrix.Insert C# (CSharp) Method

Insert() public method

Returns a new Matrix with the Vector inserted at the specified position
public Insert ( Vector v, int index, VectorType t, bool insertAfter = true ) : Matrix
v Vector Vector to insert
index int The zero based row / column.
t VectorType Vector orientation
insertAfter bool Insert after or before the last row / column
return Matrix
        public Matrix Insert(Vector v, int index, VectorType t, bool insertAfter = true)
        {
            if (t == VectorType.Col && v.Length != Rows) throw new ArgumentException("Column vector does not match matrix height");
            if (t == VectorType.Row && v.Length != Cols) throw new ArgumentException("Row vector does not match matrix width");

            if (t == VectorType.Col && (index >= Cols || index < 0) && (index != -1 || !insertAfter)) throw new ArgumentException("Column index does not match matrix width");
            if (t == VectorType.Row && (index >= Rows || index < 0) && (index != -1 || !insertAfter)) throw new ArgumentException("Row index does not match matrix height");

            var temp = ToArray().ToList();
            if (t == VectorType.Row)
            {
                if (index == temp.Count - 1 && insertAfter)
                {
                    temp.Add(v);
                }
                else
                {
                    temp.Insert(index, v);
                }
            }
            else
            {
                if (index == temp[0].Length - 1 && insertAfter)
                {
                    for (int i = 0; i < temp.Count; i++)
                    {
                        var copy = temp[i].ToList();
                        copy.Add(v[i]);
                        temp[i] = copy.ToArray();
                    }
                }
                else
                {
                    for (int i = 0; i < temp.Count; i++)
                    {
                        var copy = temp[i].ToList();
                        copy.Insert(index, v[i]);
                        temp[i] = copy.ToArray();
                    }
                }
            }

            return new Matrix(temp.ToArray());
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Converts the experience pair into their equivalent math forms.
        /// </summary>
        /// <param name="state">IMDPState instance.</param>
        /// <param name="nodes">List of nodes added to the result set.</param>
        /// <param name="states">Matrix to store contained successor state vectors.</param>
        /// <param name="actions">Vector to store the contained action values.</param>
        /// <param name="statesP">Matrix to store all contained successor transition state vectors.</param>
        /// <param name="rewards">Vector to store all contained reward values.</param>
        /// <returns>HashSet&lt;string&gt;</returns>
        private static void Convert(this IMDPState state, ref List<string> nodes, ref Matrix states, ref Vector actions, ref Matrix statesP, ref Vector rewards)
        {
            if (state != null)
            {
                foreach (IMDPSuccessor successor in state.GetSuccessors())
                {
                    if (state.Features.Length != states.Cols)
                        states = Matrix.Reshape(states, states.Rows, state.Features.Length);
                    if (state.Features.Length != statesP.Cols)
                        statesP = Matrix.Reshape(statesP, statesP.Rows, ((IMDPState) successor.State).Features.Length);

                    string id = $"{state.Id}:{successor.State.Id}";
                    if (!nodes.Contains(id))
                    {
                        states = states.Insert(state.ToVector(), states.Rows - 1, VectorType.Row);
                        actions = actions.Insert(actions.Length - 1, successor.Action.Id);
                        statesP = statesP.Insert(((IMDPState) successor.State).ToVector(), statesP.Rows - 1, VectorType.Row);
                        rewards = rewards.Insert(rewards.Length - 1, successor.Reward);
                        nodes.Add(id);
                    }

                    if (!successor.State.IsTerminal)
                    {
                        var successorState = ((IMDPState) successor.State);
                        if (successorState.Id != state.Id)
                            successorState.Convert(ref nodes, ref states, ref actions, ref statesP, ref rewards);
                    }
                }
            }
        }
All Usage Examples Of numl.Math.LinearAlgebra.Matrix::Insert