View unanswered posts | View active topics It is currently Sat Oct 31, 2020 7:10 pm

 Page 1 of 1 [ 9 posts ]
 Print view Previous topic | Next topic
recursive calculation of matrix functions (eg. determinants)
Author Message
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Tue Dec 31, 2013 4:47 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Tue Dec 31, 2013 5:43 am
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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 !

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Tue Dec 31, 2013 5:49 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Tue Dec 31, 2013 9:01 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Tue Dec 31, 2013 9:27 am
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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;

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Tue Dec 31, 2013 10:44 am
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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!)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Tue Dec 31, 2013 11:14 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Tue Dec 31, 2013 1:19 pm
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Tue Dec 31, 2013 2:28 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 9 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning