Jump to content


- - - - -

Finding Eigen Value


4 replies to this topic

#1 idreamlovey

    Member

  • Members
  • PipPip
  • 91 posts

Posted 15 May 2008 - 03:27 PM

Here is the following code to find the eigenvalues ...Tel me whether its correct or not....Cause the one coming from the solution doesn't match with the solution tested in http://www.akiti.ca/Eig3Solv.html



//------------------------------------------------------------------------------

//   Name: SolveCubicEquation(a*x^3 + b*x^2+ c*x + d = 0)

//	 Desc: Solve Equation thru Root Finding Formula

//	 http://en.wikipedia.org/wiki/Cubic_equation

//------------------------------------------------------------------------------

void SolveCubicEquation(const float &a,const float &b,const float &c, const float &d, float *fRoot, float *fRootI)

{

	float q = (3*a*c - (b*b)) / (9*a*a);

	float r = (9*a*b*c - 27*a*a*d - 2*b*b*b) / (54*a*a*a);

	float s = powf((r+sqrtf(q*q*q + r*r)), 1/3);

	float t = powf((r-sqrtf(q*q*q + r*r)), 1/3);


	if(fRoot && fRootI)

	{

		fRoot[0] =  (s+t) - b/(3*a);

		fRoot[1] = -(s+t)/2 - b/(3*a);

		fRootI[0] = sqrtf(3)*(s-t)/2; //Complex Number value

		fRoot[2] = -(s+t)/2 - b/(3*a);

		fRootI[1] = sqrtf(3)*(s-t)/2; //Complex Number value

	}

}


//------------------------------------------------------------------------------

//   Name: FindEigenValues

//         f[0]  f[1]  f[2]

//         f[3]  f[4]  f[5]

//         f[6]  f[7]  f[8]

//	 Desc: Find Eigen Value of symmetric 3X3 matrix 

//------------------------------------------------------------------------------

void FindEigenValues(const float *m, float **fE, float **fEI)

{

	float a	= -1;

	float b	= m[0]+m[1]+m[8];

	float c	= m[3]*m[1] - m[0]*m[1] - m[0]*m[8] - m[1]*m[8] + m[5]*m[7];

	float d	= m[0]*m[1]*m[8] - m[0]*m[5]*m[7] - m[1]*m[3]*m[8] + m[6]*m[5]*m[1] + 

			  m[2]*m[3]*m[7] - m[6]*m[4]*m[2];


	SolveCubicEquation(a,b,c,d,*fE, *fEI);

}



Here i got EigenValues as

fE[0], fE[1] + fEI[0], fE[1] + fEI[2]

#2 hovermonkey

    Member

  • Members
  • PipPip
  • 38 posts

Posted 15 May 2008 - 04:03 PM

1/3 == 0 in C++. (integer division)
So powf( x, 1/3 ) is always 1.0.
So your cubic solver is probably broken.
Try 1/3.0 instead.

#3 idreamlovey

    Member

  • Members
  • PipPip
  • 91 posts

Posted 15 May 2008 - 05:58 PM

Yeah..i also fix one more error there that is finding the value of "c".
But i got problem with powf function....powf(-0.0338922, 1/3.0) = -1.#IND0

Anybody tel me how to find cubic root of -0.0338922. Or is there any better method to find the cubicEquation or Eigen Value...Any links Any thing ....

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 15 May 2008 - 06:15 PM

There are problems with raising negative numbers to non-integer powers (it usually gives an imaginary number as result). I would write your own cube root function like this:
float cbrtf (float x)
{
    if (x < 0)
        return -powf(-x, 1/3.0);
    else
        return powf(x, 1/3.0);
}

That should fix the problems with negative numbers.

By the way, passing the float parameters to SolveCubicEquation as const-references isn't that great of an idea - pointers are at least as big as floats, so you're not saving any space, and you're adding extra indirections so you're not saving any performance either. Primitive types like float and int should generally be passed by value (unless they're out-parameters).
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 idreamlovey

    Member

  • Members
  • PipPip
  • 91 posts

Posted 15 May 2008 - 08:03 PM

THNX....it works... :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users