1 // Copyright (c) 2007  Anna Bigatti
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   "The example in this file shows how to create and use some \n"
12   "homomorphisms between rings.                              \n";
13 
14 const string LongDescription =
15   "CanonicalHom is an easy way to make these homomorphisms:  \n"
16   "R --> R/I,    R --> R[x],   R --> FractionField(R),       \n"
17   "R --> R,      QQ --> R,     ZZ --> R,                     \n"
18   "PolyAlgebraHom makes the R-algebra homomorphisms:         \n"
19   "R[x] --> R,   R[x] --> R[y]                               \n";
20 
21 //----------------------------------------------------------------------
22 
23 namespace CoCoA
24 {
25 
program()26   void program()
27   {
28     GlobalManager CoCoAFoundations;
29 
30     cout << ShortDescription << endl;
31 
32     // Create some rings
33     ring QQ = RingQQ();
34     ring Fp = NewZZmod(101);
35     PolyRing P = NewPolyRing(Fp, symbols("a,b"));   // P is Fp[a,b]
36 
37     cout << "-- CanonicalHom into P = " << P << endl;
38 
39     RingHom FpToP = CanonicalHom(Fp, P);  // same as CoeffEmbeddingHom(P)
40     RingHom QQToP = CanonicalHom(QQ, P);  // same as QQEmbeddingHom(P)
41     // NB!! QQToP is a partial homomorphism:
42     // e.g. we cannot compute QQToP(one(QQ)/101)
43 
44     RingElem a = RingElem(Fp, 13);
45     cout << "FpToP(" << a << ") = " << FpToP(a) << endl;
46 
47     RingElem q = RingElem(QQ, 5)/2;
48     cout << "QQToP(" << q << ") = " << QQToP(q) << endl;
49     cout << "  same as (RingElem calls CanonicalHom)" << endl;
50     cout << "  RingElem(P,q) = " << RingElem(P,q) << endl;
51     cout << endl;
52 
53     vector<RingElem> IndetImages;
54     IndetImages.push_back(RingElem(Fp,2));
55     IndetImages.push_back(RingElem(Fp,5));
56     cout << "-- PolyAlgebraHom:    "
57          << indet(P,0) << " |--> " << IndetImages[0] << "   "
58          << indet(P,1) << " |--> " << IndetImages[1] << endl;
59 
60     RingHom PToFp = PolyAlgebraHom(P, Fp, IndetImages);
61 
62     RingElem f = 100*indet(P,0) + indet(P,1);
63     cout << "PToFp(" << f << ") = " << PToFp(f) << endl;
64   }
65 
66 } // end of namespace CoCoA
67 
68 //----------------------------------------------------------------------
69 // Use main() to handle any uncaught exceptions and warn the user about them.
main()70 int main()
71 {
72   try
73   {
74     CoCoA::program();
75     return 0;
76   }
77   catch (const CoCoA::ErrorInfo& err)
78   {
79     cerr << "***ERROR***  UNCAUGHT CoCoA error";
80     ANNOUNCE(cerr, err);
81   }
82   catch (const std::exception& exc)
83   {
84     cerr << "***ERROR***  UNCAUGHT std::exception: " << exc.what() << endl;
85   }
86   catch(...)
87   {
88     cerr << "***ERROR***  UNCAUGHT UNKNOWN EXCEPTION" << endl;
89   }
90 
91   CoCoA::BuildInfo::PrintAll(cerr);
92   return 1;
93 }
94 
95 //----------------------------------------------------------------------
96 // RCS header/log in the next few lines
97 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/examples/ex-RingHom1.C,v 1.8 2018/09/28 15:54:03 abbott Exp $
98 // $Log: ex-RingHom1.C,v $
99 // Revision 1.8  2018/09/28 15:54:03  abbott
100 // Summary: Removed pseudo-ctors NewPolyRing which took just num of indets; now must specify their names
101 //
102 // Revision 1.7  2015/07/01 16:31:36  abbott
103 // Removed superfluous "using namespace CoCoA"
104 //
105 // Revision 1.6  2015/06/29 15:47:58  bigatti
106 // -- code in namespace CoCoA
107 //
108 // Revision 1.5  2013/03/15 15:19:26  bigatti
109 // -- added example for new RingElem ctor calling CanonicalHom
110 //
111 // Revision 1.4  2012/02/08 17:45:51  bigatti
112 // -- changed: Z,Q -> ZZ,QQ
113 //
114 // Revision 1.3  2010/12/17 16:07:54  abbott
115 // Ensured that all i/o in examples is on standard C++ streams
116 // (rather than GlobalInput(), etc).
117 //
118 // Revision 1.2  2009/01/23 16:08:41  abbott
119 // Clarified a comment.
120 //
121 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
122 // Imported files
123 //
124 // Revision 1.7  2007/03/08 10:24:16  bigatti
125 // -- simplest example
126 //
127 // Revision 1.6  2007/03/03 14:15:45  bigatti
128 // -- "foundations" renamed into "GlobalManager"
129 //
130 // Revision 1.5  2007/02/26 17:39:46  bigatti
131 // -- getting ready for unique ring Z: using NewZmod(N), NewRingQ()
132 //
133 // Revision 1.4  2007/02/12 16:11:12  bigatti
134 // -- added strings ShortDescription and LongDescription for indexing
135 //
136 // Revision 1.3  2007/02/10 18:44:04  cocoa
137 // Added "const" twice to each test and example.
138 // Eliminated dependency on io.H in several files.
139 // Improved BuildInfo, and added an example about how to use it.
140 // Some other minor cleaning.
141 //
142 // Revision 1.2  2007/01/18 14:34:18  cocoa
143 // -- example of alternative syntax
144 //
145 // Revision 1.1.1.1  2006/05/30 11:39:36  cocoa
146 // Imported files
147 //
148 // Revision 1.1.1.1  2005/10/17 10:46:53  cocoa
149 // Imported files
150 //
151 // Revision 1.3  2005/10/14 15:25:07  cocoa
152 // Major tidying and cleaning to small prime finite fields.
153 // Several consequential changes.  Improved their documentation.
154 //
155 // Added Makefile and script to include/CoCoA/ directory to
156 // keep library.H up to date.
157 //
158 // Revision 1.2  2005/09/22 18:04:17  cocoa
159 // It compiles; the tests run OK.  The examples compile.
160 // No documentation -- the mindless eurocrats have rendered
161 // me mindless too.
162 //
163 // Revision 1.1.1.1  2005/05/03 15:47:30  cocoa
164 // Imported files
165 //
166 // Revision 1.3  2005/04/27 16:14:56  cocoa
167 // Cleaned up example programs -- added "free use" permit.
168 // Changed a couple of ErrorInfo object names, and added
169 // ERR::NotTrueGCDDomain.
170 //
171 // Revision 1.2  2005/04/21 15:12:19  cocoa
172 // Revised NewPolyRing as Dag Arneson suggested (perhaps just an interim
173 // measure).
174 // Brought example programs up to date (new name for CoCoA error
175 // information objects).
176 //
177 // Revision 1.1.1.1  2005/01/27 15:12:13  cocoa
178 // Imported files
179 //
180 // Revision 1.3  2004/12/09 15:08:42  cocoa
181 // -- added log info
182 //
183 // Revision 1.2  2004/11/22 17:10:04  cocoa
184 // -- changed  "PolyRing" --> "AsPolyRing",  "FractionField" --> "AsFractionField"
185 //
186