📄 gsl-design.texi
字号:
optimization. These comments also apply to arguments passed by valuewhich should be made @code{const} when that is meaningful.@node Pseudo-templates, Arbitrary Constants, Constness, Design@section Pseudo-templatesThere are some pseudo-template macros available in @file{templates_on.h}and @file{templates_off.h}. See a directory link @file{block} fordetails on how to use them. Use sparingly, they are a bit of anightmare, but unavoidable in places.In particular, the convention is: templates are used for operations on"data" only (vectors, matrices, statistics, sorting). This is intendedto cover the case where the program must interface with an externaldata-source which produces a fixed type. e.g. a big array of char'sproduced by an 8-bit counter.All other functions can use double, for floating point, or theappropriate integer type for integers (e.g. unsigned long int for randomnumbers). It is not the intention to provide a fully templated versionof the library. That would be "putting a quart into a pint pot". To summarize, almosteverything should be in a "natural type" which is appropriate fortypical usage, and templates are there to handle a few cases where it isunavoidable that other data-types will be encountered.For floating point work "double" is considered a "natural type". Thissort of idea is a part of the C language.@node Arbitrary Constants, Test suites, Pseudo-templates, Design@section Arbitrary ConstantsAvoid arbitrary constants.For example, don't hard code "small" values like '1e-30', '1e-100' or@code{10*GSL_DBL_EPSILON} into the routines. This is not appropriatefor a general purpose library.Compute values accurately using IEEE arithmetic. If errors arepotentially significant then error terms should be estimated reliablyand returned to the user, by analytically deriving an error propagationformula, not using guesswork.A careful consideration of the algorithm usually shows that arbitraryconstants are unnecessary, and represent an important parameter whichshould be accessible to the user.For example, consider the following code:@exampleif (residual < 1e-30) @{ return 0.0; /* residual is zero within round-off error */@}@end example@noindentThis should be rewritten as,@example return residual;@end example@noindentin order to allow the user to determine whether the residual is significant or not.The only place where it is acceptable to use constants like@code{GSL_DBL_EPSILON} is in function approximations, (e.g. taylorseries, asymptotic expansions, etc). In these cases it is not anarbitrary constant, but an inherent part of the algorithm.@node Test suites, Compilation, Arbitrary Constants, Design@section Test suitesThe implementor of each module should provide a reasonable test suitefor the routines.The test suite should be a program that uses the library and checks theresult against known results, or invokes the library several times anddoes a statistical analysis on the results (for example in the case ofrandom number generators).Ideally the one test program per directory should aim for 100% pathcoverage of the code. Obviously it would be a lot of work to reallyachieve this, so prioritize testing on the critical parts and useinspection for the rest. Test all the error conditions by explicitlyprovoking them, because we consider it a serious defect if the functiondoes not return an error for an invalid parameter. N.B. Don't bother totest for null pointers -- it's sufficient for the library to segfault ifthe user provides an invalid pointer.The tests should be deterministic. Use the @code{gsl_test} functionsprovided to perform separate tests for each feature with a separateoutput PASS/FAIL line, so that any failure can be uniquely identified.Use realistic test cases with 'high entropy'. Tests on simple valuessuch as 1 or 0 may not reveal bugs. For example, a test using a valueof @math{x=1} will not pick up a missing factor of @math{x} in the code.Similarly, a test using a value of @math{x=0} will not pick any missingterms involving @math{x} in the code. Use values like @math{2.385} toavoid silent failures. If your test uses multiple values make sure there are no simplerelations between them that could allow bugs to be missed through silentcancellations.If you need some random floats to put in the test programs use @code{od -f/dev/random} as a source of inspiration.Don't use @code{sprintf} to create output strings in the tests. It cancause hard to find bugs in the test programs themselves. The functions@code{gsl_test_...} support format string arguments so use theseinstead.@node Compilation, Thread-safety, Test suites, Design@section CompilationMake sure everything compiles cleanly. Use the strict compilationoptions for extra checking.@smallexamplemake CFLAGS="-ansi -pedantic -Werror -W -Wall -Wtraditional -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wstrict-prototypes -fshort-enums -fno-common -Wmissing-prototypes -Wnested-externs -Dinline= -g -O4"@end smallexample@noindentAlso use @code{checkergcc} to check for memory problems on the stack andthe heap. It's the best memory checking tool. If checkergcc isn'tavailable then Electric Fence will check the heap, which is better thanno checking.There is a new tool @code{valgrind} for checking memory access. Testthe code with this as well.Make sure that the library will also compile with C++ compilers(g++). This should not be too much of a problem if you have been writingin ANSI C.@node Thread-safety, Legal issues, Compilation, Design@section Thread-safetyThe library should be usable in thread-safe programs. All the functionsshould be thread-safe, in the sense that they shouldn't use staticvariables.We don't require everything to be completely thread safe, but anythingthat isn't should be obvious. For example, some global variables areused to control the overall behavior of the library (range-checkingon/off, function to call on fatal error, etc). Since these are accesseddirectly by the user it is obvious to the multi-threaded programmer thatthey shouldn't be modified by different threads.There is no need to provide any explicit support for threads(e.g. locking mechanisms etc), just to avoid anything which would makeit impossible for someone to call a GSL routine from a multithreadedprogram.@node Legal issues, Non-UNIX portability, Thread-safety, Design@section Legal issues@itemize @bullet@itemEach contributor must make sure her code is under the GNU General PublicLicense (GPL). This means getting a disclaimer from your employer.@itemWe must understand ownership of existing code and algorithms. @itemEach contributor can retain ownership of their code, or sign it over toFSF as they prefer. In the event of any legal uncertainty code willsimply be removed from the library.There is a standard disclaimer in the GPL (take a look at it). The morespecific you make your disclaimer the more likely it is that it will beaccepted by an employer. For example,@smallexampleYoyodyne, Inc., hereby disclaims all copyright interest in the software`GNU Scientific Library - Legendre Functions' (routines for computinglegendre functions numerically in C) written by James Hacker.<signature of Ty Coon>, 1 April 1989Ty Coon, President of Vice@end smallexample@item Obviously: don't use or translate non-free code. In particular don't copy or translate code from @cite{Numerical Recipes}or @cite{ACM TOMS}.Numerical Recipes is under a strict license and is not free software.The publishers Cambridge University Press claim copyright on all aspectsof the book and the code, including function names, variable names andordering of mathematical subexpressions. Routines in GSL should notrefer to Numerical Recipes or be based on it in any way. The ACM algorithms published in TOMS (Transactions on MathematicalSoftware) are not public domain, even though they are distributed on theinternet -- the ACM uses a special non-commercial license which is notcompatible with the GPL. The details of this license can be found on thecover page of ACM Transactions on Mathematical Software or on the ACMWebsite.Only use code which is explicitly under a free license: GPL or PublicDomain. If there is no license on the code then this does not mean itis public domain -- an explicit statement is required (see the DebianFree Software Guidelines for details). If in doubt check with theauthor.@itemI @strong{think} one can reference algorithms from classic books onnumerical analysis.@end itemize@node Non-UNIX portability, Compatibility with other libraries, Legal issues, Design@section Non-UNIX portabilityThere is good reason to make this library work on non-UNIX systems. Itis probably safe to ignore DOS and only worry about windows95/windowsNTportability (so filenames can be long, I think).On the other hand, nobody should be forced to use non-UNIX systems fordevelopment.The best solution is probably to issue guidelines for portability, likesaying "don't use XYZ unless you absolutely have to". Then the Windowspeople will be able to do their porting.@node Compatibility with other libraries, Parallelism, Non-UNIX portability, Design@section Compatibility with other librariesWe do not regard compatibility with other numerical libraries as apriority.However, other libraries, such as Numerical Recipes, are widely used.If somebody writes the code to allow drop-in replacement of theselibraries it would be useful to people. If it is done, it would be as aseparate wrapper that can be maintained and shipped separately.There is a separate issue of system libraries, such as BSD math libraryand functions like @code{expm1}, @code{log1p}, @code{hypot}. Thefunctions in this library are available on nearly every platform (butnot all). In this case, it is best to write code in terms of these nativefunctions to take advantage of the vendor-supplied system library (forexample log1p is a machine instruction on the Intel x86). The libraryalso provides portable implementations e.g. @code{gsl_hypot} which areused as an automatic fall back via autoconf when necessary. See theusage of @code{hypot} in @file{gsl/complex/math.c}, the implementationof @code{gsl_hypot} and the corresponding parts of files@file{configure.in} and @file{config.h.in} as an example.@node Parallelism, Precision, Compatibility with other libraries, Design@section ParallelismWe don't intend to provide support for parallelism within the libraryitself. A parallel library would require a completely different designand would carry overhead that other applications do not need.@node Precision, Miscellaneous, Parallelism, Design@section PrecisionFor algorithms which use cutoffs or other precision-related terms pleaseexpress these in terms of GSL_DBL_EPSILON and GSL_DBL_MIN, or powers orcombinations of these. This makes it easier to port the routines todifferent precisions.@node Miscellaneous, , Precision, Design@section MiscellaneousDon't use the letter @code{l} as a variable name --- it is difficult todistinguish from the number @code{1}. (This seems to be a favorite inold Fortran programs).Final tip: one perfect routine is better than any number of routinescontaining errors.@node Copying, , Design, Top@unnumbered Copying The subroutines and source code in the @value{GSL} package are "free";this means that everyone is free to use them and free to redistributethem on a free basis. The @value{GSL}-related programs are not in thepublic domain; they are copyrighted and there are restrictions on theirdistribution, but these restrictions are designed to permit everythingthat a good cooperating citizen would want to do. What is not allowedis to try to prevent others from further sharing any version of theseprograms that they might get from you. Specifically, we want to make sure that you have the right to giveaway copies of the programs that relate to @value{GSL}, that you receivesource code or else can get it if you want it, that you can change theseprograms or use pieces of them in new free programs, and that you knowyou can do these things. To make sure that everyone has such rights, we have to forbid you todeprive anyone else of these rights. For example, if you distributecopies of the @value{GSL}-related code, you must give the recipients allthe rights that you have. You must make sure that they, too, receive orcan get the source code. And you must tell them their rights. Also, for our own protection, we must make certain that everyonefinds out that there is no warranty for the programs that relate to@value{GSL}. If these programs are modified by someone else and passedon, we want their recipients to know that what they have is not what wedistributed, so that any problems introduced by others will not reflecton our reputation. The precise conditions of the licenses for the programs currentlybeing distributed that relate to @value{GSL} are found in the GeneralPublic Licenses that accompany them.@c @printindex cp@c @node Function Index@c @unnumbered Function Index@c @printindex fn@c @node Variable Index@c @unnumbered Variable Index@c @printindex vr@c @node Type Index@c @unnumbered Type Index@c @printindex tp@bye
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -