00001 #include "GVector.h"
00002 #include <cmath>
00003
00004 using namespace GMathLib;
00005 using namespace GMathLib::IO;
00006
00007 GVector::GVector()
00008 : GMatrix()
00009 {
00010 Class_Name("GVector");
00011 }
00012
00013 GVector::~GVector()
00014 {
00015 }
00016
00017
00018 GVector::GVector(int size, int vec_type, double *p_data0)
00019 : GMatrix(1, size, p_data0)
00020 {
00021
00022
00023
00024
00025 Class_Name("GVector");
00026
00027
00028 if( vec_type == GVector::ROW_VECTOR ){
00029 Reshape(1, size);
00030
00031 }else if( vec_type == GVector::COLUMN_VECTOR ){
00032 Reshape(size, 1);
00033
00034 }else{
00035 const std::string errinfo = "Specified vector type is invalid.";
00036 GError_Output::Puts(Class_Name() +"::GVector", errinfo);
00037 }
00038 }
00039
00040
00041 int GVector::Copy(const GVector& obj, int begin_id, int end_id, int pos)
00042 {
00043 int copy_size = end_id - begin_id + 1;
00044
00045
00046 if(begin_id > obj.Size()-1 || end_id > obj.Size()-1
00047 || pos + copy_size > Size() ){
00048 const std::string errinfo = "Specified range or position is inappropriate.";
00049 GError_Output::Puts(Class_Name() +"::Copy(GVector, int, int, int)", errinfo);
00050
00051 return 1;
00052 }
00053
00054
00055 for(int i=0; i < copy_size; i++){
00056 Set(pos + i, obj.Get(begin_id + i));
00057 }
00058
00059 return 0;
00060 }
00061
00062 double GVector::Norm()
00063 {
00064 int size = Size();
00065 double tmp = 0.0;
00066
00067
00068 for(int i=0; i < size; i++){
00069 tmp += Get(i) * Get(i);
00070 }
00071
00072 return std::sqrt(tmp);
00073 }
00074
00075 double GVector::Dot_Product(GVector& vec1, GVector& vec2){
00076
00077
00078
00079
00080 if( vec1.Row() == 1 && vec2.Column() == 1 ){
00081 double tmp =0.0;
00082 double size = vec1.Row();
00083
00084 for(int i=0; i < size; i++){
00085 tmp += vec1(i) * vec2(i);
00086 }
00087
00088 return tmp;
00089
00090 }else{
00091 const std::string errinfo = "Shapes of specified vector are invalid.";
00092 GError_Output::Puts( vec1.Class_Name() +"::Dot_Product", errinfo);
00093 }
00094
00095 return -1;
00096 }
00097
00098
00099 void GVector::Cross_Product(GVector& vec1, GVector& vec2){
00100
00101
00102 if( vec1.Size() == 3 && vec2.Size() == 3 )
00103 {
00104
00105
00106 Set(0, vec1(1) * vec2(2) - vec1(2) * vec2(1));
00107 Set(1, vec1(2) * vec2(0) - vec1(0) * vec2(2));
00108 Set(2, vec1(0) * vec2(1) - vec1(1) * vec2(0));
00109
00110 }else{
00111 const std::string errinfo = "Shapes of specified vector are invalid.";
00112 GError_Output::Puts( vec1.Class_Name() +"::Cross_Product", errinfo);
00113 }
00114 }
00115