]> www.ginac.de Git - cln.git/blob - doc/cln_11.html
* All Files have been modified for inclusion of namespace cln;
[cln.git] / doc / cln_11.html
1 <HTML>
2 <HEAD>
3 <!-- Created by texi2html 1.56k from cln.texi on 28 August 2000 -->
4
5 <TITLE>CLN, a Class Library for Numbers - 11. Using the library</TITLE>
6 </HEAD>
7 <BODY>
8 Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_10.html">previous</A>, <A HREF="cln_12.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
9 <P><HR><P>
10
11
12 <H1><A NAME="SEC64" HREF="cln_toc.html#TOC64">11. Using the library</A></H1>
13
14 <P>
15 For the following discussion, we will assume that you have installed
16 the CLN source in <CODE>$CLN_DIR</CODE> and built it in <CODE>$CLN_TARGETDIR</CODE>.
17 For example, for me it's <CODE>CLN_DIR="$HOME/cln"</CODE> and
18 <CODE>CLN_TARGETDIR="$HOME/cln/linuxelf"</CODE>. You might define these as
19 environment variables, or directly substitute the appropriate values.
20
21
22
23
24 <H2><A NAME="SEC65" HREF="cln_toc.html#TOC65">11.1 Compiler options</A></H2>
25 <P>
26 <A NAME="IDX333"></A>
27
28
29 <P>
30 Until you have installed CLN in a public place, the following options are
31 needed:
32
33
34 <P>
35 When you compile CLN application code, add the flags
36
37 <PRE>
38    -I$CLN_DIR/include -I$CLN_TARGETDIR/include
39 </PRE>
40
41 <P>
42 to the C++ compiler's command line (<CODE>make</CODE> variable CFLAGS or CXXFLAGS).
43 When you link CLN application code to form an executable, add the flags
44
45 <PRE>
46    $CLN_TARGETDIR/src/libcln.a
47 </PRE>
48
49 <P>
50 to the C/C++ compiler's command line (<CODE>make</CODE> variable LIBS).
51
52
53 <P>
54 If you did a <CODE>make install</CODE>, the include files are installed in a
55 public directory (normally <CODE>/usr/local/include</CODE>), hence you don't
56 need special flags for compiling. The library has been installed to a
57 public directory as well (normally <CODE>/usr/local/lib</CODE>), hence when
58 linking a CLN application it is sufficient to give the flag <CODE>-lcln</CODE>.
59
60
61
62
63 <H2><A NAME="SEC66" HREF="cln_toc.html#TOC66">11.2 Compatibility to old CLN versions</A></H2>
64 <P>
65 <A NAME="IDX334"></A>
66 <A NAME="IDX335"></A>
67
68
69 <P>
70 As of CLN version 1.1 all non-macro identifiers were hidden in namespace
71 <CODE>cln</CODE> in order to avoid potential name clashes with other C++
72 libraries. If you have an old application, you will have to manually
73 port it to the new scheme. The following principles will help during
74 the transition:
75
76 <UL>
77 <LI>
78
79 All headers are now in a separate subdirectory. Instead of including
80 <CODE>cl_</CODE><VAR>something</VAR><CODE>.h</CODE>, include
81 <CODE>cln/</CODE><VAR>something</VAR><CODE>.h</CODE> now.
82 <LI>
83
84 All public identifiers (typenames and functions) have lost their
85 <CODE>cl_</CODE> prefix.  Exceptions are all the typenames of number types,
86 (cl_N, cl_I, cl_MI, ...), rings, symbolic types (cl_string,
87 cl_symbol) and polynomials (cl_UP_<VAR>type</VAR>).  (This is because their
88 names would not be mnemonic enough once the namespace <CODE>cln</CODE> is
89 imported. Even in a namespace we favor <CODE>cl_N</CODE> over <CODE>N</CODE>.)
90 <LI>
91
92 All public <EM>functions</EM> that had by a <CODE>cl_</CODE> in their name still
93 carry that <CODE>cl_</CODE> if it is intrinsic part of a typename (as in
94 <CODE>cl_I_to_int ()</CODE>).
95 </UL>
96
97 <P>
98 When developing other libraries, please keep in mind not to import the
99 namespace <CODE>cln</CODE> in one of your public header files by saying
100 <CODE>using namespace cln;</CODE>. This would propagate to other applications
101 and can cause name clashes there.
102
103
104
105
106 <H2><A NAME="SEC67" HREF="cln_toc.html#TOC67">11.3 Include files</A></H2>
107 <P>
108 <A NAME="IDX336"></A>
109 <A NAME="IDX337"></A>
110
111
112 <P>
113 Here is a summary of the include files and their contents.
114
115
116 <DL COMPACT>
117
118 <DT><CODE>&#60;cln/object.h&#62;</CODE>
119 <DD>
120 General definitions, reference counting, garbage collection.
121 <DT><CODE>&#60;cln/number.h&#62;</CODE>
122 <DD>
123 The class cl_number.
124 <DT><CODE>&#60;cln/complex.h&#62;</CODE>
125 <DD>
126 Functions for class cl_N, the complex numbers.
127 <DT><CODE>&#60;cln/real.h&#62;</CODE>
128 <DD>
129 Functions for class cl_R, the real numbers.
130 <DT><CODE>&#60;cln/float.h&#62;</CODE>
131 <DD>
132 Functions for class cl_F, the floats.
133 <DT><CODE>&#60;cln/sfloat.h&#62;</CODE>
134 <DD>
135 Functions for class cl_SF, the short-floats.
136 <DT><CODE>&#60;cln/ffloat.h&#62;</CODE>
137 <DD>
138 Functions for class cl_FF, the single-floats.
139 <DT><CODE>&#60;cln/dfloat.h&#62;</CODE>
140 <DD>
141 Functions for class cl_DF, the double-floats.
142 <DT><CODE>&#60;cln/lfloat.h&#62;</CODE>
143 <DD>
144 Functions for class cl_LF, the long-floats.
145 <DT><CODE>&#60;cln/rational.h&#62;</CODE>
146 <DD>
147 Functions for class cl_RA, the rational numbers.
148 <DT><CODE>&#60;cln/integer.h&#62;</CODE>
149 <DD>
150 Functions for class cl_I, the integers.
151 <DT><CODE>&#60;cln/io.h&#62;</CODE>
152 <DD>
153 Input/Output.
154 <DT><CODE>&#60;cln/complex_io.h&#62;</CODE>
155 <DD>
156 Input/Output for class cl_N, the complex numbers.
157 <DT><CODE>&#60;cln/real_io.h&#62;</CODE>
158 <DD>
159 Input/Output for class cl_R, the real numbers.
160 <DT><CODE>&#60;cln/float_io.h&#62;</CODE>
161 <DD>
162 Input/Output for class cl_F, the floats.
163 <DT><CODE>&#60;cln/sfloat_io.h&#62;</CODE>
164 <DD>
165 Input/Output for class cl_SF, the short-floats.
166 <DT><CODE>&#60;cln/ffloat_io.h&#62;</CODE>
167 <DD>
168 Input/Output for class cl_FF, the single-floats.
169 <DT><CODE>&#60;cln/dfloat_io.h&#62;</CODE>
170 <DD>
171 Input/Output for class cl_DF, the double-floats.
172 <DT><CODE>&#60;cln/lfloat_io.h&#62;</CODE>
173 <DD>
174 Input/Output for class cl_LF, the long-floats.
175 <DT><CODE>&#60;cln/rational_io.h&#62;</CODE>
176 <DD>
177 Input/Output for class cl_RA, the rational numbers.
178 <DT><CODE>&#60;cln/integer_io.h&#62;</CODE>
179 <DD>
180 Input/Output for class cl_I, the integers.
181 <DT><CODE>&#60;cln/input.h&#62;</CODE>
182 <DD>
183 Flags for customizing input operations.
184 <DT><CODE>&#60;cln/output.h&#62;</CODE>
185 <DD>
186 Flags for customizing output operations.
187 <DT><CODE>&#60;cln/malloc.h&#62;</CODE>
188 <DD>
189 <CODE>malloc_hook</CODE>, <CODE>free_hook</CODE>.
190 <DT><CODE>&#60;cln/abort.h&#62;</CODE>
191 <DD>
192 <CODE>cl_abort</CODE>.
193 <DT><CODE>&#60;cln/condition.h&#62;</CODE>
194 <DD>
195 Conditions/exceptions.
196 <DT><CODE>&#60;cln/string.h&#62;</CODE>
197 <DD>
198 Strings.
199 <DT><CODE>&#60;cln/symbol.h&#62;</CODE>
200 <DD>
201 Symbols.
202 <DT><CODE>&#60;cln/proplist.h&#62;</CODE>
203 <DD>
204 Property lists.
205 <DT><CODE>&#60;cln/ring.h&#62;</CODE>
206 <DD>
207 General rings.
208 <DT><CODE>&#60;cln/null_ring.h&#62;</CODE>
209 <DD>
210 The null ring.
211 <DT><CODE>&#60;cln/complex_ring.h&#62;</CODE>
212 <DD>
213 The ring of complex numbers.
214 <DT><CODE>&#60;cln/real_ring.h&#62;</CODE>
215 <DD>
216 The ring of real numbers.
217 <DT><CODE>&#60;cln/rational_ring.h&#62;</CODE>
218 <DD>
219 The ring of rational numbers.
220 <DT><CODE>&#60;cln/integer_ring.h&#62;</CODE>
221 <DD>
222 The ring of integers.
223 <DT><CODE>&#60;cln/numtheory.h&#62;</CODE>
224 <DD>
225 Number threory functions.
226 <DT><CODE>&#60;cln/modinteger.h&#62;</CODE>
227 <DD>
228 Modular integers.
229 <DT><CODE>&#60;cln/V.h&#62;</CODE>
230 <DD>
231 Vectors.
232 <DT><CODE>&#60;cln/GV.h&#62;</CODE>
233 <DD>
234 General vectors.
235 <DT><CODE>&#60;cln/GV_number.h&#62;</CODE>
236 <DD>
237 General vectors over cl_number.
238 <DT><CODE>&#60;cln/GV_complex.h&#62;</CODE>
239 <DD>
240 General vectors over cl_N.
241 <DT><CODE>&#60;cln/GV_real.h&#62;</CODE>
242 <DD>
243 General vectors over cl_R.
244 <DT><CODE>&#60;cln/GV_rational.h&#62;</CODE>
245 <DD>
246 General vectors over cl_RA.
247 <DT><CODE>&#60;cln/GV_integer.h&#62;</CODE>
248 <DD>
249 General vectors over cl_I.
250 <DT><CODE>&#60;cln/GV_modinteger.h&#62;</CODE>
251 <DD>
252 General vectors of modular integers.
253 <DT><CODE>&#60;cln/SV.h&#62;</CODE>
254 <DD>
255 Simple vectors.
256 <DT><CODE>&#60;cln/SV_number.h&#62;</CODE>
257 <DD>
258 Simple vectors over cl_number.
259 <DT><CODE>&#60;cln/SV_complex.h&#62;</CODE>
260 <DD>
261 Simple vectors over cl_N.
262 <DT><CODE>&#60;cln/SV_real.h&#62;</CODE>
263 <DD>
264 Simple vectors over cl_R.
265 <DT><CODE>&#60;cln/SV_rational.h&#62;</CODE>
266 <DD>
267 Simple vectors over cl_RA.
268 <DT><CODE>&#60;cln/SV_integer.h&#62;</CODE>
269 <DD>
270 Simple vectors over cl_I.
271 <DT><CODE>&#60;cln/SV_ringelt.h&#62;</CODE>
272 <DD>
273 Simple vectors of general ring elements.
274 <DT><CODE>&#60;cln/univpoly.h&#62;</CODE>
275 <DD>
276 Univariate polynomials.
277 <DT><CODE>&#60;cln/univpoly_integer.h&#62;</CODE>
278 <DD>
279 Univariate polynomials over the integers.
280 <DT><CODE>&#60;cln/univpoly_rational.h&#62;</CODE>
281 <DD>
282 Univariate polynomials over the rational numbers.
283 <DT><CODE>&#60;cln/univpoly_real.h&#62;</CODE>
284 <DD>
285 Univariate polynomials over the real numbers.
286 <DT><CODE>&#60;cln/univpoly_complex.h&#62;</CODE>
287 <DD>
288 Univariate polynomials over the complex numbers.
289 <DT><CODE>&#60;cln/univpoly_modint.h&#62;</CODE>
290 <DD>
291 Univariate polynomials over modular integer rings.
292 <DT><CODE>&#60;cln/timing.h&#62;</CODE>
293 <DD>
294 Timing facilities.
295 <DT><CODE>&#60;cln/cln.h&#62;</CODE>
296 <DD>
297 Includes all of the above.
298 </DL>
299
300
301
302 <H2><A NAME="SEC68" HREF="cln_toc.html#TOC68">11.4 An Example</A></H2>
303
304 <P>
305 A function which computes the nth Fibonacci number can be written as follows.
306 <A NAME="IDX338"></A>
307
308
309
310 <PRE>
311 #include &#60;cln/integer.h&#62;
312 #include &#60;cln/real.h&#62;
313 using namespace cln;
314
315 // Returns F_n, computed as the nearest integer to
316 // ((1+sqrt(5))/2)^n/sqrt(5). Assume n&#62;=0.
317 const cl_I fibonacci (int n)
318 {
319         // Need a precision of ((1+sqrt(5))/2)^-n.
320         cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
321         cl_R sqrt5 = sqrt(cl_float(5,prec));
322         cl_R phi = (1+sqrt5)/2;
323         return round1( expt(phi,n)/sqrt5 );
324 }
325 </PRE>
326
327 <P>
328 Let's explain what is going on in detail.
329
330
331 <P>
332 The include file <CODE>&#60;cln/integer.h&#62;</CODE> is necessary because the type
333 <CODE>cl_I</CODE> is used in the function, and the include file <CODE>&#60;cln/real.h&#62;</CODE>
334 is needed for the type <CODE>cl_R</CODE> and the floating point number functions.
335 The order of the include files does not matter.  In order not to write out
336 <CODE>cln::</CODE><VAR>foo</VAR> we can safely import the whole namespace <CODE>cln</CODE>.
337
338
339 <P>
340 Then comes the function declaration. The argument is an <CODE>int</CODE>, the
341 result an integer. The return type is defined as <SAMP>`const cl_I'</SAMP>, not
342 simply <SAMP>`cl_I'</SAMP>, because that allows the compiler to detect typos like
343 <SAMP>`fibonacci(n) = 100'</SAMP>. It would be possible to declare the return
344 type as <CODE>const cl_R</CODE> (real number) or even <CODE>const cl_N</CODE> (complex
345 number). We use the most specialized possible return type because functions
346 which call <SAMP>`fibonacci'</SAMP> will be able to profit from the compiler's type
347 analysis: Adding two integers is slightly more efficient than adding the
348 same objects declared as complex numbers, because it needs less type
349 dispatch. Also, when linking to CLN as a non-shared library, this minimizes
350 the size of the resulting executable program.
351
352
353 <P>
354 The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest
355 integer. In order to get a correct result, the absolute error should be less
356 than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)).
357 To this end, the first line computes a floating point precision for sqrt(5)
358 and phi.
359
360
361 <P>
362 Then sqrt(5) is computed by first converting the integer 5 to a floating point
363 number and than taking the square root. The converse, first taking the square
364 root of 5, and then converting to the desired precision, would not work in
365 CLN: The square root would be computed to a default precision (normally
366 single-float precision), and the following conversion could not help about
367 the lacking accuracy. This is because CLN is not a symbolic computer algebra
368 system and does not represent sqrt(5) in a non-numeric way.
369
370
371 <P>
372 The type <CODE>cl_R</CODE> for sqrt5 and, in the following line, phi is the only
373 possible choice. You cannot write <CODE>cl_F</CODE> because the C++ compiler can
374 only infer that <CODE>cl_float(5,prec)</CODE> is a real number. You cannot write
375 <CODE>cl_N</CODE> because a <SAMP>`round1'</SAMP> does not exist for general complex
376 numbers.
377
378
379 <P>
380 When the function returns, all the local variables in the function are
381 automatically reclaimed (garbage collected). Only the result survives and
382 gets passed to the caller.
383
384
385 <P>
386 The file <CODE>fibonacci.cc</CODE> in the subdirectory <CODE>examples</CODE>
387 contains this implementation together with an even faster algorithm.
388
389
390
391
392 <H2><A NAME="SEC69" HREF="cln_toc.html#TOC69">11.5 Debugging support</A></H2>
393 <P>
394 <A NAME="IDX339"></A>
395
396
397 <P>
398 When debugging a CLN application with GNU <CODE>gdb</CODE>, two facilities are
399 available from the library:
400
401
402
403 <UL>
404 <LI>The library does type checks, range checks, consistency checks at
405
406 many places. When one of these fails, the function <CODE>cl_abort()</CODE> is
407 called. Its default implementation is to perform an <CODE>exit(1)</CODE>, so
408 you won't have a core dump. But for debugging, it is best to set a
409 breakpoint at this function:
410
411 <PRE>
412 (gdb) break cl_abort
413 </PRE>
414
415 When this breakpoint is hit, look at the stack's backtrace:
416
417 <PRE>
418 (gdb) where
419 </PRE>
420
421 <LI>The debugger's normal <CODE>print</CODE> command doesn't know about
422
423 CLN's types and therefore prints mostly useless hexadecimal addresses.
424 CLN offers a function <CODE>cl_print</CODE>, callable from the debugger,
425 for printing number objects. In order to get this function, you have
426 to define the macro <SAMP>`CL_DEBUG'</SAMP> and then include all the header files
427 for which you want <CODE>cl_print</CODE> debugging support. For example:
428 <A NAME="IDX340"></A>
429
430 <PRE>
431 #define CL_DEBUG
432 #include &#60;cln/string.h&#62;
433 </PRE>
434
435 Now, if you have in your program a variable <CODE>cl_string s</CODE>, and
436 inspect it under <CODE>gdb</CODE>, the output may look like this:
437
438 <PRE>
439 (gdb) print s
440 $7 = {&#60;cl_gcpointer&#62; = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
441   word = 134568800}}, }
442 (gdb) call cl_print(s)
443 (cl_string) ""
444 $8 = 134568800
445 </PRE>
446
447 Note that the output of <CODE>cl_print</CODE> goes to the program's error output,
448 not to gdb's standard output.
449
450 Note, however, that the above facility does not work with all CLN types,
451 only with number objects and similar. Therefore CLN offers a member function
452 <CODE>debug_print()</CODE> on all CLN types. The same macro <SAMP>`CL_DEBUG'</SAMP>
453 is needed for this member function to be implemented. Under <CODE>gdb</CODE>,
454 you call it like this:
455 <A NAME="IDX341"></A>
456
457 <PRE>
458 (gdb) print s
459 $7 = {&#60;cl_gcpointer&#62; = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
460   word = 134568800}}, }
461 (gdb) call s.debug_print()
462 (cl_string) ""
463 (gdb) define cprint
464 &#62;call ($1).debug_print()
465 &#62;end
466 (gdb) cprint s
467 (cl_string) ""
468 </PRE>
469
470 Unfortunately, this feature does not seem to work under all circumstances.
471 </UL>
472
473 <P><HR><P>
474 Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_10.html">previous</A>, <A HREF="cln_12.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
475 </BODY>
476 </HTML>