1 // Copyright (c) 2011  John Abbott
2 // This file is part of the CoCoALib suite of examples.
3 // You are free to use any part of this example in your own programs.
4 
5 #include "CoCoA/library.H"
6 
7 using namespace std;
8 
9 //----------------------------------------------------------------------
10 const string ShortDescription =
11   "Example program illustrating the creation of matrix views, and some  \n"
12   "effects of \"aliasing\".  Examples of matrix views include transposes\n"
13   "submatrices, block matrices, and concatenated matrices.              \n";
14 
15 const string LongDescription =
16   "Example program illustrating the creation of matrix views, and some    \n"
17   "effects of \"aliasing\", i.e. where there is more than one way to refer\n"
18   "to a single entry of the matrix.  We gives examples of creating various\n"
19   "views: ZeroMat, IdentityMat, transpose, submat, BlockMat2x2, ConcatHor \n"
20   "and ConcatVer.";
21 
22 
23 //----------------------------------------------------------------------
24 
25 namespace CoCoA
26 {
27 
program()28   void program()
29   {
30     GlobalManager CoCoAFoundations;
31 
32     cout << ShortDescription << endl;
33 
34     // We could use any ring for these examples, but have chosen ZZ for simplicity.
35     ring R = RingZZ();
36 
37     // Here is a normal 3x3 matrix (with entries 1,2,3, 4,5,6, 7,8,9)
38     // we shall use it later on.
39     matrix M = NewDenseMat(R,3,3);
40     SetEntry(M,0,0, 1);  SetEntry(M,0,1, 2);  SetEntry(M,0,2, 3);
41     SetEntry(M,1,0, 4);  SetEntry(M,1,1, 5);  SetEntry(M,1,2, 6);
42     SetEntry(M,2,0, 7);  SetEntry(M,2,1, 8);  SetEntry(M,2,2, 9);
43 
44 
45     // Zero matrix -- useful mainly in calls to BlockMat2x2 (see below)
46     ConstMatrix Zero2x3 = ZeroMat(R, 2,3);
47     cout << "*** ZERO MATRIX ***\n"
48          << "Here is a 2x3 zero matrix: " << Zero2x3 << endl;
49     cout << "It is constant; none of its entries may be assigned to." << endl
50          << endl;
51 
52 
53     // Identity matrix
54     ConstMatrix Id3x3 = IdentityMat(R, 3);
55     cout << "*** IDENTITY MATRIX ***\n"
56          << "Here is a 3x3 identity matrix: " << Id3x3 << endl
57          << "It is constant; none of its entries may be assigned to." << endl
58          << endl;
59 
60 
61     // Transpose -- entries are shared with original matrix.
62     cout << "*** TRANSPOSE MATRIX ***\n"
63          << "Our starting matrix is  M = " << M << endl;
64     MatrixView TrM = transpose(M);
65     cout << "transpose(M) = " << TrM << endl;
66 
67     cout << "If we change an entry in M, then TrM changes too:" << endl;
68     SetEntry(M,0,1, 99);
69     cout << "After setting M(0,1) = 99, the matrix M becomes: " << M << endl;
70     cout << "and its transpose changes correspondingly: TrM = " << TrM << endl;
71 
72     cout << "Similarly, changing an entry of TrM also changes M..." << endl;
73     SetEntry(TrM,1,0, 2);
74     cout << "After setting TrM(0,1) = 2, the matrix TrM becomes: " << TrM << endl
75          << "and the original matrix changes correspondingly: M = " << M << endl
76          << endl;
77 
78     cout << "If we don't wanted shared entries, we must make an explicit copy," << endl
79          << "e.g. by calling NewDenseMat(transpose(M))" << endl;
80     matrix TrM_copy = NewDenseMat(transpose(M));
81     SetEntry(TrM_copy,2,2, 999);
82     cout << "Changing an entry in the copy does not affect the original matrix:" << endl
83          << "TrM_copy has been changed to " << TrM_copy << endl
84          << "but this did not change the original M = " << M << endl
85          << endl;
86 
87 
88     // Submatrices -- entries are shared with parent matrix.
89     cout << "*** SUBMATRIX ***\n"
90          << "We shall create the submatrix view of M which comprises just its four corners." << endl;
91     vector<long> rows; rows.push_back(0); rows.push_back(2); // rows = [0,2]
92     vector<long> cols; cols.push_back(0); cols.push_back(2); // cols = [0,2]
93     MatrixView subm = submat(M, rows, cols);
94 
95     cout << "The submatrix is subm = " << subm << endl
96          << "as for the transpose, its entries are shared with the original matrix." << endl
97          << endl;
98 
99 
100     // Block matrices -- sticking 4 matrices together to make a bigger one.
101     cout << "*** BLOCK MATRIX ***\n";
102     MatrixView BlockM = BlockMat2x2(M, zeroes, zeroes, TrM);
103     cout << "We can stick 4 matrices together to make one larger one.  For\n"
104          << "example BlockMat2x2(M,Z,Z,TrM) where Z is a 3x3 zero matrix gives:\n"
105          << BlockM << endl;
106 
107     cout << "Like submat, BlockMat2x2 does not make copies of the original matrices\n"
108          << "it simply refers to them.  In our specific case we must be extra\n"
109          << "careful, since there is aliasing between M and TrM, so there are\n"
110          << "'hidden' connections between the entries of the block matrix.\n"
111          << "Changing the (0,1) entry will also change the (4,3) entry, like this:\n";
112     SetEntry(BlockM,0,1, -99);
113     cout << BlockM << endl;
114     cout << "Naturally, also the original matrix M has changed; we now have\n"
115          << "M = " << M << endl
116          << endl
117          << "We can avoid this phenomenon by making an explicit copy, as we did\n"
118          << "for the transpose (above)." << endl
119          << endl;
120 
121     // Another BlockMat2x2
122     long n = 4;
123     RingElem I = one(RingZZ());
124     RingElem Z = zero(RingZZ());
125     cout << "Another BlockMat2x2 " <<
126       BlockMat2x2(
127                   RowMat(vector<RingElem>(n,I)), RowMat(vector<RingElem>(1,I)),
128                   StdDegLexMat(n),               ColMat(vector<RingElem>(n,Z)));
129 
130 
131     // ConcatHor & ConcatVer join two matrices horizontally/vertically
132     cout << "*** ConcatHor & ConcatVer ***\n";
133     ConstMatrix Zero3x3 = ZeroMat(R, 3,3);
134     cout << "*** CONCATENATED MATRICES ***\n"
135          << "We can join two matrices horizontally or vertically:\n"
136          << "ConcatHor(M,Z) = " << ConcatHor(M,Zero3x3) << endl
137          << endl
138          << "ConcatVer(M,Z) = " << ConcatVer(M,Zero3x3) << endl
139          << "Like BlockMat2x2, these commands don't make copies of the original matrices." << endl;
140   }
141 
142 } // end of namespace CoCoA
143 
144 
145 // Use main() to handle any uncaught exceptions and warn the user about them.
main()146 int main()
147 {
148   try
149   {
150     CoCoA::program();
151     return 0;
152   }
153   catch (const CoCoA::ErrorInfo& err)
154   {
155     cerr << "***ERROR***  UNCAUGHT CoCoA error";
156     ANNOUNCE(cerr, err);
157   }
158   catch (const std::exception& exc)
159   {
160     cerr << "***ERROR***  UNCAUGHT std::exception: " << exc.what() << endl;
161   }
162   catch(...)
163   {
164     cerr << "***ERROR***  UNCAUGHT UNKNOWN EXCEPTION" << endl;
165   }
166 
167   CoCoA::BuildInfo::PrintAll(cerr);
168   return 1;
169 }
170 
171 //----------------------------------------------------------------------
172 // RCS header/log in the next few lines
173 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/examples/ex-matrix2.C,v 1.9 2020/03/06 17:39:01 bigatti Exp $
174 // $Log: ex-matrix2.C,v $
175 // Revision 1.9  2020/03/06 17:39:01  bigatti
176 // -- added another BlockMat2x2
177 //
178 // Revision 1.8  2015/11/30 21:53:55  abbott
179 // Summary: Major update to matrices for orderings (not yet complete, some tests fail)
180 //
181 // Revision 1.7  2015/06/26 15:34:48  abbott
182 // Summary: Moved code into namespace CoCoA (see redmine 739)
183 // Author: JAA
184 //
185 // Revision 1.6  2014/07/16 15:50:08  abbott
186 // Summary: Corrected type in printed mesg
187 // Author: JAA
188 //
189 // Revision 1.5  2013/05/31 15:05:22  bigatti
190 // changed BlockMat into BlockMat2x2
191 //
192 // Revision 1.4  2012/06/19 15:44:58  abbott
193 // Improved long and short descriptions.
194 //
195 // Revision 1.3  2012/02/08 17:52:17  bigatti
196 // -- changed: Z,Q -> ZZ,QQ
197 //
198 // Revision 1.2  2011/12/23 15:27:35  bigatti
199 // -- cleaning
200 //
201 // Revision 1.1  2011/12/23 15:06:53  abbott
202 // New example for matrix views
203 //
204 // Revision 1.7  2011/09/07 09:56:41  abbott
205 // Some minor improvements.
206 //
207 // Revision 1.6  2011/03/08 18:01:53  bigatti
208 // -- changed size_t into long
209 //
210 // Revision 1.5  2010/12/17 16:07:54  abbott
211 // Ensured that all i/o in examples is on standard C++ streams
212 // (rather than GlobalInput(), etc).
213 //
214 // Revision 1.4  2009/02/13 14:55:26  bigatti
215 // -- changed a lot: rearranged basic operations and added (Const)MatrixView
216 //
217 // Revision 1.3  2008/04/18 15:35:57  abbott
218 // (long overdue) Major revision to matrices
219 //
220 // Revision 1.2  2008/04/08 15:26:42  abbott
221 // Major revision to matrix implementation: added matrix views.
222 // Lots of changes.
223 //
224 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
225 // Imported files
226 //
227 // Revision 1.6  2007/03/03 14:15:45  bigatti
228 // -- "foundations" renamed into "GlobalManager"
229 //
230 // Revision 1.5  2007/02/26 17:40:34  bigatti
231 // -- getting ready for unique ring Z: using NewZmod(N), NewRingQ()
232 //
233 // Revision 1.4  2007/02/22 17:25:40  bigatti
234 // -- added printing of separators: "-------"
235 //
236 // Revision 1.3  2007/02/12 16:27:43  bigatti
237 // -- added strings ShortDescription and LongDescription for indexing
238 //
239 // Revision 1.2  2007/02/10 18:44:03  cocoa
240 // Added "const" twice to each test and example.
241 // Eliminated dependency on io.H in several files.
242 // Improved BuildInfo, and added an example about how to use it.
243 // Some other minor cleaning.
244 //
245 // Revision 1.1.1.1  2006/05/30 11:39:36  cocoa
246 // Imported files
247 //
248 // Revision 1.1.1.1  2005/10/17 10:46:53  cocoa
249 // Imported files
250 //
251 // Revision 1.2  2005/10/14 15:25:07  cocoa
252 // Major tidying and cleaning to small prime finite fields.
253 // Several consequential changes.  Improved their documentation.
254 //
255 // Added Makefile and script to include/CoCoA/ directory to
256 // keep library.H up to date.
257 //
258 // Revision 1.1.1.1  2005/05/03 15:47:30  cocoa
259 // Imported files
260 //
261 // Revision 1.6  2005/04/27 16:14:56  cocoa
262 // Cleaned up example programs -- added "free use" permit.
263 // Changed a couple of ErrorInfo object names, and added
264 // ERR::NotTrueGCDDomain.
265 //
266 // Revision 1.5  2005/04/21 15:12:19  cocoa
267 // Revised NewPolyRing as Dag Arneson suggested (perhaps just an interim
268 // measure).
269 // Brought example programs up to date (new name for CoCoA error
270 // information objects).
271 //
272 // Revision 1.4  2005/04/19 14:06:04  cocoa
273 // Added GPL and GFDL licence stuff.
274 //
275 // Revision 1.3  2005/03/30 15:30:43  cocoa
276 // -- added: computation of rank and determinant
277 // -- changed: declaration of I4000 as ConstMatrix
278 //
279 // Revision 1.2  2005/03/02 18:46:41  cocoa
280 // Added new types ConstRefMatrix, and RefMatrix following along
281 // the lines of ConstRefRingElem and RefRingElem.  The semantics
282 // should be a bit clearer now.
283 //
284 // Revision 1.1.1.1  2005/01/27 15:12:13  cocoa
285 // Imported files
286 //
287 // Revision 1.1  2004/11/29 16:50:28  cocoa
288 // -- first import
289 //
290