向量在C ++中的交叉乘积 [英] Cross product of vectors in C++

查看:228
本文介绍了向量在C ++中的交叉乘积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我正在写的程序的一部分,我需要找到双精度向量和复数向量向量的叉积。我写了一个函数,我觉得应该这样做,但当我调用它,我得到以下错误:

 错误:没有匹配函数调用'CrossProduct1D(std :: vector< double,std :: allocator< double>>& std :: vector< std :: complex< double>,std :: allocator< std ::复杂< double>>>&)  

  #include< iostream> 
#include< fstream>
#include< sstream>
#include< string>
#include< vector>
#include< math.h>
#include< complex>
using namespace std;

// 1D cross product
template< typename T>
vector< T> CrossProduct1D(vector< T> const& a,vector< T> const& b)
{
vector& r(a.size());
r [0] = a [1] * b [2] -a [2] * b [1]
r [1] = a [2] * b [0] -a [0] * b [2]
r [2] = a [0] * b [1] -a [1] * b [0]
return r;
}

// ::::::::从文本导入数据::::::::::
vector< string>埃佐夫
ezivec.reserve(4000);

string ezidat(ez.i.txt);

ifstream ezifile;
ezifile.open(ezidat.c_str());

if(!ezifile.is_open())
{
cerr<<错误打开文件:<<< ezidat.c_str()<< endl ;
return -1;
}

string ezistr; //存储向量中的行
while(getline(ezifile,ezistr,';'))
{
ezivec.push_back(ezistr);
}

ezifile.close();

//从字符串向量转换为浮点数向量
向量< double> ezi(ezivec.size());
for(int i = 0; i {
ezi [i] = string_to_T double(ezivec [i]);
}

//

向量< string>埃兹尔韦
ezrvec.reserve(4000);

string ezrdat(ez.r.txt);

ifstream ezrfile;
ezrfile.open(ezrdat.c_str());

if(!ezrfile.is_open())
{
cerr<<错误打开文件:<<< ezrdat.c_str()<< endl ;
return -1;
}

string ezrstr;
while(getline(ezrfile,ezrstr,';'))
{
ezrvec.push_back(ezrstr);
}

ezrfile.close();

矢量< double> ezr(ezrvec.size());
for(int i = 0; i {
ezr [i] = string_to_T double(ezrvec [i]);
}

// :::::::定义向量:::::::
向量< vector< complex< double> > > E0(ezi.size(),vector< complex&Double;>(3)
for(int i = 0; i {
E0 [i] [0] .real()= 0.0;
E0 [i] [0] .imag()= 0.0;
E0 [i] [1] .real()= 0.0;
E0 [i] [1] .imag()= 0.0 ;;
E0 [i] [2] .real()= ezr [i];
E0 [i] [2] .imag()= ezi [i];
}

矢量< double> n_a(3);
n_a [0] = 1.0;
n_a [1] = 0.0;
n_a [2] = 0.0;

// :::::::调用交叉乘积:::::::
for(int j = 1; j< jmax; j ++)
{
M [j] = CrossProduct1D(n_a,E0 [j]);
}

ez.i.txt和ez.r.txt

解决方案

您的模板函数参数化为单一类型, T ,并且需要两个向量< T> ,但是你试图传递它两种不同类型的向量,所以没有单个<$ c $



您可以有两个模板参数,例如

 模板< class T,class U> CrossProduct1D(std :: vector< T> const& a,
std :: vector< u> const& b)


As a part of a program that I'm writing, I need to find the cross product of a vector of doubles and a vector of complex doubles. I've written a function that I feel should do this, but when I call it, I get the following error:

error: no matching function for call to ‘CrossProduct1D(std::vector< double, std::allocator<double> >&, std::vector<std::complex<double>, std::allocator<std::complex<double> > >&)’

Here is my code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <complex>
using namespace std;

//1D cross product
template <typename T>
vector<T> CrossProduct1D(vector<T> const &a, vector<T> const &b)
{
  vector<T> r (a.size());  
  r[0] = a[1]*b[2]-a[2]*b[1];
  r[1] = a[2]*b[0]-a[0]*b[2];
  r[2] = a[0]*b[1]-a[1]*b[0];
  return r;
}

//::::::::importing data from text::::::::::
  vector<string> ezivec;
  ezivec.reserve(4000);

  string ezidat("ez.i.txt");

  ifstream ezifile;
  ezifile.open(ezidat.c_str());

  if(!ezifile.is_open())
    {
      cerr<<"Error opening file : "<<ezidat.c_str()<<endl;
      return -1;
    }

  string ezistr; //store lines in vector
  while(getline(ezifile, ezistr, ';'))
    {
      ezivec.push_back(ezistr);
    }

  ezifile.close();

 //Converting from vector of strings to vector of floats
  vector<double> ezi (ezivec.size());
  for(int i = 0; i < ezivec.size(); ++i)
    {
      ezi[i] = string_to_T<double>(ezivec[i]);
    }

 //

  vector<string> ezrvec;
  ezrvec.reserve(4000); 

  string ezrdat("ez.r.txt");

  ifstream ezrfile;
  ezrfile.open(ezrdat.c_str());

  if(!ezrfile.is_open())
    {
      cerr<<"Error opening file : "<<ezrdat.c_str()<<endl;
      return -1;
    }

  string ezrstr;
  while(getline(ezrfile, ezrstr, ';'))
    {
      ezrvec.push_back(ezrstr);
    }

  ezrfile.close();

  vector<double> ezr (ezrvec.size());
  for(int i = 0; i < ezrvec.size(); ++i)
    {
      ezr[i] = string_to_T<double>(ezrvec[i]);
    }

//:::::::defining vectors:::::::
vector<vector<complex<double> > > E0 (ezi.size(), vector<complex<double> > (3));
for(int i = 0; i < ezi.size(); i++)
{
  E0[i][0].real() = 0.0;
  E0[i][0].imag() = 0.0;
  E0[i][1].real() = 0.0;
  E0[i][1].imag() = 0.0;;
  E0[i][2].real() = ezr[i];
  E0[i][2].imag() = ezi[i];
} 

vector<double> n_a (3);
n_a[0] = 1.0;
n_a[1] = 0.0;
n_a[2] = 0.0;

//:::::::calling cross product:::::::
for(int j = 1; j < jmax; j++)
    {
      M[j] = CrossProduct1D(n_a, E0[j]);
    }

"ez.i.txt" and "ez.r.txt" are semicolon-delimited text files of 4000 numbers.

解决方案

Your template function is parameterized on a single type, T, and takes two vector<T> but you are trying to pass it two different types of vectors so there is no single T that can be selected.

You could have two template parameters, e.g.

template<class T, class U> CrossProduct1D(std::vector<T> const& a,
                                          std::vector<U> const& b)

这篇关于向量在C ++中的交叉乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