📄 ap.english.html
字号:
<html><head><title>AP Library adapted for C++</title><style type="text/css"><!--h1 { font-family: Tahoma,sans-serif; font-size : larger; }h2 { font-family: Arial,sans-serif; font-size : 11pt; }h3 { font-family: Arial,sans-serif; font-size : 9pt; }.cond { color:blue; }.const { color:#222222; }.func { color:#111111; }--></style></head><body><h1>AP Library adapted for C++</h1><p align=justify>The document describes an AP library adapted for C++. The AP library for C++ contains a basic set of mathematical functions and collection classes needed to run the programs from the <a href="http://www.alglib.net/">ALGLIB</a> website.</p><h1>Compatibility</h1><p align=justify>This library must be compatible with any C++ compiler.</p><h1>Structure and Use</h1><p align=justify>The library includes the only module <code>ap.cpp</code> (with header file <code>ap.h</code>). Just add it to your project.</p><h1>AP Library Description</h1><font size=-1><a href="#intro">Introduction</a><br><a href="#conditionals">Conditional compilation settings</a><br><a href="#constants">Constants</a><br><a href="#functions">Functions</a><br><a href="#aperror">Class ap_error</a><br><a href="#arrays">Array classes</a><br><a href="#blas">Basic subroutines of linear algebra</a><br><a href="#complex">Class of complex numbers</a><br></font><a name="intro"><h1>Introduction</h1></a><p align=justify>In the header file <code>ap.h</code> the namespace <code>ap</code> is defined. It must be taken into account that the names of functions, constants and classes listed further should be prefixed by <i>ap::</i>.</p><a name="conditionals"><h1>Conditional Compillation Settings</h1></a><p align=justify><span class=cond>AP_ASSERT</span><br> This symbol enables checking of the array boundaries. If it is set by the "define" directive, then at each addressing to the dynamic array elements, the transferred index is verified for its correctness. In case of error the <code>ap_error</code> exception is thrown. Checking the array boundaries makes the program more reliable, but slows down the program operation.</p><p align=justify><span class=cond>NO_AP_ASSERT</span><br> This symbol disables checking of the array boundaries. If it is set by the "define" directive, then the index being outside the array boundaries is not checked when the dynamic array elements are addressed.</p><p align=justify><span class=cond>UNSAFE_MEM_COPY</span><br> The "define" directive that sets this symbol is disabled. Do not activate it. The library contains no documentation concerning this symbol.</p><a name="constants"><h1>Constants</h1></a><p align=justify><span class=const>machineepsilon</span><br> The constant represents the accuracy of machine operations, that is the minimum number for <code>1+machineepsilon≠1</code> in the given bit grid. The constant may be taken "oversized", that is real accuracy can be even higher.</p><p align=justify><span class=const>maxrealnumber</span><br> The constant represents the highest value of the positive real number, which could be represented on this machine. The constant may be taken "oversized", that is real boundary can be even higher.</p><p align=justify><span class=const>minrealnumber</span><br> The constant represents the lowest value of positive real number, which could be represented on this machine. The constant may be taken "oversized", that is real boundary can be even lower.</p><a name="functions"><h1>Functions</h1></a><p align=justify><span class=func><b>int</b> sign(<b>double</b> x)</span><br> Returns:<br> +1, if X>0<br> -1, if X<0<br> 0, if X=0</p><p align=justify><span class=func><b>double</b> randomreal()</span><br> Returns a random real number from half-interval [0,1).</p><p align=justify><span class=func><b>int</b> randominteger(<b>int</b> maxv) </span><br> Returns a random integer between 0 and maxv-1.</p><p align=justify><span class=func><b>double</b> round(<b>double</b> x)</span><br> Returns the nearest integer to x. If x is right in the middle between two integers, then the function result depends on the implementation.</p><p align=justify><span class=func><b>double</b> trunc(<b>double</b> x)</span><br> Truncates the fractional part of x.<br> trunc(1.3) = 1<br> trunc(-1.3)= -1</p><p align=justify><span class=func><b>double</b> pi()</span><br> Returns the constant π</p><p align=justify><span class=func><b>double</b> sqr(<b>double</b> x)</span><br> Returns x<sup>2</sup>.</p><p align=justify><span class=func><b>double</b> maxreal(<b>double</b> m1, <b>double</b> m2)</span><br> Returns the maximum of two real numbers.</p><p align=justify><span class=func><b>double</b> minreal(<b>double</b> m1, <b>double</b> m2)</span><br> Returns the minimum of two real numbers.</p><p align=justify><span class=func><b>int</b> maxint(<b>int</b> m1, <b>int</b> m2)</span><br> Returns the maximum of two integers.</p><p align=justify><span class=func><b>int</b> minint(<b>int</b> m1, <b>int</b> m2)</span><br> Returns the minimum of two integers.</p><a name="aperror"><h1>Class "ap_error"</h1></a><p align=justify>This is a class of exception which is thrown when different errors occur in the AP library, for example - if the array index is found incorrect when the array boundaries check is enabled. The current version of the class contains no fields and doesn't allow to find the cause for the exception generated.</p><a name="arrays"><h1>Array classes</h1></a><h2>Working with arrays</h2><p align=justify>First we will discuss general principles of working with array classes, then describe the classes and their methods.</p><p align=justify>Classes of the standard library allow operations with matrixes and vectors (one-dimensional and two-dimensional arrays) of variable size and with variable numeration of elements, that is, the array numeration can start at any number, end at any number and change dynamically. Because the array classes are templates, the arrays of the same dimension have the same set of member functions. And as the member functions of arrays with different dimensions differ only by the number of arguments, there is little difference between two-dimensional and one-dimenstional arrays. </p><p align=justify>Working with an array starts with the array creation. You should distinguish the creation of array class instance and the memory allocation for the array. When creating the class instance, you can use constructor without any parameters, that creates an empty array without any elements, or you can use copy and assignment constructors that copy one array into another. In case the array is created by the default constructor, it contains no elements and an attempt to address them may cause the program failure. If, during the copy operation, the source array has no memory allocated for the array elements, destination array will contain no elements either. If the source array has memory allocated for its elements, destination array will allocate the same amount of memory and copy the elements there. That is, the copy operation yields into two independent arrays with indentical contents.</p><p align=justify>After an empty array has been created, you should allocate the memory for its elements, using the <code:>setbounds</code:> method The method parameters set upper and lower boundaries of the array indexes. The upper boundary should be not less than the lower one. When called, memory for corresponding number of elements is allocated. The content of the created array elements is not defined and no suppositions should be made about it. If the <code:>setbounds</code:> method is called for the array with already allocated memory, then, after changing its parameters, the newly allocated elements also become undefined and the old content is deleted.</p><p align=justify>To address the array elements, an overloaded <code:>operator()</code:> is used. That is, the code addressing the element of array <code:>a</code:> with indexes <code:>a(i,j,k)</code:> will look like <code:>a(i,j,k)</code:>. Below is given an example of factorial array calculation, illustrating the work with arrays. </p><pre>integer_1d_array factarr(<b>int</b> n){ integer_1d_array result; result.setbounds(1,n); result(1) = 1; <b>for</b>(<b>int</b> i=2; i<=n; i++) result(i) = result(i-1)*i; <b>return</b> result;}</pre><h2>Class "template_1d_array"</h2><p align=justify>This class is a template of dynamical one-dimensional array with variable upper and lower boundaries. Based on this class, the following classes are constructed:</p><pre><b>typedef</b> template_1d_array<<b>int</b>> integer_1d_array;<b>typedef</b> template_1d_array<<b>double</b>> real_1d_array;<b>typedef</b> template_1d_array<<b>bool</b>> boolean_1d_array;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -