• Home
  • About

Shores of the Dirac Sea

A blog about physics… mostly.

Feeds:
Posts
Comments
« Multiverse…
Something to read »

Index ordering with pointers.

August 3, 2010 by dberenstein

Sometimes programing can be fun. Sometimes it isn’t. I particularly hate debugging.

Today I was programming a function that would compute the commutator of two square matrices in c++, which do not have a definite size in the program. I ended up implementing it in the following way, by using the pointers to the beginning of the matrices and the size of the matrix n.


void commutator(int n , double *a1, double  *a2, double *b1)
{
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
*(b1+i+n*j)=0;
for(int k=0; k<n;k++){
*(b1+i+n*j)+= *(a2+i+n*k)*(*(a1+k+j*n))-*(a1+i+n*k)*(*(a2+k+j*n)) ;
}
}
}
}

The two matrices are a1, a2, and the result is placed on b1.

The placing of parenthesis is very crucial. This was the best solution I found after trying all kinds of other ways that were giving me trouble with the compiler. This is probably good for numerics, but lousy for preventing errors in the handling if one does not pay attention. What is fascinating to me is that the symbol * does two things: both multiplication and looking up the address of a pointer. And these can appear consecutive to one another.

Oh well. Just thought I’d share my messy code.

About these ads

Rate this:

Like this:

Like Loading...

Posted in computers | 13 Comments

13 Responses

  1. on August 3, 2010 at 9:45 pm Matt Leifer

    Why don’t you use a well-developed library to handle your matrix operations, e.g. http://exmat.sourceforge.net/ ? There is no point in reinventing the wheel.


    • on August 3, 2010 at 11:13 pm dberenstein

      Hi Matt:

      Oh that’s easy to explain: I’m relearning how to program properly and I’m doing this as an exercise. Part of the reason I want to do some of these things myself has to do with optimization on multiprocessors and other tricks to get the computer to work more efficiently.


  2. on August 4, 2010 at 5:30 am Luboš Motl

    Dear David, what I like best about this posting is the CSS design of the embedded code – a part of WordPress, right? ;-)

    If you thought that the readers would be able to learn what the star can actually mean in C plus plus, well, you have failed at least in my case. I only understand * as multiplication.

    There exists a two-step way to define the commutator:

    http://www.wolfram.com/products/mathematicahomeedition/?greetingsfrom=lubos-motl-reference-frame

    commutator[a_, b_] := a.b – b.a;

    The first step listed above is to buy Mathematica 7.0.1. ;-)


    • on August 4, 2010 at 7:06 am Luboš Motl

      Oh, I see. Now I understand the pointer. The matrix is simply squeezed somewhere in the memory as a one-dimensional array, a sequence of mud.

      So if you want to move by j rows and i columns, it’s like moving by i+n*j places. Also, b1 stands for the physical address of some memory point where the b1 array-matrix-mud-junk begins

      And *(something) is the memory cell corresponding to the address calculated as “something”. So

      *(b1+i+n*j)=0;

      is the command that annihilates the content of a number at i-th column and j-th row of the array-matrix-mud-junk called b1 because the content of the parenthesis is the right address. ;-) The size of the cell is “double” i.e. 32 bits or, more likely, 64 bits?

      Funny. Thanks for your C++ lecture. It’s clearly somewhat closer to the machine code than the higher languages. I would love assemblers 25 years ago but it seems nicer to leave this machine-code-level work to others.


      • on August 4, 2010 at 5:50 pm onymous

        The code looks almost like C rather than C++, except for where the variables are declared. C++ is a significantly higher-level language than C, but it keeps the C parts around anyway. Most C++ code usually avoids pointers as much as possible, or at least wraps them in safer data structures. Apparently David likes to muck around with them. Usually the efficiency gain isn’t worth the effort, but I’ve never had to optimize parallel code, so probably he has a good reason.

        Mathematica is painfully slow.


    • on August 4, 2010 at 10:01 am dberenstein

      Hi Lubos:

      If Mathematica weren’t so slow, it could be useful for moderate numerical simulations. As it is, solving differential equations with matrices is too slow to be useful and one needs to resort to something that more directly addresses the machine.

      Oh by the way, C and C++ is notoriously famous for being able to produce code that is almost incomprehensible to people. Unlike postcript which is essentially always incomprehensible to people :)


      • on August 4, 2010 at 6:27 pm Luboš Motl

        Dear David,

        you could often be surprised that Mathematica can solve many differential equations either analytically, or self-optimize the numerical methods to solve them and outclever your C++ code. I am not saying that this will always occur but sometimes it does.

        Decades ago, I would also think that addressing the transistors of a machine directly would improve my efficiency in dealing with computers – essentially because anything else that is added upon the machine code is garbage created by inferior people. ;-)

        I no longer think so. There’s a lot of useful structure in the higher languages and you are often rewarded by great gifts for this apparent slowness. And by the way, the machine code was invented by mortal humans, too.

        Cheers
        LM


  3. on August 5, 2010 at 1:05 am Haelfix

    Premature optimization is the root of all evil!


  4. on August 8, 2010 at 3:07 am peeterjoot

    You’d probably find your own code easier to read and understand if you didn’t use both meanings for the *

    There’s really not a good reason to use:

    *(b1 + i + n*j) = 0 ;
    

    When you can write:

    b1[ i + n*j ] = 0 ;
    

    This de-messifies it a bit, especially when you have multiple array references.

    Note that any modern optimizing compiler is going to generate the same code for pair of accesses (if you don’t believe it, generate assembly for both sets of code and compare).


  5. on August 8, 2010 at 3:26 am mike

    Yuk. C++ I know not why people cause themself such pain. I’ll file it under ‘programming fetishes’


  6. on August 8, 2010 at 7:27 pm Eric Juve

    Haelfix…. “Premature optimization is the root of all evil!” Truer words were never spoken


  7. on August 9, 2010 at 7:07 am vorpal

    Not sure if the optimizer does this already, but rather than multiplying and adding, you might be better off creating dummy pointers that you increment by n and/or 1.
    rather than:
    *(a2+i+n*k)

    //prep it
    ptr2 = a2+i;
    //do bare minimum in loop
    for loop {
    ptr1 += 1;
    ptr2 +=n;
    *res += (*ptr1)*(*ptr2);
    }

    So there is no chance any superfluous multiplication is done.

    You should also const the input matrix pointers in the prototype. This tells the reader that they are input.


  8. on August 9, 2010 at 3:01 pm dberenstein

    Hi everyone:

    Thanks for the pointers :) . I’ll implement them in the code better.



