Difference in Fortran and C array storage? - c

I want to compute qr decomposition using F77_NAME(dgeqrf) function from Lapack lib in a c program.
For the matrix 3x3 :
12.000000 -51.000000 4.000000
6.000000 167.000000 -68.000000
-4.000000 24.000000 -41.000000
I get the output 3x3 (a combination of R matrix and some vectors used to construct Q)(linear form) :
-52.545219, -0.790144, 0.061972, 165.895209, -70.906839, -0.520684, 27.328842, -31.566433, -23.015097
I use then F77_NAME(dorgqr) from Lapack to extract Q matrix, get the output 3x3 (linear form) :
-0.228375, 0.970593, -0.076125, -0.618929, -0.084383, 0.780901, 0.751513, 0.225454, 0.619999
This is an example taken from wikipedia and it seems my Q differs from the wikipedia Q :
http://en.wikipedia.org/wiki/QR_decomposition#Example_2
Could the difference between fortran and c array representation be the cause?
Would a transpose on the initial matrix solve the problem ?

I hope you don't expect others to do your work. Since you almost answered your question, I don't see the point in posting it. Also, it may be a good thing to do to take some time in reading the FAQ section of this site before using it. You might find some useful info there.
As for the answer to the question:
It most probably is indeed the difference in array representation between C and Fortran the reason for your problem. See this:
http://en.wikipedia.org/wiki/Row-major_order
For general information on mixing Fortran and C code, this might be helpful:
http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html

Related

Singular Value Decomposition of 4x4 matrix in C

I am ultimately trying to create a pseudoinverse function using svd methodology. I want to first create a SVD function that will give me the U, E and V matrices that I will later use in the formula below to get the pseudoinverse:
I am not sure how to code these matrices. I understand how to do this by hand through eigen values and vectors but not sure how to translate that to c code.
I have already created functions for Transpose and matrix multiplication. Now its a matter of finding these 3 matrices.
You can see a similar asked question with a link to a source file.
They recommend to use openCV built-in function - you can find it here - the purpose is to use a ready function from the openCV library.

Eigenvectors, eigenvalues fixed point calculation in C

** Edited **
I tried changing the mentioned Jacobi algorithm to fixed point using libfixmath but I am not getting right results. What did I miss??
edited Jacobi code
makefile
C newbie here. I somehow got my self in the deep and i cant find my way out. If you could help that would be awesome!
The situation: I am trying to implement an ICA algorithm in C. I did so using floating point arithmetic (double, float). Now I want this code to transform it to fixed point so I can import it on an ARM microcontroller of 32 bits (thats why i cant use double, float etc).
I have found four libraries that I think can help me:
http:// sourceforge.net/projects/avrfix/files/
http:// www.dsprelated.com/showcode/40.php
http:// sourceforge.net/p/fixedptc/code/ci/default/tree/
libfixmath
I didnt use 1. 2. or 3. I am currently trying libfixmath because allmost all calculations are done with matrices.
My problem is when trying to find the eigenvalues and eigenvectors of a covariance matrix (positive symmetic 3x3 matrix). I searched around for libs or functions that do eigendecomposition or SVD etc. but i didnt find anything.
How to you do that sort of calculation in fixed point??
Are there any functions/libs that i didnt find out? Do I have to alter the eigen function that I have in floating-point (line by line converting to fixed point - i.e fix16_from_dbl() )?
My current eigen function (not mine of course i think it is from Numerical Recipes)
jacobi.h
Other relevant question:
Here's StackOverflow fixed SVD
****Is my first question if there is anything to correct in my question please say so....dont eat me alive! :)
** Edited **
libfixmath does Cholesky1, wiki2 and QR decomposition.
I tried to play with maths and produce the eigenvectors or eigenvalue of a matrix with this data (data from the above functions) but I failed.
If anyone knows how to do that then problem solved.
*Should I post it to math stack website?
1 http:// rosettacode.org/wiki/Cholesky_decomposition
2 http:// en.wikipedia.org/wiki/Cholesky_decomposition#The_Cholesky.E2.80.93Banachiewicz_and_Cholesky.E2.80.93Crout_algorithms
How about the GNU Scientific Library? It has a number of functions related to eigenvalue decomposition
You could use something like IQMath in order to convert from floating-point to fixed-point. There are also a number of answers for a similar question here

Singular Value Decomposition (SVD) in C

I am doing the five point essential matrix estimation in C where I need to implement SVD. I found an opensource implementation in c http://www.public.iastate.edu/~dicook/JSS/paper/code/svd.c that works on mxn matrices where m>n. The problem is that the matrix that I want to decompose is a (5x9) matrix and therefore n>m. I need the right orthogonal transformation matrix v where svd(A)=udv'
To ensure (m>n) I tried to do svd(transpose(A))=u2*d2*v2
I found that u=v2, but v is different from u2 and I need v.
How to implement SVD in C successfully for an 5x9 matrix?
Late to the party, but for future reference one can obtain a SVD implementation in C from the book "Numerical Recipes in C by William H. Press et al", in Chapter 2.6, Page 67, SVD Algorithm. To quote the book
Here is the algorithm for constructing the singular value decomposition of any
matrix.
So I'm assuming the matrix to be decomposed can be square, m < n or n < m
Warning: When googling SVD implementations in C check what assumptions are made w.r.t the input matrix. Some assume the matrix is square, some do not, etc...
Alternatively, you can use the SVD in LAPACK. Stephen Canon provided a code example on another SO question on how to use dgesdd to perform SVD. (link here)

I'm looking for a C library that is able to compute 4x4 and 3x3 matrix math

Does anyone know a good one? I'm looking for multiplication of matrices, transpose, invert, converting from 4x4 to top left corner 3x3 etc.
Like you say, rolling your own is easy enough. The inverse is tricky to get efficient unless you read this:
http://www.geometrictools.com/Documentation/LaplaceExpansionTheorem.pdf
I could send you my code, but it's a 4x4-only c++ class and does not take that paper into account yet, among other things that probably don't fit your needs.
Try BLAS or LAPACK.
Intel's Math Kernel or Numerical Receipes in C by Press, Flannery, Teukolsky and Vetterling

Eigenvector (Spectral) Decomposition

I am trying to find a program in C code that will allow me to compute a eigenvalue (spectral) decomposition for a square matrix. I am specifically trying to find code where the highest eigenvalue (and therefore its associated eigenvalue) are located int the first column.
The reason I need the output to be in this order is because I am trying to compute eigenvector centrality and therefore I only really need to calculate the eigenvector associated with the highest eigenvalue. Thanks in advance!
In any case I would recommend to use a dedicated linear algebra package like Lapack (Fortran but can be called from C) or CLapack. Both are free and offer routines for almost any eigenvalue problem. If the matrix is large it might be preferable to exploit its sparseness e.g. by using Arpack. All of these libraries tend to sort the eigenvectors according to the eigenvalues if they can (real or purely imaginary eigenvalues).
See the book "Numerical recipes in C"
And the #1 google hit (search: eigenvalue decomposition code C#)
http://crsouza.blogspot.com/2010/06/generalized-eigenvalue-decomposition-in.html
does not help?

Resources