public void Demo2()
        {
            foreach (var f in new[] { DoubleFactory2D.Dense, DoubleFactory2D.Sparse })
            {
                const DoubleMatrix2D N = null;
                DoubleMatrix2D       a = f.Make(2, 2, 1);
                DoubleMatrix2D       b = f.Make(4, 4, 2);
                DoubleMatrix2D       c = f.Make(4, 3, 3);
                DoubleMatrix2D       d = f.Make(2, 2, 4);
                var            parts1  = new[] { new[] { N, a, N }, new[] { b, N, c }, new[] { N, d, N } };
                DoubleMatrix2D matrix  = f.Compose(parts1);
                var            sm      = matrix.ToString();

                a.Assign(9);
                b.Assign(9);
                c.Assign(9);
                d.Assign(9);
                f.Decompose(parts1, matrix);
                var sa = a.ToString();
                var sb = b.ToString();
                var sc = c.ToString();
                var sd = d.ToString();
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// A = A * s <=> A[row,col] = A[row,col] * s
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Mult(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Mult(s));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// A = A - s <=> A[row,col] = A[row,col] - s
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Minus(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Minus(s));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// A[row,col] = A[row,col] < B[row,col] ? 1 : 0
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Less(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Less);
 }
Ejemplo n.º 5
0
 public void Dscal(double alpha, DoubleMatrix2D A)
 {
     A.Assign(F1.Mult(alpha));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// A[row,col] = System.Math.Abs(A[row,col])
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Abs(DoubleMatrix2D A)
 {
     return A.Assign(F1.Abs);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// A = A<sup>s</sup> &lt;=> A[row,col] = System.Math.Pow(A[row,col], s)
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Pow(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Pow(s));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// A = A + B <=> A[row,col] = A[row,col] + B[row,col]
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Plus(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Plus);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// A[row,col] = A[row,col] == s ? 1 : 0; ignores tolerance.
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Equals(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Equals(s));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// A = A / B <=> A[row,col] = A[row,col] / B[row,col]
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Div(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Div);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// A = A / s <=> A[row,col] = A[row,col] / s
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Div(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Div(s));
 }
Ejemplo n.º 12
0
 public void Dcopy(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     B.Assign(A);
 }
Ejemplo n.º 13
0
 public void Daxpy(double alpha, DoubleMatrix2D A, DoubleMatrix2D B)
 {
     B.Assign(A, F2.PlusMult(alpha));
 }
Ejemplo n.º 14
0
 public void Assign(DoubleMatrix2D A, DoubleMatrix2D B, Cern.Colt.Function.DoubleDoubleFunction function)
 {
     A.Assign(B, function);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// A = A * B <=> A[row,col] = A[row,col] * B[row,col]
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Mult(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Mult);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// A = -A <=> A[row,col] = -A[row,col]
 /// </summary>
 /// <param name="A"></param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Negate(DoubleMatrix2D A)
 {
     return A.Assign(F1.Mult(-1));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// A[row,col] = A[row,col] == B[row,col] ? 1 : 0; ignores tolerance.
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Equals(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Equals);
 }
Ejemplo n.º 18
0
 /// <summary>
 /// A = A + B*s <=> A[row,col] = A[row,col] + B[row,col]*s
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D PlusMult(DoubleMatrix2D A, DoubleMatrix2D B, double s)
 {
     return A.Assign(B, F2.PlusMult(s));
 }
Ejemplo n.º 19
0
 /// <summary>
 /// A[row,col] = A[row,col] > s ? 1 : 0
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Greater(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Greater(s));
 }
Ejemplo n.º 20
0
 /// <summary>
 /// A = A<sup>B</sup> &lt;=> A[row,col] = System.Math.Pow(A[row,col], B[row,col])
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Pow(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Pow);
 }
Ejemplo n.º 21
0
 /// <summary>
 /// A[row,col] = A[row,col] > B[row,col] ? 1 : 0
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="B">the matrix to stay unaffected.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Greater(DoubleMatrix2D A, DoubleMatrix2D B)
 {
     return A.Assign(B, F2.Greater);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// A[row,col] = A[row,col] < s ? 1 : 0
 /// </summary>
 /// <param name="A">the matrix to modify.</param>
 /// <param name="s">the scalar; can have any value.</param>
 /// <returns><i>A</i> (for convenience only).</returns>
 public static DoubleMatrix2D Less(DoubleMatrix2D A, double s)
 {
     return A.Assign(F1.Less(s));
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Linear algebraic matrix power; <i>B = A<sup>k</sup> &lt;==> B = A*A*...*A</i>.
        /// <ul>
        /// <li><i>p &gt;= 1: B = A*A*...*A</i>.</li>
        /// <li><i>p == 0: B = identity matrix</i>.</li>
        /// <li><i>p &lt;  0: B = pow(inverse(A),-p)</i>.</li>
        /// </ul>
        /// Implementation: Based on logarithms of 2, memory usage minimized.
        /// </summary>
        /// <param name="A">the source matrix; must be square; stays unaffected by this operation.</param>
        /// <param name="p">the exponent, can be any number.</param>
        /// <returns><i>B</i>, a newly constructed result matrix; storage-independent of <i>A</i>.</returns>
        ///<exception cref="ArgumentException">if <i>!property().isSquare(A)</i>.</exception>
        public static DoubleMatrix2D Pow(DoubleMatrix2D A, int p)
        {
            // matrix multiplication based on log2 method: A*A*....*A is slow, ((A * A)^2)^2 * ..D is faster
            // allocates two auxiliary matrices as work space

            IBlas blas = SmpBlas.smpBlas; // for parallel matrix mult; if not initialized defaults to sequential blas

            Property.DEFAULT.CheckSquare(A);
            if (p < 0)
            {
                A = Inverse(A);
                p = -p;
            }
            if (p == 0)
            {
                return(DoubleFactory2D.Dense.Identity(A.Rows));
            }
            DoubleMatrix2D T = A.Like(); // temporary

            if (p == 1)
            {
                return(T.Assign(A));         // safes one auxiliary matrix allocation
            }
            if (p == 2)
            {
                blas.Dgemm(false, false, 1, A, A, 0, T); // mult(A,A); // safes one auxiliary matrix allocation
                return(T);
            }

            int k = Cern.Colt.Bitvector.QuickBitVector.MostSignificantBit(p); // index of highest bit in state "true"

            /*
             * this is the naive version:
             * DoubleMatrix2D B = A.Copy();
             * for (int i=0; i<p-1; i++) {
             *  B = mult(B,A);
             * }
             * return B;
             */

            // here comes the optimized version:
            //cern.colt.Timer timer = new cern.colt.Timer().start();

            int i = 0;

            while (i <= k && (p & (1 << i)) == 0)
            {                                             // while (bit i of p == false)
              // A = mult(A,A); would allocate a lot of temporary memory
                blas.Dgemm(false, false, 1, A, A, 0, T);  // A.zMult(A,T);
                DoubleMatrix2D swap = A; A = T; T = swap; // swap A with T
                i++;
            }

            DoubleMatrix2D B = A.Copy();

            i++;
            for (; i <= k; i++)
            {
                // A = mult(A,A); would allocate a lot of temporary memory
                blas.Dgemm(false, false, 1, A, A, 0, T);  // A.zMult(A,T);
                DoubleMatrix2D swap = A; A = T; T = swap; // swap A with T

                if ((p & (1 << i)) != 0)
                {                                            // if (bit i of p == true)
                  // B = mult(B,A); would allocate a lot of temporary memory
                    blas.Dgemm(false, false, 1, B, A, 0, T); // B.zMult(A,T);
                    swap = B; B = T; T = swap;               // swap B with T
                }
            }
            //timer.stop().Display();
            return(B);
        }