Thanks in advance for any and all help.
I'm attempting to get the inverse of a matrix using SGETRF, then SGETRI, but I'm running into an error that I can't decipher. My C code is simple, I initialize matrix A[2][2], then call sgetrf_, then sgetri_ but sgetri_ chokes on the third parameter.
Here's the code in entirety:
- Code: Select all
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
extern void sgetri_ ( int* n, const void* A, int* lda,
int* ipiv, const void* work, int* lwork, int* info );
extern void sgetrf_ ( int* m, int* n, const void* a, int* lda,
int* ipiv, int* info );
int main (void) {
int i,j,n;
float A[2][2]; //input matrix
float alpha, beta;
int lda, arows, acols;
int ipiv[4];
int lwork;
int info;
float work[100]; //dimension totally made up - what should this be?
// silly matrix A is 2x2
A[0][0]=1.0; A[0][1]=2.0; A[1][0]=3.0; A[1][1]=4.0;
printf ("Matrix A before any calls:\n");
for (i=0;i<2;i++) {
for (j=0;j<2;j++)
printf ("A[%d][%d]:%f\t", i,j, A[i][j]);
printf ("\n");
}
printf ("\n");
arows=acols=2;
lda=2;
alpha = 1.0; beta=1.0;
memset (ipiv, 0, sizeof(int)*4);
memset (work, 0, sizeof(float)*100);
sgetrf_ ( &arows, &acols, A, &lda, ipiv, &info );
printf ("Matrix A after sgetrf\n");
for (i=0;i<2;i++) {
for (j=0;j<2;j++)
printf ("A[%d][%d]:%f\t", i,j, A[i][j]);
printf ("\n");
}
printf ("info for sgetrf_:%d\n\n", info);
n=acols*arows;
lda=2;
lwork=100; //magic number - dimension of work
sgetri_ (&n, A, &lda, ipiv, work, &lwork, &info);
printf ("Matrix A after sgetri\n");
for (i=0;i<2;i++) {
for (j=0;j<2;j++)
printf ("A[%d][%d]:%f\t", i,j, A[i][j]);
printf ("\n");
}
printf ("info for sgetri_:%d\n\n", info);
return(0);
}
I'm compiling this as so:
- Code: Select all
/usr/bin/gcc -c -g -Wall inverse.c
/usr/bin/gcc -o inverse inverse.o -framework vecLib
and the output is this:
- Code: Select all
Matrix A before any calls:
A[0][0]:1.000000 A[0][1]:2.000000
A[1][0]:3.000000 A[1][1]:4.000000
Matrix A after sgetrf
A[0][0]:2.000000 A[0][1]:0.500000
A[1][0]:4.000000 A[1][1]:1.000000
info for sgetrf_:0
Parameter 3 to routine SGETRI was incorrect
Mac OS BLAS parameter error in SGETRI, parameter #0, (unavailable), is 0
What the heck am I doing wrong? Why is parameter 3, lda, incorrect?
Susan