MyraMath
permute


Source: tests/sparse/permute.cpp

1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
11 // For making random permutations p/q
12 #include <algorithm>
13 
14 // Containers.
15 #include <myramath/dense/Vector.h>
19 
20 // Algorithms.
22 #include <myramath/dense/swaps.h>
24 #include <myramath/sparse/gemv.h>
28 
29 // Reporting.
30 #include <tests/myratest.h>
31 
32 using namespace myra;
33 
34 namespace {
35 
36 void test(int I, int J, int N)
37  {
38  // Make random A and x.
39  auto A = SparseMatrix<double>::random(I,J,N);
40  auto x = Vector<double>::random(J);
41  // Make random permutation P
42  auto p = Permutation::random(I);
43  auto P = p.make_SparseMatrix<double>();
44  // Make random permutation Q
45  auto q = Permutation::random(J);
46  auto Q = q.make_SparseMatrix<double>();
47  // Permute A into P'*A*Q
48  auto PAQ = permute(p,A,q);
49  // Verify P'*A*Q*b = PAQ*b
50  auto b1 = transpose(P)*(A*(Q*x));
51  auto b2 = PAQ*x;
52  double error = frobenius(b1-b2);
53  myra::out() << "|P'*(A*(Q*x)) - (P'*A*Q)*x| = " << error << std::endl;
54  }
55 
56 } // namespace
57 
58 ADD_TEST("permute","[sparse]")
59  {
60  test(0,0,0);
61  test(1,5,3);
62  test(7,1,2);
63  test(5,5,15);
64  test(111,142,4273);
65  }
Returns a transposed copy of a SparseMatrix.
Interface class for representing subranges of dense Vector&#39;s.
Routines for computing Frobenius norms of various algebraic containers.
static SparseMatrix< Number > random(int I, int J, int N)
Generates a random SparseMatrix with size IxJ and (approximately) N nonzeros.
Definition: SparseMatrix.cpp:493
General purpose compressed-sparse-column (CSC) container.
static Vector< Number > random(int N)
Generates a random Vector of specified size.
Definition: Vector.cpp:276
Definition: syntax.dox:1
Routines related to swap sequences, often used during pivoting.
Signatures for sparse matrix * dense vector multiplies. All delegate to gemm() under the hood...
Returns a vector of int&#39;s, over [min,max)
Container for either a column vector or row vector (depends upon the usage context) ...
Given a SparseMatrix A and permutations P and Q, returns P&#39;*A*Q.
Aggregates a (perm, iperm, swaps) triple into a vocabulary type.
static Permutation random(int N)
Generates a random Matrix of specified size.
Definition: Permutation.cpp:188
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

|P'*(A*(Q*x)) - (P'*A*Q)*x| = 0
|P'*(A*(Q*x)) - (P'*A*Q)*x| = 0
|P'*(A*(Q*x)) - (P'*A*Q)*x| = 0
|P'*(A*(Q*x)) - (P'*A*Q)*x| = 0
|P'*(A*(Q*x)) - (P'*A*Q)*x| = 5.02725e-15


Go back to Summary of /test programs.