Exemplo n.º 1
0
        public static MathMatrix Identity(int dim)
        {
            MathMatrix retval = MathMatrix.CreateMatrix(dim, dim, 0);

            for (int idx = 0; idx < dim; ++idx)
            {
                retval[idx, idx] = 1;
            }
            return(retval);
        }
Exemplo n.º 2
0
        // THE DOMAIN OF THIS FUNCTION AS OF NOW DOES NOT INCLUDE COMPLEX NUMBERS.
        public static MathMatrix Sqrt_Elmtwise(MathMatrix A)
        {
            MathMatrix sqrt = MathMatrix.CreateMatrix(A.RowCount, A.ColCount);

            for (int r = 0; r < A.RowCount; ++r)
            {
                for (int c = 0; c < A.ColCount; ++c)
                {
                    sqrt[r, c] = Math.Sqrt(A[r, c]);
                }
            }

            return(sqrt);
        }
Exemplo n.º 3
0
        public static MathMatrix Transpose(MathMatrix inMatrix)
        {
            MathMatrix retval = MathMatrix.CreateMatrix(inMatrix.ColCount, inMatrix.RowCount);

            for (int m = 0; m < inMatrix.RowCount; ++m)
            {
                for (int n = 0; n < inMatrix.ColCount; ++n)
                {
                    retval[n, m] = inMatrix[m, n];
                }
            }

            return(retval);
        }
Exemplo n.º 4
0
        public static MathMatrix Normal(int count, double mu = 1, double sigmasq = 0)
        {
            MathMatrix normalvec = MathMatrix.CreateMatrix(1, count);

            for (int idx = 0; idx < count; ++idx)
            {
                Random rand          = new Random();
                double u1            = 1.0 - rand.NextDouble();
                double u2            = 1.0 - rand.NextDouble();
                double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
                double randNormal    = mu + Math.Sqrt(sigmasq) * randStdNormal;
                normalvec[0, idx] = randNormal;
            }

            return(normalvec);
        }
Exemplo n.º 5
0
        public static MathMatrix SteppedSequence(double begin, double end, double step)
        {
            List <decimal> seq = new List <decimal>();

            for (decimal idx = Convert.ToDecimal(begin); idx <= Convert.ToDecimal(end); idx += Convert.ToDecimal(step))
            {
                seq.Add(idx);
            }

            MathMatrix retval = MathMatrix.CreateMatrix(1, seq.Count);

            for (int idx = 0; idx < seq.Count; ++idx)
            {
                retval[0, idx] = Convert.ToDouble(seq[idx]);
            }

            return(retval);
        }
Exemplo n.º 6
0
        public static MathMatrix Subtract(MathMatrix A, MathMatrix B)
        {
            if (A.RowCount != B.RowCount || A.ColCount != B.ColCount)
            {
                throw new InvalidMatrixAddDimensionsException();
            }

            MathMatrix difference = MathMatrix.CreateMatrix(A.RowCount, A.ColCount);

            for (int r = 0; r < A.RowCount; ++r)
            {
                for (int c = 0; c < B.ColCount; ++c)
                {
                    difference[r, c] = A[r, c] - B[r, c];
                }
            }

            return(difference);
        }
Exemplo n.º 7
0
        public static MathMatrix Add(MathMatrix A, MathMatrix B)
        {
            if (A.RowCount != B.RowCount || A.ColCount != B.ColCount)
            {
                throw new InvalidMatrixAddDimensionsException();
            }

            MathMatrix sum = MathMatrix.CreateMatrix(A.RowCount, A.ColCount);

            for (int r = 0; r < A.RowCount; ++r)
            {
                for (int c = 0; c < B.ColCount; ++c)
                {
                    sum[r, c] = A[r, c] + B[r, c];
                }
            }

            return(sum);
        }
Exemplo n.º 8
0
        public static MathMatrix Multiply(MathMatrix A, MathMatrix B)
        {
            if (A.ColCount != B.RowCount)
            {
                throw new InvalidMatrixMultiplicationDimensionsException();
            }

            MathMatrix product = MathMatrix.CreateMatrix(A.RowCount, B.ColCount, 0);

            for (int r = 0; r < A.RowCount; ++r)
            {
                for (int c = 0; c < B.ColCount; ++c)
                {
                    for (int x = 0; x < B.RowCount; ++x)
                    {
                        product[r, c] += A[r, x] * B[x, c];
                    }
                }
            }

            return(product);
        }