?? nrutil.cpp
字號(hào):
//nrutil.cpp
#include "StdAfx.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
//報(bào)告錯(cuò)誤
void nrerror(char *error_text)
{
printf("Numerical Recipes run-time error...\n");
printf("%s\n",error_text);
printf("...now exiting to system...\n");
exit(1);
}
/*****************************************************************************
* 功能:申請(qǐng)一個(gè)實(shí)數(shù)數(shù)組
* 參數(shù):nl:數(shù)組的最低下標(biāo)
* nh:數(shù)組的最高下標(biāo)
* 返回:數(shù)組地址
*/
float *vector(long nl,long nh)
{
float *v;
v=(float *)calloc((unsigned) (nh-nl+1),sizeof(float));
if (!v) nrerror("allocation failure in vector()");
return v-nl;
}
/*****************************************************************************
* 函數(shù)名稱:ivector
* 功能:申請(qǐng)一個(gè)整數(shù)數(shù)組
* 參數(shù):nl:數(shù)組的最低下標(biāo)
* nh:數(shù)組的最高下標(biāo)
* 返回:數(shù)組地址
*/
int *ivector(long nl,long nh)
{
int *v;
v=(int *)calloc((unsigned) (nh-nl+1),sizeof(int));
if (!v) nrerror("allocation failure in ivector()");
return v-nl;
}
/*****************************************************************************
* 函數(shù)名稱:cvector
* 功能:申請(qǐng)一個(gè)無符號(hào)字符數(shù)組
* 參數(shù):nl:數(shù)組的最低下標(biāo)
* nh:數(shù)組的最高下標(biāo)
* 返回:數(shù)組地址
*/
unsigned char *cvector(long nl,long nh)
{
unsigned char *v;
v=(unsigned char *)calloc((unsigned) (nh-nl+1),sizeof(unsigned char));
if (!v) nrerror("allocation failure in cvector()");
return v-nl;
}
/*****************************************************************************
* 函數(shù)名稱:lvector
* 功能:申請(qǐng)一個(gè)長(zhǎng)整數(shù)型數(shù)組
* 參數(shù):nl:數(shù)組的最低下標(biāo)
* nh:數(shù)組的最高下標(biāo)
* 返回:數(shù)組地址
*/
unsigned long *lvector(long nl,long nh)
{
unsigned long *v;
v=(unsigned long *)calloc((unsigned) (nh-nl+1),sizeof(unsigned long));
if (!v) nrerror("allocation failure in lvector()");
return v-nl;
}
/*****************************************************************************
* 函數(shù)名稱:dvector
* 功能:申請(qǐng)一個(gè)雙精度實(shí)數(shù)數(shù)組
* 參數(shù):nl:數(shù)組的最低下標(biāo)
* nh:數(shù)組的最高下標(biāo)
* 返回:數(shù)組地址
*/
double *dvector(long nl,long nh)
{
double *v;
v=(double *)calloc((unsigned) (nh-nl+1),sizeof(double));
if (!v) nrerror("allocation failure in dvector()");
return v-nl;
}
/*****************************************************************************
* 函數(shù)名稱:matrix
* 功能:申請(qǐng)一個(gè)實(shí)數(shù)二維數(shù)組(矩陣)
* 參數(shù):nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 返回:數(shù)組地址
*/
float **matrix(long nrl,long nrh,long ncl,long nch)
{
int i;
float **m;
m=(float **) calloc((unsigned) (nrh-nrl+1),sizeof(float*));
if (!m) nrerror("allocation failure 1 in matrix()");
m -= nrl;
for(i=nrl;i<=nrh;i++) {
m[i]=(float *) calloc((unsigned) (nch-ncl+1),sizeof(float));
if (!m[i]) nrerror("allocation failure 2 in matrix()");
m[i] -= ncl;
}
return m;
}
/*****************************************************************************
* 函數(shù)名稱:dmatrix
* 功能:申請(qǐng)一個(gè)雙精度實(shí)數(shù)二維數(shù)組(矩陣)
* 參數(shù):nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 返回:數(shù)組地址
*/
double **dmatrix(long nrl,long nrh,long ncl,long nch)
{
int i;
double **m;
m=(double **) calloc((unsigned) (nrh-nrl+1),sizeof(double*));
if (!m) nrerror("allocation failure 1 in dmatrix()");
m -= nrl;
for(i=nrl;i<=nrh;i++) {
m[i]=(double *) calloc((unsigned) (nch-ncl+1),sizeof(double));
if (!m[i]) nrerror("allocation failure 2 in dmatrix()");
m[i] -= ncl;
}
return m;
}
/*****************************************************************************
* 函數(shù)名稱:imatrix
* 功能:申請(qǐng)一個(gè)整數(shù)二維數(shù)組(矩陣)
* 參數(shù):nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 返回:數(shù)組地址
*/
int **imatrix(long nrl,long nrh,long ncl,long nch)
{
int i,**m;
m=(int **)calloc((unsigned) (nrh-nrl+1),sizeof(int*));
if (!m) nrerror("allocation failure 1 in imatrix()");
m -= nrl;
for(i=nrl;i<=nrh;i++) {
m[i]=(int *)calloc((unsigned) (nch-ncl+1),sizeof(int));
if (!m[i]) nrerror("allocation failure 2 in imatrix()");
m[i] -= ncl;
}
return m;
}
/*****************************************************************************
* 函數(shù)名稱:cmatrix
* 功能:申請(qǐng)一個(gè)無符號(hào)字符型二維數(shù)組(矩陣)
* 參數(shù):nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 返回:數(shù)組地址
*/
unsigned char **cmatrix(long nrl,long nrh,long ncl,long nch)
{
int i;
unsigned char **m;
m=(unsigned char **) calloc((unsigned) (nrh-nrl+1),sizeof(unsigned char*));
if (!m) nrerror("allocation failure 1 in dmatrix()");
m -= nrl;
for(i=nrl;i<=nrh;i++) {
m[i]=(unsigned char *) calloc((unsigned) (nch-ncl+1),sizeof(unsigned char));
if (!m[i]) nrerror("allocation failure 2 in dmatrix()");
m[i] -= ncl;
}
return m;
}
/*****************************************************************************
* 函數(shù)名稱:submatrix
* 功能:求一個(gè)二維矩陣的子矩陣
* 參數(shù):a:二維矩陣的首地址
* oldrl:數(shù)組的行最低下標(biāo)
* oldrh:數(shù)組的行最高下標(biāo)
* oldcl:數(shù)組的列最低下標(biāo)
* oldch:數(shù)組的列最高下標(biāo)
* newrl:新數(shù)組的行最低下標(biāo)
* newcl:新數(shù)組的列最低下標(biāo)
* 返回:數(shù)組地址
*/
float **submatrix(float **a,long oldrl,long oldrh,long oldcl,long oldch,long newrl,long newcl)
{
int i,j;
float **m;
m=(float **) calloc((unsigned) (oldrh-oldrl+1),sizeof(float*));
if (!m) nrerror("allocation failure in submatrix()");
m -= newrl;
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+oldcl-newcl;
return m;
}
/*****************************************************************************
* 函數(shù)名稱:convert_matrix
* 功能:將一個(gè)實(shí)數(shù)一維數(shù)組轉(zhuǎn)化為二維數(shù)組(矩陣)
* 參數(shù):a:原一維數(shù)組
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 返回:二維數(shù)組地址
*/
float **convert_matrix(float *a,long nrl,long nrh,long ncl,long nch)
{
int i,j,nrow,ncol;
float **m;
nrow=nrh-nrl+1;
ncol=nch-ncl+1;
m = (float **) calloc((unsigned) (nrow),sizeof(float*));
if (!m) nrerror("allocation failure in convert_matrix()");
m -= nrl;
for(i=0,j=nrl;i<=nrow-1;i++,j++) m[j]=a+ncol*i-ncl;
return m;
}
/*****************************************************************************
* 函數(shù)名稱:free_vector
* 功能:釋放一個(gè)實(shí)數(shù)一維數(shù)組
* 參數(shù):nl:數(shù)組的行最低下標(biāo)
* nh:數(shù)組的行最高下標(biāo)
* 無返回值
*/
void free_vector(float *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數(shù)名稱:free_dvector
* 功能:釋放一個(gè)整型一維數(shù)組
* 參數(shù):nl:數(shù)組的行最低下標(biāo)
* nh:數(shù)組的行最高下標(biāo)
* 無返回值
*/
void free_ivector(int *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數(shù)名稱:free_vector
* 功能:釋放一個(gè)無符號(hào)字符型一維數(shù)組
* 參數(shù):nl:數(shù)組的行最低下標(biāo)
* nh:數(shù)組的行最高下標(biāo)
* 無返回值
*/
void free_cvector(unsigned char *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數(shù)名稱:free_vector
* 功能:釋放一個(gè)長(zhǎng)整型一維數(shù)組
* 參數(shù):nl:數(shù)組的行最低下標(biāo)
* nh:數(shù)組的行最高下標(biāo)
* 無返回值
*/
void free_lvector(unsigned long *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數(shù)名稱:free_vector
* 功能:釋放一個(gè)雙精度一維數(shù)組
* 參數(shù):nl:數(shù)組的行最低下標(biāo)
* nh:數(shù)組的行最高下標(biāo)
* 無返回值
*/
void free_dvector(double *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數(shù)名稱:free_matrix
* 功能:釋放一個(gè)實(shí)數(shù)二維數(shù)組(矩陣)
* 參數(shù):m:原數(shù)組地址
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 無返回值
*/
void free_matrix(float **m,long nrl,long nrh,long ncl,long nch)
{
int i;
for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
free((char*) (m+nrl));
}
/*****************************************************************************
* 函數(shù)名稱:free_dmatrix
* 功能:釋放一個(gè)雙精度二維數(shù)組(矩陣)
* 參數(shù):m:原數(shù)組地址
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 無返回值
*/
void free_dmatrix(double **m,long nrl,long nrh,long ncl,long nch)
{
int i;
for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
free((char*) (m+nrl));
}
/*****************************************************************************
* 函數(shù)名稱:free_imatrix
* 功能:釋放一個(gè)整型二維數(shù)組(矩陣)
* 參數(shù):m:原數(shù)組地址
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 無返回值
*/
void free_imatrix(int **m,long nrl,long nrh,long ncl,long nch)
{
int i;
for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
free((char*) (m+nrl));
}
/*****************************************************************************
* 函數(shù)名稱:free_cmatrix
* 功能:釋放一個(gè)無符號(hào)字符二維數(shù)組(矩陣)
* 參數(shù):m:原數(shù)組地址
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 無返回值
*/
void free_cmatrix(unsigned char **m,long nrl,long nrh,long ncl,long nch)
{
int i;
for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
free((char*) (m+nrl));
}
/*****************************************************************************
* 函數(shù)名稱:free_submatrix
* 功能:釋放一個(gè)子二維數(shù)組(矩陣)
* 參數(shù):b:原數(shù)組地址
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 無返回值
*/
void free_submatrix(float **b,long nrl,long nrh,long ncl,long nch)
{
free((char*) (b+nrl));
}
/*****************************************************************************
* 函數(shù)名稱:free_convert_matrix
* 功能:釋放轉(zhuǎn)化后的二維數(shù)組(矩陣)
* 參數(shù):b:原數(shù)組地址
* nrl:數(shù)組的行最低下標(biāo)
* nrh:數(shù)組的行最高下標(biāo)
* ncl:數(shù)組的列最低下標(biāo)
* nch:數(shù)組的列最高下標(biāo)
* 無返回值
*/
void free_convert_matrix(float **b,long nrl,long nrh,long ncl,long nch)
{
free((char*) (b+nrl));
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -