Inheritance: MonoBehaviour
Exemple #1
0
        /// <summary>
        /// Count the number of twins
        /// </summary>
        /// <param name="solver">Solver</param>
        /// <param name="column">Column</param>
        /// <param name="twin1">First twin element</param>
        /// <param name="twin2">Second twin element</param>
        /// <param name="magnitude">Magnitude method</param>
        /// <returns></returns>
        private static int CountTwins <T>(Solver <T> solver, int column, ref MatrixElement <T> twin1, ref MatrixElement <T> twin2, Func <T, double> magnitude) where T : IFormattable, IEquatable <T>
        {
            int twins = 0;

            // Begin `CountTwins'.

            var cTwin1 = solver.FirstInReorderedColumn(column);

            while (cTwin1 != null)
            {
                // if (Math.Abs(pTwin1.Element.Magnitude) == 1.0)
                if (magnitude(cTwin1.Value).Equals(1.0))
                {
                    var row    = cTwin1.Row;
                    var cTwin2 = solver.FirstInReorderedColumn(row);
                    while (cTwin2 != null && cTwin2.Row != column)
                    {
                        cTwin2 = cTwin2.Below;
                    }
                    if (cTwin2 != null && magnitude(cTwin2.Value).Equals(1.0))
                    {
                        // Found symmetric twins.
                        if (++twins >= 2)
                        {
                            return(twins);
                        }
                        twin1 = cTwin1;
                        twin2 = cTwin2;
                    }
                }
                cTwin1 = cTwin1.Below;
            }
            return(twins);
        }
 /// <summary>
 /// Unsetup
 /// </summary>
 /// <param name="simulation"></param>
 public override void Unsetup(Simulation simulation)
 {
     // Remove references
     DrainDrainPtr             = null;
     GateGatePtr               = null;
     SourceSourcePtr           = null;
     BulkBulkPtr               = null;
     DrainPrimeDrainPrimePtr   = null;
     SourcePrimeSourcePrimePtr = null;
     DrainDrainPrimePtr        = null;
     GateBulkPtr               = null;
     GateDrainPrimePtrPtr      = null;
     GateSourcePrimePtr        = null;
     SourceSourcePrimePtr      = null;
     BulkDrainPrimePtr         = null;
     BulkSourcePrimePtr        = null;
     DrainPrimeSourcePrimePtr  = null;
     DrainPrimeDrainPtr        = null;
     BulkGatePtr               = null;
     DrainPrimeGatePtr         = null;
     SourcePrimeGatePtr        = null;
     SourcePrimeSourcePtr      = null;
     DrainPrimeBulkPtr         = null;
     SourcePrimeBulkPtr        = null;
     SourcePrimeDrainPrimePtr  = null;
 }
Exemple #3
0
        /// <summary>
        /// Binds the behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">The context.</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            // Get node connections
            if (context is ComponentBindingContext cc)
            {
                PosNode = cc.Pins[0];
                NegNode = cc.Pins[1];
            }

            // First we copy the branch equation from the biasing behavior
            int branchEq = context.GetBehavior <BiasingBehavior>().BranchEq;

            PosIndex = 0;
            NegIndex = branchEq;

            // Do other behavioral stuff
            base.Bind(simulation, context);

            // Get matrix pointers
            var solver = State.Solver;

            PosBranchPtr = solver.GetMatrixElement(PosNode, branchEq);
            NegBranchPtr = solver.GetMatrixElement(NegNode, branchEq);
            BranchPosPtr = solver.GetMatrixElement(branchEq, PosNode);
            BranchNegPtr = solver.GetMatrixElement(branchEq, NegNode);
        }
Exemple #4
0
        /// <summary>
        /// Allocate elements in the Y-matrix and Rhs-vector to populate during loading. Additional
        /// equations can also be allocated here.
        /// </summary>
        /// <param name="variables">The variable set.</param>
        /// <param name="solver">The solver.</param>
        public void GetEquationPointers(VariableSet variables, Solver <double> solver)
        {
            // Allocate branch equations first
            Internal1 = variables.Create(Name.Combine("int1")).Index;
            Internal2 = variables.Create(Name.Combine("int2")).Index;
            BranchEq1 = variables.Create(Name.Combine("branch1"), VariableType.Current).Index;
            BranchEq2 = variables.Create(Name.Combine("branch2"), VariableType.Current).Index;

            Pos1Pos1Ptr = solver.GetMatrixElement(Pos1, Pos1);
            Pos1Int1Ptr = solver.GetMatrixElement(Pos1, Internal1);
            Int1Pos1Ptr = solver.GetMatrixElement(Internal1, Pos1);
            Int1Int1Ptr = solver.GetMatrixElement(Internal1, Internal1);
            Int1Ibr1Ptr = solver.GetMatrixElement(Internal1, BranchEq1);
            Ibr1Int1Ptr = solver.GetMatrixElement(BranchEq1, Internal1);
            Neg1Ibr1Ptr = solver.GetMatrixElement(Neg1, BranchEq1);
            Ibr1Neg1Ptr = solver.GetMatrixElement(BranchEq1, Neg1);

            Pos2Pos2Ptr = solver.GetMatrixElement(Pos2, Pos2);
            Pos2Int2Ptr = solver.GetMatrixElement(Pos2, Internal2);
            Int2Pos2Ptr = solver.GetMatrixElement(Internal2, Pos2);
            Int2Int2Ptr = solver.GetMatrixElement(Internal2, Internal2);
            Int2Ibr2Ptr = solver.GetMatrixElement(Internal2, BranchEq2);
            Ibr2Int2Ptr = solver.GetMatrixElement(BranchEq2, Internal2);
            Neg2Ibr2Ptr = solver.GetMatrixElement(Neg2, BranchEq2);
            Ibr2Neg2Ptr = solver.GetMatrixElement(BranchEq2, Neg2);

            // These pointers are only used to calculate the DC operating point
            Ibr1Pos1Ptr = solver.GetMatrixElement(BranchEq1, Pos1);
            Ibr1Pos2Ptr = solver.GetMatrixElement(BranchEq1, Pos2);
            Ibr1Neg2Ptr = solver.GetMatrixElement(BranchEq1, Neg2);
            Ibr2Ibr1Ptr = solver.GetMatrixElement(BranchEq2, BranchEq1);
            Ibr2Ibr2Ptr = solver.GetMatrixElement(BranchEq2, BranchEq2);
        }
 /// <summary>
 /// Unbind the behavior.
 /// </summary>
 public override void Unbind()
 {
     base.Unbind();
     _state = null;
     CCollectorCollectorPrimePtr    = null;
     CBaseBasePrimePtr              = null;
     CEmitterEmitterPrimePtr        = null;
     CCollectorPrimeCollectorPtr    = null;
     CCollectorPrimeBasePrimePtr    = null;
     CCollectorPrimeEmitterPrimePtr = null;
     CBasePrimeBasePtr              = null;
     CBasePrimeCollectorPrimePtr    = null;
     CBasePrimeEmitterPrimePtr      = null;
     CEmitterPrimeEmitterPtr        = null;
     CEmitterPrimeCollectorPrimePtr = null;
     CEmitterPrimeBasePrimePtr      = null;
     CCollectorCollectorPtr         = null;
     CBaseBasePtr       = null;
     CEmitterEmitterPtr = null;
     CCollectorPrimeCollectorPrimePtr = null;
     CBasePrimeBasePrimePtr           = null;
     CEmitterPrimeEmitterPrimePtr     = null;
     CSubstrateSubstratePtr           = null;
     CCollectorPrimeSubstratePtr      = null;
     CSubstrateCollectorPrimePtr      = null;
     CBaseCollectorPrimePtr           = null;
     CCollectorPrimeBasePtr           = null;
 }
Exemple #6
0
        /// <summary>
        /// This method will check whether or not a pivot element is valid or not.
        /// It checks for the submatrix right/below of the pivot.
        /// </summary>
        /// <param name="pivot">The pivot candidate.</param>
        /// <returns>
        /// True if the pivot can be used.
        /// </returns>
        /// <exception cref="ArgumentNullException">pivot</exception>
        public override bool IsValidPivot(MatrixElement <T> pivot)
        {
            if (pivot == null)
            {
                throw new ArgumentNullException(nameof(pivot));
            }

            // Get the magnitude of the current pivot
            var magnitude = Magnitude(pivot.Value);

            // Search for the largest element below the pivot
            var element = pivot.Below;
            var largest = 0.0;

            while (element != null)
            {
                largest = Math.Max(largest, Magnitude(element.Value));
                element = element.Below;
            }

            // Check the validity
            if (largest * RelativePivotThreshold < magnitude)
            {
                return(true);
            }
            return(false);
        }
Exemple #7
0
        private void InternalInsert(int lineIndex, int columnIndex, int value)
        {
            MatrixElement previous = null;

            foreach (var element in _elements)
            {
                if (element.Column == columnIndex && element.Line == lineIndex)
                {
                    element.Value = value;
                    return;
                }
                previous = element;
            }

            var newElement = new MatrixElement(lineIndex, columnIndex, value);

            if (previous == null)
            {
                _first = newElement;
            }
            else
            {
                previous.NextItem = newElement;
            }
        }
        /// <summary>
        /// Bind the behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">The context.</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);

            _state = ((FrequencySimulation)simulation).ComplexState;
            var solver = _state.Solver;

            CCollectorCollectorPrimePtr    = solver.GetMatrixElement(CollectorNode, CollectorPrimeNode);
            CBaseBasePrimePtr              = solver.GetMatrixElement(BaseNode, BasePrimeNode);
            CEmitterEmitterPrimePtr        = solver.GetMatrixElement(EmitterNode, EmitterPrimeNode);
            CCollectorPrimeCollectorPtr    = solver.GetMatrixElement(CollectorPrimeNode, CollectorNode);
            CCollectorPrimeBasePrimePtr    = solver.GetMatrixElement(CollectorPrimeNode, BasePrimeNode);
            CCollectorPrimeEmitterPrimePtr = solver.GetMatrixElement(CollectorPrimeNode, EmitterPrimeNode);
            CBasePrimeBasePtr              = solver.GetMatrixElement(BasePrimeNode, BaseNode);
            CBasePrimeCollectorPrimePtr    = solver.GetMatrixElement(BasePrimeNode, CollectorPrimeNode);
            CBasePrimeEmitterPrimePtr      = solver.GetMatrixElement(BasePrimeNode, EmitterPrimeNode);
            CEmitterPrimeEmitterPtr        = solver.GetMatrixElement(EmitterPrimeNode, EmitterNode);
            CEmitterPrimeCollectorPrimePtr = solver.GetMatrixElement(EmitterPrimeNode, CollectorPrimeNode);
            CEmitterPrimeBasePrimePtr      = solver.GetMatrixElement(EmitterPrimeNode, BasePrimeNode);
            CCollectorCollectorPtr         = solver.GetMatrixElement(CollectorNode, CollectorNode);
            CBaseBasePtr       = solver.GetMatrixElement(BaseNode, BaseNode);
            CEmitterEmitterPtr = solver.GetMatrixElement(EmitterNode, EmitterNode);
            CCollectorPrimeCollectorPrimePtr = solver.GetMatrixElement(CollectorPrimeNode, CollectorPrimeNode);
            CBasePrimeBasePrimePtr           = solver.GetMatrixElement(BasePrimeNode, BasePrimeNode);
            CEmitterPrimeEmitterPrimePtr     = solver.GetMatrixElement(EmitterPrimeNode, EmitterPrimeNode);
            CSubstrateSubstratePtr           = solver.GetMatrixElement(SubstrateNode, SubstrateNode);
            CCollectorPrimeSubstratePtr      = solver.GetMatrixElement(CollectorPrimeNode, SubstrateNode);
            CSubstrateCollectorPrimePtr      = solver.GetMatrixElement(SubstrateNode, CollectorPrimeNode);
            CBaseCollectorPrimePtr           = solver.GetMatrixElement(BaseNode, CollectorPrimeNode);
            CCollectorPrimeBasePtr           = solver.GetMatrixElement(CollectorPrimeNode, BaseNode);
        }
Exemple #9
0
        /// <summary>
        /// Bind behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">The context.</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);

            // Get parameters
            BaseParameters = context.GetParameterSet <BaseParameters>();

            if (context is ComponentBindingContext cc)
            {
                PosNode     = cc.Pins[0];
                NegNode     = cc.Pins[1];
                ContPosNode = cc.Pins[2];
                ContNegNode = cc.Pins[3];
            }

            _state = ((BaseSimulation)simulation).RealState;
            var solver    = _state.Solver;
            var variables = simulation.Variables;

            BranchEq            = variables.Create(Name.Combine("branch"), VariableType.Current).Index;
            PosBranchPtr        = solver.GetMatrixElement(PosNode, BranchEq);
            NegBranchPtr        = solver.GetMatrixElement(NegNode, BranchEq);
            BranchPosPtr        = solver.GetMatrixElement(BranchEq, PosNode);
            BranchNegPtr        = solver.GetMatrixElement(BranchEq, NegNode);
            BranchControlPosPtr = solver.GetMatrixElement(BranchEq, ContPosNode);
            BranchControlNegPtr = solver.GetMatrixElement(BranchEq, ContNegNode);
        }
Exemple #10
0
        /// <summary>
        /// Bind the behavior.
        /// </summary>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);

            // Cache some objects that we will use often
            _bp         = context.GetParameterSet <BaseParameters>();
            _state      = ((BaseSimulation)simulation).RealState;
            _baseConfig = simulation.Configurations.Get <BaseConfiguration>();

            // Find the nodes that the resistor is connected to
            if (context is ComponentBindingContext cbc)
            {
                _nodeA = cbc.Pins[0];
                _nodeB = cbc.Pins[1];
            }

            // We need 4 matrix elements here
            var solver = _state.Solver;

            _aaPtr = solver.GetMatrixElement(_nodeA, _nodeA);
            _abPtr = solver.GetMatrixElement(_nodeA, _nodeB);
            _baPtr = solver.GetMatrixElement(_nodeB, _nodeA);
            _bbPtr = solver.GetMatrixElement(_nodeB, _nodeB);

            // We also need 2 RHS vector elements
            _aPtr = solver.GetRhsElement(_nodeA);
            _bPtr = solver.GetRhsElement(_nodeB);
        }
        /// <summary>
        /// Gets matrix pointers
        /// </summary>
        /// <param name="solver">Matrix</param>
        public override void GetEquationPointers(Solver <Complex> solver)
        {
            if (solver == null)
            {
                throw new ArgumentNullException(nameof(solver));
            }

            // Get extra equations
            _drainNodePrime  = _load.DrainNodePrime;
            _sourceNodePrime = _load.SourceNodePrime;

            // Get matrix pointers
            DrainDrainPtr             = solver.GetMatrixElement(_drainNode, _drainNode);
            GateGatePtr               = solver.GetMatrixElement(_gateNode, _gateNode);
            SourceSourcePtr           = solver.GetMatrixElement(_sourceNode, _sourceNode);
            BulkBulkPtr               = solver.GetMatrixElement(_bulkNode, _bulkNode);
            DrainPrimeDrainPrimePtr   = solver.GetMatrixElement(_drainNodePrime, _drainNodePrime);
            SourcePrimeSourcePrimePtr = solver.GetMatrixElement(_sourceNodePrime, _sourceNodePrime);
            DrainDrainPrimePtr        = solver.GetMatrixElement(_drainNode, _drainNodePrime);
            GateBulkPtr               = solver.GetMatrixElement(_gateNode, _bulkNode);
            GateDrainPrimePtrPtr      = solver.GetMatrixElement(_gateNode, _drainNodePrime);
            GateSourcePrimePtr        = solver.GetMatrixElement(_gateNode, _sourceNodePrime);
            SourceSourcePrimePtr      = solver.GetMatrixElement(_sourceNode, _sourceNodePrime);
            BulkDrainPrimePtr         = solver.GetMatrixElement(_bulkNode, _drainNodePrime);
            BulkSourcePrimePtr        = solver.GetMatrixElement(_bulkNode, _sourceNodePrime);
            DrainPrimeSourcePrimePtr  = solver.GetMatrixElement(_drainNodePrime, _sourceNodePrime);
            DrainPrimeDrainPtr        = solver.GetMatrixElement(_drainNodePrime, _drainNode);
            BulkGatePtr               = solver.GetMatrixElement(_bulkNode, _gateNode);
            DrainPrimeGatePtr         = solver.GetMatrixElement(_drainNodePrime, _gateNode);
            SourcePrimeGatePtr        = solver.GetMatrixElement(_sourceNodePrime, _gateNode);
            SourcePrimeSourcePtr      = solver.GetMatrixElement(_sourceNodePrime, _sourceNode);
            DrainPrimeBulkPtr         = solver.GetMatrixElement(_drainNodePrime, _bulkNode);
            SourcePrimeBulkPtr        = solver.GetMatrixElement(_sourceNodePrime, _bulkNode);
            SourcePrimeDrainPrimePtr  = solver.GetMatrixElement(_sourceNodePrime, _drainNodePrime);
        }
        // Depth First Search
        static void Main()
        {
            // MEMORY + WRONG

            // TODO: Better Way to Read input

            // input size Col and Row on a single input line
            string[] Sizes = Console.ReadLine().Split(' ');

            int inputRows = int.Parse(Sizes[0]);
            int inputCols = int.Parse(Sizes[1]);

            // N Sizes[0] Lines with M Sizes[1] Strings
            // Break each row into separate elements
            MatrixElement[,] toSearch =                    // Array of Matrix Elements
                new MatrixElement[inputRows, inputCols];   //

            for (int row = 0; row < inputRows; row++)                   // For each row
            {                                                           // Break the input string
                string[] inputString = Console.ReadLine()
                                              .Trim(' ')
                                              .Split(' ');              // into a helper array
                                                                        //
                for (int col = 0; col < inputCols; col++)               //
                {                                                       //
                    toSearch[row, col] = new MatrixElement();           // create a new Matrix() element
                                                                        //
                    toSearch[row, col].Value = inputString[col];        // assign it's value
                }
            }

            // Helper Variables
            int curLength = 0;
            int MaxLength = int.MinValue;

            // Check Each unchecked element
            for (int row = 0; row < inputRows; row++)
            {
                for (int col = 0; col < inputCols; col++)
                {
                    // reset current sequence length before checking different numbers
                    curLength = 1;

                    if (toSearch[row, col].isChecked == false)          // if the current element has
                    {                                                   // not been checked already
                        curLength = Search(toSearch,                    // check the elements around
                            toSearch[row, col].Value,                   // and go deeper in to the
                            row, col, curLength);                       // into the sequence
                    }                                                   // before switching side
                                                                        //
                    if (curLength > MaxLength)                          // Check whether
                    {                                                   // current sequence is larger
                        MaxLength = curLength;                          // than the highest previous one
                    }
                }
            }

            // print Output
            Console.WriteLine(MaxLength);
        }
Exemple #13
0
    //----< display the matrix >------------------------------
    public override void VisitMatrixElement(MatrixElement element)
    {
        List<RowElement> rows = element.getRows();
          int numOfRows = rows.Count;
          if (numOfRows < 1)
          {
          return;
          }
          else
          {

          if (numOfRows == 1)
          {
              Console.Write("[");
              PrintRow(rows, 0);
              Console.WriteLine("]");
          }
          else
          {
              Console.Write("[");
              PrintRow(rows, 0);
              for (int i = 1; i < numOfRows; ++i)
              {
                  Console.Write(";");
                  Console.WriteLine();
                  PrintRow(rows, i);
              }
              Console.WriteLine("]");
          }
          }
    }
Exemple #14
0
 /// <summary>
 /// Unbind the behavior.
 /// </summary>
 public override void Unbind()
 {
     base.Unbind();
     _state                     = null;
     CDrainDrainPtr             = null;
     CGateGatePtr               = null;
     CSourceSourcePtr           = null;
     CBulkBulkPtr               = null;
     CDrainPrimeDrainPrimePtr   = null;
     CSourcePrimeSourcePrimePtr = null;
     CDrainDrainPrimePtr        = null;
     CGateBulkPtr               = null;
     CGateDrainPrimePtr         = null;
     CGateSourcePrimePtr        = null;
     CSourceSourcePrimePtr      = null;
     CBulkDrainPrimePtr         = null;
     CBulkSourcePrimePtr        = null;
     CDrainPrimeSourcePrimePtr  = null;
     CDrainPrimeDrainPtr        = null;
     CBulkGatePtr               = null;
     CDrainPrimeGatePtr         = null;
     CSourcePrimeGatePtr        = null;
     CSourcePrimeSourcePtr      = null;
     CDrainPrimeBulkPtr         = null;
     CSourcePrimeBulkPtr        = null;
     CSourcePrimeDrainPrimePtr  = null;
 }
Exemple #15
0
        /// <summary>
        /// Reset the row to 0.0 and return true if the row is a current equation
        /// </summary>
        /// <param name="solver">Solver</param>
        /// <param name="variables">List of unknowns/variables</param>
        /// <param name="rowIndex">Row number</param>
        /// <returns></returns>
        protected static bool ZeroNoncurrentRow(SparseLinearSystem <double> solver, VariableSet variables, int rowIndex)
        {
            if (solver == null)
            {
                throw new ArgumentNullException(nameof(solver));
            }
            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            bool currents = false;

            for (int n = 0; n < variables.Count; n++)
            {
                var node = variables[n];
                MatrixElement <double> x = solver.FindMatrixElement(rowIndex, node.Index);
                if (x != null && !x.Value.Equals(0.0))
                {
                    if (node.UnknownType == VariableType.Current)
                    {
                        currents = true;
                    }
                    else
                    {
                        x.Value = 0.0;
                    }
                }
            }
            return(currents);
        }
 /// <summary>
 /// Allocate elements in the Y-matrix and Rhs-vector to populate during loading.
 /// </summary>
 /// <param name="solver">The solver.</param>
 public void GetEquationPointers(Solver <Complex> solver)
 {
     CPos1Pos1Ptr = solver.GetMatrixElement(Pos1, Pos1);
     CPos1Int1Ptr = solver.GetMatrixElement(Pos1, Internal1);
     CNeg1Ibr1Ptr = solver.GetMatrixElement(Neg1, BranchEq1);
     CPos2Pos2Ptr = solver.GetMatrixElement(Pos2, Pos2);
     CNeg2Ibr2Ptr = solver.GetMatrixElement(Neg2, BranchEq2);
     CInt1Pos1Ptr = solver.GetMatrixElement(Internal1, Pos1);
     CInt1Int1Ptr = solver.GetMatrixElement(Internal1, Internal1);
     CInt1Ibr1Ptr = solver.GetMatrixElement(Internal1, BranchEq1);
     CInt2Int2Ptr = solver.GetMatrixElement(Internal2, Internal2);
     CInt2Ibr2Ptr = solver.GetMatrixElement(Internal2, BranchEq2);
     CIbr1Neg1Ptr = solver.GetMatrixElement(BranchEq1, Neg1);
     CIbr1Pos2Ptr = solver.GetMatrixElement(BranchEq1, Pos2);
     CIbr1Neg2Ptr = solver.GetMatrixElement(BranchEq1, Neg2);
     CIbr1Int1Ptr = solver.GetMatrixElement(BranchEq1, Internal1);
     CIbr1Ibr2Ptr = solver.GetMatrixElement(BranchEq1, BranchEq2);
     CIbr2Pos1Ptr = solver.GetMatrixElement(BranchEq2, Pos1);
     CIbr2Neg1Ptr = solver.GetMatrixElement(BranchEq2, Neg1);
     CIbr2Neg2Ptr = solver.GetMatrixElement(BranchEq2, Neg2);
     CIbr2Int2Ptr = solver.GetMatrixElement(BranchEq2, Internal2);
     CIbr2Ibr1Ptr = solver.GetMatrixElement(BranchEq2, BranchEq1);
     CPos2Int2Ptr = solver.GetMatrixElement(Pos2, Internal2);
     CInt2Pos2Ptr = solver.GetMatrixElement(Internal2, Pos2);
 }
        /// <summary>
        /// Gets matrix pionters
        /// </summary>
        /// <param name="solver">Matrix</param>
        public void GetEquationPointers(Solver <Complex> solver)
        {
            solver.ThrowIfNull(nameof(solver));

            // Get matrix pointers
            CDrainDrainPtr             = solver.GetMatrixElement(DrainNode, DrainNode);
            CGateGatePtr               = solver.GetMatrixElement(GateNode, GateNode);
            CSourceSourcePtr           = solver.GetMatrixElement(SourceNode, SourceNode);
            CBulkBulkPtr               = solver.GetMatrixElement(BulkNode, BulkNode);
            CDrainPrimeDrainPrimePtr   = solver.GetMatrixElement(DrainNodePrime, DrainNodePrime);
            CSourcePrimeSourcePrimePtr = solver.GetMatrixElement(SourceNodePrime, SourceNodePrime);
            CDrainDrainPrimePtr        = solver.GetMatrixElement(DrainNode, DrainNodePrime);
            CGateBulkPtr               = solver.GetMatrixElement(GateNode, BulkNode);
            CGateDrainPrimePtr         = solver.GetMatrixElement(GateNode, DrainNodePrime);
            CGateSourcePrimePtr        = solver.GetMatrixElement(GateNode, SourceNodePrime);
            CSourceSourcePrimePtr      = solver.GetMatrixElement(SourceNode, SourceNodePrime);
            CBulkDrainPrimePtr         = solver.GetMatrixElement(BulkNode, DrainNodePrime);
            CBulkSourcePrimePtr        = solver.GetMatrixElement(BulkNode, SourceNodePrime);
            CDrainPrimeSourcePrimePtr  = solver.GetMatrixElement(DrainNodePrime, SourceNodePrime);
            CDrainPrimeDrainPtr        = solver.GetMatrixElement(DrainNodePrime, DrainNode);
            CBulkGatePtr               = solver.GetMatrixElement(BulkNode, GateNode);
            CDrainPrimeGatePtr         = solver.GetMatrixElement(DrainNodePrime, GateNode);
            CSourcePrimeGatePtr        = solver.GetMatrixElement(SourceNodePrime, GateNode);
            CSourcePrimeSourcePtr      = solver.GetMatrixElement(SourceNodePrime, SourceNode);
            CDrainPrimeBulkPtr         = solver.GetMatrixElement(DrainNodePrime, BulkNode);
            CSourcePrimeBulkPtr        = solver.GetMatrixElement(SourceNodePrime, BulkNode);
            CSourcePrimeDrainPrimePtr  = solver.GetMatrixElement(SourceNodePrime, DrainNodePrime);
        }
        /// <summary>
        /// Bind behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">The context.</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);

            // Get parameters
            BaseParameters = context.GetParameterSet <BaseParameters>();

            // Get behaviors
            VoltageLoad = context.GetBehavior <VoltageSourceBehaviors.BiasingBehavior>("control");

            if (context is ComponentBindingContext cc)
            {
                PosNode = cc.Pins[0];
                NegNode = cc.Pins[1];
            }

            _state = ((BaseSimulation)simulation).RealState;
            var solver    = _state.Solver;
            var variables = simulation.Variables;

            // Create/get nodes
            ContBranchEq = VoltageLoad.BranchEq;
            BranchEq     = variables.Create(Name.Combine("branch"), VariableType.Current).Index;

            // Get matrix pointers
            PosBranchPtr           = solver.GetMatrixElement(PosNode, BranchEq);
            NegBranchPtr           = solver.GetMatrixElement(NegNode, BranchEq);
            BranchPosPtr           = solver.GetMatrixElement(BranchEq, PosNode);
            BranchNegPtr           = solver.GetMatrixElement(BranchEq, NegNode);
            BranchControlBranchPtr = solver.GetMatrixElement(BranchEq, ContBranchEq);
        }
 /// <summary>
 /// Unsetup the behavior
 /// </summary>
 public override void Unsetup()
 {
     PosPosPtr = null;
     NegNegPtr = null;
     PosNegPtr = null;
     NegPosPtr = null;
 }
Exemple #20
0
        /// <summary>
        /// Bind behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">Data provider</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);
            context.ThrowIfNull(nameof(context));

            // Get parameters
            BaseParameters = context.GetParameterSet <BaseParameters>();

            // Get behaviors (0 = CCCS behaviors, 1 = VSRC behaviors)
            VoltageLoad = context.GetBehavior <VoltageSourceBehaviors.BiasingBehavior>("control");

            // Connect
            if (context is ComponentBindingContext cc)
            {
                PosNode = cc.Pins[0];
                NegNode = cc.Pins[1];
            }

            _state = ((BaseSimulation)simulation).RealState.ThrowIfNull("state");
            var solver = _state.Solver;

            ControlBranchEq     = VoltageLoad.BranchEq;
            PosControlBranchPtr = solver.GetMatrixElement(PosNode, ControlBranchEq);
            NegControlBranchPtr = solver.GetMatrixElement(NegNode, ControlBranchEq);
        }
        public IEnumerable <SparseVector <T> > ReadRows()
        {
            if (this.streamReader == null)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // seek to matrix start position
            this.streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
            this.streamReader.DiscardBufferedData();

            // skip header
            var header = this.streamReader.ReadLine();

            // skip stats
            var parsedMatrixElements = (from line in SkippedEmptyAndCommentsLines(this.streamReader).Skip(1) // skip stats line
                                        let trimmedLine = line.Trim()
                                                          where trimmedLine != string.Empty && !trimmedLine.StartsWith("%")
                                                          select lineParser.ParseLine(trimmedLine));

            return(MatrixElement <T> .ToSparceVectorsFromOrderedMatrixElements(this.RowsCount,
                                                                               this.ColumnsCount,
                                                                               this.ElementsCount,
                                                                               parsedMatrixElements,
                                                                               1, 1));
        }
Exemple #22
0
        /// <summary>
        /// Bind the behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">The context.</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            // Get the nodes
            if (context is ComponentBindingContext cc)
            {
                PosNode = cc.Pins[0];
                NegNode = cc.Pins[1];
            }

            // Create a new branch equation for the current through the voltage source
            var variables = simulation.Variables;

            BranchEq = variables.Create(Name.Combine("branch"), VariableType.Current).Index;

            // Note: Because the voltage source equation looks like "v1 - v2 = f(...)" this translates to
            // "v1 - v2 - f(...) = 0", so that means we need the negative sign here - hence NegIndex.
            PosIndex = 0;
            NegIndex = BranchEq;

            // Do other behavioral source stuff
            base.Bind(simulation, context);

            // Get matrix pointers
            var solver = State.Solver;

            PosBranchPtr = solver.GetMatrixElement(PosNode, BranchEq);
            NegBranchPtr = solver.GetMatrixElement(NegNode, BranchEq);
            BranchPosPtr = solver.GetMatrixElement(BranchEq, PosNode);
            BranchNegPtr = solver.GetMatrixElement(BranchEq, NegNode);
        }
        //logic:
        //The matrix is sorted in both rows and column
        //so the first element will be the smallest.
        //build min heap from the first row..
        //heap node should have data, rowno, colno
        //get the min element in heap..
        //get the col no and get the nextrow on the same and update into the heap (we can do insert if we pop on previou step ) but update and heapify is better for this
        //get the next smallest
        //http://www.geeksforgeeks.org/kth-smallest-element-in-a-row-wise-and-column-wise-sorted-2d-array-set-1/

        public int FindthekthSmalleseElementInMatrix(int[,] matrix, int k)
        {
            MinHeap <MatrixElement> myheap = new MinHeap <MatrixElement>(new MinMatrixElementComparer());

            //add first row to minheap//all col in first row
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                myheap.Insert(new MatrixElement(matrix[0, col], 0, col));
            }

            // to find kth smallest..iterate k times
            for (int i = 1; i < k; i++)
            {
                //get the min
                MatrixElement minelement = myheap.Peek();
                //Find the next element in the next row of the same colmumn
                //check the next row is not ourside bounds
                if (minelement.Row + 1 <= matrix.GetLength(1) - 1)
                {
                    MatrixElement nextelement = new MatrixElement(matrix[minelement.Row + 1, minelement.Col], minelement.Row + 1, minelement.Col);

                    //we can update the heap root element and then call minheapify of the root
                    //my heap fon't prov that so
                    myheap.PopRoot();
                    myheap.Insert(nextelement); //this will make sure to have the smallest
                }
                else
                {
                    myheap.PopRoot();
                }
            }

            return(myheap.Peek().Data);
        }
Exemple #24
0
        /// <summary>
        /// Bind the behavior.
        /// </summary>
        /// <param name="simulation"></param>
        /// <param name="context"></param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);

            _state = ((FrequencySimulation)simulation).ComplexState;
            var solver = _state.Solver;

            CDrainDrainPtr             = solver.GetMatrixElement(DrainNode, DrainNode);
            CGateGatePtr               = solver.GetMatrixElement(GateNode, GateNode);
            CSourceSourcePtr           = solver.GetMatrixElement(SourceNode, SourceNode);
            CBulkBulkPtr               = solver.GetMatrixElement(BulkNode, BulkNode);
            CDrainPrimeDrainPrimePtr   = solver.GetMatrixElement(DrainNodePrime, DrainNodePrime);
            CSourcePrimeSourcePrimePtr = solver.GetMatrixElement(SourceNodePrime, SourceNodePrime);
            CDrainDrainPrimePtr        = solver.GetMatrixElement(DrainNode, DrainNodePrime);
            CGateBulkPtr               = solver.GetMatrixElement(GateNode, BulkNode);
            CGateDrainPrimePtr         = solver.GetMatrixElement(GateNode, DrainNodePrime);
            CGateSourcePrimePtr        = solver.GetMatrixElement(GateNode, SourceNodePrime);
            CSourceSourcePrimePtr      = solver.GetMatrixElement(SourceNode, SourceNodePrime);
            CBulkDrainPrimePtr         = solver.GetMatrixElement(BulkNode, DrainNodePrime);
            CBulkSourcePrimePtr        = solver.GetMatrixElement(BulkNode, SourceNodePrime);
            CDrainPrimeSourcePrimePtr  = solver.GetMatrixElement(DrainNodePrime, SourceNodePrime);
            CDrainPrimeDrainPtr        = solver.GetMatrixElement(DrainNodePrime, DrainNode);
            CBulkGatePtr               = solver.GetMatrixElement(BulkNode, GateNode);
            CDrainPrimeGatePtr         = solver.GetMatrixElement(DrainNodePrime, GateNode);
            CSourcePrimeGatePtr        = solver.GetMatrixElement(SourceNodePrime, GateNode);
            CSourcePrimeSourcePtr      = solver.GetMatrixElement(SourceNodePrime, SourceNode);
            CDrainPrimeBulkPtr         = solver.GetMatrixElement(DrainNodePrime, BulkNode);
            CSourcePrimeBulkPtr        = solver.GetMatrixElement(SourceNodePrime, BulkNode);
            CSourcePrimeDrainPrimePtr  = solver.GetMatrixElement(SourceNodePrime, DrainNodePrime);
        }
Exemple #25
0
        /// <summary>
        /// Notifies the strategy that a fill-in has been created
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="fillin">The fill-in.</param>
        public override void CreateFillin(SparseMatrix <T> matrix, MatrixElement <T> fillin)
        {
            matrix.ThrowIfNull(nameof(matrix));
            fillin.ThrowIfNull(nameof(fillin));

            // Update the markowitz row count
            int index = fillin.Row;

            _markowitzRow[index]++;
            _markowitzProduct[index] =
                Math.Min(_markowitzRow[index] * _markowitzColumn[index], MaxMarkowitzCount);
            if (_markowitzRow[index] == 1 && _markowitzColumn[index] != 0)
            {
                Singletons--;
            }

            // Update the markowitz column count
            index = fillin.Column;
            _markowitzColumn[index]++;
            _markowitzProduct[index] =
                Math.Min(_markowitzRow[index] * _markowitzColumn[index], MaxMarkowitzCount);
            if (_markowitzRow[index] != 0 && _markowitzColumn[index] == 1)
            {
                Singletons--;
            }
        }
Exemple #26
0
 /// <summary>
 /// Unsetup the behavior
 /// </summary>
 /// <param name="simulation"></param>
 public override void Unsetup(Simulation simulation)
 {
     PosPosPtr = null;
     PosNegPtr = null;
     NegPosPtr = null;
     NegNegPtr = null;
 }
Exemple #27
0
        /// <summary>
        /// Bind the behavior.
        /// </summary>
        /// <param name="simulation">The simulation.</param>
        /// <param name="context">The context.</param>
        public override void Bind(Simulation simulation, BindingContext context)
        {
            base.Bind(simulation, context);

            _state = ((FrequencySimulation)simulation).ComplexState;
            var solver = _state.Solver.ThrowIfNull("solver");

            CPos1Pos1Ptr = solver.GetMatrixElement(Pos1, Pos1);
            CPos1Int1Ptr = solver.GetMatrixElement(Pos1, Internal1);
            CNeg1Ibr1Ptr = solver.GetMatrixElement(Neg1, BranchEq1);
            CPos2Pos2Ptr = solver.GetMatrixElement(Pos2, Pos2);
            CNeg2Ibr2Ptr = solver.GetMatrixElement(Neg2, BranchEq2);
            CInt1Pos1Ptr = solver.GetMatrixElement(Internal1, Pos1);
            CInt1Int1Ptr = solver.GetMatrixElement(Internal1, Internal1);
            CInt1Ibr1Ptr = solver.GetMatrixElement(Internal1, BranchEq1);
            CInt2Int2Ptr = solver.GetMatrixElement(Internal2, Internal2);
            CInt2Ibr2Ptr = solver.GetMatrixElement(Internal2, BranchEq2);
            CIbr1Neg1Ptr = solver.GetMatrixElement(BranchEq1, Neg1);
            CIbr1Pos2Ptr = solver.GetMatrixElement(BranchEq1, Pos2);
            CIbr1Neg2Ptr = solver.GetMatrixElement(BranchEq1, Neg2);
            CIbr1Int1Ptr = solver.GetMatrixElement(BranchEq1, Internal1);
            CIbr1Ibr2Ptr = solver.GetMatrixElement(BranchEq1, BranchEq2);
            CIbr2Pos1Ptr = solver.GetMatrixElement(BranchEq2, Pos1);
            CIbr2Neg1Ptr = solver.GetMatrixElement(BranchEq2, Neg1);
            CIbr2Neg2Ptr = solver.GetMatrixElement(BranchEq2, Neg2);
            CIbr2Int2Ptr = solver.GetMatrixElement(BranchEq2, Internal2);
            CIbr2Ibr1Ptr = solver.GetMatrixElement(BranchEq2, BranchEq1);
            CPos2Int2Ptr = solver.GetMatrixElement(Pos2, Internal2);
            CInt2Pos2Ptr = solver.GetMatrixElement(Internal2, Pos2);
        }
Exemple #28
0
 /// <summary>
 /// Gets matrix pointers
 /// </summary>
 /// <param name="variables">Nodes</param>
 /// <param name="solver">Solver</param>
 public void GetEquationPointers(VariableSet variables, Solver <double> solver)
 {
     solver.ThrowIfNull(nameof(solver));
     ControlBranchEq     = VoltageLoad.BranchEq;
     PosControlBranchPtr = solver.GetMatrixElement(PosNode, ControlBranchEq);
     NegControlBranchPtr = solver.GetMatrixElement(NegNode, ControlBranchEq);
 }
Exemple #29
0
        /// <summary>
        /// Gets matrix pointers
        /// </summary>
        /// <param name="solver">Solver</param>
        public void GetEquationPointers(Solver <Complex> solver)
        {
            solver.ThrowIfNull(nameof(solver));

            // CGet matrix pointers
            CCollectorCollectorPrimePtr    = solver.GetMatrixElement(CollectorNode, CollectorPrimeNode);
            CBaseBasePrimePtr              = solver.GetMatrixElement(BaseNode, BasePrimeNode);
            CEmitterEmitterPrimePtr        = solver.GetMatrixElement(EmitterNode, EmitterPrimeNode);
            CCollectorPrimeCollectorPtr    = solver.GetMatrixElement(CollectorPrimeNode, CollectorNode);
            CCollectorPrimeBasePrimePtr    = solver.GetMatrixElement(CollectorPrimeNode, BasePrimeNode);
            CCollectorPrimeEmitterPrimePtr = solver.GetMatrixElement(CollectorPrimeNode, EmitterPrimeNode);
            CBasePrimeBasePtr              = solver.GetMatrixElement(BasePrimeNode, BaseNode);
            CBasePrimeCollectorPrimePtr    = solver.GetMatrixElement(BasePrimeNode, CollectorPrimeNode);
            CBasePrimeEmitterPrimePtr      = solver.GetMatrixElement(BasePrimeNode, EmitterPrimeNode);
            CEmitterPrimeEmitterPtr        = solver.GetMatrixElement(EmitterPrimeNode, EmitterNode);
            CEmitterPrimeCollectorPrimePtr = solver.GetMatrixElement(EmitterPrimeNode, CollectorPrimeNode);
            CEmitterPrimeBasePrimePtr      = solver.GetMatrixElement(EmitterPrimeNode, BasePrimeNode);
            CCollectorCollectorPtr         = solver.GetMatrixElement(CollectorNode, CollectorNode);
            CBaseBasePtr       = solver.GetMatrixElement(BaseNode, BaseNode);
            CEmitterEmitterPtr = solver.GetMatrixElement(EmitterNode, EmitterNode);
            CCollectorPrimeCollectorPrimePtr = solver.GetMatrixElement(CollectorPrimeNode, CollectorPrimeNode);
            CBasePrimeBasePrimePtr           = solver.GetMatrixElement(BasePrimeNode, BasePrimeNode);
            CEmitterPrimeEmitterPrimePtr     = solver.GetMatrixElement(EmitterPrimeNode, EmitterPrimeNode);
            CSubstrateSubstratePtr           = solver.GetMatrixElement(SubstrateNode, SubstrateNode);
            CCollectorPrimeSubstratePtr      = solver.GetMatrixElement(CollectorPrimeNode, SubstrateNode);
            CSubstrateCollectorPrimePtr      = solver.GetMatrixElement(SubstrateNode, CollectorPrimeNode);
            CBaseCollectorPrimePtr           = solver.GetMatrixElement(BaseNode, CollectorPrimeNode);
            CCollectorPrimeBasePtr           = solver.GetMatrixElement(CollectorPrimeNode, BaseNode);
        }
Exemple #30
0
 /// <summary>
 /// Unbind the behavior.
 /// </summary>
 public override void Unbind()
 {
     base.Unbind();
     CPos1Pos1Ptr = null;
     CPos1Int1Ptr = null;
     CNeg1Ibr1Ptr = null;
     CPos2Pos2Ptr = null;
     CNeg2Ibr2Ptr = null;
     CInt1Pos1Ptr = null;
     CInt1Int1Ptr = null;
     CInt1Ibr1Ptr = null;
     CInt2Int2Ptr = null;
     CInt2Ibr2Ptr = null;
     CIbr1Neg1Ptr = null;
     CIbr1Pos2Ptr = null;
     CIbr1Neg2Ptr = null;
     CIbr1Int1Ptr = null;
     CIbr1Ibr2Ptr = null;
     CIbr2Pos1Ptr = null;
     CIbr2Neg1Ptr = null;
     CIbr2Neg2Ptr = null;
     CIbr2Int2Ptr = null;
     CIbr2Ibr1Ptr = null;
     CPos2Int2Ptr = null;
     CInt2Pos2Ptr = null;
 }
        /// <summary>
        /// Gets matrix pointers
        /// </summary>
        /// <param name="solver">Solver</param>
        public void GetEquationPointers(Solver <Complex> solver)
        {
            if (solver == null)
            {
                throw new ArgumentNullException(nameof(solver));
            }

            // CGet matrix pointers
            CCollectorCollectorPrimePtr    = solver.GetMatrixElement(CollectorNode, CollectorPrimeNode);
            CBaseBasePrimePtr              = solver.GetMatrixElement(BaseNode, BasePrimeNode);
            CEmitterEmitterPrimePtr        = solver.GetMatrixElement(EmitterNode, EmitterPrimeNode);
            CCollectorPrimeCollectorPtr    = solver.GetMatrixElement(CollectorPrimeNode, CollectorNode);
            CCollectorPrimeBasePrimePtr    = solver.GetMatrixElement(CollectorPrimeNode, BasePrimeNode);
            CCollectorPrimeEmitterPrimePtr = solver.GetMatrixElement(CollectorPrimeNode, EmitterPrimeNode);
            CBasePrimeBasePtr              = solver.GetMatrixElement(BasePrimeNode, BaseNode);
            CBasePrimeCollectorPrimePtr    = solver.GetMatrixElement(BasePrimeNode, CollectorPrimeNode);
            CBasePrimeEmitterPrimePtr      = solver.GetMatrixElement(BasePrimeNode, EmitterPrimeNode);
            CEmitterPrimeEmitterPtr        = solver.GetMatrixElement(EmitterPrimeNode, EmitterNode);
            CEmitterPrimeCollectorPrimePtr = solver.GetMatrixElement(EmitterPrimeNode, CollectorPrimeNode);
            CEmitterPrimeBasePrimePtr      = solver.GetMatrixElement(EmitterPrimeNode, BasePrimeNode);
            CCollectorCollectorPtr         = solver.GetMatrixElement(CollectorNode, CollectorNode);
            CBaseBasePtr       = solver.GetMatrixElement(BaseNode, BaseNode);
            CEmitterEmitterPtr = solver.GetMatrixElement(EmitterNode, EmitterNode);
            CCollectorPrimeCollectorPrimePtr = solver.GetMatrixElement(CollectorPrimeNode, CollectorPrimeNode);
            CBasePrimeBasePrimePtr           = solver.GetMatrixElement(BasePrimeNode, BasePrimeNode);
            CEmitterPrimeEmitterPrimePtr     = solver.GetMatrixElement(EmitterPrimeNode, EmitterPrimeNode);
            CSubstrateSubstratePtr           = solver.GetMatrixElement(SubstrateNode, SubstrateNode);
            CCollectorPrimeSubstratePtr      = solver.GetMatrixElement(CollectorPrimeNode, SubstrateNode);
            CSubstrateCollectorPrimePtr      = solver.GetMatrixElement(SubstrateNode, CollectorPrimeNode);
            CBaseCollectorPrimePtr           = solver.GetMatrixElement(BaseNode, CollectorPrimeNode);
            CCollectorPrimeBasePtr           = solver.GetMatrixElement(CollectorPrimeNode, BaseNode);
        }
Exemple #32
0
 /// <summary>
 /// Unbind the behavior.
 /// </summary>
 public override void Unbind()
 {
     base.Unbind();
     _state = null;
     PosControlBranchPtr = null;
     NegControlBranchPtr = null;
 }
Exemple #33
0
    public void Swap(MatrixElement elem)
    {
        var val = Technicolor.Instance.CurSpectrumState.width / 1F;
        var newIndx = 0;
        if (val < 0.2F)
        {
            newIndx = elem.Index - 1;
        }
        else
        {
            newIndx = elem.Index + 1;
        }

        if (newIndx < 0)
        {
            newIndx = _elements.Count - 1;
        }
        else if (newIndx >= _elements.Count)
        {
            newIndx = 0;
        }

        var target = _indexes[newIndx];

        if (target.Swap(elem))
        {
            _indexes[target.Index] = target;
            _indexes[elem.Index] = elem;
        }
    }
        // Tag elements part of the current sequence
        public static int Search(MatrixElement[,] toSearch,     // Start with a unique element in the array
                                 string curElelment,            // Recursively go deeper into tree
                                 int Row,                       // Explore different branches on the way up
                                 int Col,                       // South -> East -> North -> West
                                 int curLength)
        {
            // Keep track of sequence length
            // Check Down
            if (Row + 1 < toSearch.GetLength(0))    // if in range of array
            {
                if (toSearch[Row + 1, Col].Value == curElelment.ToString()              // If current Element
                    && toSearch[Row + 1, Col].isChecked == false)                       // is equal to the one before
                {                                                                       // AND has not been checked before
                    curLength++;                                                        // increment length
                    toSearch[Row + 1, Col].isChecked = true;                            // tag as checked
                    curLength = Search(toSearch, curElelment, Row + 1, Col, curLength); // recursively check
                }                                                                       // the area around
            }

            // Check Right
            if (Col + 1 < toSearch.GetLength(1))    // if in range of array
            {
                if (toSearch[Row, Col + 1].Value == curElelment.ToString()               // If current Element
                    && toSearch[Row, Col + 1].isChecked == false)                        // is equal to the one before
                {                                                                        // AND has not been checked before
                    curLength++;                                                         // increment length
                    toSearch[Row, Col + 1].isChecked = true;                             // tag as checked
                    curLength = Search(toSearch, curElelment, Row, Col + 1, curLength);  // recursively check
                }                                                                        // the area around
            }

            // Check Up
            if (Row - 1 >= 0)                       // if in range of array
            {                                                                            // If current Element
                if (toSearch[Row - 1, Col].Value == curElelment.ToString()               // is equal to the one before
                    && toSearch[Row - 1, Col].isChecked == false)                        // AND has not been checked before
                {
                    curLength++;                                                         // increment length
                    toSearch[Row - 1, Col].isChecked = true;                             // tag as checked
                    curLength = Search(toSearch, curElelment, Row - 1, Col, curLength);  // recursively check
                }                                                                        // the area around
            }

            // Check Left
            if (Col - 1 > 0)                        // if in range of array
            {                                                                            // If current Element
                if (toSearch[Row, Col - 1].Value == curElelment.ToString()               // is equal to the one before
                    && toSearch[Row, Col - 1].isChecked == false)                        // AND has not been checked before
                {
                    curLength++;                                                         // increment length
                    toSearch[Row, Col - 1].isChecked = true;                             // tag as checked
                    curLength = Search(toSearch, curElelment, Row, Col - 1, curLength);  // recursively check
                }                                                                        // the area around
            }

            // Return the accumulated sequence length
            //
            return curLength;
        }
Exemple #35
0
    public bool Swap(MatrixElement other)
    {
        if (other == this || other._swapLock > 0 || _swapLock > 0)
        {
            return false;
        }

        var idx = _idx;
        _idx = other._idx;
        other._idx = idx;

        _swapLock = 0.01F;
        other._swapLock = 0.01F;
        return true;
    }
Exemple #36
0
   //-----------<Test Stub>--------
 static void main(string[] args)
 {
     Console.WriteLine("\nTesting the MatrixElement class");
     Console.WriteLine("\n================================\n");
     MatrixElement elem_mat = new MatrixElement();
     VariableElement elem_var = new VariableElement();
     IntegerElement elem_int = new IntegerElement();
     string var = "matrix";
     string row = "3";
     string column = "4";
     elem_var.setText(var);
     elem_mat.setVar(elem_var);
     elem_int.setText(row);
     elem_mat.setRow(elem_int);
     elem_int.setText(column);
     elem_mat.setColumn(elem_int);
     VariableElement varelement = elem_mat.getVar();
     IntegerElement rowelement = elem_mat.getRow();
     IntegerElement columnelement = elem_mat.getColumn();
     Console.WriteLine("The Matrix element is:{0},[{1}],[{2}]", varelement.getText(),rowelement.getText(),columnelement.getText());
 }
Exemple #37
0
    //----< Matrix Addition O(n*m) >------------------------------
    public bool Addition(MatrixElement a, MatrixElement b, ref MatrixElement result)
    {
        Console.WriteLine("matrix addition");
        List<RowElement> aRows = a.getRows();
        List<RowElement> bRows = b.getRows();

        if (aRows.Count != bRows.Count)
            return false;

        List<RowElement> resRow = new List<RowElement>();

        for (int i = 0; i < aRows.Count; ++i)
        {
            RowElement r = new RowElement();
            for (int j = 0; j < aRows[i].Count(); ++j)
            {
                r.addElement(aRows[i].getElement(j) + bRows[i].getElement(j));
            }
            result.addRows(r);
        }

        return true;
    }
Exemple #38
0
 //----< visit a Matrix Element >------------------------------
 public override void VisitMatrixElement(MatrixElement element)
 {
     //Console.WriteLine("VisitMatrixElement");
       //MatrixElement element_value = int.Parse(element.getText());
       mStack.Push(element);
 }
Exemple #39
0
    //----< Matrix Multiplication O(n*n*n) >------------------------------
    public bool Multiplication(MatrixElement a, MatrixElement b, ref MatrixElement result)
    {
        int rowNumOfA = a.GetNumOfRows();
        int colNumOfA = a.GetNumOfCols();

        int rowNumOfB = b.GetNumOfRows();
        int colNumOfB = b.GetNumOfCols();

        MatrixElement reverseB = new MatrixElement();
        b.Reverse(out reverseB);

        List<RowElement> RowA = a.getRows();
        List<RowElement> RowBReverse = reverseB.getRows();

        for (int i = 0; i < rowNumOfA; ++i)
        {
            RowElement r = new RowElement();
            RowElement partA = RowA[i];

            for (int j = 0; j < colNumOfB; ++j)
            {
                int sum = 0;
                RowElement partB = RowBReverse[j];
                for (int k = 0; k < colNumOfA; ++k)
                {
                    int first = partA.getElement(k);
                    int second = partB.getElement(k);
                    sum += first * second;
                }
                r.addElement(sum);
            }
            result.addRows(r);
        }

        return true;
    }
 public override void VisitMatrixSingleElement(MatrixElement element)
 {
     //throw new NotImplementedException();
 }
Exemple #41
0
 //----< visit Matrix Multiplication Operation >------------------------------
 public override void VisitMatrixMultiplicationOperationElement(MatrixMultiplicationOperationElement element)
 {
     VisitElement(element.getLhs());
       VisitElement(element.getRhs());
       MatrixElement rhs = mStack.Pop();
       MatrixElement lhs = mStack.Pop();
       MatrixElement result = new MatrixElement();
       bool ret = lhs.Multiplication(lhs, rhs, ref result);
       mStack.Push(result);
 }
Exemple #42
0
    //----< transpose of a matrix >------------------------------
    public void Reverse(out MatrixElement element)
    {
        element = new MatrixElement();

        List<int> all = Serialize();
        int cols = all.Count / numOfRows;

        for (int i = 0; i < cols; ++i)
        {
            RowElement r = new RowElement();
            for (int j = 0; j < numOfRows; ++j)
            {
                r.addElement(all[j * cols + i]);
            }
            element.addRows(r);
        }
    }
Exemple #43
0
 private int getMatrixCol(MatrixElement temp)
 {
     try
     {
         if (temp.getColumn() is IntegerElement)
         {
             string s = ((IntegerElement)temp.getColumn()).getText();
             return int.Parse(s);
         }
         else
         {
             string s = ((IntegerElement)(mVariableMap[((VariableElement)temp.getColumn()).getText()])).getText();
             return int.Parse(s);
         }
     }
     catch (Exception e) { sendres(112, "Invalid column\n"); e.GetType(); return 0; }
 }
Exemple #44
0
 public override void VisitMatrixSingleElement(MatrixElement element)
 {
     try
     {
         MatrixElement temp = element;
         if (inParallelFor == 1)
         {
             if (parallelVars.Contains(element.getVar().getText()))
             {
             }
             else
             {
                 matrixData(element.getVar().getText());
             }
             parallelString.Append(temp.getVar().getText());
             parallelString.Append("[");
             if (temp.getRow() is VariableElement)
                 parallelString.Append(((VariableElement)temp.getRow()).getText());
             else if (temp.getRow() is IntegerElement)
                 parallelString.Append(((IntegerElement)temp.getRow()).getText());
             parallelString.Append("]");
             parallelString.Append("[");
             if (temp.getColumn() is VariableElement)
                 parallelString.Append(((VariableElement)temp.getColumn()).getText());
             else if (temp.getColumn() is IntegerElement)
                 parallelString.Append(((IntegerElement)temp.getColumn()).getText());
             parallelString.Append("]");
         }
         else
         {
             PerformMatrixSingleElementOperation(element); 
         }
     }
     catch (Exception e) { sendres(112, "Error in matrix element\n"); e.GetType(); }
 }
Exemple #45
0
 public abstract void VisitMatrixSingleElement(MatrixElement element);
Exemple #46
0
        private void PerformMatrixSingleElementOperation(MatrixElement temp)
        {
            if (map_contains_matrix(temp.getVar().getText()))
            {
                MatrixVariableDeclaration matTemp = null;
                if (temp != null)
                {
                    matTemp = (MatrixVariableDeclaration)mVariableMap[temp.getVar().getText()];
                    int row = getMatrixRow(temp);
                    int col = getMatrixCol(temp);

                    if (matTemp != null)
                    {
                        if (row < int.Parse(matTemp.getRow().getText()) && col < int.Parse(matTemp.getColumn().getText()))
                        {
                            if (matTemp.getType() == "int")
                            {
                                int val = matTemp.getintValueat(row, col);
                                IntegerElement elem = new IntegerElement();
                                elem.setText(val.ToString());
                                mat_stack.Push(elem);
                            }
                            else if (matTemp.getType() == "double")
                            {
                                double val = matTemp.getdoubleValueat(row, col);
                                DoubleElement elem = new DoubleElement();
                                elem.setText(val.ToString());
                                mat_stack.Push(elem);
                            }
                        }
                        else
                        {
                            Console.Write("Range out of bound\n");
                            sendres(112, "Range out of bound\n");
                        }
                    }
                }
            }
        }