Example #1
0
        /// <summary>
        /// <paramref name="m"/> matrix 의 요소값을 <paramref name="s"/>의 요소로 설정합니다.
        /// </summary>
        /// <param name="m"></param>
        /// <param name="s"></param>
        /// <param name="rows"></param>
        /// <param name="c0"></param>
        /// <param name="c1"></param>
        public static void SetMatrix(this Matrix m, Matrix s, int[] rows, int c0, int c1) {
            m.ShouldNotBeNull("m");
            s.ShouldNotBeNull("s");
            rows.ShouldNotBeNull("rows");
            Guard.Assert(c1 > c0, "c1 should be greater than c0, c0=[{0}], c1=[{1}]", c0, c1);

            var cols = new int[c1 - c0 + 1];
            for(var c = 0; c < cols.Length; c++)
                cols[c] = c0 + c;

            SetMatrix(m, s, rows, cols);
        }
Example #2
0
        /// <summary>
        /// 두개의 <see cref="Matrix"/>가 같은 차원인지 검사합니다.
        /// </summary>
        /// <param name="m1"></param>
        /// <param name="m2"></param>
        /// <returns></returns>
        public static bool IsSameDimension(this Matrix m1, Matrix m2) {
            m1.ShouldNotBeNull("m1");
            m2.ShouldNotBeNull("m2");

            return IsSameDimension(m1.Data, m2.Data);
        }
Example #3
0
        /// <summary>
        /// <paramref name="m"/> matrix 의 요소값을 <paramref name="s"/>의 요소로 설정합니다.
        /// </summary>
        /// <param name="m"></param>
        /// <param name="s"></param>
        /// <param name="r0"></param>
        /// <param name="r1"></param>
        /// <param name="c0"></param>
        /// <param name="c1"></param>
        public static void SetMatrix(this Matrix m, Matrix s, int r0, int r1, int c0, int c1) {
            m.ShouldNotBeNull("m");
            s.ShouldNotBeNull("s");
            Guard.Assert(r1 > r0, "r1 should be greater than r0, r0=[{0}], r1=[{1}]", r0, r1);
            Guard.Assert(c1 > c0, "c1 should be greater than c0, c0=[{0}], c1=[{1}]", c0, c1);

            var rows = new int[r1 - r0 + 1];
            for(int r = 0; r < rows.Length; r++)
                rows[r] = r0 + r;

            var cols = new int[c1 - c0 + 1];
            for(int c = 0; c < cols.Length; c++)
                cols[c] = c0 + c;

            SetMatrix(m, s, rows, cols);
        }
Example #4
0
        /// <summary>
        /// <paramref name="m"/> matrix 의 요소값을 <paramref name="s"/>의 요소로 설정합니다.
        /// </summary>
        /// <param name="m"></param>
        /// <param name="s"></param>
        /// <param name="r0"></param>
        /// <param name="r1"></param>
        /// <param name="cols"></param>
        public static void SetMatrix(this Matrix m, Matrix s, int r0, int r1, int[] cols) {
            m.ShouldNotBeNull("m");
            s.ShouldNotBeNull("s");
            Guard.Assert(r1 > r0, "r1 should be greater than r0, r0=[{0}], r1=[{1}]", r0, r1);
            cols.ShouldNotBeNull("cols");

            var rows = new int[r1 - r0 + 1];
            for(int r = 0; r < rows.Length; r++)
                rows[r] = r0 + r;

            SetMatrix(m, s, rows, cols);
        }
Example #5
0
        /// <summary>
        /// result = a  / s 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="a"></param>
        /// <param name="s"></param>
        public static void Divide(this Matrix result, Matrix a, double s) {
            result.ShouldNotBeNull("result");
            a.ShouldNotBeNull("a");
            Guard.Assert<DivideByZeroException>(s != 0.0, @"s is zero.");
            CheckSameDimension(result, a);

            for(var r = 0; r < a.Rows; r++)
                for(var c = 0; c < a.Cols; c++)
                    result[r, c] = a[r, c] / s;
        }
Example #6
0
        /// <summary>
        /// <paramref name="m"/> matrix 의 요소값을 <paramref name="s"/>의 요소로 설정합니다.
        /// </summary>
        /// <param name="m"></param>
        /// <param name="s"></param>
        /// <param name="rows"></param>
        /// <param name="cols"></param>
        public static void SetMatrix(this Matrix m, Matrix s, int[] rows, int[] cols) {
            m.ShouldNotBeNull("m");
            s.ShouldNotBeNull("s");
            rows.ShouldNotBeNull("rows");
            cols.ShouldNotBeNull("cols");
            Guard.Assert(s.Rows >= rows.Length, @"s.Rows >= rows.Length, s.Rows=[{0}], rows.Length=[{1}]", s.Rows, rows.Length);
            Guard.Assert(s.Cols >= cols.Length, @"s.Cols >= cols.Length, s.Cols=[{0}], cols.Length=[{1}]", s.Cols, cols.Length);

            for(var r = 0; r < rows.Length; r++)
                for(var c = 0; c < cols.Length; c++)
                    m[rows[r], cols[c]] = s[r, c];
        }
Example #7
0
        /// <summary>
        /// result = a  / b 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void Divide(this Matrix result, Matrix a, Matrix b) {
            result.ShouldNotBeNull("result");
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");
            CheckSameDimension(result, a);
            CheckSameDimension(a, b);

            for(var r = 0; r < a.Rows; r++)
                for(var c = 0; c < b.Cols; c++)
                    result[r, c] = a[r, c] / b[r, c];
        }
Example #8
0
        /// <summary>
        /// return a / s
        /// </summary>
        /// <param name="a"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Matrix Divide(Matrix a, double s) {
            a.ShouldNotBeNull("a");
            Guard.Assert<DivideByZeroException>(s != 0.0, @"s is zero.");

            var result = new Matrix(a.Rows, a.Cols);
            result.Divide(a, s);
            return result;
        }
Example #9
0
        /// <summary>
        /// result = a * s
        /// </summary>
        /// <param name="result"></param>
        /// <param name="a"></param>
        /// <param name="s"></param>
        public static void Multiply(this Matrix result, Matrix a, double s) {
            result.ShouldNotBeNull("result");
            a.ShouldNotBeNull("a");
            CheckSameDimension(result, a);

            for(var r = 0; r < a.Rows; r++)
                for(var c = 0; c < a.Cols; c++)
                    result[r, c] = a[r, c] * s;
        }
Example #10
0
        /// <summary>
        /// return a / b
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Matrix Divide(Matrix a, Matrix b) {
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");
            CheckSameDimension(a, b);
            var result = new Matrix(a.Rows, b.Cols);
            result.Divide(a, b);

            return result;
        }
Example #11
0
        /// <summary>
        /// return a * s
        /// </summary>
        /// <param name="a"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Matrix Multiply(Matrix a, double s) {
            a.ShouldNotBeNull("a");

            var result = new Matrix(a.Rows, a.Cols);
            result.Multiply(a, s);
            return result;
        }
Example #12
0
        /// <summary>
        /// result = a * b
        /// </summary>
        /// <param name="result"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void Multiply(this Matrix result, Matrix a, Matrix b) {
            result.ShouldNotBeNull("result");
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");

            Guard.Assert(result.Rows == a.Rows, "result.Rows is not equals a.Rows, [{0}] <> [{1}]", result.Rows, a.Rows);
            Guard.Assert(result.Cols == b.Cols, "result.Cols is not equals b.Cols, [{0}] <> [{1}]", result.Cols, b.Cols);

            result.Initialize();

            for(var r = 0; r < a.Rows; r++)
                for(var c = 0; c < b.Cols; c++)
                    for(var k = 0; k < a.Cols; k++)
                        result[r, c] += a[r, k] * b[k, c];
        }
Example #13
0
        /// <summary>
        /// return a * b
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Matrix Multiply(Matrix a, Matrix b) {
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");
            Guard.Assert(a.Cols == b.Rows, "Matrix dimension is not match. a.Cols equals b.Rows for Multipling.");

            var result = new Matrix(a.Rows, b.Cols);
            result.Multiply(a, b);

            return result;
        }
Example #14
0
        /// <summary>
        /// result = a  b
        /// </summary>
        /// <param name="result"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void Subtract(this Matrix result, Matrix a, Matrix b) {
            result.ShouldNotBeNull("result");
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");
            CheckSameDimension(a, b);
            CheckSameDimension(a, result);

            for(var r = 0; r < a.Rows; r++)
                for(var c = 0; c < a.Cols; c++)
                    result[r, c] = a[r, c] - b[r, c];
        }
Example #15
0
        /// <summary>
        /// a - b
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Matrix Subtract(Matrix a, Matrix b) {
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");
            CheckSameDimension(a, b);

            var result = CreateMatrix(a.Rows, a.Cols);
            result.Subtract(a, b);

            return result;
        }