行列式中的分段错误(核心转储)

时间:2013-02-23 21:20:20

标签: c++ arrays matrix double

我在设置时遇到以下代码中的错误段故障(核心转储):

int et=pow(2,10)

或更多。 事实上,我正在计算n * n矩阵行列式。对于n=pow(2,10)或更多,代码会出错。 我真的不知道该怎么做

这是我的代码:

#include <cfloat>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;


int dimention = 3;
int et = pow(2,9);

在上面我放et= pow(2, 10)或更多时,它会给出错误。

int N = 1 ;
vector<                 
vector<                 
vector<double> > > r;   

//random function
double unifRand(){
   double X=((double)rand()/(double)RAND_MAX);  // creating random between 0 1
   return X;
}
void seed(){
    srand(time(0));  //initializing random
}
void initialize(){
    seed();
    r.clear();
    for (int n=0; n<N; n++){
        vector < vector<double> > exyz;
        for (int e=0; e<et; e++){
            vector < double > xyz;
            for (int d=0; d<dimention; d++)
                 xyz.push_back(unifRand()-0.5);
            exyz.push_back(xyz);
        }
        r.push_back(exyz);
    }

}

  void Det(vector<double>& r_et ){
       vector< vector<double> > Psi;
        for(int i=0; i<et; i++){
                vector<double> Psie;
                for (int e=0; e<et; e++){
                        double Ps= -2*r_et[e]+(r_et[e]-r_et[i]);
                        double Pse = exp(Ps);  
                        Psie.push_back(Pse);
                }
                Psi.push_back(Psie);
        }

        /* Transform matrix into upper triangular */
        int counti = 0;
        double temp=0;
        for(int i = 0; i < et - 1 ; i++){
        /* Elementary Row Operation I */
                if(Psi[i][i] == 0.0){
                        for(int k = i; k < et; k++){
                                if(Psi[k][i] != 0.0){
                                        for(int j = 0; j < et; j++){
                                                temp = Psi[i][j];
                                                Psi[i][j] = Psi[k][j];
                                               Psi[k][j] = temp;
                                       }
                                       k = et;
                               }
                       }   
               counti++;
               }
       /* Elementary Row Operation III */
               if(Psi[i][i] != 0.0){
                       for(int k = i + 1; k < et; k++){    // k is the row number
                               for(int j = i; j < et; j++){
                                   double factor = Psi[i][j] /  Psi[i][i];
                                   if (factor < (10^-10)){
                                       factor = 1;
                                   }

                  Psi[k][j] = Psi[k][j] + (-1.0 * Psi[k][i]*(Psi[i][j] /  Psi[i][i]) );
                               }
                       }
               }
       }

    /* Display upper triangular matrix */
    /* Calculate determinant */
    double temp2=1;
    for(int i = 0; i < et; i++){
            temp2 *= Psi[i][i];
           // cout << Psi[i][i];
       }

    /* Modify determinant */
        cout<<"\nDeterminant:\n";
       if(counti % 2 == 0){
               cout<<endl<<"     "<<temp2<<endl<<endl;
       }else{
               cout<<endl<<"     "<<-1*temp2<<endl<<endl;
       }

  }

  //compute trial wave function
  void step(int w){
      //make a trial move of each electron
      vector<double> r_td(dimention);
          for (int ee=0; ee<et; ee++){
                  for (int dd=0; dd<dimention; dd++){
                          r_td[ee] += r[w][ee][dd]*r[w][ee][dd];
                  }
                  r_td[ee]=sqrt(r_td[ee]);
          }

      Det(r_td);

  }

  int main(){
           initialize();

        for (int n=0; n<N; n++)
               step(n);
  return 0;
 }

1 个答案:

答案 0 :(得分:1)

  vector<double> r_td(dimention);  // dim 3   ---> et
      for (int ee=0; ee<et; ee++){
              for (int dd=0; dd<dimention; dd++){
                      r_td[ee] += r[w][ee][dd]*r[w][ee][dd];
                           ^^  pow(2,10)  ??