2012-04-10 3 views
-2

제 방법을 사용하기가 힘듭니다. 저는 C++의 초보자입니다. 두 벡터의 내적을 찾으려고합니다. for 루프에서 몇 가지 오류가 발생했습니다. 누군가 나를 도와주세요.C++에서의 점 제품 구현

float dot(Matrixf const& vec1, Matrixf const& vec2) { 

    // error check 
    if (!vec1.isVector() || !vec2.isVector()) { 
     throw std::runtime_error("Unable to do dot product: not column vectors."); 
    } 
    if (vec1.nrows() != vec2.nrows()) { 
     throw std::runtime_error("Unable to do dot product: vector lengths not equal."); 
    } 

    /** implementing dot product *************************************/ 

    float ret = 0; 

    for(unsigned i = 0; i < vec1.ncols(); ++i){ 
     for(unsigned j =0; j< vec2.nrows(); ++j){ 
     ret += vec1[i] * vec2[j]; 

} 
    } 
    return ret; 
} 

Matrixf 클래스

#include "matrixf.h" 

#include <iostream> 

Matrixf::Matrixf(unsigned int rows, unsigned int cols) { 
    rows_ = rows; 
    cols_ = cols; 
    data_ = new float[rows_ * cols_]; 

    // set all initial values to zero 
    for (unsigned int r = 0; r < rows_; ++r) { 
     for (unsigned int c = 0; c < cols_; ++c) { 
      data_[r * cols_ + c] = 0; 
     } 
    } 
} 

Matrixf::~Matrixf() { 
    delete data_; 
} 

Matrixf::Matrixf(Matrixf const& other) { 
    rows_ = other.rows_; 
    cols_ = other.cols_; 
    data_ = new float[rows_ * cols_]; 
    for (unsigned int i = 0; i < rows_ * cols_; ++i) { 
     data_[i] = other.data_[i]; 
    } 
} 

Matrixf& Matrixf::operator=(Matrixf const& other) { 
    // handles self assignment 
    if (this == &other) { 
     return *this; 
    } 

    delete data_; 
    rows_ = other.rows_; 
    cols_ = other.cols_; 
    data_ = new float[rows_ * cols_]; 
    for (unsigned int i = 0; i < rows_ * cols_; ++i) { 
     data_[i] = other.data_[i]; 
    } 
    return *this; 
} 

float Matrixf::get(unsigned int row, unsigned int col) const { 
#ifndef NDEBUG 
    if (row >= rows_ || col >= cols_) { 
     throw std::runtime_error("Matrix index out of bounds."); 
    } 
#endif 

    return data_[row * cols_ + col]; 
} 

void Matrixf::set(unsigned int row, unsigned int col, float val) { 
#ifndef NDEBUG 
    if (row >= rows_ || col >= cols_) { 
     throw std::runtime_error("Matrix index out of bounds."); 
    } 
#endif 
    data_[row * cols_ + col] = val; 
} 

float& Matrixf::operator()(unsigned int row, unsigned int col) { 
    return data_[row * cols_ + col]; 
} 

float Matrixf::operator()(unsigned int row, unsigned int col) const { 
    return data_[row * cols_ + col]; 
} 

unsigned int Matrixf::nrows() const { 
    return rows_; 
} 

unsigned int Matrixf::ncols() const { 
    return cols_; 
} 

bool Matrixf::isVector() const { 
    return (cols_ == 1); 
} 

Matrixf Matrixf::eye(unsigned int size) { 
    Matrixf e(size, size); 
    for (unsigned int i = 0; i < size; ++i) { 
     e.set(i, i, 1); 
    } 

    return e; 
} 

std::ostream& operator << (std::ostream& os, Matrixf const& matrix) { 
    for (unsigned int r = 0; r < matrix.nrows(); ++r) { 
     for (unsigned int c = 0; c < matrix.ncols(); ++c) { 
      os << matrix.data_[r * matrix.cols_ + c] << " "; 
     } 
     os << "\n"; 
    } 

    return os; 
} 

답변

2

난 당신이 하나 개의 루프를 원한다고 생각 :

for(unsigned i = 0; i < vec1.ncols(); ++i){ 
    ret += vec1[i] * vec2[i]; 
} 

나는 또한 당신이 비교 것을 알 수

vec1.nrows() != vec2.nrows() 

하지만 루프에서는 ncols()을 사용합니다. 어느 쪽이 원하는거야?

+0

여전히 vec1 [i]에서 오류가 발생합니다. "오류 : 피연산자 [없음]은 피연산자와 일치하지 않습니다. – Ice

+0

작동하는 한 그 중 하나는 괜찮습니다. – Ice

+0

클래스에 대해 연산자 []를 오버로드 했습니까? Matrixf '? – chrisaycock

0

다른 질문에서 레이 트레이서를 쓰는 것을 봅니다.

레이 트레이서에서는 거의 항상 항상 다르게 사용되기 때문에 일반적으로 벡터와 행렬에 대해 별도의 데이터 구조를 사용하는 경우가 많으며 프로그래밍의 전문화로 인해 항상 더 빠른 코드가 생성됩니다.

벡터에 대해서만 내적을 정의하면 내적 코드가 단순 해집니다.