Example of Namespace
//============================================================================
// Name : namespace.cpp
// Author : chd
// Version :
// Copyright : Your copyright notice
// Description : Example of namespace C++, Ansi-style
//============================================================================
#include <iostream>
#include <math.h>
//#include <nurbs.h>
using namespace std;
namespace Vector {
const int maxsize=100000;
double onenorm(const double* const, int);
double twonorm(const double* const, int);
double maxnorm(const double* const, int);
} // namespace Vector
namespace Matrix {
double onenorm(double** , int);
double frobnorm(double**, int);
double maxnorm( double** , int);
//using namespace Vector;
} // namespace Matrix
int main() {
int size=100;
double**a=new double*[size];
for (int i = 0; i < size; ++i) {
a[i]=new double [size];
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
a[i][j]=1;
}
}
cout<<"The one norm of the vector is:"<<Vector::onenorm(a[1],size)<<endl;
cout<<"The two norm of the vector is:"<<Vector::twonorm(a[1],size)<<endl;
cout<<"The max norm of the vector is:"<<Vector::maxnorm(a[1],size)<<endl;
cout<<"The one norm of the matrix is:"<<Matrix::onenorm(a,size)<<endl;
cout<<"The frobenius norm of the matrix is:"<<Matrix::frobnorm(a,size)<<endl;
cout<<"The max norm of the matrix is:"<<Matrix::maxnorm(a,size)<<endl;
return 0;
}
double Vector::onenorm(const double* const v, int size){
double res=0;
if (size>maxsize) {
cout<<"vector size too large"<<endl;
}
for (int i = 0; i < size; ++i) {
res=res+fabs(v[i]);
}
return res;
}
double Vector::twonorm(const double* const v, int size){
double res=0;
if (size>maxsize) {
cout<<"vector size too large"<<endl;
}
for (int i = 0; i < size; ++i) {
res=res+v[i]*v[i];
}
return sqrt(res);
}
double Vector::maxnorm(const double* const v, int size){
double max=fabs(v[0]);
for (int i = 1; i < size; ++i) {
if (fabs(v[i])>max) {
max=v[i];
}
}
return max;
}
double Matrix::onenorm( double** a,int size){
double max=a[0][0];
for (int j = 0; j < size; ++j) {
double sum=0;
for (int i = 0; i < size; ++i) {
sum+=fabs(a[i][j]);
}
if (sum>max) {
max=sum;
}
}
return max;
}
double Matrix::maxnorm(double** a,int size){
using Vector::onenorm;
double max=Vector::onenorm(a[0],size);
for (int i = 1; i < size; ++i) {
if (onenorm(a[i],size)>max) {
max=onenorm(a[i],size);
}
}
return max;
}
double Matrix::frobnorm(double** a, int size){
double res=0;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
res+=a[i][j]*a[i][j];
}
}
return sqrt(res);
}
Like this:
Like Loading...
Related