Re: matrix stuff (solving b = A*x) --> using numerical recipes
- From: "stathis gotsis" <stathisgotsis@xxxxxxxxxxx>
- Date: Fri, 3 Mar 2006 22:11:00 +0200
"Martin Jørgensen" <unoder.spam@xxxxxxxxxxxx> wrote in message
news:u4bmd3-v01.ln1@xxxxxxxxxxxxxx
Since I'm a newbie, I still need some comments on my code. For avoiding
confusion, I post all of the code I got except the functions from
Numerical recipes (can be seen on
http://www.library.cornell.edu/nr/bookcpdf/c2-4.pdf p.52-54 AFAIR).
-------------
/* Including header files */
You are missing <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "nrutil.h"
/* definitions */
#define number_of_rows 7
#define TINY 1.0e-20
#define SWAP(a,b) {dum=(a);(a)=(b);(b)=dum;}
/* prototypes */
void banmul(float **a, unsigned long n, int left, int right, float x[],
float b[]);
void bandec(float **a, unsigned long n, int left, int right, float **al,
unsigned long indx[], float *d);
void banbks(float **a, unsigned long n, int left, int right, float **al,
unsigned long indx[], float b[]);
/* Start of main program */
int main(void)
{
/* declaration */
float **a;
float testvector[number_of_rows][number_of_rows];
You intend to initialize this array, you should consider doing that upon
declaration.
int i, j;
float x[number_of_rows], b[number_of_rows]; // remember that we're
solving "b = A * x"
testvector[][number_of_rows] = // number_of_cols = number_of_rows
{
{3.,1.,0.,0.,0.,0.,0.},
{4.,1.,5.,0.,0.,0.,0.},
{9.,2.,6.,5.,0.,0.,0.},
{0.,3.,5.,8.,9.,0.,0.},
{0.,0.,7.,9.,3.,2.,0.},
{0.,0.,0.,3.,8.,4.,6.},
{0.,0.,0.,0.,2.,4.,4.}
};
This is not a proper initialization, that is why the compiler complained.
float **a = malloc(number_of_rows * sizeof *a);
You are re-declaring a. Try: a = malloc(number_of_rows * sizeof *a);
if(a != NULL)
{
for(i = 0; i < number_of_rows; i++)
{
a[i] = malloc(number_of_rows * sizeof *a[i]);
if(a[i] != NULL)
{
for(j = 0; j < number_of_rows; j++)
{
/* might as well use your other array to initialise with */
a[i][j] = testvector[i][j];
}
}
else
{
printf("You're running low on memory - you should do something.\n");
exit(1);
}
}
}
else
{
printf("You're running low on memory - you should do something.\n");
exit(1);
}
banmul(a, number_of_rows, 2, 1, x, b);
printf("\nFinishing program now.\n\n");
return(0);
}
/* Here comes the NR functions */
....
-------------
Compiler errors:
1) error C2059: syntax error : ']'
This is really strange! I don't understand that - it used to work before
I think.... Error 1) happen in the line: testvector[][number_of_rows] =
.... { {3.,1.,0.,0.,......},{4.,1.,..} etc};
2) error C2143: syntax error : missing ';' before 'type'
Error 2) Happens just after the testvector (matrix actually) is defined
- in the line: float **a = malloc(number_of_rows * sizeof *a);
Therefore error 2 is probably connected to error 1.
.
- Follow-Ups:
- Re: matrix stuff (solving b = A*x) --> using numerical recipes
- From: Martin Jørgensen
- Re: matrix stuff (solving b = A*x) --> using numerical recipes
- References:
- matrix stuff (solving b = A*x) --> using numerical recipes
- From: Martin Jørgensen
- Re: matrix stuff (solving b = A*x) --> using numerical recipes
- From: Richard Heathfield
- Re: matrix stuff (solving b = A*x) --> using numerical recipes
- From: Martin Jørgensen
- Re: matrix stuff (solving b = A*x) --> using numerical recipes
- From: Richard Heathfield
- Re: matrix stuff (solving b = A*x) --> using numerical recipes
- From: Martin Jørgensen
- matrix stuff (solving b = A*x) --> using numerical recipes
- Prev by Date: Re: Interview question - any suggestions
- Next by Date: Re: Compile error listing
- Previous by thread: Re: matrix stuff (solving b = A*x) --> using numerical recipes
- Next by thread: Re: matrix stuff (solving b = A*x) --> using numerical recipes
- Index(es):
Relevant Pages
|