?? nrutil.cpp
字號:
//nrutil.cpp
#include "StdAfx.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
//報告錯誤
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);
}
/*****************************************************************************
* 函數名稱:vector
* 功能:申請一個實數數組
* 參數:nl:數組的最低下標
* nh:數組的最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:ivector
* 功能:申請一個整數數組
* 參數:nl:數組的最低下標
* nh:數組的最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:cvector
* 功能:申請一個無符號字符數組
* 參數:nl:數組的最低下標
* nh:數組的最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:lvector
* 功能:申請一個長整數型數組
* 參數:nl:數組的最低下標
* nh:數組的最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:dvector
* 功能:申請一個雙精度實數數組
* 參數:nl:數組的最低下標
* nh:數組的最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:matrix
* 功能:申請一個實數二維數組(矩陣)
* 參數:nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:dmatrix
* 功能:申請一個雙精度實數二維數組(矩陣)
* 參數:nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:imatrix
* 功能:申請一個整數二維數組(矩陣)
* 參數:nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:cmatrix
* 功能:申請一個無符號字符型二維數組(矩陣)
* 參數:nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:submatrix
* 功能:求一個二維矩陣的子矩陣
* 參數:a:二維矩陣的首地址
* oldrl:數組的行最低下標
* oldrh:數組的行最高下標
* oldcl:數組的列最低下標
* oldch:數組的列最高下標
* newrl:新數組的行最低下標
* newcl:新數組的列最低下標
* 返回:數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:convert_matrix
* 功能:將一個實數一維數組轉化為二維數組(矩陣)
* 參數:a:原一維數組
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 返回:二維數組地址
*/
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;
}
/*****************************************************************************
* 函數名稱:free_vector
* 功能:釋放一個實數一維數組
* 參數:nl:數組的行最低下標
* nh:數組的行最高下標
* 無返回值
*/
void free_vector(float *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數名稱:free_dvector
* 功能:釋放一個整型一維數組
* 參數:nl:數組的行最低下標
* nh:數組的行最高下標
* 無返回值
*/
void free_ivector(int *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數名稱:free_vector
* 功能:釋放一個無符號字符型一維數組
* 參數:nl:數組的行最低下標
* nh:數組的行最高下標
* 無返回值
*/
void free_cvector(unsigned char *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數名稱:free_vector
* 功能:釋放一個長整型一維數組
* 參數:nl:數組的行最低下標
* nh:數組的行最高下標
* 無返回值
*/
void free_lvector(unsigned long *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數名稱:free_vector
* 功能:釋放一個雙精度一維數組
* 參數:nl:數組的行最低下標
* nh:數組的行最高下標
* 無返回值
*/
void free_dvector(double *v,long nl,long nh)
{
free((char*) (v+nl));
}
/*****************************************************************************
* 函數名稱:free_matrix
* 功能:釋放一個實數二維數組(矩陣)
* 參數:m:原數組地址
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 無返回值
*/
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));
}
/*****************************************************************************
* 函數名稱:free_dmatrix
* 功能:釋放一個雙精度二維數組(矩陣)
* 參數:m:原數組地址
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 無返回值
*/
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));
}
/*****************************************************************************
* 函數名稱:free_imatrix
* 功能:釋放一個整型二維數組(矩陣)
* 參數:m:原數組地址
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 無返回值
*/
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));
}
/*****************************************************************************
* 函數名稱:free_cmatrix
* 功能:釋放一個無符號字符二維數組(矩陣)
* 參數:m:原數組地址
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 無返回值
*/
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));
}
/*****************************************************************************
* 函數名稱:free_submatrix
* 功能:釋放一個子二維數組(矩陣)
* 參數:b:原數組地址
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 無返回值
*/
void free_submatrix(float **b,long nrl,long nrh,long ncl,long nch)
{
free((char*) (b+nrl));
}
/*****************************************************************************
* 函數名稱:free_convert_matrix
* 功能:釋放轉化后的二維數組(矩陣)
* 參數:b:原數組地址
* nrl:數組的行最低下標
* nrh:數組的行最高下標
* ncl:數組的列最低下標
* nch:數組的列最高下標
* 無返回值
*/
void free_convert_matrix(float **b,long nrl,long nrh,long ncl,long nch)
{
free((char*) (b+nrl));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -