CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2008
    Posts
    84

    How would I utilize this function?

    Ok, long story short, one of my homework assignments requires us the take a matrix to a power. I am fairly new to programming (not exactly a beginner but not up to the intermediate level yet). The files I downloaded for the assignment (pre-built code so we dont have to write our own matrix definitions) contains the following for taking a matrix to a power:

    Code:
    template<typename MatrixT> struct Power; //this is up towards the top of the file
    
    //the following is all further down, mixed in with many other matrix functions.
    template<typename MatrixT>
    struct Power
    // PURPOSE: Returns a matrix with all its elements raised to a defined power.
    // REQUIRE: A matrix conforming to the template class Matrix<ValueT,MatrixTypeP,MathP>.
    // PROMISE: A matrix with all the original elements raised to a defined power will be returned.
    {
    	typedef typename MatrixT::value_type value_type;
    
    	MatrixT operator()(const MatrixT& matrix, const value_type& power)
    	{
    		MatrixT powerMatrix;
    		(*this)(matrix, powerMatrix, power);
    		return powerMatrix;
    	}
    	void operator()(const MatrixT& matrix, MatrixT& powerMatrix, const value_type& power)
    	{
    		const size_t maxRows = matrix.row.size();
    		const size_t maxCols = matrix.col.size();
    
    		powerMatrix.resize(maxRows, maxCols);
    		for (size_t i=0; i<maxRows; ++i)
    		{
    			for (size_t j=0; j<maxCols; ++j) powerMatrix(i,j) = pow(matrix(i,j), power);	
    		}
    	}
    };
    I asked my professor and my professor suggested just creating a loop in the program to raise the matrix to a power. I have done that (the assignment is complete) but I would like to know how to call (and utilize) this function.

    At first I just tried

    matrix2 = Power(matrix1, 3);

    to raise matrix1 to the power of 3, but that gave me an error.

    NOTE: All this is, is my curiosity, I am not asking for homework help (as I said the assignment is already done), I am just curious as to how I can call the above function instead of using my own loop.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How would I utilize this function?

    It's amazing how often people will mention "an error" and seem to think that the contents of the error could not possibly be important to resolving it.

    It would help to at least know the template parameters that matrix1 and matrix2 were constructed with.

  3. #3
    Join Date
    May 2008
    Posts
    84

    Re: How would I utilize this function?

    Quote Originally Posted by Lindley View Post
    It's amazing how often people will mention "an error" and seem to think that the contents of the error could not possibly be important to resolving it.

    It would help to at least know the template parameters that matrix1 and matrix2 were constructed with.
    I didnt mention the error because I figured I was trying to call it improperly:

    Code:
    error C2955: 'YMatrix::Power' : use of class template requires template argument list
    see declaration of 'YMatrix::Power'
    YMatrix is the namespace in which the power function resides.

    matrix1 and matrix2 are both 7 x 7 matrices, matrix1 has some doubles in it (such as 1, 0, 0.33, etc) and matrix2 is empty (but already constructed).

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How would I utilize this function?

    I'm more interested in where matrix1 and matrix2 are declared, and what their class definition looks like. The functor should be able to deduce its template arguments from the function arguments, assuming they're properly specified.....

  5. #5
    Join Date
    May 2008
    Posts
    84

    Re: How would I utilize this function?

    Quote Originally Posted by Lindley View Post
    I'm more interested in where matrix1 and matrix2 are declared, and what their class definition looks like. The functor should be able to deduce its template arguments from the function arguments, assuming they're properly specified.....
    Hopefully this is what your looking for:

    Code:
    //This is all in the main section of my program
    typedef Matrix<double,DenseMatrix,MathMatrix> MatrixM;
    
    double a1[49] = {1,0,0,0,0,0,0, 0.67,0,0.33,0,0,0,0, 0,0.67,0,0.33,0,0,0, 0,0,0.67,0,0.33,0,0, 0,0,0,0.67,0,0.33,0, 0,0,0,0,0.67,0,0.33, 0,0,0,0,0,0,1};
    
    MatrixM matrix1(7,7);
    // fill matrices with array
    matrix1 = a1; //a1 is an array of values, what I mentioned about
    
    matrix2(7,7);
    Attached is the file I mentioned in my first post (the downloaded one) that contains all the constructors and what not for the matrices. Everything about the matrices works fine, but I cannot figure out the Power function. (I can for example, use matrix2 = matrix1 * matrix1; to get matrix1 to the power of 2, and that part works perfectly fine)
    Attached Files Attached Files

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How would I utilize this function?

    I don't know templates as well as some, but the following *should* work (although I'm not sure why it's necessary).....

    Code:
    matrix2 = Power<MatrixM>(matrix1, 3);
    You may need to change 3 to 3.0, I'm not sure about that.

  7. #7
    Join Date
    May 2008
    Posts
    84

    Re: How would I utilize this function?

    Tried that and got a different error:

    Code:
    error C2661: 'YMatrix::Power<MatrixT>::Power' : no overloaded function takes 2 arguments
    Which is kind of odd because looking at the Power function, it looks like it accepts 2 arguments, then creates a third matrix (powerMatrix) that it passes to itself to overload the part of the function that takes 3 arguments.

    I also tried it with 3 and 3.0, same thing on both.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How would I utilize this function?

    Oh, I think I know what's happening. See, Power is a function object, not just a function----so you need to create the object before you can use it:

    Code:
    Power<Matrix> powM;
    matrix2 = powM(matrix1, 3.0);
    Try that and see what happens.

  9. #9
    Join Date
    May 2008
    Posts
    84

    Re: How would I utilize this function?

    It was actually:
    Code:
    Power<MatrixM> powM;
    With that, it worked, raising all the elements to the power of 3, the problem though, with normal math (lets say 2^3) you get 8, which is the same as 2 * 2 * 2, I just realized, the Power function provided in the matrix.hpp file, it raises each indivual value by the power. This causes multiplying matrix1 * matrix1 * matrix1, to come out differently than raising matrix1 to the power of 3 (via the Power function).

    Multiply:
    Code:
    for (size_t i=0; i<sizerow1; ++i)
    {
    	for (size_t j=0; j<sizeCol2; ++j)
    	{
    		for (size_t k=0; k<sizeCol1; ++k)
    		{
    			tempMatrix(i, j) += childMatrix_(i,k) * x(k,j);
    		}
    	}
    }
    Whereas the Power function object is:

    Code:
    for (size_t i=0; i<maxRows; ++i)
    {
    	for (size_t j=0; j<maxCols; ++j) powerMatrix(i,j) = pow(matrix(i,j), power);	
    }
    That being said, thanks for the help, but the Power function gives a completely different answer than multiplying them through a loop. I think the professor is wrong on this one (multiplying a matrix by another matrix is very different than raising a matrix to a power), the way I think of it, if 2 states have a probability of 0.33&#37; of the game entering that state, then if you make 2 moves (each move means it will change state), there is a 0.1089% chance the game will enter that state on the second move.

    As I said, thanks for the help, got it working, now to check with my professor to see if his example (and multiplying via loop) were wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured