You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

431 lines
12 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. <HTML>
  2. <HEAD>
  3. <!-- Created by texi2html 1.56k from cln.texi on 19 May 2000 -->
  4. <TITLE>CLN, a Class Library for Numbers - 11. Using the library</TITLE>
  5. </HEAD>
  6. <BODY>
  7. 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>.
  8. <P><HR><P>
  9. <H1><A NAME="SEC64" HREF="cln_toc.html#TOC64">11. Using the library</A></H1>
  10. <P>
  11. For the following discussion, we will assume that you have installed
  12. the CLN source in <CODE>$CLN_DIR</CODE> and built it in <CODE>$CLN_TARGETDIR</CODE>.
  13. For example, for me it's <CODE>CLN_DIR="$HOME/cln"</CODE> and
  14. <CODE>CLN_TARGETDIR="$HOME/cln/linuxelf"</CODE>. You might define these as
  15. environment variables, or directly substitute the appropriate values.
  16. <H2><A NAME="SEC65" HREF="cln_toc.html#TOC65">11.1 Compiler options</A></H2>
  17. <P>
  18. <A NAME="IDX320"></A>
  19. <P>
  20. Until you have installed CLN in a public place, the following options are
  21. needed:
  22. <P>
  23. When you compile CLN application code, add the flags
  24. <PRE>
  25. -I$CLN_DIR/include -I$CLN_TARGETDIR/include
  26. </PRE>
  27. <P>
  28. to the C++ compiler's command line (<CODE>make</CODE> variable CFLAGS or CXXFLAGS).
  29. When you link CLN application code to form an executable, add the flags
  30. <PRE>
  31. $CLN_TARGETDIR/src/libcln.a
  32. </PRE>
  33. <P>
  34. to the C/C++ compiler's command line (<CODE>make</CODE> variable LIBS).
  35. <P>
  36. If you did a <CODE>make install</CODE>, the include files are installed in a
  37. public directory (normally <CODE>/usr/local/include</CODE>), hence you don't
  38. need special flags for compiling. The library has been installed to a
  39. public directory as well (normally <CODE>/usr/local/lib</CODE>), hence when
  40. linking a CLN application it is sufficient to give the flag <CODE>-lcln</CODE>.
  41. <H2><A NAME="SEC66" HREF="cln_toc.html#TOC66">11.2 Include files</A></H2>
  42. <P>
  43. <A NAME="IDX321"></A>
  44. <A NAME="IDX322"></A>
  45. <P>
  46. Here is a summary of the include files and their contents.
  47. <DL COMPACT>
  48. <DT><CODE>&#60;cl_object.h&#62;</CODE>
  49. <DD>
  50. General definitions, reference counting, garbage collection.
  51. <DT><CODE>&#60;cl_number.h&#62;</CODE>
  52. <DD>
  53. The class cl_number.
  54. <DT><CODE>&#60;cl_complex.h&#62;</CODE>
  55. <DD>
  56. Functions for class cl_N, the complex numbers.
  57. <DT><CODE>&#60;cl_real.h&#62;</CODE>
  58. <DD>
  59. Functions for class cl_R, the real numbers.
  60. <DT><CODE>&#60;cl_float.h&#62;</CODE>
  61. <DD>
  62. Functions for class cl_F, the floats.
  63. <DT><CODE>&#60;cl_sfloat.h&#62;</CODE>
  64. <DD>
  65. Functions for class cl_SF, the short-floats.
  66. <DT><CODE>&#60;cl_ffloat.h&#62;</CODE>
  67. <DD>
  68. Functions for class cl_FF, the single-floats.
  69. <DT><CODE>&#60;cl_dfloat.h&#62;</CODE>
  70. <DD>
  71. Functions for class cl_DF, the double-floats.
  72. <DT><CODE>&#60;cl_lfloat.h&#62;</CODE>
  73. <DD>
  74. Functions for class cl_LF, the long-floats.
  75. <DT><CODE>&#60;cl_rational.h&#62;</CODE>
  76. <DD>
  77. Functions for class cl_RA, the rational numbers.
  78. <DT><CODE>&#60;cl_integer.h&#62;</CODE>
  79. <DD>
  80. Functions for class cl_I, the integers.
  81. <DT><CODE>&#60;cl_io.h&#62;</CODE>
  82. <DD>
  83. Input/Output.
  84. <DT><CODE>&#60;cl_complex_io.h&#62;</CODE>
  85. <DD>
  86. Input/Output for class cl_N, the complex numbers.
  87. <DT><CODE>&#60;cl_real_io.h&#62;</CODE>
  88. <DD>
  89. Input/Output for class cl_R, the real numbers.
  90. <DT><CODE>&#60;cl_float_io.h&#62;</CODE>
  91. <DD>
  92. Input/Output for class cl_F, the floats.
  93. <DT><CODE>&#60;cl_sfloat_io.h&#62;</CODE>
  94. <DD>
  95. Input/Output for class cl_SF, the short-floats.
  96. <DT><CODE>&#60;cl_ffloat_io.h&#62;</CODE>
  97. <DD>
  98. Input/Output for class cl_FF, the single-floats.
  99. <DT><CODE>&#60;cl_dfloat_io.h&#62;</CODE>
  100. <DD>
  101. Input/Output for class cl_DF, the double-floats.
  102. <DT><CODE>&#60;cl_lfloat_io.h&#62;</CODE>
  103. <DD>
  104. Input/Output for class cl_LF, the long-floats.
  105. <DT><CODE>&#60;cl_rational_io.h&#62;</CODE>
  106. <DD>
  107. Input/Output for class cl_RA, the rational numbers.
  108. <DT><CODE>&#60;cl_integer_io.h&#62;</CODE>
  109. <DD>
  110. Input/Output for class cl_I, the integers.
  111. <DT><CODE>&#60;cl_input.h&#62;</CODE>
  112. <DD>
  113. Flags for customizing input operations.
  114. <DT><CODE>&#60;cl_output.h&#62;</CODE>
  115. <DD>
  116. Flags for customizing output operations.
  117. <DT><CODE>&#60;cl_malloc.h&#62;</CODE>
  118. <DD>
  119. <CODE>cl_malloc_hook</CODE>, <CODE>cl_free_hook</CODE>.
  120. <DT><CODE>&#60;cl_abort.h&#62;</CODE>
  121. <DD>
  122. <CODE>cl_abort</CODE>.
  123. <DT><CODE>&#60;cl_condition.h&#62;</CODE>
  124. <DD>
  125. Conditions/exceptions.
  126. <DT><CODE>&#60;cl_string.h&#62;</CODE>
  127. <DD>
  128. Strings.
  129. <DT><CODE>&#60;cl_symbol.h&#62;</CODE>
  130. <DD>
  131. Symbols.
  132. <DT><CODE>&#60;cl_proplist.h&#62;</CODE>
  133. <DD>
  134. Property lists.
  135. <DT><CODE>&#60;cl_ring.h&#62;</CODE>
  136. <DD>
  137. General rings.
  138. <DT><CODE>&#60;cl_null_ring.h&#62;</CODE>
  139. <DD>
  140. The null ring.
  141. <DT><CODE>&#60;cl_complex_ring.h&#62;</CODE>
  142. <DD>
  143. The ring of complex numbers.
  144. <DT><CODE>&#60;cl_real_ring.h&#62;</CODE>
  145. <DD>
  146. The ring of real numbers.
  147. <DT><CODE>&#60;cl_rational_ring.h&#62;</CODE>
  148. <DD>
  149. The ring of rational numbers.
  150. <DT><CODE>&#60;cl_integer_ring.h&#62;</CODE>
  151. <DD>
  152. The ring of integers.
  153. <DT><CODE>&#60;cl_numtheory.h&#62;</CODE>
  154. <DD>
  155. Number threory functions.
  156. <DT><CODE>&#60;cl_modinteger.h&#62;</CODE>
  157. <DD>
  158. Modular integers.
  159. <DT><CODE>&#60;cl_V.h&#62;</CODE>
  160. <DD>
  161. Vectors.
  162. <DT><CODE>&#60;cl_GV.h&#62;</CODE>
  163. <DD>
  164. General vectors.
  165. <DT><CODE>&#60;cl_GV_number.h&#62;</CODE>
  166. <DD>
  167. General vectors over cl_number.
  168. <DT><CODE>&#60;cl_GV_complex.h&#62;</CODE>
  169. <DD>
  170. General vectors over cl_N.
  171. <DT><CODE>&#60;cl_GV_real.h&#62;</CODE>
  172. <DD>
  173. General vectors over cl_R.
  174. <DT><CODE>&#60;cl_GV_rational.h&#62;</CODE>
  175. <DD>
  176. General vectors over cl_RA.
  177. <DT><CODE>&#60;cl_GV_integer.h&#62;</CODE>
  178. <DD>
  179. General vectors over cl_I.
  180. <DT><CODE>&#60;cl_GV_modinteger.h&#62;</CODE>
  181. <DD>
  182. General vectors of modular integers.
  183. <DT><CODE>&#60;cl_SV.h&#62;</CODE>
  184. <DD>
  185. Simple vectors.
  186. <DT><CODE>&#60;cl_SV_number.h&#62;</CODE>
  187. <DD>
  188. Simple vectors over cl_number.
  189. <DT><CODE>&#60;cl_SV_complex.h&#62;</CODE>
  190. <DD>
  191. Simple vectors over cl_N.
  192. <DT><CODE>&#60;cl_SV_real.h&#62;</CODE>
  193. <DD>
  194. Simple vectors over cl_R.
  195. <DT><CODE>&#60;cl_SV_rational.h&#62;</CODE>
  196. <DD>
  197. Simple vectors over cl_RA.
  198. <DT><CODE>&#60;cl_SV_integer.h&#62;</CODE>
  199. <DD>
  200. Simple vectors over cl_I.
  201. <DT><CODE>&#60;cl_SV_ringelt.h&#62;</CODE>
  202. <DD>
  203. Simple vectors of general ring elements.
  204. <DT><CODE>&#60;cl_univpoly.h&#62;</CODE>
  205. <DD>
  206. Univariate polynomials.
  207. <DT><CODE>&#60;cl_univpoly_integer.h&#62;</CODE>
  208. <DD>
  209. Univariate polynomials over the integers.
  210. <DT><CODE>&#60;cl_univpoly_rational.h&#62;</CODE>
  211. <DD>
  212. Univariate polynomials over the rational numbers.
  213. <DT><CODE>&#60;cl_univpoly_real.h&#62;</CODE>
  214. <DD>
  215. Univariate polynomials over the real numbers.
  216. <DT><CODE>&#60;cl_univpoly_complex.h&#62;</CODE>
  217. <DD>
  218. Univariate polynomials over the complex numbers.
  219. <DT><CODE>&#60;cl_univpoly_modint.h&#62;</CODE>
  220. <DD>
  221. Univariate polynomials over modular integer rings.
  222. <DT><CODE>&#60;cl_timing.h&#62;</CODE>
  223. <DD>
  224. Timing facilities.
  225. <DT><CODE>&#60;cln.h&#62;</CODE>
  226. <DD>
  227. Includes all of the above.
  228. </DL>
  229. <H2><A NAME="SEC67" HREF="cln_toc.html#TOC67">11.3 An Example</A></H2>
  230. <P>
  231. A function which computes the nth Fibonacci number can be written as follows.
  232. <A NAME="IDX323"></A>
  233. <PRE>
  234. #include &#60;cl_integer.h&#62;
  235. #include &#60;cl_real.h&#62;
  236. // Returns F_n, computed as the nearest integer to
  237. // ((1+sqrt(5))/2)^n/sqrt(5). Assume n&#62;=0.
  238. const cl_I fibonacci (int n)
  239. {
  240. // Need a precision of ((1+sqrt(5))/2)^-n.
  241. cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
  242. cl_R sqrt5 = sqrt(cl_float(5,prec));
  243. cl_R phi = (1+sqrt5)/2;
  244. return round1( expt(phi,n)/sqrt5 );
  245. }
  246. </PRE>
  247. <P>
  248. Let's explain what is going on in detail.
  249. <P>
  250. The include file <CODE>&#60;cl_integer.h&#62;</CODE> is necessary because the type
  251. <CODE>cl_I</CODE> is used in the function, and the include file <CODE>&#60;cl_real.h&#62;</CODE>
  252. is needed for the type <CODE>cl_R</CODE> and the floating point number functions.
  253. The order of the include files does not matter.
  254. <P>
  255. Then comes the function declaration. The argument is an <CODE>int</CODE>, the
  256. result an integer. The return type is defined as <SAMP>`const cl_I'</SAMP>, not
  257. simply <SAMP>`cl_I'</SAMP>, because that allows the compiler to detect typos like
  258. <SAMP>`fibonacci(n) = 100'</SAMP>. It would be possible to declare the return
  259. type as <CODE>const cl_R</CODE> (real number) or even <CODE>const cl_N</CODE> (complex
  260. number). We use the most specialized possible return type because functions
  261. which call <SAMP>`fibonacci'</SAMP> will be able to profit from the compiler's type
  262. analysis: Adding two integers is slightly more efficient than adding the
  263. same objects declared as complex numbers, because it needs less type
  264. dispatch. Also, when linking to CLN as a non-shared library, this minimizes
  265. the size of the resulting executable program.
  266. <P>
  267. The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest
  268. integer. In order to get a correct result, the absolute error should be less
  269. than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)).
  270. To this end, the first line computes a floating point precision for sqrt(5)
  271. and phi.
  272. <P>
  273. Then sqrt(5) is computed by first converting the integer 5 to a floating point
  274. number and than taking the square root. The converse, first taking the square
  275. root of 5, and then converting to the desired precision, would not work in
  276. CLN: The square root would be computed to a default precision (normally
  277. single-float precision), and the following conversion could not help about
  278. the lacking accuracy. This is because CLN is not a symbolic computer algebra
  279. system and does not represent sqrt(5) in a non-numeric way.
  280. <P>
  281. The type <CODE>cl_R</CODE> for sqrt5 and, in the following line, phi is the only
  282. possible choice. You cannot write <CODE>cl_F</CODE> because the C++ compiler can
  283. only infer that <CODE>cl_float(5,prec)</CODE> is a real number. You cannot write
  284. <CODE>cl_N</CODE> because a <SAMP>`round1'</SAMP> does not exist for general complex
  285. numbers.
  286. <P>
  287. When the function returns, all the local variables in the function are
  288. automatically reclaimed (garbage collected). Only the result survives and
  289. gets passed to the caller.
  290. <P>
  291. The file <CODE>fibonacci.cc</CODE> in the subdirectory <CODE>examples</CODE>
  292. contains this implementation together with an even faster algorithm.
  293. <H2><A NAME="SEC68" HREF="cln_toc.html#TOC68">11.4 Debugging support</A></H2>
  294. <P>
  295. <A NAME="IDX324"></A>
  296. <P>
  297. When debugging a CLN application with GNU <CODE>gdb</CODE>, two facilities are
  298. available from the library:
  299. <UL>
  300. <LI>The library does type checks, range checks, consistency checks at
  301. many places. When one of these fails, the function <CODE>cl_abort()</CODE> is
  302. called. Its default implementation is to perform an <CODE>exit(1)</CODE>, so
  303. you won't have a core dump. But for debugging, it is best to set a
  304. breakpoint at this function:
  305. <PRE>
  306. (gdb) break cl_abort
  307. </PRE>
  308. When this breakpoint is hit, look at the stack's backtrace:
  309. <PRE>
  310. (gdb) where
  311. </PRE>
  312. <LI>The debugger's normal <CODE>print</CODE> command doesn't know about
  313. CLN's types and therefore prints mostly useless hexadecimal addresses.
  314. CLN offers a function <CODE>cl_print</CODE>, callable from the debugger,
  315. for printing number objects. In order to get this function, you have
  316. to define the macro <SAMP>`CL_DEBUG'</SAMP> and then include all the header files
  317. for which you want <CODE>cl_print</CODE> debugging support. For example:
  318. <A NAME="IDX325"></A>
  319. <PRE>
  320. #define CL_DEBUG
  321. #include &#60;cl_string.h&#62;
  322. </PRE>
  323. Now, if you have in your program a variable <CODE>cl_string s</CODE>, and
  324. inspect it under <CODE>gdb</CODE>, the output may look like this:
  325. <PRE>
  326. (gdb) print s
  327. $7 = {&#60;cl_gcpointer&#62; = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
  328. word = 134568800}}, }
  329. (gdb) call cl_print(s)
  330. (cl_string) ""
  331. $8 = 134568800
  332. </PRE>
  333. Note that the output of <CODE>cl_print</CODE> goes to the program's error output,
  334. not to gdb's standard output.
  335. Note, however, that the above facility does not work with all CLN types,
  336. only with number objects and similar. Therefore CLN offers a member function
  337. <CODE>debug_print()</CODE> on all CLN types. The same macro <SAMP>`CL_DEBUG'</SAMP>
  338. is needed for this member function to be implemented. Under <CODE>gdb</CODE>,
  339. you call it like this:
  340. <A NAME="IDX326"></A>
  341. <PRE>
  342. (gdb) print s
  343. $7 = {&#60;cl_gcpointer&#62; = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
  344. word = 134568800}}, }
  345. (gdb) call s.debug_print()
  346. (cl_string) ""
  347. (gdb) define cprint
  348. &#62;call ($1).debug_print()
  349. &#62;end
  350. (gdb) cprint s
  351. (cl_string) ""
  352. </PRE>
  353. Unfortunately, this feature does not seem to work under all circumstances.
  354. </UL>
  355. <P><HR><P>
  356. 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>.
  357. </BODY>
  358. </HTML>