1

2 つのベクトルの daxpy 操作を実行して結果を出力すると思われるこのコードがありますが、実行すると 3 つの 4 が返されます (3 つの 6 が返されると思われます)。

私は、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);
  }
}
4

2 に答える 2

2

あなたはするつもりはありませんか:

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

それと、配列の境界外のインデックスにアクセスしています。配列サイズが 3 の場合、最大インデックスは A[2] です。

于 2013-05-17T19:19:51.760 に答える
1

ダクピーの情報はこちら!ついに!わかりやすくするために、コードにコメントを付けました。私は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

于 2013-05-17T20:19:40.830 に答える