3 Replies - 448 Views - Last Post: 26 April 2012 - 08:30 AM Rate Topic: -----

#1 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

How do I fix this error?

Posted 26 April 2012 - 07:59 AM

Hi, My code keeps returning this error:

[andre220@bps1240-15 wk4]$ g++ Energy.cpp -o Energy
Energy.cpp: In function ‘void eigen(double (*)[5], double*, double (*)[5], double*, double (*)[5], double (*)[5], double*, double*, double (*)[5])’:
Energy.cpp:183: error: cannot convert ‘double (*)[5]’ to ‘double’ for argument ‘1’ to ‘void matrix_iter(double, double, double, double, double, double, double, double, double)’
Energy.cpp: In function ‘void matrix_iter(double (*)[5], double*, double*, double (*)[5], double (*)[5], double*, double, double*, double (*)[5])’:
Energy.cpp:226: error: cannot convert ‘double (*)[5]’ to ‘double’ for argument ‘1’ to ‘void multiply(double, double, double)’
Energy.cpp:300: error: cannot convert ‘double (*)[5]’ to ‘double’ for argument ‘1’ to ‘void multiply(double, double, double)’

And I am not sure why because I checked and all of the arrays seem to be declared correctly, however, this is obviously not the case.
Here is my code:

/*---------------------Compiler Flags------------------------*/
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
/*-----------------------------------------------------------*/

/*-------------------------Constants-------------------------*/
const double hbar = 1;	//Planck's constant/ 2pi
const double m = 1;	//particle mass
const double L = 10.0; //Length
const int N = 5;	//number of lattice points
const int nvec = 1;
const double omega = 1;	//oscillator frequency
const double A = -0.25; //Kinetic Energy Constant
/*----------------------------------------------------------*/

/*----------------Functions----------------------------------*/
double V(double);
double KE_Mat_setup(double, double);
//KE_Mat_Setup(Kin_Mat)
double V_Mat_setup(double, double);
//V_Mat_Setup(xvec,Potential)
double KE();
//Constant for for KE
double Hamil_Mat_setup(double, double, double,double);
//Hamilton(Kin_Mat, V_Mat, xvec, H_Mat)
void Eigen(double, double, double, double, double, double, double, double, double, double);
//Eigen(H_Mat)

void matrix_iter(double, double, double, double, double, double, double, double, double);

void multiply(double, double, double);
/*----------------------------------------------------------*/

double V(double x) 
{
	// V(x) = 1/2 * m * w^2 * x^2
	return 0.5 * m * omega * omega * x * x;
}

//Kinetic Matrix setup 
double KE_Mat_setup(double Kin_Mat[N][N])
{
	for (int i = 0; i<N; i++)
	{
		for (int j = 0; j<N; j++)
		{
			//Diagonal elements
			if (i == j)
			{
				Kin_Mat[i][j] = -2 * A;
			}
			//Off diagonal elements by 1
			else if (abs(i-j) == 1)
			{
				Kin_Mat[i][j] = 1 * A;
			}
			//Other off diagonal elements
			else 
			{
				Kin_Mat[i][j] = 0;
			}
			
		}
	}

	//Print Matrix
	/*
	for (int i=0; i<N; ++i)
  	{
    		for (int j=0; j<N; ++j)
    		{
      		printf("%03f ", Kin_Mat[i][j]);
      	}
    			cout << '\n';
  	}	
	*/
	return Kin_Mat[N][N];
}


//Potential Matrix setup	
double V_Mat_setup(double xvec[N], double V_Mat[N][N])
{	
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			//Diagonal elements
			if (i==j)
			{
				V_Mat[i][j] = V(xvec[i]);
			}
			//Off diagonal elements
			else
			{
				V_Mat[i][j] = 0;
			}
		}
	}

	//Print Matrix
	/*
	for (int i=0; i<N; ++i)
  	{
    		for (int j=0; j<N; ++j)
    		{
      		printf("%03f ", V_Mat[i][j]);
      	}
    			cout << '\n';
  	}
	*/
	return V_Mat[N][N];
}


//Setting up hamiltonian matrix
double Hamil_Mat_setup(double Kin_Mat[N][N], double V_Mat[N][N], double xvec[N], double H_Mat[N][N])
{
	//Sets each initial element in H_Mat = 0
	for (int i = 0; i < N; i ++)
	{
		for (int j = 0; j < N; j++)
		{
			H_Mat[i][j] = 0;
		}
	}
	
	//Adds each ij element of Kin_Mat and V_Mat to get H_Mat
	for (int i = 0; i < N; i ++)
	{
		for (int j = 0; j < N; j ++)
		{
			H_Mat[i][j] += Kin_Mat[i][j] + V_Mat[i][j];
		}
	}
	
	//Subtracts a large constant from each ij element of H_Mat
	for (int i = 0; i < N; i ++)
	{
		for (int j = 0; j < N; j++)
		{
			H_Mat[i][j] -= 100;
		}
	}

	//Print Matrix
	
	cout << endl << "Hamiltonian" << endl;
	for (int i=0; i<N; ++i)
  	{
    		for (int j=0; j<N; ++j)
    		{
      		printf("%03f ", H_Mat[i][j]);
      	}
    			cout << '\n';
  	}
  	
	return H_Mat[N][N];
}

void eigen(double H_Mat[N][N], double mat_y[N], double mat_d[N][N], double mat_x[N], double mat_b[N][N], double mat_c[N][N], double mat_a[N], double freq[N], double eig[N][N]) 
{
	int i, j;
	double eps = 1e-10;
	
	for (i=0; i < N; i ++)
	{
		mat_y[i] = 1.0;
	}
	
	for ( i =0; i < N; i ++)
	{
		for (j = 0; j < N; j ++)
		{
			mat_d[i][j] = H_Mat[i][j];
		}
	}

	matrix_iter(mat_d, mat_x, mat_y, mat_b, mat_c, mat_a, eps, freq, eig);

	cout << endl << "Normalization Factor" << endl << endl;
	
	for (i = 0; i < nvec; i ++)
		cout << freq[i] << endl;
	cout << endl << endl << endl << "Eigenvectors:" << endl << endl;
	
	for (i = 0; i < N; i ++)
	{
		for (j = 0; j < nvec; j ++)
		{
			cout << eig[i][j] << endl;
		}
	}
	cout << endl << "The Matrix D" << endl;
	for (int i=0; i<N; ++i)
  	{
    		for (int j=0; j<N; ++j)
    		{
      		printf("%03f ", mat_d[i][j]);
      	}
    			cout << '\n';
  	}
	return;	
}

//Iteration Method
void matrix_iter( double mat_d[N][N], double mat_x[N], double mat_y[N], double mat_b[N][N], double mat_c[N][N], double mat_a[N], double eps, double freq[N], double eig[N][N])
{
	int i, j, icon, ii;
	double con, alam, sum, alp;
	
	con = mat_y[0];
	
	for (i = 0; i < N; i ++)
	{
		mat_x[i] = mat_y[i] / con;
	}
	
	do 
	{
		icon = 0;
		multiply(mat_d, mat_x, mat_a);
		
		alam = mat_a[0];
		
		for (i = 0; i < N; i ++)
		{
			mat_a[i] = mat_a[i] / alam;
		}
		
		for (i = 0; i < N; i ++)
		{
			if (fabs((mat_a[i] - mat_x[i]) / mat_x[i]) > eps)
				icon = 1;
		}
		
		for (i = 0; i < N; i ++)
		{
			mat_x[i] = mat_a[i];
		}
	} while (icon != 0);
	
	icon = 0;
	freq[0] = sqrt(1.0 / alam);
	
	for ( i = 0; i < N; i ++)
	{
		eig[i][0] = mat_x[i];
	}
	
	ii = 1;
	
	while (ii != nvec)
	{
		ii = ii + 1;
		sum = 0.0;
		for (i = 0; i < N; i ++)
		{
			sum = sum + mat_x[i]* mat_x[i];
		}
		
		alp = sqrt(1.0 / sum);
		
		for (i = 0; i < N; i ++)
		{
			mat_x[i] = mat_x[i] * alp;
		}
		
		for (i = 0; i < N; i ++)
		{
			for (j = 0; j < N; j++)
			{
				mat_c[i][j] = mat_x[i] * mat_x[j];
			}
		}
		
		for (i = 0; i < N; i ++)
		{
			for (j = 0; j < N; j ++)
			{
				mat_d[i][j] = mat_d[i][j] - alam * mat_b[i][j];
			}
		}
		
		con = mat_y[0];
		
		for (i = 0; i < N; i ++)
		{
			mat_x[i] = mat_y[i] / con;
		}
		
		do 
		{
			icon = 0;
			
			multiply(mat_d, mat_x, mat_a);
			
			alam = mat_a[0];
			
			for (i = 0; i < N; i ++)
			{
				if (fabs((mat_a[i] - mat_x[i]) / mat_x[i]) > eps)
					icon = 1;
			}
			
			for (i = 0; i < N; i ++)
			{
				mat_x[i] = mat_a[i];
			}
		} while (icon != 0);
		
		icon = 0;
		freq[ii-1] = sqrt(1.0 / alam);
		
		for (i = 0; i < N; i ++)
		{
			eig[i][ii-1] = mat_x[i];
		}
	}
	
	return;

}


void multiply(double mat_d[N][N], double mat_x[N], double mat_a[N])
{	
	for (int i = 0; i < N; i ++)
	{
		mat_a[i] = 0.0;
		
		for (int j = 0; j < N; j ++)
		{
			mat_a[i] = mat_a[i] + mat_d[i][j] * mat_x[j];
		}
	}
	
	return;
}

int main()
{
	double H_Mat[N][N];
	double V_Mat[N][N];
	double Kin_Mat[N][N];
	double xvec[N];

	double mat_d[N][N], mat_x[N], mat_y[N], mat_b[N][N], mat_c[N][N], freq[N], eig[N][N], mat_a[N];

	int j;

	//Sets up xvec[N]
	for (j = 0; j < N; j++)
	{
		xvec[j] = -L + 2*L* (j + 0.5)/ N;
	}
	
	//Set up Kin_Mat
	KE_Mat_setup(Kin_Mat);
	
	//Set up V_Mat
	V_Mat_setup(xvec, V_Mat);
	
	//Set up Hamiltonian Matrix
	Hamil_Mat_setup(Kin_Mat, V_Mat, xvec, H_Mat);

	cout << "The energy Eigenvectors of the Ground state " << endl;
	
	eigen(H_Mat, mat_y, mat_d, mat_x, mat_b, mat_c, mat_a, freq, eig);
}



Is This A Good Question/Topic? 0
  • +

Replies To: How do I fix this error?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: How do I fix this error?

Posted 26 April 2012 - 08:04 AM

You're declaring your functions to take doubles, not arrays of doubles. That's why the compiler complains when you try to pass arrays. Change your forward-declarations to match the signature of your definitions.
Was This Post Helpful? 0
  • +
  • -

#3 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: How do I fix this error?

Posted 26 April 2012 - 08:13 AM

Ok thank you for the reply, could you give me an example of how to do this.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: How do I fix this error?

Posted 26 April 2012 - 08:30 AM

Just copy and paste the signature you use when defining the function to the line where you declare it (and remove the wrong declaration).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1