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

📄 e.cpp

📁 一个非常有用的开源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
               REAL *  tr,        /* solution ........................*/
               REAL *  ti
             );

void SetVec             /* initialize vector .........................*/
           (int n, REAL x[], REAL val);

void CopyVec            /* copy vector ...............................*/
            (int n, REAL source[], REAL dest[]);

int ReadVec             /* read vector from input index starts at zero*/
           (FILE *fp, int n, REAL x[]);

int ReadVec1            /* read vector from input index starts at one */
           (FILE *fp, int n, REAL x[]);

int WriteVec            /* write vector to output index starts at zero*/
           (FILE *fp, int n, REAL x[]);

int WriteVec1           /* write vector to output index starts at one */
           (FILE *fp, int n, REAL x[]);

void SetMat             /* initialize matrix .........................*/
           (int m, int n, REAL * a[], REAL val);

void CopyMat            /* copy matrix ...............................*/
            (int m, int n, REAL * source[], REAL * dest[]);

int ReadMat             /* read matrix from input index starts at zero*/
           (FILE *fp, int m, int n, REAL * a[]);
int ReadMat1            /* read matrix from input index starts at one */
           (FILE *fp, int m, int n, REAL * a[]);

int WriteMat            /* write matrix to output index starts at zero*/
            (FILE *fp, int m, int n, REAL * mat[]);
int WriteMat1           /* write matrix to output index starts at one */
            (FILE *fp, int m, int n, REAL * mat[]);

int WriteHead  (FILE *fp, char *s); /* write header to file   ........*/

int WriteEnd  (FILE *fp);         /* write separator to file   .......*/

void LogError           /* write error message to stdout .............*/
             (char *s, int rc, char *file, int line);



#endif

/* -------------------------- END basis.h --------------------------- */









/**********************************************************
*  Reduced and modified version of basis.c by J-P Moreau  *
* ------------------------------------------------------- *
* Reference for original basis.c:                         *
*                                                         *
* "Numerical Algorithms with C, by Gisela Engeln-Muellges *
*  and Frank Uhlig, Springer-Verlag, 1996".               *
**********************************************************/   
     
  REAL sqr(REAL x)                    /* square a floating point number */
  /***********************************************************************
  * Compute the square of a floating point number.                       *
  *                                                                      *
  * global names used:                                                   *
  * ==================                                                   *
  * REAL                                                                 *
  ***********************************************************************/
  {
    return x * x;
  }

  REAL norm_max      /* Find the maximum norm of a REAL vector .........*/
             (
              REAL vektor[],               /* vector .................*/
              int  n                       /* length of vector .......*/
             )                             /* Maximum norm ...........*/

  /***********************************************************************
  * Return the maximum norm of a [0..n-1] vector  v.                     *
  *                                                                      *
  * global names used:                                                   *
  * ==================                                                   *
  * REAL, FABS, ZERO                                                     *
  ***********************************************************************/
  {
    REAL norm,                                             /* local max */
         betrag;                            /* magnitude of a component */

    for (n--, norm = ZERO; n >= 0; n--, vektor++)
      if ((betrag = FABS(*vektor)) > norm)
        norm = betrag;

    return norm;
  }
  
  void copy_vector        /* copy a REAL vector ........................*/
                (
                 REAL ziel[],            /* copied vector ............*/
                 REAL quelle[],          /* original vector ..........*/
                 int  n                  /* length of vector .........*/
                )
  /***********************************************************************
  * copy the n elements of the vector quelle into the vector ziel.       *
  *                                                                      *
  * global names used:                                                   *
  * ==================                                                   *
  * REAL                                                                 *
  ***********************************************************************/
  {
    for (n--; n >= 0; n--)
      *ziel++ = *quelle++;
  }  

  /***********************************************************************
  * read a vector x of size n from input file fp (index begins at zero)  *
  *                                                                      *
  * global names used:                                                   *
  * ==================                                                   *
  * REAL                                                                 *
  ***********************************************************************/
  int ReadVec (FILE *fp, int n, REAL x[])
  {
    int i;
    REAL tmp;

    for (i = 0; i < n; i++)
    {
      if (fscanf (fp,FORMAT_IN, &tmp) <= 0) return (-1);
      x[i] = (REAL) tmp;
    }

    return (0);
  }
  
  /***********************************************************************
  * read a vector x of size n from input file fp (index begins at one)   *
  *                                                                      *
  * global names used:                                                   *
  * ==================                                                   *
  * REAL                                                                 *
  ***********************************************************************/
  int ReadVec1 (FILE *fp, int n, REAL x[])
  {
    int i;
    REAL tmp;

    for (i = 1; i < n+1; i++)
    {
      if (fscanf (fp,FORMAT_IN, &tmp) <= 0) return (-1);
      x[i] = (REAL) tmp;
    }

    return (0);
  }
     
  void SetVec (int n, REAL x[], REAL val)
  {
    int i;

    for (i = 0; i < n; i++)
      x[i] = val;
  }     
     
int WriteVec (FILE *fp, int n, REAL x[])
/*====================================================================*
 *                                                                    *
 *  Put out vector of length n to output file. Index begins at zero.  *
 *                                                                    *
 *====================================================================*
 *                                                                    *
 *  Input parameters:                                                 *
 *  ================                                                  *
 *                                                                    *
 *      n        int n;                                               *
 *               lenght of vector                                     *
 *      x        REAL x[];                                            *
 *               vector                                               *
 *                                                                    *
 *   Return value :                                                   *
 *   =============                                                    *
 *      =  0     All ok                                               *
 *      = -1     Error putting out to stdout                          *
 *                                                                    *
 *====================================================================*/
{
  int i;

  for (i = 0; i < n; i++)
    if (fprintf (fp,FORMAT_126LF, x[i]) <= 0) return (-1);
  if (fprintf (fp,"\n") <= 0) return (-1);

  return 0;
}     

int WriteVec1 (FILE *fp, int n, REAL x[])
/*====================================================================*
 *                                                                    *
 *  Put out vector of length n to output file. Index begins at one.   *
 *                                                                    *
 *====================================================================*
 *                                                                    *
 *  Input parameters:                                                 *
 *  ================                                                  *
 *                                                                    *
 *      n        int n;                                               *
 *               lenght of vector                                     *
 *      x        REAL x[];                                            *
 *               vector                                               *
 *                                                                    *
 *   Return value :                                                   *
 *   =============                                                    *
 *      =  0     All ok                                               *
 *      = -1     Error putting out to stdout                          *
 *                                                                    *
 *====================================================================*/
{
  int i;

  for (i = 1; i < n+1; i++)
    if (fprintf (fp,FORMAT_126LF, x[i]) <= 0) return (-1);
  if (fprintf (fp,"\n") <= 0) return (-1);

  return 0;
}     
     
  REAL pi() {
    return 4.0*atan(1.0);
  }     
    
void CopyMat (int m, int n, REAL * source[], REAL * dest[])
/*====================================================================*
 *                                                                    *
 *  Copy the m x n matrix source to the  m x n matrix dest.           *
 *                                                                    *
 *====================================================================*
 *                                                                    *
 *  Input parameters:                                                 *
 *  ================                                                  *
 *      m        int m; ( m > 0 )                                     *
 *               numnber of rows of matrix                            *
 *      n        int n; ( n > 0 )                                     *
 *               number of columns of matrix                          *
 *      source   REAL * source[];                                     *
 *               matrix                                               *
 *      dest     REAL * dest[];                                       *
 *               matrix to be copied to                               *
 *                                                                    *
 *   Output parameter:                                                *
 *   ================                                                 *
 *      dest     same as above                                        *
 *                                                                    *
 *   ATTENTION : WE do not allocate storage for dest here.            *
 *                                                                    *
 *====================================================================*/
{
  int i, j;

  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      dest[i][j] = source[i][j];
}    
           
REAL maxroot(void)    /* Root of the largest representable number ....*/
/***********************************************************************
* Compute the square root of the largest machine number 2 ^ (MAX_EXP/2)*
* if not already done                                                  *
*                                                                      *
* global names used:                                                   *
* ==================                                                   *
* REAL, boolean, FALSE, TRUE, SQRT, POSMAX                             *
***********************************************************************/
{
  static REAL       save_maxroot;
  static boolean    schon_berechnet = FALSE;
  REAL              faktor;
  unsigned long int n;

  if (! schon_berechnet)
  {
    save_maxroot = ONE;
    faktor       = TWO;
    for (n = MAX_EXP / 2; n > 1; n /= 2, faktor *= faktor)
      if (n % 2 != 0)
        save_maxroot *= faktor;
    save_maxroot    *= faktor;
    schon_berechnet  = TRUE;
  }

  return save_maxroot;

⌨️ 快捷键说明

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