Ejemplo n.º 1
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("]");
          }
          }
    }
Ejemplo n.º 2
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;
    }
Ejemplo n.º 3
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;
    }