用C++实现向量二范数
#include<iostream>
#include<math.h>
#include <iomanip>
#include <fstream>
using namespace std;
double norm2(double *cvel1,double *cvel2,int N)
{
double c=0;
for(int i=0;i<N;i++)
c=c+(cvel1[i]-cvel2[i])*(cvel1[i]-cvel2[i]);
c=sqrt(c);
return c;
}
int main()
{
int N=10;
int M=10;
double *cvel1=new double [(N+1)*(M+1)];
for(int bk=0;bk<(N+1)*(M+1);bk++)
{
cvel1[bk]=1;
// cout<<testx[bk]<<" ";
}
double *cvel2=new double [(N+1)*(M+1)];
for(int bk=0;bk<(N+1)*(M+1);bk++)
{
cvel2[bk]=0;
// cout<<testx[bk]<<" ";
}
cout<<norm2(cvel1,cvel2,(N+1)*(M+1));
return 0;
}