2012-04-10 50 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 2012-04-10 00:50:09

+0

任何一個都沒問題,只要它有效 – Ice 2012-04-10 00:50:50

+0

您是否爲類重載'operator []''Matrixf '? – chrisaycock 2012-04-10 00:51:05

0

我從你的另一個問題中看到你寫了一個光線跟蹤器。

在射線追蹤器中,常見的情況是,您對矢量和矩陣具有不同的數據結構,因爲它們幾乎總是以不同方式使用,編程中的專業化幾乎總是會導致更快的代碼。

如果您隨後只爲矢量定義點積,則點積產品代碼將變得簡單。