Exemplo n.º 1
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;
    }
Exemplo n.º 2
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);
        }
    }
Exemplo 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;
    }