RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Problem with Gauss Matrix Code

Join Date: May 2007
Location: USA
Posts: 53
Reputation: tformed is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
tformed's Avatar
tformed tformed is offline Offline
Junior Poster in Training

Question Problem with Gauss Matrix Code

  #1  
Jul 27th, 2007
I got this code from a book I bought. I wanted to see if it words but it gives me errors.

Here is the source code.

  1. #include<math.h>
  2. #define NRANSI
  3. #include"nrutil.h"
  4. #define SWAP(a,b)
  5. {
  6. temp=(a);(a)=(b);(b)=temp;
  7. }
  8.  
  9. void gaussj(float **a, int n, float **b, int m)
  10.  
  11. {
  12.  
  13. int *indxc,*indxr,*ipiv;
  14. int i,icol,irow,j,k,l,ll;
  15. float big,dum,pivinv,temp;
  16. indxc=ivector(1,n);
  17. indxr=ivector(1,n);
  18. ipiv=ivector(1,n);
  19. for (j=1;j<=n;j++) ipiv[j]=0;
  20. for (i=1;i<=n;i++)
  21. {
  22.  
  23. big=0.0;
  24. for (j=1;j<=n;j++)
  25. if (ipiv[j] != 1)
  26. for (k=1;k<=n;k++)
  27. {
  28.  
  29. if (ipiv[k] == 0)
  30. {
  31.  
  32. if (fabs(a[j][k]) >= big)
  33. {
  34.  
  35. big=fabs(a[j][k]);
  36. irow=j;
  37. icol=k;
  38.  
  39. }
  40.  
  41.  
  42. }
  43. else if (ipiv[k] > 1) nrerror("gaussj: Singular Matrix-1");
  44.  
  45. }
  46.  
  47. ++(ipiv[icol]);
  48. if (irow != icol)
  49. {
  50.  
  51. for (l=1;l<=n;l++) SWAP(a[irow][l],a[icol][l])
  52. for (l=1;l<=m;l++) SWAP(b[irow][l],b[icol][l])
  53.  
  54. }
  55.  
  56. indxr[i]=irow;
  57. indxc[i]=icol;
  58. if (a[icol][icol] == 0.0) nrerror("gaussj: Singular Matrix-2");
  59. pivinv=1.0/a[icol][icol];
  60. a[icol][icol]=1.0;
  61. for (l=1;l<=n;l++) a[icol][l] *= pivinv;
  62. for (l=1;l<=m;l++) b[icol][l] *= pivinv;
  63. for (ll=1;ll<=n;ll++)
  64. if (ll != icol)
  65. {
  66.  
  67. dum=a[ll][icol];
  68. a[ll][icol]=0.0;
  69. for (l=1;l<=n;l++) a[ll][l] -= a[icol][l]*dum;
  70. for (l=1;l<=m;l++) b[ll][l] -= b[icol][l]*dum;
  71.  
  72. }
  73.  
  74.  
  75. }
  76.  
  77. for (l=n;l>=1;l--)
  78. {
  79.  
  80. if (indxr[l] != indxc[l])
  81. for (k=1;k<=n;k++)
  82. SWAP(a[k][indxr[l]],a[k][indxc[l]]);
  83.  
  84. }
  85.  
  86. free_ivector(ipiv,1,n);
  87. free_ivector(indxr,1,n);
  88. free_ivector(indxc,1,n);
  89.  
  90. }
  91.  
  92. #undef SWAP
  93. #undef NRANSI



and the header file:



  1. #ifndef _NR_UTILS_H_
  2. #define _NR_UTILS_H_
  3. staticfloat sqrarg;
  4. #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
  5. staticdouble dsqrarg;
  6. #define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)
  7. staticdouble dmaxarg1,dmaxarg2;
  8. #define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
  9. (dmaxarg1) : (dmaxarg2))
  10. staticdouble dminarg1,dminarg2;
  11. #define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
  12. (dminarg1) : (dminarg2))
  13. staticfloat maxarg1,maxarg2;
  14. #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
  15. (maxarg1) : (maxarg2))
  16. staticfloat minarg1,minarg2;
  17. #define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
  18. (minarg1) : (minarg2))
  19. staticlong lmaxarg1,lmaxarg2;
  20. #define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
  21. (lmaxarg1) : (lmaxarg2))
  22. staticlong lminarg1,lminarg2;
  23. #define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
  24. (lminarg1) : (lminarg2))
  25. staticint imaxarg1,imaxarg2;
  26. #define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
  27. (imaxarg1) : (imaxarg2))
  28. staticint iminarg1,iminarg2;
  29. #define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
  30. (iminarg1) : (iminarg2))
  31. #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
  32. #ifdefined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
  33. void nrerror(char error_text[]);
  34. float *vector(long nl, long nh);
  35. int *ivector(long nl, long nh);
  36. unsignedchar *cvector(long nl, long nh);
  37. unsignedlong *lvector(long nl, long nh);
  38. double *dvector(long nl, long nh);
  39. float **matrix(long nrl, long nrh, long ncl, long nch);
  40. double **dmatrix(long nrl, long nrh, long ncl, long nch);
  41. int **imatrix(long nrl, long nrh, long ncl, long nch);
  42. float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
  43. long newrl, long newcl);
  44. float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch);
  45. float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
  46. void free_vector(float *v, long nl, long nh);
  47. void free_ivector(int *v, long nl, long nh);
  48. void free_cvector(unsignedchar *v, long nl, long nh);
  49. void free_lvector(unsignedlong *v, long nl, long nh);
  50. void free_dvector(double *v, long nl, long nh);
  51. void free_matrix(float **m, long nrl, long nrh, long ncl, long nch);
  52. void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
  53. void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch);
  54. void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch);
  55. void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch);
  56. void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
  57. long ndl, long ndh);
  58. #else/* ANSI */
  59. /* traditional - K&R */
  60. void nrerror();
  61. float *vector();
  62. float **matrix();
  63. float **submatrix();
  64. float **convert_matrix();
  65. float ***f3tensor();
  66. double *dvector();
  67. double **dmatrix();
  68. int *ivector();
  69. int **imatrix();
  70. unsignedchar *cvector();
  71. unsignedlong *lvector();
  72. void free_vector();
  73. void free_dvector();
  74. void free_ivector();
  75. void free_cvector();
  76. void free_lvector();
  77. void free_matrix();
  78. void free_submatrix();
  79. void free_convert_matrix();
  80. void free_dmatrix();
  81. void free_imatrix();
  82. void free_f3tensor();
  83. #endif/* ANSI */
  84. #endif/* _NR_UTILS_H_ */


The errors I keep on getting is:

1>Linking...
1>gaussj.obj : error LNK2019: unresolved external symbol _free_ivector referenced in function _gaussj
1>gaussj.obj : error LNK2019: unresolved external symbol _nrerror referenced in function _gaussj
1>gaussj.obj : error LNK2019: unresolved external symbol _ivector referenced in function _gaussj
1>LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Documents\Visual Studio 2005\Projects\Linear Equation\Debug\Linear Equation.exe : fatal error LNK1120: 4 unresolved externals
Last edited by Ancient Dragon : Jul 27th, 2007 at 7:32 pm. Reason: remove color codes, correct code tags, and beautify code (whew!)
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:17 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC