2013-05-17 1 views
1

J'ai ce code supposé faire l'opération daxpy de deux vecteurs et sortir le résultat, mais il me donne 3 4 quand je l'exécute (je pense que c'est supposé me donner 3 6). J'ai l'impression qu'il me manque quelque chose d'important à propos de Daxpy, mais je n'ai aucune idée de quoi il s'agit.Pourquoi ce daxpy ne fonctionne pas en C++?

Voici le code:

#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); 
    } 
} 

Répondre

1

Voici l'information sur daxpy! Finalement! J'ai commenté le code pour le rendre plus facile à comprendre. J'appelais Daxpy complètement faux. Cela fonctionnera bien !!!

La réponse est 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! 
} 

Comment compiler: g++ (program name) -lblas //Note the -lblas is calling the blas stuff

2

Ne vous vouliez faire:

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

Ce et vous accédez à un index en dehors des limites du tableau. L'index maximum est A [2] pour une taille de tableau de 3.

+0

oh ... droite. Ça marche. Je ne comprends toujours pas la bonne réponse. – Mechy

+0

Je ne comprends pas. Ma boucle for ne va qu'à 2 comme ça. Comme la condition que j'ai définie était que je devais être inférieur à 3. Une fois qu'il atteint 3, il ne fonctionnera pas. Oh peu importe. Je vois de quoi tu parlais maintenant. C'est corrigé – Mechy

+0

C'est ce qu'il devrait faire. De cette façon, vous indexez 'A [0]', 'A [1]' et 'A [2]' qui sont tous les éléments d'un vecteur de longueur 3. –