示例#1
0
    public bool Equals(DynamicMatrix m)
    {
        if (m == null)
        {
            return(false);
        }

        if (this == m)
        {
            return(true);
        }

        if (numRows != m.numRows || numCols != m.numCols)
        {
            return(false);
        }

        for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < numCols; j++)
            {
                if (get(i, j) != m.get(i, j))
                {
                    return(false);
                }
            }
        }

        return(true);
    }
示例#2
0
 public DynamicMatrix(DynamicMatrix m)
 {
     createElement(m.getNumRows(), m.getNumCols());
     for (int row = 0; row < getNumRows(); row++)
     {
         for (int col = 0; col < getNumCols(); col++)
         {
             set(row, col, m.get(row, col));
         }
     }
 }
示例#3
0
    public float dot(DynamicMatrix m)
    {
        if (getNumRows() != m.getNumRows() || getNumCols() != m.getNumCols())
        {
            throw new Exception("dimensions bad in dot()");
        }
        float sum = 0;

        for (int r = 0; r < getNumRows(); r++)
        {
            for (int c = 0; c < getNumCols(); c++)
            {
                sum += this.get(r, c) * m.get(r, c);
            }
        }

        return(sum);
    }
示例#4
0
    public DynamicMatrix subtractFrom(DynamicMatrix m)
    {
        if (getNumRows() != m.getNumRows() || getNumCols() != m.getNumCols())
        {
            throw new Exception("dimensions bad in addTo()");
        }
        DynamicMatrix ret = new DynamicMatrix(getNumRows(), getNumCols());

        for (int r = 0; r < getNumRows(); r++)
        {
            for (int c = 0; c < getNumCols(); c++)
            {
                ret.set(r, c, this.get(r, c) - m.get(r, c));
            }
        }

        return(ret);
    }
示例#5
0
    public DynamicMatrix multiply(DynamicMatrix m)
    {
        if (getNumCols() != m.getNumRows())
        {
            throw new Exception(this.getDimension() + " * " + m.getDimension() + " dimensions bad in multiply()");
        }

        DynamicMatrix ret = new DynamicMatrix(getNumRows(), m.getNumCols());

        for (int r = 0; r < getNumRows(); r++)
        {
            for (int c = 0; c < m.getNumCols(); c++)
            {
                for (int k = 0; k < getNumCols(); k++)
                {
                    ret.set(r, c, ret.get(r, c) + (this.get(r, k) * m.get(k, c)));
                }
            }
        }

        return(ret);
    }
示例#6
0
    /**
     * Calculates the matrix's Moore-Penrose pseudoinverse
     * @return an MxN (row x col) matrix which is the matrix's pseudoinverse.
     */
    public DynamicMatrix pseudoInverse()
    {
        int r, c;

        int           k = 1;
        DynamicMatrix ak = new DynamicMatrix(getNumRows(), 1);
        DynamicMatrix dk, ck, bk;

        DynamicMatrix R_plus;

        for (r = 0; r < getNumRows(); r++)
        {
            ak.set(r, 0, this.get(r, 0));
        }

        if (!ak.EqualsZero())
        {
//		if (!ak.equals(0.0)) {
            R_plus = ak.transpose().multiply(1.0f / (ak.dot(ak)));
        }
        else
        {
            R_plus = new DynamicMatrix(1, getNumCols());
        }

        while (k < this.getNumCols())
        {
            for (r = 0; r < getNumRows(); r++)
            {
                ak.set(r, 0, this.get(r, k));
            }

            dk = R_plus.multiply(ak);
            DynamicMatrix T = new DynamicMatrix(getNumRows(), k);
            for (r = 0; r < getNumRows(); r++)
            {
                for (c = 0; c < k; c++)
                {
                    T.set(r, c, this.get(r, c));
                }
            }

            ck = ak.subtractFrom(T.multiply(dk));

            if (!ck.EqualsZero())
            {
//			if (!ck.equals(0.0)) {
                bk = ck.transpose().multiply(1.0f / (ck.dot(ck)));
            }
            else
            {
                bk = dk.transpose().multiply(1.0f / (1.0f + dk.dot(dk)))
                     .multiply(R_plus);
            }

            DynamicMatrix N = R_plus.subtractFrom(dk.multiply(bk));
            R_plus = new DynamicMatrix(N.getNumRows() + 1, N.getNumCols());

            for (r = 0; r < N.getNumRows(); r++)
            {
                for (c = 0; c < N.getNumCols(); c++)
                {
                    R_plus.set(r, c, N.get(r, c));
                }
            }
            for (c = 0; c < N.getNumCols(); c++)
            {
                R_plus.set(R_plus.getNumRows() - 1, c, bk.get(0, c));
            }

            k++;
        }
        return(R_plus);
    }