I'm writing a code which calculates the inverse matrix given a matrix, the thing is, I need that to be included in other code that makes statistical fits, so I need something like a function that receives the size of the matrix (matrix is square matrix) and the matrix itself and returns his inverse, I found something about the syntax and then have this (Gauss-Jordan)
float* inv(int n, float *A)
{
float* I = 0;//*
float aux;
float pivote;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(i == j)
{
*(I+i*n+j) = 1.0; //*
}
else {
*(I+i*n+j) = 0.0;
}
}
}
for(int i = 0; i<n; i++)
{
pivote = *(A+i*n+i);
for(int k = 0; k<n; k++)
{
*(A+i*n+k) = *(A+i*n+k)/pivote;//*
*(I+i*n+k) = *(I+i*n+k)/pivote;//*
}
for(int j = 0; j<n; j++)
{
if(i!=j)
{
aux = *(A+j*n+i);//*
for(int k = 0; k<n;k++)
{
*(A+j*n+k)=*(A+j*n+k)-aux**(A+i*n+k);//*
*(I+j*n+k)=*(I+j*n+k)-aux**(I+i*n+k);//*
}
}
}
}
return I;//*
}
There where I put the //*
is where I have my doubts, is the syntax correct? the declarations, there in the return should be something else than just I
?. When I compile I get a segmentation fault, Following Taekahn recommendations, compiling with sanitizers g++ -fsanitize=address -fsanitize=undefined -fsanitize=leak inverse.cpp
I get
inverse.cpp:148:28: runtime error: store to null pointer of type 'float'
AddressSanitizer:DEADLYSIGNAL
=================================================================
==11993==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x00000040338c bp 0x7ffdd6a14510 sp 0x7ffdd6a144b0 T0)
==11993==The signal is caused by a WRITE memory access.
==11993==Hint: address points to the zero page.
#0 0x40338b in inv(int, float*) (/home/live/med_elect/a.out+0x40338b)
#1 0x402f30 in main (/home/live/med_elect/a.out+0x402f30)
#2 0x7f90ffed9e5a in __libc_start_main (/lib64/libc.so.6+0x23e5a)
#3 0x402289 in _start (/home/live/med_elect/a.out+0x402289)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/home/live/med_elect/a.out+0x40338b) in inv(int, float*)
==11993==ABORTING
I really appreciate if you can help me, thank you very much in advance and thank you very much for the feedback in the comments, I'm new here with the questions.
UPDATE: Thanks to nasy for the answer, It is important to note that many people mentioned the vector approach, so, to anyone reading this, check the comments and better try the vector approach.