Re: matrix stuff (solving b = A*x) --> using numerical recipes



"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.


.



Relevant Pages

  • Re: libc/printf bug
    ... I don't see that the cast should have any effect at all ... How would you print a float in hex? ... So seems printf gets confused because you present it with a float when i t wants an integer. ... This behavior I have seen in other cases where wrong or missing arguments were presented too. ...
    (comp.os.linux.development.apps)
  • Re: Comparission cause problem
    ... > I think I may be missing some points thats why, I have some doubts on the ... that is wider than a float. ... In simple terms the natural floating point type for C and C++ is double ...
    (alt.comp.lang.learn.c-cpp)
  • Re: openning a file
    ... 37 float **Allocate2DFloat(int rows, int columns) ... Missing semicolon ';' at the end. ... ReadClassFiletest.c:40: error: syntax error before '{' token ...
    (comp.lang.c)
  • Re: doubles or not
    ... > even a float can not be enough for this, we may need doubles. ... the point that you are missing is "..when integers will do". ... integer math. ...
    (microsoft.public.vc.language)