CoCoALib Examples

ex-empty.C This is a template file for example programs. The program itself does nothing whatsoever.
Make a copy of this file (called "foo.C", say) and put your code 
inside the procedure "program".                                  
To compile your file in the examples directory just do this:       
  make foo                                                         


ex-AlexanderDual.C Program showing how to compute the Alexander Dual of a mon'l sqfr ideal.
It offers the function "PrimaryDecomposition" for a mon'l sqfr ideal
which will be included in CoCoALib (sooner or later).


ex-ApproxPts1.C A short example showing how to use the approximate point preprocessing algorithms. See the file ex-ApproxPt1.in for a sample input.
ex-BuildInfo.C This is a very short example showing what you can do with BuildInfo.
BuildInfo::PrintAll gives important information in the (enormously 
unlikely :-) event that you need to report a bug in CoCoALib. 


ex-DivMask1.C This example program shows the use of DivMasks. See also the example program ex-PPWithMask1.C
We show how to use DivMasks directly -- it is a bit tedious!             
See also the example ex-PPWithMask1.C, which has a friendlier interface. 
We show how the various different DivMaskRules can be selected.          


ex-DivMask2.C This example program performs some divisibility tests on PPs using DivMasks; it measures the effectiveness of the various DivMaskRules. See ex-PPWithMask2 for some similar timing tests.
This example program illustrates the effectiveness of the various    
DivMaskRules when using DivMasks as a quick non-divisibility check.  
Using DivMasks to check divisibility we can obtain two answers:      
'Surely not' or 'Maybe'.  The intention here is to see how often     
'Maybe' really means 'Yes, divisible'.  We see that the effectiveness
depends also on the presence of unused indeterminate in the PPMonoid.


ex-factor1.C This example shows how to interpret the result of a factorization.
This example shows how to interpret the result of a factorization. 
It creates a ring element (belonging to a polynomial ring), and    
factorizes it.  The result is a "factorization object".  We show 
how to access the various parts of this object.                    


ex-GMPAllocator.C Program to find lengths of 3n+1 sequences. (uses CoCoA::GMPAllocator)
This example shows how a CoCoA::GMPAllocator can be used.  If you do     
computations involving many small big-integers (up to about 40 decimal   
digits) then the custom allocator may give slightly better run-time      
performance -- you must compile with debugging turned off!               
The crucial command is the very first line of "program" just comment it 
out and recompile to use the default system allocator for GMP values.    


ex-hilbert1.C This example is just for testing the Hilbert code. It might disappear as soon as HilbertSeries is included in CoCoALib.
This code also shows how to create the "chess examples".           


ex-io.C Program to demonstrate changing the global i/o channels.
This example shows how to change the global output channel GlobalOutput. 
The most important point is that the variable  out  is at top-level      
inside program().  If it were inside the then-block, the ofstream        
would be destroyed as soon as control leaves the then-block; and at      
that point the global output stream would contain a dangling reference.  


ex-matrix1.C Example program illustrating the creation of matrices, and some some operations on them.
Please note that the matrix code is still rather young. 


ex-module1.C Example program illustrating the creation of free modules, and some operations on them.
Please note that the module code is still rather young. 


ex-MVT.C Example of use of the Mayer-Vietoris trees.
Example of use of the Mayer-Vietoris trees.  


ex-NF.C Example program illustrating an implementation of Normal Remainder wrt a list of polynomials. If the list is a Groebner Basis, NR returns the Normal Form.
This is just an example!  If you want to compute Normal Forms 
you should use the library function "NF".                   


ex-NumTheory1.C This program illustrates the use of some basic number theoretic functions.
This programs show how to use some of the basic number theoretic functions.  
Many of the examples use machine integers for convenience, but the functions 
work equally well with ZZ values (except NextPrime and PrevPrime).           


ex-OrderingGrading1.C Predefined and user-defined orderings and gradings on PPMonoid and PolyRing.
Each ordering is degree-compatible with grading over Z^GradingDim 
i.e. the grading is given by the first GradingDim rows            
of the ordering matrix.                                           


ex-PolyInput1.C This program shows a way to read a polynomial from the input.
ex-PolyIterator1.C Program showing how to iterate through "sparse" polynomials.
ex-PolyIterator2.C Program showing how to homogenize a sparse polynomial using iterators.
This is just an example!  If you want to homogenize polynomials  
you should use the library function "homog".                   


ex-PolyRing1.C Example showing how to create some simple polynomial rings. It also shows some of the operations specific to elements of PolyRings. See also ex-ring2.C
This example program exhibits two things: various ways of creating 
polynomial rings, and several operations specific to elements of a 
polynomial ring.                                                   
In the procedure "program" there are examples of creating        
polynomial rings.                                                  
In the procedure "SomeComputations" there are brief examples of  
the operations specific to elements of PolyRings (e.g. deg).       
See also ex-ring2.C.                                               


ex-PolyRing2.C Example showing how to write polynomials using "monomial" See also ex-PolyRing1.C
Because of C priority on operator "^" we cannot overload it  
to define powers, as a consequence writing power-products can  
be quite tedious or difficult to read.                         
Here we show how to write a simple function to create exponent 
vectors to be passed to "monomial".                          
I wish there were a better way to initialise a C++ vector...   


ex-PPMonoidElem1.C Example of use of power products and PPMonoids. Program exhibiting most functions on power products.
ex-PPMonoidElem2.C Example of use of power products in different PPMonoids. Program exhibiting timings of the different implementations.
The implementations of PPMonoids are optimized for different uses:  
PPMonoidEv:   stores the Exponent vector                            
              good for accessing the exponents, slow for ordering   
PPMonoidOv:   stores the Order vector                               
              good for ordering, slow for accessing the exponents   
PPMonoidEvOv: stores the Exponent vector and the Order vector       
              good for accessing the exponents and for ordering     
              uses more memory and takes more time to assign        
PPMonoidEvZZ: stores the Exponent vector at ZZ's                    
              necessary if you use big exponents (>2^10)            
              obviously slow   


ex-PPWithMask1.C This example program shows the use of PPWithMasks for testing divisibility between PPs. Compare with ex-DivMask1.C.
We show how to use PPWithMasks for testing divisibility.      
This program is very similar to ex-DivMask1.C.  The main      
differences are that this program is shorter and clearer, and 
it does a proper job of testing divisibility (even when the   
 useless null rule is used).                                  


ex-PPWithMask2.C This example program performs some divisibility speed tests using PPWithMasks; it shows the difference in speed which can be achieved using various DivMaskRules.
This example program test the speed of the divisibility test  
on values of type PPwithMask.  The main aim is to show that   
different DivMaskRules can produces differing behaviour, and  
which rule is best depends on the problem (e.g. few indets    
and high degrees, or many indets and low degrees).  Here we   
see also that unused indets can affect the speed.             
In case you're interested, the program ex-DivMask2 measures   
the effectiveness of the various DivMaskRules.                


ex-QQ1.C Program illustrating basic use of QQ values (i.e. rational numbers) showing that they can be used with a natural syntax.
Program giving example of basic arithmetic with exact rational numbers 
represented as values of type QQ.  The syntax recalls that used for    
the built-in C++ numerical types.  Emphasis is on convenience rather   
utmost execution speed.  To understand better the difference between   
a QQ value and an element of the ring RingQ, contrast this example     
with ex-RingQ1.C.                                                      


ex-QuotientBasis.C Program showing how to compute a quotient basis of a 0-dimensional ideal.
It offers the function "QuotientBasisRec"          
which will be included in CoCoALib (sooner or later).


ex-RandomBitStream1.C This program illustrates use of the pseudo-random bit generator of CoCoALib. The bits are independent, identically distributed; each with equal probability of being true or false. It is also possible to generate biased bits. See RandomLongStream if you want random (machine) integers.
CoCoALib offers a pseudorandom bit generator.  The generator can be   
seeded when it is created; this allows different pseudorandom         
sequences to be produced, though the sequence is completely determined
by the initial seed value.  The `prob' function filters a random bit  
sequence to produce `true' with a specified probability.              


ex-RandomLongStream1.C This program illustrates use of the pseudo-random number generator of CoCoALib. The numbers are independent and uniformly distributed in the given range; both ends of the range are reachable. See RandomBitStream if you want random bits.
CoCoALib offers a way to make uniform pseudo-random number generators.     
When creating the generator you must specify the upper and lower bounds for
the random numbers which wll be generated (both bounds may be generated).  
A generator seeded when it is created; this allows different pseudo-random 
sequences to be produced, though the sequence is completely determined by  
the initial seed value.                                                    


ex-ring1.C This example program shows how to use the rings Z and Q, and how to perform various operations on ring elements (RingElem).
Use of the fundamental rings Z and Q.                                
Creation of ring elements (C++ type RingElem).                       
Operations allowed on elements of the same ring:                     
  zero(R) and one(R)                                                 
  a + b, a - b, a * b                                                
  -a                                                                 
  a = b   (assignment)                                               
  a == b  and  a != b  (comparison)                                  
  IsZero(a), IsOne(a), IsMinusOne(a)                                 
Moreover other operations might be allowed, for example:             
  a > b   if the ring is ordered                                     
  a / b   if exact division is possible (and implemented!)           
See ex-RingHom*.C for how to move elements from one ring to another. 


ex-ring2.C This example program shows how to create various types of ring, and several operations one can perform on rings (e.g. querying their properties).
This example creates several different sorts of ring,            
and then calls PrintRingInfo on each one.                        
PrintRingInfo calls various functions to obtain information      
about each ring passed to it.  Naturally, some query functions   
make sense only for certain types of ring (e.g. NumIndets(R)).   


ex-RingElem1.C Example showing operations on RingElem for a ring or a PolyRing.
This is a long list of function calls from different rings.  


ex-RingFp1.C Inefficient program to compute sqrt(2) modulo a given number. Simple example using finite fields or integers modulo N.
The program asks the user for the value of N, it creates the     
ring of integers mod N, and finally uses "brute force" to find 
all square-roots of 2 modulo N.                                  


ex-RingFp2.C Example program showing how to create small prime finite fields implemented in a specific manner -- normally it is best to let the function NewZmod choose automatically the best implementation.
The program asks the user for the characteristic N, and then tries 
different ways of making Z mod N.


ex-RingHom1.C The example in this file shows how to create and use some homomorphisms between rings.
CanonicalHom is an easy way to make these homomorphisms:  
R --> R/I,    R --> R[x],   R --> FractionField(R),       
R --> R,      Q --> R,      Z --> R,                      
PolyAlgebraHom makes the R-algebra homomorphisms:         
R[x] --> R,   R[x] --> R[y]                               


ex-RingHom2.C Operations between elements of different rings are not allowed but we can use homomorphisms to map the elements into the same ring.
The example in this file shows how to create and use some      
homomorphisms between rings.  In particular, it gives a simple 
example of mixed ring arithmetic: the user must map all values 
into a single ring before combining them arithmetically.       


ex-RingHom3.C This program shows how we define a ring homomorphism to perform a change of coordinates in a polynomial ring.
ex-RingHom4.C This program shows how we define a ring homomorphism to evaluate polynomials.
ex-RingHom5.C The example in this file shows how to create and use some homomorphisms between rings.
We compute these polynomials (with parameters) in some rings: 
f = (2*a/3-1)*x[0] + 1/a;  g = x[0]-a;                        


ex-RingQ1.C Some simple computations with rational numbers. This example illustrates how to create the field of rational numbers. It shows that we can compute 7/3 in Q but not in Z. It shows how to map an integer into a rational number.
Familiarize yourself with the example ex-RingZ1.C before proceeding.  
As C++ does not natively have any rings, we must construct them from  
scratch.                                                              


