473,320 Members | 1,832 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,320 software developers and data experts.

still segmentation fault in my SVD code

The code where the error occurs is below in svdfit. This is a
difficult one becuse changing the reporting (cout) can move the error.
dblvec and dblmat are std::vectors of doubles. I am using the
template below for debuging these vectors, but it is not showing
anything useful I can see. I output the dimensions of each vec to show
what it is, is the same as what it should be. Multifunc returns a
vector afunc (I can post more details) I checked the dimensions of the
other objects x, y etc. I feel there is somethin I doing wrong with
C++ data types rather than a algebraic syntax type mistake (like a bad
dimension). Any help would be appreciated.

void ffit::testfnfit(dblmat &x, dblvec &y, int ndata, dblvec &a, int
ma,
int max_iters, double &chisq) // , idxmultifunc *funcs)
{
DEBUG_LOG("test..");
int i;
dblvec sig, w;
sig.reserve(ndata);
sig.resize(ndata);
w.reserve(ma);
w.resize(ma);
dblmat u, v;
u.reserve(ndata);
u.resize(ndata);
for (i = 0; i < ma; i++)
u[i].resize(ma);
v.resize(ma);
for (i = 0; i < ma; i++)
v[i].resize(ma);
// DEBUG_LOG("test6");
for (i = 0; i < ndata; i++)
sig[i] = 1.0;
// DEBUG_LOG();
std::cout << "calling svd " << ma << std::endl;
svdfit(x, y, sig, ndata, a, u, v, w, ma, max_iters, chisq); // ,
funcs);
}

void ffit::svdfit(dblmat &x, dblvec &y, dblvec &sig, const int &ndata,
dblvec &a, dblmat &u, dblmat &v, dblvec &w, const int &ma,
const int &max_iters, double &chisq)
{
int i, j;
dblvec afunc, b;
double tmp, wmax, thresh, s;
bool conv;
double const tol = 1.0E-5;

DEBUG_LOG("SVDFit");

afunc.reserve(ma);
afunc.resize(ma);
b.reserve(ndata);
b.resize(ndata);

std::cout << "sig " << sig.size() << "~" << ndata << std::endl;
std::cout << "u " << u.size() << "~" << ndata << " " << u[0].size() <<
"~" << ma << std::endl;
std::cout << "a " << a.size() << "~" << ma << std::endl;
std::cout << "y " << y.size() << "~" << ndata << std::endl;
for (i = 0; i < ndata; i++)
{
// funcs(i, ma, x, afunc);
multifn(i, ma, x, afunc);
DEBUG_LOG("SVDFits");
std::cout << i << " sig " << sig[i] << " ";
tmp = 1/sig[i];
std::cout << "afsig " << tmp << " " << afunc[j] << std::endl;
for (j = 0; j < ma; j++)
u[i][j] = afunc[j]*tmp;
std::cout << "afsig i=" << i << std::endl;
// for (j = 0; j < ma; j++)
// std::cout << "a " << j << " " << afunc[j] << std::endl;
std::cout << "y " << y[i] << std::endl;
std::cout << "afsig2" << std::endl;
b[i] = y[i]*tmp;
}
DEBUG_LOG("b4svdcmp");
svdcmp(u, ndata, ma, max_iters, w, v, conv);
std::cout << "w " << w.size() << " " << ma << std::endl;
wmax = 0.0;
for (j = 0; j < ma; j++)
{
if (w[j] wmax)
wmax = w[j];
}
std::cout << "w " << w.size() << " " << ma << std::endl;
thresh = tol*wmax;
for (j = 0; j < ma; j++)
{
if (w[j] < thresh)
w[j] = 0.0;
}
std::cout << "a " << a.size() << std::endl;
svdksb(u, w, v, ndata, ma, b, a);
DEBUG_LOG("svdksb end");
chisq = 0.0;
for (i = 0; i < ndata; i++)
{
multifn(i, ma, x, afunc);
s = 0.0;
for (j = 0; j < ma; j++)
s = s + a[j]*afunc[j];
tmp = (y[i] - s)/sig[i];
chisq = chisq + tmp*tmp;
}
DEBUG_LOG("svdfit end");
}

template < typename T >
class Vec : public std::vector< T {
public:
Vec(): std::vector<T>() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i)
{
return this -at(i);
}
const T& operator[](int i) const
{
return this -at(i);
}
};

