2013-05-17 1 views
1

У меня есть этот код, который, как предполагается, выполняет операцию daxpy двух векторов и выводит результат, но он дает мне 3 4, когда я запускаю его (я думаю, что это должно дать мне 3 6).Почему эта daxpy не работает в C++?

Мне кажется, что у меня что-то имеет ключевое значение в отношении daxpy, но я понятия не имею, что это такое.

Вот код:

#include <iostream> 
using namespace std; 



extern "C" 
{ 

    double daxpy_(double *A, double *B, int *n, double *a); 
//The daxpy fortran function shown above multiplies a first matrix 'A' by a constant 'a' 
//and adds the result to a second matrix 'B.' Both matrices are of size 'n.' 
} 

void daxpy(double *A, double *B, int n, double a); 
//The function above is declared in order to utilize c-notation to perform the fortran 
//daxpy function which takes a first matrix 'A' and multiplies the matrix by a costant 'a' 
//and then adds the result to a second matrix 'B.' 

int main(int argc, char *argv[]) 
{ 
    double A[3], B[3]; 
    int n=3; 
    double a=1.0; 
    for (int i=0;i<3;i++) 
    { 
     A[i]=2; 
     B[i]=4; 
    } 
    daxpy(A, B, n, a); 
    for (int i=0;i<3;i++) 
    { 
     cout << B[i] << endl; 
    } 
} 

void daxpy(double *A, double *B, int n, double a) 
{ 
    for (int i=0;i<n;i++) 
    { 
    B[i]=daxpy_(A, B, &n, &a); 
    } 
} 

ответ

1

Вот информация о daxpy! В заключение! Я прокомментировал код, чтобы было легче понять. Я назвал daxpy совершенно неправильным. Это будет работать, хотя !!!

Ответ 6 6 6!

#include <iostream> 
using namespace std; 


extern "C" //This is important to get the function from the -lblas library you will use when compiling 
{ 
    double daxpy_(int *n, double *a, double *A, int *incA, double *B, int *incB); 
//The daxpy fortran function shown above multiplies a first matrix 'A' by a constant 'a' 
//and adds the result to a second matrix 'B.' Both matrices are of size 'n.' 
} 

void daxpy(int n, double a, double *A, int incA, double *B, int incB); 
//The function above is declared in order to utilize c-notation to perform the fortran 
//daxpy function which takes a first matrix 'A' and multiplies the matrix by a costant 'a' 
//and then adds the result to a second matrix 'B.' 

int main(int argc, char *argv[]) 
{ 
    double A[3], B[3]; //Just initializing and filling up some matrices here 
    int n=3, incA=1, incB=1; 
    double a=1.0; 
    for (int i=0;i<3;i++) 
    { 
     A[i]=2; 
     B[i]=4; 
    } 
    daxpy(n, a, A, incA, B, incB); //This is the important part! Note the call notation 
    for (int i=0;i<3;i++) 
    { 
     cout << B[i] << endl; 
    } 
} 

void daxpy(int n, double a, double *A, int incA, double *B, int incB) 
{ 
    daxpy_(&n, &a, A, &incA, B, &incB); //Once again, note the call notation. Important! 
} 

Как скомпилировать: g++ (program name) -lblas //Note the -lblas is calling the blas stuff

2

Вы не хотел делать:

for (int i=0;i<3;i++) 
{ 
    A[i]=2; 
    B[i]=4; 
} 

Это и вы обращаетесь индекс вне границ массива. Максимальный индекс A [2] для размера массива 3.

+0

о ... правильно. Это работает. Тем не менее, я все равно не получаю правильный ответ. – Mechy

+0

Не понимаю. Мой цикл for только доходит до 2 так, как есть. Поскольку условие, которое я установил, было то, что я должен быть меньше 3. Как только он достигнет 3, он не будет работать. О, неважно. Я понимаю, о чем вы сейчас говорили. Это исправлено – Mechy

+0

Это то, что он должен делать. Таким образом вы индексируете 'A [0]', 'A [1]' и 'A [2]', которые являются всеми элементами в векторе длины 3. –