ex-RingZ1.C This is a basic example about the creation and use of the ring of integers. It illustrates the CoCoALib "philosophy" of first creating a ring, and then computing with values in that ring. The C++ commands for performing arithmetic on RingElems have a natural syntax (except we cannot use ^ for powers). It warns about "mixed ring arithmetic", which is forbidden in CoCoALib.
To calculate with elements of a ring we must first create the   
ring, then we can create C++ objects of type RingElem which     
belong to the ring -- a RingElem can change its value but not   
the ring to which it belongs.                                   


ex-RingTwinFloat1.C Example showing some features of RingTwinFloat. Program to explore the precision offered by RingTwinFloat
ex-RingTwinFloat2.C Program exhibiting a way of using ever higher precisions... This example shows how failure can be a success: this pathological computation produces the same wrong result when using normal floating point arithmetic at any given finite precision! However, since twin floats are self-checking, we detect that there is a problem.
Example showing iterative increase of precision using RingTwinFloat    
until the answer is found (or some maximum precision is reached).      
This program will always fail to find the limit: J-M Muller's sequence 
actually converges to 6 (rather slowly), however it is pathological    
because it converges to 100 using any finite precision arithmetic.     
RingTwinFloat detects the onset of pathological convergence to 100, and
throws an InsufficientPrecision exception.                             


ex-RingTwinFloat3.C Program showing how some RingTwinFloat values can have more precision than that requested.
Example showing that certain RingTwinFloat values may have a precision  
higher than that requested.  In this case we request 64 bits (i.e.      
about 19 decimal digits), but are able to remove the nine most          
significant digits and still have a result with the requested precision.
So the original value of the variable third did in fact have at         
least 93 bits correct (i.e. about 28 decimal digits).                   


ex-RingWeyl1.C An example about RingWeyl, the interface is not quite settled yet.
All these examples about RingWeyl will probably be merged into one.


ex-RingWeyl2.C An example about RingWeyl, the interface is not quite settled yet.
This shows a computation of a Groebner Basis.
All these examples about RingWeyl will probably be merged into one.


ex-RingWeyl3.C An example about RingWeyl, the interface is not quite settled yet.
This shows a computation of a Groebner Basis.
All these examples about RingWeyl will probably be merged into one.


ex-RingWeyl4.C An example about RingWeyl, the interface is not quite settled yet.
This shows a computation of a Groebner Basis.
All these examples about RingWeyl will probably be merged into one.


ex-RingWeyl5.C An example about RingWeyl, the interface is not quite settled yet.
This shows a computation of a Groebner Basis.
All these examples about RingWeyl will probably be merged into one.


ex-symbol1.C Creation of symbols, and some simple operations on them.
Symbols are used to give print names to indeterminates.  Their main   
use is as an argument to a pseudo-ctor for a PPMonoid or a polynomial 
ring -- see examples for those types.                                 


ex-ZZ-prime1.C Program to find a 'big' prime number, suitable for use in cryptographic applications (with some more primality testing).
This program illustrates that ZZ values can be used much like normal C++ ints 
except that there is a almost no limit on the magnitude of the values. 
It shows use of some more advanced functions for ZZ values.            
NB If you need extreme efficiency then use the GMP library directly.   


ex-ZZ1.C Program showing basic use of ZZ values: creation, printing, and some simple arithmetic.
This program illustrates basic operations on ZZ values, showing that they  
can be used much like normal C++ ints except that there is a almost no     
limit on the magnitude of the values.                                      
NB If you need extreme efficiency then use the GMP library directly.       
Contrast this example with ex-RingZ1.                                      


ex-ZZ2.C Program to calculate the modular inverse of a number. You must give as input the modulus and the residue; the residue need not be reduced.
This program illustrates that ZZ values can be used much like normal C++    
ints except that there is a almost no limit on the magnitude of the values. 
NB If you need extreme efficiency then use the GMP library directly.        
Contrast this example with ex-RingZ1.