⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsl-design.texi

📁 开放gsl矩阵运算
💻 TEXI
📖 第 1 页 / 共 3 页
字号:
@node Using Return Values, Variable Names, Persistence, Design@section Using Return ValuesAlways assign a return value to a variable before using it.  This allowseasier debugging of the function, and inspection and modification of thereturn value.  If the variable is only needed temporarily then encloseit in a suitable scope.For example, instead of writing,@examplea = f(g(h(x,y)))@end example@noindentuse temporary variables to store the intermediate values,@example@{  double u = h(x,y);  double v = g(u);  a = f(v);@}@end example@noindentThese can then be inspected more easily in the debugger, and breakpointscan be placed more precisely.  The compiler will eliminate the temporaryvariables automatically when the program is compiled with optimization.@node Variable Names, Datatype widths, Using Return Values, Design@section Variable NamesTry to follow existing conventions for variable names,@table @code@item dimnumber of dimensions@item wpointer to workspace @item statepointer to state variable (use @code{s} if you need to save characters)@item resultpointer to result (output variable)@item abserrabsolute error @item relerrrelative error@item sizethe size of an array or vector e.g. double array[size] @item stridethe stride of a vector@item size1the number of rows in a matrix@item size2the number of columns in a matrix@item ngeneral integer number, e.g. number of elements of array, in fft, etc@item rrandom number generator (gsl_rng)@end table@node Datatype widths, size_t, Variable Names, Design@section Datatype widthsBe aware that in ANSI C the type @code{int} is only guaranteed toprovide 16-bits. It may provide more, but is not guaranteed to.Therefore if you require 32 bits you must use @code{long int}, whichwill have 32 bits or more.  Of course, on many platforms the type@code{int} does have 32 bits instead of 16 bits but we have to code tothe ANSI standard rather than a specific platform.@node size_t, Arrays vs Pointers, Datatype widths, Design@section size_tAll objects (blocks of memory, etc) should be measured in terms of a@code{size_t} type.  Therefore any iterations (e.g. @code{for(i=0; i<N;i++)}) should also use an index of type @code{size_t}.  If you need towrite a descending loop you have to be careful because @code{size_t} isunsigned, so instead of@examplefor (i = N - 1; i >= 0; i--) @{ ... @}@end example@noindentuse something like@examplefor (i = N; i > 0 && i--;) @{ ... @}@end example@noindentto avoid problems with wrap-around at @code{i=0}.If you really want to avoid confusion use a separate variable to invertthe loop order,@examplefor (i = 0; i < N; i++) @{ j = N - i; ... @}@end example@node Arrays vs Pointers, Constness, size_t, Design@section Arrays vs PointersA function can be declared with either pointer arguments or arrayarguments.  The C standard considers these to be equivalent. However, itis useful to distinguish between the case of a pointer, representing asingle object which is being modified, and an array which represents aset of objects with unit stride (that are modified or not depending onthe presence of @code{const}).  For vectors, where the stride is notrequired to be unity, the pointer form is preferred.@example/* real value, set on output */int foo (double * x);                           /* real vector, modified */int foo (double * x, size_t stride, size_t n);  /* constant real vector */int foo (const double * x, size_t stride, size_t n);  /* real array, modified */int bar (double x[], size_t n);                 /* real array, not modified */int baz (const double x[], size_t n);           @end example@node Constness, Pseudo-templates, Arrays vs Pointers, Design@section ConstnessUse @code{const} in function prototypes wherever an object pointed to bya pointer is constant (obviously).  For variables which are meaningfullyconstant within a function/scope use @code{const} also.  This preventsyou from accidentally modifying a variable which should be constant(e.g. length of an array, etc).  It can also help the compiler dooptimization.  These comments also apply to arguments passed by valuewhich should be made @code{const} when that is meaningful.@node Pseudo-templates, Test suites, 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 Test suites, Compilation, Pseudo-templates, Design@section Test suitesThe implementor of each library should provide a reasonable test suitefor that library.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.@node Compilation, Thread-safety, Test suites, Design@section CompilationMake sure everything compiles cleanly.  Use the strict compilationoptions for extra checking.@examplemake 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 example@noindentAlso use checkergcc to check for memory problems on the stack and theheap.  It's the best memory checking tool.  If checkergcc isn't availablethen Electric Fence will check the heap, which is better than nothing.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).@itemWe must understand ownership of existing code and algorithms.@itemize @minus@itemObviously: don't use or translate non-free code. Such as: NumericalRecipes, ACM TOMS.  Note that the ACM algorithms are not public domain, even though they aredistributed on the internet -- the ACM uses a special non-commerciallicense which is not compatible with the GPL. The details of thislicense can be found on the cover page of ACM Transactions onMathematical Software or on the ACM Website.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@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.We should also read up on what Cygnus is doing for their win32portability.@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, Miscellaneous, 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 Miscellaneous,  , Parallelism, 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).@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@c -@shortcontents@contents@bye

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -