473,726 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi,

I've made a program from numerical recipes. Looks like I'm not allowed
to distribute the source code from numerical recipes but it shouldn't
even be necessary to do that.

My problem is that I'm not very experienced with pointers, pointers to
pointers and the like and I got 4 compiler warnings + I don't completely
understand how to build this "compact matrix" (see later).

I'm also not completely sure if I understand the text correct... So far
my program looks like this:

#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 */
double x[number_of_rows], b[number_of_rows]; // remember that we're
solving "b = A * x"

/* initialisation */
double a[][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.}
};

banmul(a, number_of_rows, 2, 1, x, b);

printf("\nFinis hing program now.\n\n");
}
If there's anything you need to know you can read it here:

http://www.library.cornell.edu/nr/bookcpdf/c2-4.pdf (start from the
chapter with "Band diagonal system, p.52".

You can see my 2D-matrix is the same... The text talks about transposing
the matrix to a more compact form... I must admit I need some help
because I didn't understand the text :-(

My compiler warnings (don't know what to do):

----
warning C4047: 'function' : 'float **' differs in levels of indirection
from 'double [7][7]'

warning C4024: 'banmul' : different types for formal and actual parameter 1

warning C4133: 'function' : incompatible types - from 'double [7]' to
'float *'

warning C4133: 'function' : incompatible types - from 'double [7]' to
'float *'
----

The problem is the line: "banmul(a, ....)";
So, generally, I don't really understand what I'm doing :-(

The x-array (vector) is not defined. If somebody could show me an
example of how to solve this matrix system, I would be very happy (you
just make x whatever you like: x=[2,5,2,1,6,2,1] or whatever...

AFAIK the text from numerical recipes and my code should be enough for
you to understand my code and I hope somebody can give me some hints
that can get me closer to a solution.

Thanks in advance for any (hopefully good) inputs...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 1 '06 #1
16 3270
Martin Jørgensen said:
My problem is that I'm not very experienced with pointers, pointers to
pointers and the like and I got 4 compiler warnings + I don't completely
understand how to build this "compact matrix" (see later).
doubles are not floats, and arrays are not pointers.
void banmul(float **a, unsigned long n, int left, int right, float x[],
float b[]); /* declaration */
double x[number_of_rows], b[number_of_rows]; // remember that we're
solving "b = A * x"

/* initialisation */
double a[][number_of_rows] = // number_of_cols = number_of_rows
{ [...] };

banmul(a, number_of_rows, 2, 1, x, b);


banmul takes float ** for parameter 1, but you're actually passing it a
double (*)[7], which is not the same thing by several rows of apple trees.

banmul takes float * for parameters 5 and 6, and you are trying to pass it
double *.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 1 '06 #2
Martin Jørgensen <un*********@sp am.jay.net> writes:
[...]
warning C4047: 'function' : 'float **' differs in levels of
indirection from 'double [7][7]'


Arrays are not pointers, and 2-dimensional arrays are not pointers to
pointers. The comp.lang.c FAQ is at <http://www.c-faq.com/>; start
with section 6, "Arrays and Pointers".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 2 '06 #3
Richard Heathfield wrote:
Martin Jørgensen said: -snip-
banmul(a, number_of_rows, 2, 1, x, b);

banmul takes float ** for parameter 1, but you're actually passing it a
double (*)[7], which is not the same thing by several rows of apple trees.


Damn... I understand what you're writing. I just don't know how to fix
the problem... Could you/somebody please help?

I tried:

int main(void)
{
int i;

/* declaration */
float x[number_of_rows], b[number_of_rows]; // remember that we're
solving "b = A * x"
float **a = malloc((number_ of_rows)*sizeof (float*));
a[0] = malloc((number_ of_rows*number_ of_rows)*sizeof (float));

for (i = 1; i < number_of_rows ;i++)
a[i] = a[i-1] + number_of_rows+ 1;

a = //
{
{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.}
};

banmul(a, number_of_rows, 2, 1, x, b);

printf("\nFinis hing program now.\n\n");
}

This gives 3 problems:

1) warning C4047: 'initializing' : 'float **' differs in levels of
indirection from 'int' - this is the line: float **a =
malloc((number_ of_rows)*sizeof (float*));

2) warning C4047: '=' : 'float *' differs in levels of indirection from
'int' - this is the line: a[0] =
malloc((number_ of_rows*number_ of_rows)*sizeof (float));

3) syntax error : '{' - I now have a problem with initializing the 2D
"a"-matrix with this notation...
banmul takes float * for parameters 5 and 6, and you are trying to pass it
double *.


Damn... Stupid mistake. I fixed that just by changing the definition.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 2 '06 #4
Keith Thompson wrote:
Martin Jørgensen <un*********@sp am.jay.net> writes:
[...]
warning C4047: 'function' : 'float **' differs in levels of
indirection from 'double [7][7]'

Arrays are not pointers, and 2-dimensional arrays are not pointers to
pointers. The comp.lang.c FAQ is at <http://www.c-faq.com/>; start
with section 6, "Arrays and Pointers".


I read it and felt I understood it... But I need more help yet... See my
other post for a more thorough description of what I now tried...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 2 '06 #5
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:q3******** ***@news.tdc.dk ...
I tried:
Hopefully you are including <stdlib.h>
int main(void)
{
int i;
Where do you declare number_of_rows?
/* declaration */
float x[number_of_rows], b[number_of_rows]; // remember that we're
solving "b = A * x"
float **a = malloc((number_ of_rows)*sizeof (float*));
a[0] = malloc((number_ of_rows*number_ of_rows)*sizeof (float));

for (i = 1; i < number_of_rows ;i++)
a[i] = a[i-1] + number_of_rows+ 1;
Why "+1"?
a = //
{
{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.}
};
You would better initialize some other 2D-array this way and copy its
contents into a.
banmul(a, number_of_rows, 2, 1, x, b);

printf("\nFinis hing program now.\n\n");
Returning 0 would not be a bad idea.
}



Mar 3 '06 #6
Martin Jørgensen said:
Richard Heathfield wrote:
Martin Jørgensen said: -snip-
banmul(a, number_of_rows, 2, 1, x, b);

banmul takes float ** for parameter 1, but you're actually passing it a
double (*)[7], which is not the same thing by several rows of apple
trees.


Damn... I understand what you're writing. I just don't know how to fix
the problem...


Presuming you want a square:

float **p = malloc(number_o f_rows * sizeof *p);
if(p != NULL)
{
for(i = 0; i < number_of_rows; i++)
{
p[i] = malloc(number_o f_rows * sizeof *p[i]);
if(p[i] != NULL)
{
for(j = 0; j < number_of_rows; j++)
{
/* might as well use your other array to initialise with */
p[i][j] = a[i][j];
}
}
else
{
you're running low on memory, and you might want to do something
about that
}
}
}
else
{
you're running low on memory, and you might want to do something
about that
}

Now, this method is a bit wasteful in the sense that it uses your original
array *as well* - for initialising. But I can't help wondering whether,
right now, you'd rather have something than nothing.
This gives 3 problems:

1) warning C4047: 'initializing' : 'float **' differs in levels of
indirection from 'int' - this is the line: float **a =
malloc((number_ of_rows)*sizeof (float*));


That's because you forgot to prototype malloc, so the compiler is forced to
assume it returns int, whereas in fact it returns void *. Simply add:

#include <stdlib.h>

to fix that problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 3 '06 #7
Richard Heathfield wrote:
Martin Jørgensen said: -snip-
Presuming you want a square: -snip-
Now, this method is a bit wasteful in the sense that it uses your original
array *as well* - for initialising. But I can't help wondering whether,
right now, you'd rather have something than nothing.


Completely correct! You read my mind, oh great mindreader :-)
This gives 3 problems:

1) warning C4047: 'initializing' : 'float **' differs in levels of
indirection from 'int' - this is the line: float **a =
malloc((numbe r_of_rows)*size of(float*));

That's because you forgot to prototype malloc, so the compiler is forced to
assume it returns int, whereas in fact it returns void *. Simply add:

#include <stdlib.h>

to fix that problem.


Dough! Fixed that now.

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 */
#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];
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.}
};
float **a = malloc(number_o f_rows * sizeof *a);
if(a != NULL)
{
for(i = 0; i < number_of_rows; i++)
{
a[i] = malloc(number_o f_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("\nFinis hing 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_o f_rows * sizeof *a);

Therefore error 2 is probably connected to error 1.

Thanks in advance for a solution that will fix my problems...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 3 '06 #8
stathis gotsis wrote:
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:q3******** ***@news.tdc.dk ...

I tried:

Hopefully you are including <stdlib.h>


Damn... Thanks...
int main(void)
{
int i;

Where do you declare number_of_rows?


Yeah, sorry. It's in the top and I forgot to include that part in my
posting. You can see my new code which is still causing me some trouble
in my other reply to Richard.
/* declaration */
float x[number_of_rows], b[number_of_rows]; // remember that we're
solving "b = A * x"
float **a = malloc((number_ of_rows)*sizeof (float*));
a[0] = malloc((number_ of_rows*number_ of_rows)*sizeof (float));

for (i = 1; i < number_of_rows ;i++)
a[i] = a[i-1] + number_of_rows+ 1;

Why "+1"?


Good question... I think it's because I took it from some other code
where I had a mesh consisting of nx=6 elements (or whatever) but they
didn't start at index 0 - but at index 1... Using +1 was probably a
mistake in this new code, so I changed it...
a = //
{
{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.}
};

You would better initialize some other 2D-array this way and copy its
contents into a.


Understood... But I now get a stupid error, which I can't figure out
of... Probably something *really* simple - see me other reply...
banmul(a, number_of_rows, 2, 1, x, b);

printf("\nFin ishing program now.\n\n");

Returning 0 would not be a bad idea.


You're right :-)

Code changed a bit. Thanks for the comments.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 3 '06 #9
"Martin Jørgensen" <un*********@sp am.jay.net> wrote in message
news:u4******** ****@news.tdc.d k...
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_o f_rows * sizeof *a);
You are re-declaring a. Try: a = malloc(number_o f_rows * sizeof *a);
if(a != NULL)
{
for(i = 0; i < number_of_rows; i++)
{
a[i] = malloc(number_o f_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("\nFinis hing 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_o f_rows * sizeof *a);

Therefore error 2 is probably connected to error 1.

Mar 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3839
by: mma | last post by:
I have been using the lubksb routine in Visual C++ 6.0 and noticed what looks like an error to me. The last section of the method looks like this: for(i=n;i>=1;i--) { sum=b; for(j=i+1;j<=n;j++) sum -= a*b; b=sum/a;
5
3029
by: Edward Hua | last post by:
Hi, I'm wondering if anybody has ever copied the quicksort algorithm from the book Numerical Recipes in C: The Art of Scientific Computing (2nd ed.), by Press, Teukolsky, Vetterling, and Flannery, in Chapter 8. Quicsort is supposed to sort an array of, say, doubles, in ascending order. I tried to copy this algorithm line by line to test it. It seems that there's an error in this algorithm as given in this book. Namely, the
23
15676
by: Abhi | last post by:
Hi.. I wanted the C source code in machine readable format for the book "Numerical Recipes in C". I got hold of the pdf version of the book somehow. Does anyone have the complete C code of the book?. If yes,..can you please mail me the code or somehow share it with me?. With Regards, Abhishek S
11
3365
by: lcw1964 | last post by:
Greetings groups! I am a rank novice in both C programming and numerical analysis, so I ask in advance your indulgence. Also, this question is directed specifically to those familiar with Numerical Recipes in C (not C++), in practice or at least in theory. I have taken an interest in the the least-squares SVD alternative to the Remes algorithm offered in section 5.13 of NR, 2nd ed. (see here,...
1
3769
by: j.f.c.neves | last post by:
Hello, I need some help in using the rlft3 (Numerical Recipes in c++ book, Chapter 12) to apply a Gaussian smoothing to a 2D image. How do I create a suitable filter function (page 535)? Thanks, Joao
10
5423
by: Babak | last post by:
Hi, I've developed a C program which contains a large number of vectors and matrices operations. Throughout my code, I used the template from the Numerical Recipes book to define vectors and matrices and access their elements. For example, to define a vector I used the function: my_vector=vector(0,n-1); Which actually allocate the memory as follows:
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.