Comments are closed.

  • Recent Posts

    • Woof Woof
    • Happy 3.1415926535… day
    • Unstable Universes
    • Bad science reporting versus good science reporting
    • If some of my students were writing problems
  • Archives

    • April 2013
    • March 2013
    • February 2013
    • January 2013
    • November 2012
    • September 2012
    • August 2012
    • July 2012
    • May 2012
    • March 2012
    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • September 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • August 2010
    M T W T F S S
    « Jul   Sep »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  
  • Recent Comments

    Plato on Woof Woof
    Pepe on Woof Woof
    dberenstein on Woof Woof
    Lubos Motl on Woof Woof
    Wyrd Smythe on Happy 3.1415926535……
  • Physics/Math/Science Blogs

    • Asymptotia (Clifford Johnson)
    • Backreaction
    • Coctail Party Physics
    • Cosmic Variance
    • Dmitry Podolsky
    • Jeffrey Epstein Science
    • John Baez
    • Michael Nielsen
    • Musings (Jacques Distler)
    • Not even wrong
    • Resonaances
    • Robert Helling
    • Shtetl Optimized
    • Sunclipse
    • Terry Tao
    • Tomasso Dorigo
    • Uncertain Principles
  • Science Resources

    • Physics (APS journal)
    • Scientific American
  • Some More Blogs

    • Evil Inc
    • Fafblog
    • phd Comics
    • Regator
    • Scenes from a multiverse
    • Site Meter
    • WordPress.com
    • WordPress.org
  • Pages

    • About
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.com

Blog at WordPress.com.

Theme: MistyLook by WPThemes.


Follow

Get every new post delivered to your Inbox.

Join 33 other followers

Powered by WordPress.com
%d bloggers like this: