Exemple #1
0
        public static Matrixf Div(Matrixf a, Matrixf b)
        {
            if (b.rows != b.columns)
            {
                throw new ArgumentException("Can not divide the matrixf b because it's non-square matrixf.", nameof(b));
            }
            if (a.columns != b.rows)
            {
                throw new ArgumentException("Wrong match, can not divide two matrixf with different columns or rows.", nameof(a));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf       = new Matrixf(a.rows, a.columns);
            Matrixf mInverse = Inverse(b);
            float   sum      = 0.0f;

            for (int i = 0; i < a.rows; i++)
            {
                for (int j = 0; j < mInverse.columns; j++)
                {
                    for (int k = 0; k < a.columns; k++)
                    {
                        sum = sum + a.matrixf[i, k] * mInverse.matrixf[k, j]; // a Div b = a Mul Inverse(b)
                    }
                    mf.matrixf[i, j] = sum;
                    sum = 0.0f;
                }
            }

            return(mf);
        }
Exemple #2
0
        public static Matrixf Mul(Matrixf a, Matrixf b)
        {
            if (a.columns != b.rows)
            {
                throw new ArgumentException("Wrong match, can not multiply two matrixf with different columns or rows.", nameof(a));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf  = new Matrixf(a.rows, b.columns);
            float   sum = 0.0f;

            for (int i = 0; i < a.rows; i++)
            {
                for (int j = 0; j < b.columns; j++)
                {
                    for (int k = 0; k < a.columns; k++)
                    {
                        sum = sum + a.matrixf[i, k] * b.matrixf[k, j]; // a Mul b = matrix product(a,b)
                    }
                    mf.matrixf[i, j] = sum;
                    sum = 0.0f;
                }
            }

            return(mf);
        }
Exemple #3
0
        public static Matrixf Identity(int row, int column)
        {
            if (row == 0 || column == 0)
            {
                throw new ArgumentException("Can not set one or zero dimension matrixf to identity.", nameof(row));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf = new Matrixf(row, column);

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    if (i == j)
                    {
                        mf.matrixf[i, j] = 1.0f;
                    }
                    else
                    {
                        mf.matrixf[i, j] = 0.0f;
                    }
                }
            }

            return(mf);
        }
Exemple #4
0
        // set identity method
        public static Matrixf Identity(Matrixf a)
        {
            if (a.rows == 0 || a.columns == 0)
            {
                throw new ArgumentException("Can not set one or zero dimension matrixf to identity.", nameof(a));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf = new Matrixf(a.rows, a.columns);

            for (int i = 0; i < a.rows; i++)
            {
                for (int j = 0; j < a.columns; j++)
                {
                    if (i == j)
                    {
                        mf.matrixf[i, j] = 1.0f;
                    }
                    else
                    {
                        mf.matrixf[i, j] = 0.0f;
                    }
                }
            }

            return(mf);
        }
Exemple #5
0
        // get the transpose method
        public static Matrixf Transpose(Matrixf a)
        {
            if (a.rows == 0 || a.columns == 0)
            {
                throw new ArgumentException("Wrong matrixf, can not transpose one or zero dimension matrixf.", nameof(a));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf = new Matrixf(a.columns, a.rows); // define the transpose matrixf

            for (int i = 0; i < a.columns; i++)
            {
                for (int j = 0; j < a.rows; j++)
                {
                    mf.matrixf[i, j] = a.matrixf[j, i];
                }
            }

            return(mf);
        }
Exemple #6
0
        public static Matrixf operator /(Matrixf a, float b)
        {
            if (b.GetType() != typeof(float))
            {
                throw new ArgumentException("Wrong number, the matrixf must divide float type number.", nameof(b));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf = new Matrixf(a.rows, a.columns);

            for (int i = 0; i < a.rows; i++)
            {
                for (int j = 0; j < a.columns; j++)
                {
                    mf.matrixf[i, j] = a.matrixf[i, j] / b;
                }
            }

            return(mf);
        }
Exemple #7
0
        // divide method
        public static Matrixf operator /(Matrixf a, Matrixf b)
        {
            if (a.rows != b.rows || a.columns != b.columns)
            {
                throw new ArgumentException("Can not divide two matrixf with different rows or columns.", nameof(a));
            }

            // Matrixf mf = new Matrixf();
            Matrixf mf = new Matrixf(a.rows, a.columns);

            for (int i = 0; i < a.rows; i++)
            {
                for (int j = 0; j < a.columns; j++)
                {
                    mf.matrixf[i, j] = a.matrixf[i, j] / b.matrixf[i, j];
                }
            }

            return(mf);
        }
Exemple #8
0
        // get the inverse method
        public static Matrixf Inverse(Matrixf a)
        {
            if (a.rows != a.columns)
            {
                throw new ArgumentException("Can not get the inverse of the non-square matrixf.", nameof(a));
            }

            // construct the transform matrix
            int     row    = a.rows;
            int     col    = a.columns;
            Matrixf mTrans = new Matrixf(row, 2 * col);
            Matrixf mTemp  = new Matrixf(row, 2 * col);
            Matrixf mf     = new Matrixf(row, col);

            // initialize the transform matrix
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    mTrans.matrixf[i, j] = a.matrixf[i, j];
                    if (i == j)
                    {
                        mTrans.matrixf[i, j + col] = 1.0f;
                    }
                    else
                    {
                        mTrans.matrixf[i, j + row] = 0.0f;
                    }
                }
            }

            // process the elementary row transformation
            int   rankFlag  = 0;
            float scaleTemp = 0.0f;

            for (int q = 0; q < col; q++)
            {
                // set the elements to ones in row order for every row of the matrixf
                for (int p = q; p < row; p++)
                {
                    if (mTrans.matrixf[p, q] != 0.0f)
                    {
                        if (mTrans.matrixf[p, q] != 1.0f)
                        {
                            scaleTemp = mTrans.matrixf[p, q];
                            for (int r = q; r < 2 * col; r++)
                            {
                                mTrans.matrixf[p, r] = mTrans.matrixf[p, r] / scaleTemp;
                            }
                        }
                        // reset the scaleTemp
                        scaleTemp = 0.0f;
                    }
                    else
                    {
                        rankFlag++;
                        continue;
                    }
                }

                // check the rank of the matrixf
                if (rankFlag == (row - q))
                {
                    throw new ArgumentException("The matrixf can not get inverse because of its non-full rank.", nameof(rankFlag));
                }

                // check whether the main element equals zero and change it to one by exchanging the rows
                if (mTrans.matrixf[q, q] == 0.0f)
                {
                    for (int s = q + 1; s < row; s++)
                    {
                        // check relative column elements for exchange
                        if (mTrans.matrixf[s, q] != 0.0f)
                        {
                            // mTemp = mTrans;
                            mTemp.matrixf = (float[, ])Clone(mTrans.matrixf);
                            for (int t = 0; t < 2 * col; t++)
                            {
                                mTrans.matrixf[q, t] = mTemp.matrixf[s, t];
                                mTrans.matrixf[s, t] = mTemp.matrixf[q, t];
                            }
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    // clear the mTemp matrixf buffer
                    mTemp = Zeros(mTemp);
                }

                // set the non-main element to zeros in relative column of the matrixf
                for (int u = q + 1; u < row; u++)
                {
                    if (mTrans.matrixf[u, q] != 0.0f)
                    {
                        for (int v = 0; v < 2 * col; v++)
                        {
                            mTrans.matrixf[u, v] = mTrans.matrixf[u, v] - mTrans.matrixf[q, v];
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                // reset the rankFlag
                rankFlag = 0;
            }

            // set the non-main element to zeros in right-up area of the matrixf in column order
            for (int x = col - 1; x > 0; x--) // check the columns from the last column to second column
            {
                // check the rows from second-to-last row to first row
                for (int y = x - 1; y >= 0; y--)
                {
                    scaleTemp = mTrans.matrixf[y, x] / mTrans.matrixf[x, x];
                    for (int z = 0; z < 2 * col; z++) // set the every element of the picked row
                    {
                        mTrans.matrixf[y, z] = mTrans.matrixf[y, z] - scaleTemp * mTrans.matrixf[x, z];
                    }
                    // reset the scaleTemp
                    scaleTemp = 0.0f;
                }
            }

            // get the inverse of the original matrixf a
            for (int m = 0; m < row; m++)
            {
                for (int n = 0; n < col; n++)
                {
                    mf.matrixf[m, n] = mTrans.matrixf[m, n + col];
                }
            }

            return(mf);
        }