473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Error in Numerical Recipes lubksb routine

mma
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[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
}

Where a is an n X n matrix (C array of arrays) which go from 1..n
(through the use of Numerical Recipes offset index method). See page
48 here:
<a href="http://www.library.cornell.edu/nr/bookcpdf/c2-3.pdf">Numerical
Recipes sample pdf</a> for more information.

It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?

--Mike
Nov 13 '05 #1
5 3820
mma wrote:
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[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
}

Where a is an n X n matrix (C array of arrays) which go from 1..n
(through the use of Numerical Recipes offset index method). See page
48 here:
<a href="http://www.library.cornell.edu/nr/bookcpdf/c2-3.pdf">Numerical
Recipes sample pdf</a> for more information.

It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?

The condition j<=n causes the inner loop to end without doing anything for
the first value of i. While it might be slightly more efficient on some
platform to write a separate case for j=i+1, with no inner loop, the way it
is written is the most succinct. If your compiler fetches the operands
before testing whether they are needed, and this causes a segfault, that's
the fault of your compiler.
--
Tim Prince
Nov 13 '05 #2
In article <news:1b**************************@posting.google. com>
mma <mm****@foobox.net> writes:
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[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
}

Where a is an n X n matrix (C array of arrays) which go from 1..n
(through the use of Numerical Recipes offset index method).
Be aware that this method is not strictly portable, although it
works in practice on most if not all systems. For more detail,
see the FAQ.
It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?


When i==n, the inner loop does this:

for (j = i + 1;

- This sets j to i + 1, which is also n + 1, as you note.

j <= n;

- Now compare j to n. Since j = n + 1, j > n, and j <= n is false
(produces the integer value 0 in an expression, and is treated
as false in a loop-controlling-expression). The inner loop
therefore does not run at all.

After the inner loop (immediately) terminates, the outer loop sets
b[i] to sum / a[i][i]. The variable sum is unmodified from the
original b[i] assigned to it, so this divides b[i] by a[i][i].
The outer loop then decrements i and resumes at its test expression
(which must have already been true exactly once).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #3
On 7 Dec 2003 13:57:55 -0800, mm****@foobox.net (mma) wrote:
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[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
}

Where a is an n X n matrix (C array of arrays) which go from 1..n
(through the use of Numerical Recipes offset index method). See page
48 here:
<a href="http://www.library.cornell.edu/nr/bookcpdf/c2-3.pdf">Numerical
Recipes sample pdf</a> for more information.

It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?


When i is set to n, then j=i+1 will indeed set j to n+1. However, the
terminating condition (j<=n) is examined at the top of the loop so the
loop will not execute at all.

If it did execute, it would cause undefined behavior. A segmentation
fault is one of the more user friendly manifestations of undefined
behavior but not guaranteed.
<<Remove the del for email>>
Nov 13 '05 #4
mma wrote:
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[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
(snip)
It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?


The j<=n; right after the j=i+1;

-- glen

Nov 14 '05 #5
!
On Tue, 09 Dec 2003 05:17:58 +0000, glen herrmannsfeldt wrote:
for(i=n;i>=1;i--)
{
sum=b[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];


(snip)
It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?


The j<=n; right after the j=i+1;


for(i1; i2; i3) S;

is a shortcut for:

i1;
while(i2) { S; i3; }

--
A
..

Nov 14 '05 #6

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

Similar topics

65
by: Pmb | last post by:
I'm confused as to what the compiler error message I'm getting is refering to. Can someone take a gander and let me know what I did wrong? The program is below. When I compile it I get the...
14
by: Steven T. Hatton | last post by:
Can anyone recommend a good book on numerical methods that presents the subject using C++ in an ideal manner? -- "If our hypothesis is about anything and not about some one or more particular...
5
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...
16
by: Martin Jørgensen | last post by:
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...
23
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...
11
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...
1
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)? ...
1
by: Peter Graf | last post by:
Hi, i tried to evaluate the 2F1(a,b,c,z) hypergeometric function for a=1, b=2-0.1i, c=2.5-0.01i and z=-5e8. I've used the numerical recipes routine but this routine couldn't act with this...
10
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.