| T O P I C R E V I E W |
| Rimmer |
Posted - 06/27/2002 : 11:32:37 AM I don't know if it's me, but everytime I try something new I come up against a brickwall. I have managed to create a number of matrices of various dimensions, and am now trying to perform some matrix operations on these. However, there seems to by absolutley no help whatsoever in regards to using the Transpose() function. I also want to multiply several matrices together and also obtain the inverse of a matrix.
Given that I have a known matrix X and Y [6x6] and [1x6] respectively and an unknown matrix a [1x6]. I want to be able to determine a by means of the following
a=(X_Transpose * X)^(-1) * X_Transpose * y
Hope my ramblings make sence. Any code examples showing how to find the inverse of a matrx, the tranpose, and any other matrix operations would be really appreciated.
--- Michael |
| 7 L A T E S T R E P L I E S (Newest First) |
| member1 |
Posted - 06/28/2002 : 1:53:09 PM Sorry, there is a mistake. vector pivot; should be changed to vector<int> pivot; Thanks
Edited by - member1 on 06/28/2002 14:00:01 |
| member1 |
Posted - 06/28/2002 : 11:29:45 AM Hi, Origin's matrix class will soon have a menber function called inverse to get the inverse matrix. now I suggest you to use the following function to get the inverse of the matrix: #define MATRIX_INVERSE_SIZE_ERROR -1 #define MATRIX_SINGUAL_ERROR -2
int invertMatrix(matrix mInput, matrix & mOutput) { matrix matMatrixTemp;
//first check the matrix size is n*n int nSize = mInput.GetNumRows(); if(nSize != mInput.GetNumCols()) { return MATRIX_INVERSE_SIZE_ERROR; } matMatrixTemp.SetSize(nSize,nSize);//set the result matrix matMatrixTemp = mInput; //first check the matrix is sigular or not //int nag_real_lu(int n,double a[],int tda,int pivot[],double *detf,int *dete) double detf; int dete; //variable to store the determinants vector pivot; pivot.SetSize(nSize*nSize); int i,j; matrix mUnitAndResult; mUnitAndResult.SetSize(nSize,nSize); // construct a nSize*nSize unit matrix for(int ii = 0; ii < nSize; ii++) for(int jj = 0; jj< nSize; jj++) { if(ii != jj) mUnitAndResult[ii][jj] = 0; else mUnitAndResult[ii][jj] = 1; } //to get the LU form of the matrix int iRet = nag_real_lu(nSize,matMatrixTemp,nSize,pivot,&detf,&dete); if(iRet) return iRet; if(dete < 200) //some times if the det is very large, pow function will overflow. { if(abs(detf*pow(2, dete)) < 0.000000000001) return MATRIX_SINGUAL_ERROR; } //solve the linear equations iRet = nag_real_lu_solve_mult_rhs(nSize,nSize, matMatrixTemp,nSize, pivot, mUnitAndResult,nSize); if(iRet) return iRet; mOutput = mUnitAndResult; return 0;
} //you should set the size of mOutput before you call the function. hope this will help.
Edited by - member1 on 06/28/2002 16:06:26 |
| easwar |
Posted - 06/27/2002 : 4:47:20 PM Hi,
Mike Buess posted a sample code for transpose. You can find NAG functions in the Linear Algebra Support functions for matrix-vector product, matrix-matrix product etc. As for inverse, it looks like for now you will have to resort to LabTalk with some code like below:
void dd() { matrix matA = { { 2, 3, 4}, {4, 7, 9}, {7, 2, 3} }; matrix matB; Matrix MatO("Matrix1"); MatO = matA; int ii; _LT_Obj { mat.matName$ = "Matrix1"; int ii = mat.inv(); } if(ii != 0)printf("Inversion failed\n"); matB = MatO; }
Here, the internal Origin C matrix matA, is first copied to an external Origin matrix "Matrix1", and then LabTalk is called to do the inverse. If the inverse operation succeeds, the result which is in "Matrix1" is copied to the internal Origin C matrix matB.
We will work on adding support for inverse. This code segment assumes that you have a matrix window in Origin called "Matrix1" and it is set to data type of double (which is the default data type for a new matrix window).
If you need further help, please let us know.
Easwar OriginLab.
Edited by - easwar on 06/27/2002 16:48:30 |
| Rimmer |
Posted - 06/27/2002 : 2:57:29 PM Yes. I found the nag function below and tried it. The results I obtained by using it were rather poor. So I was hoping to solve my problem using a Vandermonde matrix.
So it would be nice to know the answers to the above questions
-- Michael |
| easwar |
Posted - 06/27/2002 : 2:34:10 PM Hi Michael,
A sample code for the above already exists in our header file: look under Origin C help->Global Functions-> NAG Functions-> nag_real_lin_eqn
And the following is a simplified version of the code in the help file:
/////// #include <origin.h> // Add only the necessary NAG header to make compilation faster #include <NAG/OCN_f.h>
void test_nag_real_lin_eqn() { int n = 3; matrix a = { {33, 16, 72}, {-24, -10, -57}, {-8, -4, -17} }; vector b = {-359, 281, 85}; vector x(3); nag_real_lin_eqn(n, a, n, b, x); printf("Solution\n"); for (int i=0; i<n; i++) printf("%9.4f\n",x[i]); } //////
Easwar OriginLab.
Edited by - easwar on 06/27/2002 14:35:34 |
| easwar |
Posted - 06/27/2002 : 1:45:15 PM Hi Michael,
I suggest that you use the NAG library to do solution of a set of linear equations. Take a look at the function nag_real_lin_eqn in the header file OCN_f.h and the following PDF location for more information: http://www.nag.com/numeric/cl/manual/pdf/F04/f04arc_cl05.pdf
Our header file in this area is rather sparse and does not have enough examples. We will work on providing an example for you.
Easwar OriginLab.
Edited by - easwar on 06/27/2002 13:52:12 |
| Mike Buess |
Posted - 06/27/2002 : 1:20:28 PM Here's a simple example of transposing in OriginC...
void Test(string strName) { Matrix mm(strName); mm.Transpose(); }
To transpose the active matrix enter this command in the script window...
Test(%H);
I'm not sure how to invert a matrix in OriginC (or even if it can be done). Somebody else?
You might also find something useful in my Matrixbase examples at labtalk.nmrtools.com.
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 06/27/2002 13:22:07
Edited by - Mike Buess on 06/27/2002 13:38:04 |
|
|