Ejemplo n.º 1
0
        /// <summary>
        /// Multiply vector a as column vector with vector b as row vector resulting a matrix [b.Dim x a.Dim]
        /// </summary>
        public static Matrix <double> MultiplyTransposed(this Vector <double> a, Vector <double> b)
        {
            var result = new double[b.Dim * a.Dim];

            double[] a0 = a.Data, a1 = b.Data;
            long     f1 = b.Origin, e1 = f1 + b.DSX, d1 = b.D;

            for (long i0 = a.Origin, ri = 0, e0 = i0 + a.DSX, d0 = a.D; i0 != e0; i0 += d0)
            {
                for (long i1 = f1; i1 != e1; i1 += d1, ri++)
                {
                    result[ri] = a0[i0] * a1[i1];
                }
            }
            return(Matrix.Create(result, b.Dim, a.Dim));
        }
Ejemplo n.º 2
0
        public static Matrix <double> Add(this Matrix <double> mat, double a)
        {
            var result = new double[mat.SX * mat.SY];

            var  data0 = mat.Data;
            long my0 = mat.DY;
            long mf0 = mat.FirstIndex;
            long ds0 = mat.DSX, d0 = mat.DX;

            for (long ri = 0, ye = mat.FirstIndex + mat.DSY, f0 = mat.FirstIndex, e0 = f0 + ds0;
                 f0 != ye; f0 += my0, e0 += my0)
            {
                for (long i0 = f0; i0 != e0; i0 += d0, ri++)
                {
                    result[ri] = data0[i0] + a;
                }
            }
            return(Matrix.Create(result, mat.SX, mat.SY));
        }
Ejemplo n.º 3
0
        public static Matrix <double> Add(this Matrix <double> m0, Matrix <double> m1)
        {
            if (m0.Dim != m1.Dim)
            {
                throw new InvalidOperationException(String.Format("Cannot add matrix A={0} and matrix B={1}", m0.Dim, m1.Dim));
            }

            var result = new double[m0.SX * m0.SY];

            var  data0 = m0.Data; var data1 = m1.Data;
            long mf0 = m0.FirstIndex, my0 = m0.DY, my1 = m1.DY;
            long ds0 = m0.DSX, d0 = m0.DX, d1 = m1.DX;

            for (long ye = mf0 + m0.DSY, f0 = mf0, f1 = m1.FirstIndex, ri = 0;
                 f0 != ye; f0 += my0, f1 += my1)
            {
                for (long xe = f0 + ds0, i0 = f0, i1 = f1; i0 != xe; i0 += d0, i1 += d1, ri++)
                {
                    result[ri] = data0[i0] + data1[i1];
                }
            }
            return(Matrix.Create(result, m0.SX, m0.SY));
        }