📄 pmatlib.h
字号:
algorithm will stop when the: (last_lamda - final_lamda) < Error_threshold */float pmineigenf(float **A, float *v, float *lamda, int M, int Max_iterations, float Error_threshold);double pmineigenlf(double **A, double *v, double *lamda, int M, int Max_iterations, double Error_threshold);/******************************************************************/void pfreealltemps(void); /* free all the temporary vectors and matrices used by the linear algebra routines *//******************************************************************//* Stuff for reading ascii from files *//******************************************************************/#ifndef TKUCHAR#define TKUCHAR 0 /* Unsigned char */#endif#ifndef TKSHINT#define TKSHINT 1 /* Short int */#endif#ifndef TKINT#define TKINT 2 /* Int */#endif#ifndef TKFLOAT#define TKFLOAT 3 /* Float */#endif#ifndef TKDOUBLE#define TKDOUBLE 4 /* Double */#endif#ifndef TKLINT#define TKLINT 5 /* Long int */#endif#ifndef TKSCHAR#define TKSCHAR 6 /* Signed char */#endif#ifndef TKCHAR#define TKCHAR 9 /* Ascii char */#endif#ifndef TKHEX#define TKHEX 10 /* Hexadecimal int */#endif/* asread -- Read a scalar from the file fname with name varname into varptr, with type, where type is specified by the standard bread library types. Return 1 if successful and 0 if not successful */int asread(char *fname, char *varname,void *varptr, int type);/* avread -- Read a vector from the file fname with name varname into p withtype, where type is specified by the standard bread library types.The number of elements read is set in n.If the variable is not found, 0 is returned; otherwise 1 is returned */int avread(char *fname, char *varname,void **varptr,int *n, int type);/* amread -- Read a matrix from the file fname with name varname into p withtype, where type is specified by the standard bread library types.The number of rows and columns are set in m and n.Returns 1 if successful and 0 if not successful */int amread(char *fname, char *varname, void ***varptr, int *m, int *n,int type) ;/* read a (3-dimensional) tensor from the data file the tensor is assumed to be stored in the form p[m][n][q] p[0][0][0] p[0][0][1] ...p[0][1][0] p[0][1][1] ...p[0][2][0] p[0][2][1] ...p[1][0][0] p[1][0][1] ...p[1][1][0] p[1][1][1] ...p[1][2][0] p[1][2][1] ... ...Note that m is NOT determined -- it must be passed in to determine howto block the information. If the number of rows detected is not amultiple of m, then an error is declared. Otherwise, the value of nis determined by (number of rows)/m.*/int atread(char *fname, char *varname,void ****varptr, int m, int *n, int *q, int type);/*The routines asread, avread, amread, and atread allow for convenient input ofascii (human-readable) data into programs. by which this can be accomplished. Variable-by-variable readingHere is a sample data file:; Comments can appear on lines by themselves% Comments are delimited by ; or % or # or C++ or # C comments, as long as the C comments begin and end on the ; same linen: 2 ; colon after a variable name is okm 3 ; only white space after a variable name is okn = 2 ; = sign after a variable is ok j = 4 ; white space preceeding the variable does not matternvect = 2 3 4 ; vectors can be specifiedf -1.34e-2 ; floating point formats are okg = 34.23fvect 3.45 23.4; blank lines don't matterimat 2 3 ; elements are counted in the first line of matrix to 1 2 ; determine matrix width 5 ; incomplete data will be set to zero.fmat = 2.34, 2 3. 6Note the following syntax rules:1) In general white space is ignored2) ;, # or % all indicate the beginning of a comment; all text following the character is ignored3) C or C++ comments can be used, provided that the C comment fits all within one line3) Any separator (that does not denote a comment) can be used betweendata fields.4) For vectors, the number of fields read determines the size of thevector. It may be necessary to pad with zeros in order to get adesired size.5) Blank lines are not necessary to separate the fields, even whenreading matrices.6) Do not put a complete comment line in the middle of a matrix. Thatis, do not do this:fmat 2 3; bad place for a comment! 4 5The data above can be read with one of the following statements: retval = asread(fname,varname,&var,type); // read a scalar retval = avread(fname,varname,&var,&nr,type); // read a vector retval = amread(fname,varname,&var,&nr,&nc,type); // read a matrix retval = atread(fname,varnme,&var,nstack,&nr,&nc,type); // read a tensorThe retval==0 indicates an error.The type variable is defined as follows: 0 - unsigned char UCHAR 1 - short int SHINT 2 - int INT 3 - float FLOAT 4 - double DOUBLE 5 - long int LINT 6 - signed char SCHAR 9 - character ascii CHAR 10 - hexadecimal HEX (can be prefaced by 0x)There is not strong type checking between the unsigned char andsigned char. Caveat Emptor.Note the following specifics for various types:ASCII CHARACTERS:1) The white space preceeding the characters is skipped.2) For strings, the string is read up until the end of line, and any \n character is removed.3) The strings are returned null terminated.4) For character matrcies, the ^ character is used to denote continuation(so the program can distinguish from variable identifiers).For example:strmat first_input^ second_input^ third_inputThe ^ and preceeding white space for each line is stripped.5) The longest line of input is used to determine the width of the array.6) To preserve comment characters, use \ before them: \; \# \% or quote them "#" '#' ";" ';' "%" '%'7) For reading single characters, no quotes are necessary. However, quotes can be used: c1 = $ ; this is ok c2 = '@' ; a quoted character --- will read @ c3 = "#" ; a quoted character --- will read # To get the quoted itself, put an escape before it: c4 = \' ; read ' c5 = \" ; read " or quote it: c6 = '"' ; read " c7 = "'" ; read ' To get \, just put it by itself c8 = \ or quote it c9 = '\' c10 = "\" To get a space, it must be quoted or used with \: c11 = ' ' c12 = " " c13 = \ Other than preceding comment or quote characters or space, the \ isnot regarded as special.8) For reading character strings, comment indicators $ % ; inside quotes are ignored. However, removal of quoting characters ', ", or \ is not done.9) Hex can be just the number: 3a or the number preceded by 0: 03a or the number preceded by 0x: 0x3a However, when forming a hex array, you must make sure that the first number on each row does not look like a C identifier, preceding it with 0 or 0x if necessary.One such read statement is required for each variable. For example,the preceding data could be read with the following piece of programcode: int n,m,j; // declare the variables used int *nvect; float f,g; float *fvect; int **imat; int **fmat; char ac; char *str; // ascii string char **strmat; // character matrix int nvect_n; // the number read into nvect int fvect_n; // the number read into fvect int nvect_comp; // the number read into vect_comp int imat_r,imat_c; // imat rows and columns int fmat_r,fmag_c; // fmat rows and columns char fname[20]; // the file name // ... asread(fname,"m",&m,TKINT); // read scalars asread(fname,"j",&j,TKINT); asread(fname,"n",&n,TKINT); asread(fname,"f",&f,TKFLOAT); asread(fname,"g",&g,TKFLOAT); asread(fname,"ac",&ac,TKCHAR); // read a character as ascii asread(fname,"scal_comp",&scal_comp,12); avread(fname,"nvect",&nvect,&nvect_n,TKINT); // read vectors avread(fname,"fvect",&fvect,&fvect_n,TKFLOAT); avread(fname,"str",&str,&strn,CHAR); // character string vect_comp = (complx *)avread(fname,"vect_comp",&vect_comp,&nvect_comp,12); amread(fname,"imat",&imat,&imat_r,&imat_c,TKINT); // read matrices amread(fname,"fmat",&fmat,&fmat_r,&fmat_c,TKFLOAT); amread(fname,"strmat",&strmat,&nrow,&nchars,TKCHAR);Note that the ORDER IN WHICH THE VARIABLES ARE READ DOES NOT MATTER.This is one of the useful and flexible strengths of this library.When there are multiple appearences of a variable in the data file, theFIRST one found it used. (After finding a variable, no more searchingis performed.)If the named variable is not found in the file, then the variable pointer is NOT modified. Thie means that you can set up default values which may beoverwritten by calling a*read. int newvar=5; // set up variable with default value asread(fname,"newvar",&newvar,TKINT); // if "newvar" not found, // the original value is retainedHowever, for avread, amread, and atread, the size parameters are set to zeroif the named variable is not found in the file. Also, if the variable is not found, the returnvalue is 0: if(!asread(fname,"newvar",&newvar,TKINT)) { newvar = 2; // set up default value this way }Todd K. Moon. Utah State University*//* sort array into INCREASING order, and shuffle array2 at the same time if array2 = 0,1,...,N and A represents the original _unsorted_ array, then after the sort, A[array2[0]], A[array2[1]], ... represents the sorted data in array.*/void sort2lfd(int num_elements, double *array, int *array2);void sort2fd(int num_elements, float *array, int *array2);/* sort array into increasing order */void sort1lf(int num_elements, double *array);void sort1f(int num_elements, float *array);/**********************************************************************//* Generate a unit-variance random number. This function calls rand(), so you can control the see using void srand(unsigned int seed);*/double gran(void);/* Generate a uniform random number between 0 and 1 */double uran(void);#endif /* end of _PMATLIB_H_ *//*Local Variables:compile-command: "gcc -o testpmatlib -g testpmatlib.c pmatlib.c -lm"End:*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -