home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18771 < prev    next >
Encoding:
Text File  |  1993-01-06  |  4.3 KB  |  102 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!gatech!swrinde!elroy.jpl.nasa.gov!ucla-cs!maui.cs.ucla.edu!edwin
  3. From: edwin@maui.cs.ucla.edu (Edwin Tisdale)
  4. Subject: Re: Overloading [] for 2d+ matrixes
  5. Message-ID: <1993Jan6.202822.12632@cs.ucla.edu>
  6. Sender: usenet@cs.ucla.edu (Mr Usenet)
  7. Nntp-Posting-Host: maui.cs.ucla.edu
  8. Organization: UCLA, Computer Science Department
  9. References: <1iarmlINN882@ub.d.umn.edu> <HUGH.93Jan5194117@kahu.cosc.canterbury.ac.nz> <1idfu8INNen2@shelley.u.washington.edu>
  10. Date: Wed, 6 Jan 93 20:28:22 GMT
  11. Lines: 89
  12.  
  13. In article <1idfu8INNen2@shelley.u.washington.edu>
  14. rons@hardy.u.washington.edu (Ronald Schoenberg) writes:
  15. >In article <HUGH.93Jan5194117@kahu.cosc.canterbury.ac.nz>
  16. >hugh@kahu.cosc.canterbury.ac.nz (Hugh Emberson) writes:
  17. >
  18. >[...elision....]
  19. >
  20. >>
  21. >>2) The not so elegant solution.
  22. >>
  23. >>Don't use [], use () instead.
  24. >>
  25. >>    Y& X::operator () (whatever, whatever)
  26. >>
  27. >>and use
  28. >>
  29. >>    a(i,j);
  30. >>
  31. >>
  32. >>It should be a bit faster, but its not as pretty.  It has the same
  33. >>precedence as [] too.
  34. >
  35. >Ugliness here is in the eye of beholder.  Personally, I think addressing
  36. >a two-dimensional array as a[i][j] is ugly.  And more work.  Maybe it's
  37. >because I touch-type, but I find a(i,j) a lot easier to type than a[i][j].
  38. >
  39. >Trying to overload operator [] for matrix classes generally seems to 
  40. >create more problems than it solves.  I've some experience with matrix
  41. >classes and that experience suggests that it is better to stick to 
  42.  
  43. An array `a' declared `<type> a[M][N]' is not a two dimensional array.
  44. It is a one dimensional array of M one dimensional arrays of N <type> elements.
  45. The reference `a[j]' is a pointer to the j'th array and `a[j][i]' references
  46. the i'th element of that array.  But compilers do in fact allocate a contiguous
  47. block of memory for the all M arrays in sequence.  This obviates the need for
  48. a separate array of pointers to locate each of the M arrays since the pointers
  49. can be easily computed on-the-fly.  If you think of `a[j]' as representing the
  50. j'th column of a matrix, then `a[j][i]' would be the element in the i'th row
  51. of that column.  But apparently there is such a strong compulsion to associate
  52. the first "index", j, with the row of a matrix and the second "index", i, with
  53. the column of a matrix that so-called two dimensional arrays are said to be
  54. organized in row major order in C programs.
  55.  
  56. It really shouldn't matter much whether you use `[][]' or `(,)' if you
  57. implement them as inline functions because the optimizing compiler will
  58. end up generating pretty much the same code either way.  Nor is there
  59. any reason why you shouldn't implement both indexing schemes.
  60.  
  61. The C++ Matrix class is available via anonymous ftp from `ftp.cs.ucla.edu'.
  62. Get the compressed tarfile `pub/Matrix.tar.Z'.  Uncompress `Matrix.tar.Z'.
  63. Extract the `Matrix' directory from `Matrix.tar'.  Then go to the `Matrix'
  64. directory and type `make'.  Now print and read the paper in `Matrix.dvi'.
  65.  
  66. In order to demonstrate an application of the \verb"Matrix" class,
  67. it was used to implement the backward error propagation algorithm
  68. for multi-layer, feed-forward artificial neural networks.
  69.  
  70. Enjoy, Bob Tisdale (edwin@cs.ucla.edu)
  71.  
  72. P.S.  I hope the following notes will be helpful.
  73.  
  74. unix% ftp ftp.cs.ucla.edu
  75. Name (ftp.cs.ucla.edu:your_login_ID): anonymous
  76. Password: your_login_ID
  77. ftp> cd pub
  78. ftp> binary
  79. ftp> get Matrix.tar.Z
  80. ftp> bye
  81. unix% uncompress Matrix.tar.Z
  82. unix% tar xvf Matrix.tar
  83. unix% cd Matrix
  84. unix% make
  85. unix% dvips Matrix | lpr
  86. unix% mv ffnet.new ffnet.old
  87. unix% cat ffnet.old ffnet.S | (backprop -v > ffnet.new) >>& ffnet.err
  88. unix% cat ffnet.new ffnet.S | evaluate | graph -m 0 | plot
  89.  
  90. Note: If you have trouble, verify that the `SYS5' variable
  91.       in the `Matrix/src/genclass' shell script contains
  92.       the path to the System V version of the `m4' macro
  93.       pre-processor.
  94.       The `fig2dev' program is part of the `transfig' package
  95.       available via anonymous ftp from `ftp.cs.cornell.edu'.
  96.       Get `pub/transfig/transfig.tar.Z'.
  97.       The EPIC style file is available via anonymous ftp from
  98.       `ymir.claremont.edu'. Get `tex/inputs/latex-contrib/epic.sty'.
  99.       File `pub/Matrix.sun.tar.Z' includes all the files created by
  100.       `make' on UCLA CSD Sun4 computers.  It may be useful to Sun
  101.       computer users or those who just want to print the documentation.
  102.