本文目錄一覽:
設計一個一元稀疏多項式簡單計算器。
#includestdio.h
#includemalloc.h
#includeiostream.h
typedef struct Polynode
{
float coef; //系數
int exp; //指數
struct Polynode *next;
}*Poly,Polynode; //Poly為結點指針類型void Insert(Poly p,Poly head)
{
if(p-coef==0) //系數為0時釋放結點
free(p);
else
{
Poly q1,q2;
q1=head;
q2=head-next;
while(q2p-expq2-exp)
{ //后移查找插入位置
q1=q2;
q2=q2-next;
}
if(q2p-exp==q2-exp)
{ //將指數相同的相合租叢并
q2-coef+=p-coef;
free(p);
if(!q2-coef) //系數為襪型瞎0時釋放結點
{
q1-next=q2-next;
free(q2);
}
}
else
{
p-next=q2;
q1-next=p;
}
}
}//InsertPoly CreateList(Poly head,int m)
{ //建立一個頭指針為head、項數為m的一元多項式
int i;
Poly p;
Polynode *q;
p=head=(Poly)malloc(sizeof(struct Polynode));
head-next=NULL;
for(i=0;im;i++)
{
p=(Poly)malloc(sizeof(struct Polynode));//建立告空新結點以接收數據
cout"請輸入第"i+1"項的系數與指數: ";
cinp-coefp-exp;
Insert(p,head); //調用Insert函數插入結點
}
q=head-next;
while(q!=NULL)
{
cout"系數:"q-coef"\t""指數 : "q-expendl;
q=q-next;
}
return head;
}//CreatePolyvoid DestroyList(Poly p)
{ //銷毀多項式p
Poly q1,q2;
if(p-next!=NULL)
{
q1=p-next;
q2=q1-next;
while(q1-next)
{
free(q1);
q1=q2;//指針后移
q2=q2-next;
}
}
}int OutputList(Poly P)
{ //輸出多項式
Poly q=P-next;
int flag=1;//項數計數器
if(!q)
{ //若多項式為空,輸出0
cout" 0 "endl;
return(0);
}
while (q)
{
if(q-coef0flag!=1) //系數大于0且不是第一項
cout"+";
if(q-coef!=1q-coef!=-1)//系數非1或-1的普通情況
{
coutq-coef;
if(q-exp==1)
cout"X";
else if(q-exp)
cout"X^"q-exp;
}
else
{
if(q-coef==1)
{
if(!q-exp)
cout"1";
else if(q-exp==1)
cout"X";
else if(q-exp)
cout"X^"q-exp;
}
if(q-coef==-1)
{
if(!q-exp) cout"-1";
else if(q-exp==1) cout"-X";
else cout"-X^"q-exp;
}
}
q=q-next;
flag++;
}//while
coutendl;
return(0);
}//OutputPolyint compare(Poly a,Poly b)
{
if(ab)
{
if(!b||a-expb-exp) return 1;
else if(!a||a-expb-exp) return -1;
else return 0;
}
else if(!ab) return -1;//a多項式已空,但b多項式非空
else return 1;//b多項式已空,但a多項式非空
}//comparePoly AddPoly(Poly pa,Poly pb)
{//求解并建立和多項式a+b,返回其頭指針
Poly qa=pa-next;
Poly qb=pb-next;
Poly headc,hc,qc;
hc=(Poly)malloc(sizeof(struct Polynode));//建立頭結點
hc-next=NULL;
headc=hc;
while(qa||qb){
qc=(Poly)malloc(sizeof(struct Polynode));
switch(compare(qa,qb))
{
case 1:
{
qc-coef=qa-coef;
qc-exp=qa-exp;
qa=qa-next;
break;
}
case 0:
{
qc-coef=qa-coef+qb-coef;
qc-exp=qa-exp;
qa=qa-next;
qb=qb-next;
break;
}
case -1:
{
qc-coef=qb-coef;
qc-exp=qb-exp;
qb=qb-next;
break;
}
}//switch
if(qc-coef!=0)
{
qc-next=hc-next;
hc-next=qc;
hc=qc;
}
else free(qc);//當相加系數為0時,釋放該結點
}//while
return headc;
}//AddPolyPoly SubtractPoly(Poly pa,Poly pb)
{//求解并建立和多項式a-b,返回其頭指針
Poly qa=pa-next;
Poly qb=pb-next;
Poly headc,hc,qc;
hc=(Poly)malloc(sizeof(struct Polynode));//建立頭結點
hc-next=NULL;
headc=hc;
while(qa||qb)
{
qc=(Poly)malloc(sizeof(struct Polynode));
switch(compare(qa,qb))
{
case 1:
{
qc-coef=qa-coef;
qc-exp=qa-exp;
qa=qa-next;
break;
}
case 0:
{
qc-coef=qa-coef-qb-coef;
qc-exp=qa-exp;
qa=qa-next;
qb=qb-next;
break;
}
case -1:
{
qc-coef=-qb-coef;
qc-exp=qb-exp;
qb=qb-next;
break;
}
}//switch
if(qc-coef!=0)
{
qc-next=hc-next;
hc-next=qc;
hc=qc;
}
else free(qc);//當相減系數為0時,釋放該結點
}//while
return headc;
}//AddPolyPoly MultiplyPoly(Poly pa,Poly pb)
{//求解并建立積多項式a*b,返回其頭指針
Poly hf,pf;
Poly qa=pa-next;
Poly qb=pb-next;
hf=(Poly)malloc(sizeof(struct Polynode));//建立頭結點
hf-next=NULL;
for(qa=pa-next;qa;qa=qa-next)
{
for(qb=pb-next;qb;qb=qb-next)
{
pf=(Poly)malloc(sizeof(struct Polynode));
pf-coef=qa-coef*qb-coef;
pf-exp=qa-exp+qb-exp;
Insert(pf,hf);//調用Insert函數以合并指數相同的項
}
}
return hf;
}//MultiplyPolyvoid DevicePoly(Poly pa,Poly pb)
{//求解并建立商多項式a/b,返回其頭指針
Poly hf,pf,temp1,temp2;
Poly qa=pa-next;
Poly qb=pb-next;
hf=(Poly)malloc(sizeof(struct Polynode));//建立頭結點,存儲商
hf-next=NULL;
pf=(Poly)malloc(sizeof(struct Polynode));//建立頭結點,存儲余數
pf-next=NULL;
temp1=(Poly)malloc(sizeof(struct Polynode));
temp1-next=NULL;
temp2=(Poly)malloc(sizeof(struct Polynode));
temp2-next=NULL;
temp1=AddPoly(temp1,pa);
while(qa!=NULLqa-exp=qb-exp)
{
temp2-next=(Poly)malloc(sizeof(struct Polynode));
temp2-next-coef=(qa-coef)/(qb-coef);
temp2-next-exp=(qa-exp)-(qb-exp);
Insert(temp2-next,hf);
pa=SubtractPoly(pa,MultiplyPoly(pb,temp2));
qa=pa-next;
temp2-next=NULL;
}
pf=SubtractPoly(temp1,MultiplyPoly(hf,pb));
cout"商是:";
OutputList(hf);
cout"余數是:";
OutputList(pf);
}//DevicePolyfloat ValuePoly(Poly head,float x)
{//輸入x值,計算并返回多項式的值
Poly p;
int i;
float sum=0,t;
for(p=head-next;p;p=p-next)
{
t=1;
for(i=p-exp;i!=0;)
{
if(i0)
{
t/=x;
i++;
}//指數小于0,進行除法
else
{
t*=x;
i--;
}//指數大于0,進行乘法
}
sum+=p-coef*t;
}
return sum;
}//ValuePolyPoly Derivative(Poly head)
{//求解并建立a的導函數多項式,并返回其頭指針
Poly q=head-next,p1,p2,hd;
hd=p1=(Poly)malloc(sizeof(struct Polynode));//建立頭結點
hd-next=NULL;
while(q)
{
if(q-exp!=0)
{ //該項不是常數項時
p2=(Poly)malloc(sizeof(struct Polynode));
p2-coef=q-coef*q-exp;
p2-exp=q-exp-1;
p2-next=p1-next;//尾插法插入結點
p1-next=p2;
p1=p2;
}
else if(q-exp==0) //該項為常數項
break;
q=q-next;
}
return hd;
}//Dervativeint main()
{
int m,n,flag=0;
float x;
Poly pa=0,pb=0,pc,pd,pe,pf;//定義各式的頭指針,pa與pb在使用前付初值NULL
cout"請輸入a的項數:" ;
cinm;
pa=CreateList(pa,m);//建立多項式a
cout"請輸入b的項數:" ;
cinn;
pb=CreateList(pb,n);//建立多項式a
//輸出菜單
cout"*********************************************************************"endl;
cout" 1.輸出多項式a和b 2.建立多項式a+b 3.建立多項式a-b"endl;
cout" 4.建立多項式a*b 5.建立多項式a/b"endl;
cout" 6.計算多項式a在x處的值 7.求多項式a的導函數 8.退出程序"endl;
cout"*********************************************************************"endl;
for(;;flag=0)
{
cout"執行操作為:" ;
cinflag;
if(flag==1)
{
cout"多項式a為:";
OutputList(pa);
cout"多項式b為:";
OutputList(pb);
continue;
}
if(flag==2)
{
pc=AddPoly(pa,pb);
cout"多項式a+b為:";
OutputList(pc);
DestroyList(pc);
continue;
}
if(flag==3)
{
pd=SubtractPoly(pa,pb);
cout"多項式a-b為:";
OutputList(pd);
DestroyList(pd);
continue;
}
if(flag==4)
{
pe=MultiplyPoly(pa,pb);
cout"多項式a*b為:";
OutputList(pe);
DestroyList(pe);
continue;
}
if(flag==5)
{
DevicePoly(pa,pb);
continue;
}
if(flag==6)
{
cout"請輸入x的值:x=";
cinx;
cout"多項式a的值為:"ValuePoly(pa,x)endl;
continue;
}
if(flag==7)
{
pf=Derivative(pa);
cout"多項式a的導函數為:";
OutputList(pf);
DestroyList(pf);
continue;
}
if(flag==8)
break;
if(flag1||flag8)
cout"輸入錯誤!!!請重新選擇!!";
continue;
}
DestroyList(pa);
DestroyList(pb);
return 0;
}
翻譯難點2:multi-和poly-前綴形容“多”時有何不同?
poly強調數量眾多的;multi不孫搜賣強調數量多,只客觀反映是由多個、兩個以則逗上構成的.比如multi-national不強調國家很多,只說明漏寬這是一個跨國的企業
matlab中的最小二乘法
都用上7.5啦?!
ver
-------------------------------------------------
MATLAB Version 7.3.0.267 (R2006b)
help lsqnonlin
LSQNONLIN solves non-linear least squares problems.
LSQNONLIN attempts to solve problems of the form:
min sum {FUN(X).^2} where X and the values returned by FUN can be
x vectors or matrices.
用lookfor找
lookfor least
LCM Least common multiple.
LSCOV Least squares with known covariance.
LSQNONNEG Linear least squares with nonnegativity constraints.
SPAUGMENT Form least squares augmented system.
LMS Construct a least mean square (LMS) adaptive algorithm object.
RLS Construct a recursive least squares (RLS) adaptive algorithm object.
ADAPTLMS Least mean squared (LMS) FIR adaptive filter.
ADAPTNLMS Normalized least mean squared (LMS) FIR adaptive filter.
ADAPTRLS Recursive least-squares (RLS) FIR adaptive filter.
FIRLPNORM Least P-norm optimal FIR filter design.
IIRLPNORM Least P-norm optimal IIR filter design.
IIRLPNORMC Constrained least P-norm optimal IIR filter design.
firlpnormdemo.m: %% Least Pth-norm Optimal FIR Filter Design
iirlpnormdemo.m: %% Least Pth-Norm Optimal IIR Filter Design
ECMLSRMLE Least-squares regression (with missing data).
ECMLSROBJ Objective function for least-squares regression (with missing data).
MVNRFISH Fisher information for multivariate normal or least-squares regression.
PLSR Determine impulse response coefficients via Partial Least Squares.
APRECON Banded preconditioner function for least-squares problems.
LSQCURVEFIT solves non-linear least squares problems.
LSQLIN Constrained linear least squares.
LSQNONLIN solves non-linear least squares problems.
optdeblur.m: %% Large-Scale Constrained Linear Least-Squares
ylwk.m: %YWALK Recursive filter design using a least-squares method.
FIRCLS Linear-phase FIR filter design by constrained least-squares.
FIRCLS1 Low high pass FIR filter design by constrained least-squares.
FIRLS Linear-phase FIR filter design using least-squares error minimization.
INVFREQS Analog filter least squares fit to frequency response data.
INVFREQZ Discrete filter least squares fit to frequency response data.
YULEWALK Recursive filter design using a least-squares method.
fdfirls Firls - Least Squares Module for filtdes.
SPAP2 Least squares spline approximation.
LSLINE Add least-squares fit line to scatter plot.
NLINFIT Nonlinear least-squares regression.
REGRESS Multiple linear regression using least squares.
MSSGOLAY provides least-squares polynomial smoothing of mass spectrometry
XREGLSQ linear least squares for large sparse problems
XREGPRECOND preconditioner for least squares problems
DSPBLKFIRLS Mask dynamic dialog function for least-squares FIR filter block
DSPBLKFIRLS2 Mask dynamic dialog function for least-squares FIR filter block
LCM Least common multiple.
gls_fitB.m: % LOCALBSPLINE/GLS_FITB least-squares estimation of localbspline
fit.m: % LOCALMOD/FIT obtains least squares estimate of a Local Model fit
GLS Generalized least squares (with weights)
gls_costB.m: % LOCALMOD/GLS_COSTB generalised least-squares cost function for localmod coefficients
OLS Ordinary least squares (with weights)
GLS_FITB least-squares estimation of localmulti
gls_fitB.m: % POLYNOM/GLS_FITB least-squares estimation of localpspline
gls_fitB.m: % localpspline/GLS_FITB least-squares estimation of localpspline
gls_fitB.m: % POLYNOM/GLS_FITB least-squares estimation of localpspline
gls_fitB.m: % TRUNC/GLS_FITB least-squares estimation of localtruncps
gls_fitB.m: %LOCALUSERMOD/GLS_FITB least-squares estimation of localpspline
FITMODEL Obtain least squares estimate of model
LEASTSQ least squares estimate of model
InitModel.m: % XREGINTERPRBF/INITSTORE initialises model for use by stats and leastsq
InitModel.m: %INITSTORE initialises model for use by stats and leastsq
INITSTORE initialises model for use by stats and leastsq
LEASTSQ least squares estimate of model
lsqom.m: % XREGLINEAR/LSQOM ordinary least squares fit
minpress.m: % XREGLINEAR/MINPRESS minimum press least squares fit
PRUNE Prune least squares model from last term
QRDECOMP QR decompostion for least squares
quicklsq.m: % xreglinear/LEASTSQ least squares estimate of model
FITMODEL Obtain least squares estimate of model
InitModel.m: % xreglinear/INITSTORE initialises model for use by stats and leastsq
InitStore.m: % xreglinear/INITSTORE initialises model for use by stats and leastsq
leastsq.m: % xreglinear/LEASTSQ least squares estimate of model
LEASTSQ Initialise and train neural network
genetic.m: % FITALGORITHM/NLLEASTSQ
nlleastsq.m: % FITALGORITHM/NLLEASTSQ
LEASTSQ least squares estimate of model
ROLS regularized orthogonal least squares (rols)
leastsq.m: %LEASTSQ
leastsq.m: % xregusermod/LEASTSQ
lsqopt.m: %XREGUSERMOD/LSQOPT cost function for least squares (fmincon)
optimargs.m: %XREGUSERMOD/OPTIMARGS input arguments for least squares optimisation
APRECON Banded preconditioner function for least-squares problems.
CFLSQCURVEFIT Solves non-linear least squares problems.
CFLSQLIN Constrained linear least squares.
LSQNCOMMON Solves non-linear least squares problems.
LSQSUB Linear least squares constrained subproblem.
NLSQ Solves non-linear least squares problems.
SEARCHQ Line search routine for FMINU and LEASTSQ functions.
SNLS Sparse nonlinear least squares solver.
FQUAD Evaluate quadratic or linear least squares function.
LSQNCOMMON Solves non-linear least squares problems.
NLSQ Helper function that solves non-linear least squares problems.
SLLSBOX Linear least-squares with bounds
SNLS Sparse nonlinear least squares solver.
LSQISOTONIC Isotonic least squares.