typedef Vec<doubledblvec;
typedef Vec<dblvecdblmat;
Output is as follows

[Session started at 2006-10-05 14:53:08 +0100.]
main
0
test fit
test..
calling svd 2
SVDFit
sig 10~10
u 10~10 2~2
a 2~2
y 10~10
fn 0 0 sz 1
fn 0 1 sz 1
multidone
SVDFits
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
0 sig 1 afsig 1
nlps has exited due to signal 6 (SIGABRT).

Oct 5 '06 #1
4 2065
tmp = 1/sig[i];
std::cout << "afsig " << tmp << " " << afunc[j] << std::endl;
Oops, sloppy debugging. There is one.
Now I'm getting an assertion error here...
for (j = 0; j < ma; j++)
u[i][j] = afunc[j]*tmp;
....it does not make sense it looks ok.

Oct 5 '06 #2

Greg napsal:
tmp = 1/sig[i];
std::cout << "afsig " << tmp << " " << afunc[j] << std::endl;

Oops, sloppy debugging. There is one.
Now I'm getting an assertion error here...
for (j = 0; j < ma; j++)
u[i][j] = afunc[j]*tmp;

...it does not make sense it looks ok.
The bug is in the test routine.

for (i = 0; i < ma; i++)
u[i].resize(ma);

should be

for (i = 0; i < ndata; i++)

Regards
Jiri Palecek

Oct 5 '06 #3
The bug is in the test routine.
>
for (i = 0; i < ma; i++)
u[i].resize(ma);

should be

for (i = 0; i < ndata; i++)

Regards
Jiri Palecek
Well done, I spotted it too believe it or not, although it has taken me
much longer than it has taken you. At least it was in the code I posted
and not lurking somewhere else. It tests ok now.

Oct 6 '06 #4

Greg wrote in message
<11*********************@k70g2000cwa.googlegroups. com>...
[snip]
typedef Vec<doubledblvec;
typedef Vec<dblvecdblmat;
>void ffit::testfnfit(dblmat &x, dblvec &y, int ndata, dblvec &a, int
ma, int max_iters, double &chisq) // , idxmultifunc *funcs){
DEBUG_LOG("test..");
int i;
dblvec sig, w;
sig.reserve(ndata);
sig.resize(ndata);
w.reserve(ma);
w.resize(ma);
Hi Greg,
Just thought I'd rag on you a bit about using '.reserve()' and '.resize()',
one after the other.
In your case, you only need the '.resize()'[0]. You would use '.reserve()' if
you knew in advance that your vector would grow to a certain size, but
initially be smaller.

Check this out:

// includes here <iostream>, <ostream>, <vector>, etc.

void PrintVec( std::vector<intconst &vec, std::ostream &sout){
sout<<" size="<<vec.size()<<" cap="<<vec.capacity()<<std::endl;
return;
} // PrintVec( vector<intconst &, std::ostream &)

int main(){
using std::cout;
cout<<"\n--- VecInt(10) size test ---"<<std::endl;
std::vector<intVecInt(10);
PrintVec( VecInt, cout);
VecInt.push_back( 1 );
PrintVec( VecInt, cout);
for(size_t i(0); i < 11; ++i){ VecInt.push_back( i );}
PrintVec( VecInt, cout);
VecInt.resize( 50 );
PrintVec( VecInt, cout);
VecInt.push_back( 1 );
PrintVec( VecInt, cout);
VecInt.resize( 40 );
PrintVec( VecInt, cout);
cout<<"--- VecInt(10) size test ---END"<<std::endl;
return 0;
} // main end

/* --- output ---
--- VecInt(10) size test ---
size=10 cap=10
size=11 cap=20
size=22 cap=40
size=50 cap=50
size=51 cap=100
size=40 cap=100
--- VecInt(10) size test ---END
*/

Note how '.resize(50)' set the capacity to 50 (instead of doubling the
capacity).
Then note that '.resize(40)' reduced the size, but not the capacity [1].

The way you did does not 'hurt' anything, but does add 'clutter', like doing:

int number(50); // initialize to 50
number = 50; // 'set' it to 50. (not needed, but also not an error)

[ if you knew all this, sorry. Maybe it will help some newbie. <G]

[0] - or initialize like Chris[val] showed you (that was you?) in other NG.
[1] - on my machine (win98, MinGW)
--
Bob R
POVrookie
Oct 7 '06 #5

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.