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(); } }
/// <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)); }
/// <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)); }
/// <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); }
public void Dscal(double alpha, DoubleMatrix2D A) { A.Assign(F1.Mult(alpha)); }
/// <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); }
/// <summary> /// A = A<sup>s</sup> <=> 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)); }
/// <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); }
/// <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)); }
/// <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); }
/// <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)); }
public void Dcopy(DoubleMatrix2D A, DoubleMatrix2D B) { B.Assign(A); }
public void Daxpy(double alpha, DoubleMatrix2D A, DoubleMatrix2D B) { B.Assign(A, F2.PlusMult(alpha)); }
public void Assign(DoubleMatrix2D A, DoubleMatrix2D B, Cern.Colt.Function.DoubleDoubleFunction function) { A.Assign(B, function); }
/// <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); }
/// <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)); }
/// <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); }
/// <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)); }
/// <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)); }
/// <summary> /// A = A<sup>B</sup> <=> 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); }
/// <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); }
/// <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)); }
/// <summary> /// Linear algebraic matrix power; <i>B = A<sup>k</sup> <==> B = A*A*...*A</i>. /// <ul> /// <li><i>p >= 1: B = A*A*...*A</i>.</li> /// <li><i>p == 0: B = identity matrix</i>.</li> /// <li><i>p < 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); }