ROBOTC.net forumshttp://robotc.net/forums/ recursive calculation of matrix functions (eg. determinants)http://robotc.net/forums/viewtopic.php?f=1&t=7547 Page 1 of 1

Author:  Ford Prefect [ Tue Dec 31, 2013 4:47 am ]
Post subject:  recursive calculation of matrix functions (eg. determinants)

hey,
I'm curious how to port the following recursive C code into RobotC (exemplarily: the determinant of square matrices):

 Code:// matrix determinantdouble MatrixDet(int N, double A[N][N]) // or float instead{    int i,j,i_count,j_count, count=0;    double Asub[N-1][N-1], det=0;    if(N==1) return A[0][0];    if(N==2) return (A[0][0]*A[1][1] - A[0][1]*A[1][0]);    for(count=0; count

 Author: mightor [ Tue Dec 31, 2013 5:43 am ] Post subject: Re: recursive calculation of matrix functions (eg. determina You need to find a way to get rid of the runtime sized array and then it would be fine.= Xander

 Author: Ford Prefect [ Tue Dec 31, 2013 5:49 am ] Post subject: Re: recursive calculation of matrix functions (eg. determina yes, that's the point. IIRC: you already tried it, too, didn't you?But you can't "get rid of it", it's simply the way the methematical algorithm works.ps,the recursive computation of submatrices is not only an issue for determinants but also for matrix adjugates or inverse matrices !

 Author: mightor [ Tue Dec 31, 2013 9:01 am ] Post subject: Re: recursive calculation of matrix functions (eg. determina Well, I didn't try very hard Mostly because I don't know enough about the algorithm or the actual operation that this is. I think there may be a way to do this, I'll see what I can come up with.= Xander

Author:  mightor [ Tue Dec 31, 2013 9:27 am ]
Post subject:  Re: recursive calculation of matrix functions (eg. determina

Does this do what it's supposed to?

 Code:#define MAX_N 6float MatrixDet(int N, float *A) // or float instead{   writeDebugStreamLine("MatrixDet: N=%d, A: %p", N, A);   int i,j,i_count,j_count, count=0;   float Asub[MAX_N * MAX_N];   float det = 0;   if(N==1) return A[0];   if(N==2) return (A[0*N+0]*A[1*N+1] - A[0*N+1]*A[1*N+0]);   for(count=0; count

= Xander

Author:  Ford Prefect [ Tue Dec 31, 2013 10:44 am ]
Post subject:  Re: recursive calculation of matrix functions (eg. determina

Do I see it correctly that you have to linearize the matrix?

what result do you get with the 6x6 matrix above?

---

and then as a test this one:

 Code:  A[0][0]=-1;  A[0][1]= 2;  A[0][2]=-3;  A[0][3]= 0;  A[0][4]= 0;  A[0][5]= 0;  A[1][0]= 2;  A[1][1]= 1;  A[1][2]= 0;  A[1][3]= 0;  A[1][4]= 0;  A[1][5]= 0;  A[2][0]= 4;  A[2][1]=-2;  A[2][2]= 0;  A[2][3]= 0;  A[2][4]=23;  A[2][5]=24;  A[3][0]= 0;  A[3][1]= 0;  A[3][2]= 0;  A[3][3]= 2;  A[3][4]= 1;  A[3][5]=-1;  A[4][0]= 0;  A[4][1]= 0;  A[4][2]=10;  A[4][3]= 2;  A[4][4]= 1;  A[4][5]= 0;  A[5][0]= 0;  A[5][1]= 0;  A[5][2]= 0;  A[5][3]= 5;  A[5][4]= 2;  A[5][5]=-3;

Author:  Ford Prefect [ Tue Dec 31, 2013 11:14 am ]
Post subject:  Re: recursive calculation of matrix functions (eg. determina

for test reasons:
this is the program for the EV3:
 Code:// hw MatrixLib#include #include #include #include #include #include #include "ev3_button.h"#include "ev3_lcd.h"#include "ev3_constants.h"#include "ev3_command.h"#include "ev3_timer.h"#include "ev3_lcd.h"#include "ev3_sound.h"#include "ev3_output.h"double MatrixDet(int N, double A[N][N]) // or float instead{    int i,j,i_count,j_count, count=0;    double Asub[N-1][N-1], det=0;    if(N==1) return A[0][0];    if(N==2) return (A[0][0]*A[1][1] - A[0][1]*A[1][0]);    for(count=0; count

this matrix has a det of 74, the det of the matrix of the TOP is 0.
(very exact results!)

Author:  mightor [ Tue Dec 31, 2013 1:19 pm ]
Post subject:  Re: recursive calculation of matrix functions (eg. determina

This program seems to produce the required results:

 Code:// matrix determinant#define MAX_N 6// taken fromfloat Determinant(int n, float *a){  int i,j,j1,j2;  float det = 0;  float m[MAX_N * MAX_N];  if (n < 1)  {    /* Error */  }  else if (n == 1)  { /* Shouldn't get used */    det = a[0*n+0];  } else if (n == 2) {    det = a[0*n+0] * a[1*n+1] - a[1*n+0] * a[0*n+1];  } else {    det = 0;    for (j1=0;j1

 Author: Ford Prefect [ Tue Dec 31, 2013 2:28 pm ] Post subject: Re: recursive calculation of matrix functions (eg. determina yes, great, thanks, and that's actually almost exactly my code except this original ** thing (and your linearized adaption)! happy new year everybody

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/