1

ベクトルと行列 (バージョン 2 の Vec_DP と Mat_DP) の数値レシピ定義を Visual C++ 2008 の MFC アプリケーション プロジェクトに使用しています。ほとんどですが、私のプログラムでは、使用されているベクトルと行列の内容を「クリア」するためにリセット関数が必要です。

私はすでに delete(mat)、delete[] (mat)、free(mat)、mat.~NRMat() を試しましたが、以前の作業はどれもありませんでした。私の最初の解決策は、ループを作成してすべての要素をゼロにすることでしたが、よりエレガントな解決策になるはずです。何かアイデアはありますか?

私のコードの例:

#include "stdafx.h"
#include <math.h>
#include <cmath>
#include <stdio.h>
#include "nrtypes.h"
#include "nrutil.h"

void TestFunc(Vec_I_DP &data);
void printVecDP(Vec_I_DP & v);
void printMatDP (Mat_I_DP & m);

Mat_DP mat(5,6);
Mat_DP mat2;
Mat_DP (*kktest);  

int main()
{
    DP v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
    Vec_DP vdp(v, 5);
    Vec_DP vdp2; 

    vdp2 = vdp;
    cout << " Here's vdp: " << endl;
    printVecDP(vdp);
    cout << " Here's vdp2:" << endl;
    printVecDP(vdp2);

    TestFunc(vdp2);
    cout << " matrix:" << endl;
    printMatDP(mat);
    printMatDP(mat2);
    cout << " matrix2:" << endl;
    printMatDP(*kktest);

    /* none of this is working */
    //mat.~NRMat();  
    //~NRMat(mat);
    //delete[](mat);
    //free(mat);

    for (int i=0; i<mat.nrows(); i++){
        for (int j=0; j<mat.ncols(); j++){
            mat[i][j] = 0;
        }
    }

    cout << " matrix:" << endl;
    printMatDP(mat);
    printMatDP(mat2);
    cout << " matrix2:" << endl;
    printMatDP(*kktest);

    return 0;
}

void TestFunc(Vec_I_DP &data)
{
    int n, m, t=data.size();
    Vec_DP rsp = data;  
    for (n=0; n<t; n++)
        rsp[n] = data[n]*10.0;


    for (n=0; n<t; n++){
        for (m=0; m<6; m++){
            mat[n][m] = rsp[n];
        }
    }
    mat2=mat;
    kktest = &mat;
}

void printVecDP (Vec_I_DP & v)
{
    if (v.size() == 0) {
        cout << " In printVecDP: The vector is empty." << endl;
    }
    else {
        for (int i = 0; i < v.size(); i++) {
            cout << " v[" << i << "] = " << v[i] << endl;
        }
    }
    cout << endl;
}

void printMatDP (Mat_I_DP & m)
{
    if ((m.nrows() == 0) | (m.ncols() ==0)) {
        cout << " In printMatDP: The matrix is empty." << endl;
    }
    else {
        for (int i = 0; i < m.nrows(); i++) {
            for (int j = 0; j < m.ncols(); j++) {
                cout << " m["<<i<<"]"<<"["<<j<<"] = " << m[i][j];
            }
            cout << endl;
        }
    }
    cout << endl;
}

PS : NR フォーラムで質問したいのですが、特権のため、またはフォーラムが新しい投稿に対して閉鎖されていたため、新しいスレッドを投稿できませんでした。

4

0 に答える 0