Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Programming

Journal Journal: Boost UBLAS matrix iterators and templates - Solved

UPDATE: I had a look around, figured I would try substituting a std::vector<std::vector<double> > for the uBLAS matrix<double>, still got the same error. So I started looking better into templates (no, I'm not quite done with vol2 of "Thinking in C++") and found out about typename. Seems to fix the problem.

I know I should probably post this to stackoverflow or the Boost/UBLAS mailing list, but I figure there are plenty of smart people here at slashdot.

Let's say you are using UBLAS from Boost and you want to implement a cumulative summing function for matrices. Here's what I think is a fairly straightforward way to do it:

// For boost::numeric::ublas::matrix<>.
#include <boost/numeric/ublas/matrix.hpp>

// For std::partial_sum().
#include <numeric>

template<class T>
boost::numeric::ublas::matrix<T> cumSum
(const boost::numeric::ublas::matrix<T>& input_,
const bool& colWise_ = true)
{
using namespace boost::numeric::ublas;
using namespace std;

matrix<T> result_(input_);

if (colWise_)
for (matrix<T>::iterator2 colIter = result_.begin2();
colIter < result_.end2();
colIter++)
partial_sum(colIter.begin(),
colIter.end(),
colIter.begin());
else
for (matrix<T>::iterator1 rowIter = result_.begin1();
rowIter < result_.end1();
rowIter++)
partial_sum(rowIter.begin(),
rowIter.end(),
rowIter.begin());

return result_;
}

For now, I'm ignoring completely templatizing this to make the row-wise/column-wise distinction disappear in the code and focusing on just getting it working. Only it doesn't work; won't compile. Couldn't figure out why, but g++ kept saying it was expecting a ';' before colIter and rowIter. I had a hunch and replaced one of the iterator's 'T's with 'double' and it stopped complaining about that one. Am I missing something, or does UBLAS not implement iterators properly?What am I missing?

User Journal

Journal Journal: Tragic Love Story Junkies anonymous 4

Hi, my name is Nat^H^H^HJim, and I admit, I have a problem: I'm addicted to tragic love stories.

It started simply enough with "Romeo and Juliet", but then I saw "Crouching Tiger, Hidden Dragon", and I was hooked, because it had not one, but TWO sets of star-crossed lovers.

Next was "Moulin Rouge", which even my gay brother despises. But it holds a special place in my heart.

I was okay for a while, high on such movies as "Fight Club". But then that bastard Chris Nolan had to make "Memento" (I'm particularly drawn to stories about men who have lost the woman they love). For some odd reason, the "Star Wars" prequels didn't really strike a chord, although they were close.

Then I got married, and I thought I was doing better. Then along comes "Inception". Oh sure, everyone hypes it for being "mind-bending" (what? it wasn't like it was "Primer"), but I secretly believe that Nolan knows how to make an excellent tragic love story, and it shows in not only "Memento" but "Inception".

As I sit here listening to the final track of the "Inception" soundtrack CD (the music from baggage claim to the credits; my favorite by far), I find myself hungry for more. I'm not even sure how to find more, as it's hard to describe. Some other stories I'm acquainted with touch close on similar feelings: the ending to "Lord of Light" by Zelazney, "Permutation City" by Egan (and even further off track, but still close in tone "Diaspora"). "The Fountain" by Aronofsky is definitely another movie that meets the criterion, as well as "Chasing Amy" by Kevin Smith.

I guess I could at least take a stab at some adjectives: a sense of loss, a longing for those truly special people we will never meet again, a feeling of mystery and awe; stories that end with catharsis. So, could you do a junkie a favor and find him one more fix? Thanks :)

User Journal

Journal Journal: Fucking Microsoft 4

You know why I hate Microsoft? They can't follow standards. Or they declare themselves a de facto standards body, even over things they have no rights to dictate standards on. Like C++. I had forgotten that MSVS has a broken version of std::copy(). It doesn't work on simple char arrays. Works fine on VxWorks and Linux. Fucking Microsoft.

Slashback

Journal Journal: Dear slashdot 3

The foe/friend limit is ridiculous, even with subscription. 400 friends/foes is simply not enough to foe all the people who post baseless assertions and get modded to +5, and simultaneously friend people who who get modded troll for posting an informative link that blows the moderators' beliefs out of the water. Pls fx, kthxbye.

User Journal

Journal Journal: Profiling 3

No, not the racial kind. This has to do with code efficiency. I was recently writing patch acceptance guidelines, and was trying to explain that maintainability matters far more than efficiency. I was trying to be polite about people who harp on "efficiency", but my basic feelings boil down to: "People who talk about software efficiency, yet aren't familiar with software profiling, are idiots." Discuss.

User Journal

Journal Journal: Rare earth metals 3

Just a quick thought before I have to go into work today: so China is possibly looking at cutting back their rare earth metal exports, but that doesn't seem to stop them putting them in children's toys and/or jewelry. So, why not this: don't chastise China for bad behavior; just buy up all the heavy metal toys as they are imported and melt them down! Heck, it even says in the article that the jewelry "easily sheds" cadmium.

I mean, lead was one thing, we're not in need of that (apparently we use something else for our pipes now). But cadmium? Last I checked, that was good for electronics, and we *always* need more of those.

GNU is Not Unix

Journal Journal: Stable API Nonsense 2

Recently there was a discussion about how Linux cell phones are basically DOA, and within that discussion was a thread on a stable driver interface in Linux. Rather than respond to every comment as to why this is a bad idea, I figured I would just post the original response to this request for a stable driver API which was put together the first time people noticed it was hard to ship binary drivers for Linux:

Let me just point out that this approach *works* and *has worked* quite well for quite sometime. These days, when I buy a new piece of hardware, I throw out the driver CD without even looking at it. I plug in the hardware to my Linux box fully expecting it to work; if it doesn't, I figure it's broken. Occasionally, it's not broken hardware but rather I have made a mistake and invested in a piece of hardware from a company that has not seen the light and open sourced their drivers. I make a point never to buy from that company again, and I get a refund for the hardware. I might point out that this is a very rare occurrence. Most of the time there is support for hardware out of the box under Linux - no driver CDs, no install and no reboot required - just plug and go.

Implementing a stable API for drivers would lead to bloat, insecurity and instability; we've seen it happen in Windows, and even starting to happen with binary drivers under Linux! The technical argument is that Linus (and his kernel devs) reserve the right to rip out or reimplement anything they please in the kernel, with no regard whatsoever to backwards compatibility. This model has worked very well so far, as evidenced by the fact that you can plug in 99% of hardware into a Linux box and have it just work, no driver CD, download or install required.

Still, some people argue this is a political issue, not a technical one. I would agree, on one point: the hardware vendors are the ones making this a political issue. By refusing to release their source code, they limit how well their device can be supported. For what purpose do they need to keep their drivers closed? They sell hardware, not software, therefore they don't even have an economical reason. The arguments for releasing source are legion: support in ALL distros on ALL architectures by default; more stable and better tuned drivers; no need to ship driver CDs; no worry about having to support your drivers as they are now in the kernel, etc. I can think of no reason other than petty greed to keep drivers closed. And it's greed that doesn't even function properly at that, as it doesn't gain the greedy companies anything.

Is there any *technical* reason to keep drivers closed source? Please elucidate, and remember that even if there is, it must somehow outweigh all the benefits of having the same driver as an open source driver in the kernel in order to argue that there should be a stable driver API. This isn't an ideological, or even political issue; open source drivers have been shown *in practice* to work better, and provide all of the benefits I've listed. Why should we throw out one model that works for one that has been shown to not work nearly as well?

Music

Journal Journal: Found something that I think sounds cool 6

*Sigh*, I'm still waiting on my 1TB HDDs for the file server, which I will use to replace the 250GB drive, which I can then put in the web and email server. For now, that means I still post journal/blog entries here, as I don't see the point of upgrading the software on the web/email server until I have the drive available for it. Anyway, my pointless rambling is all a preface for this:

http://www.archive.org/details/Snu-op025-Caravan

I found it while looking for recordings of sheet music we play in our Big Band (guess which song I was looking for :). I thought the song sounded pretty cool, so I'm sharing it with you. Hope you enjoy it!

GNU is Not Unix

Journal Journal: "Free Software doesn't have end users" 3

So, I was going through the links on BBSpot today, and I found what I can only describe as one of the most lucid, well-written articles on Free culture I've seen in a long time: Thank you for giving me the opportunity to explain this to you. It's so fan-fucking-tastically good that I'm severely tempted to make my new signature on slashdot a link to that article with the title "Free Software doesn't have end users".

Every time I see some whiny bastard say "but Linux will never take over the desktop! you have to pay attention to MEEEE -*- cough -*- I mean, you have to pay attention to the end users!" I just want to bitchslap the little schmuck. What have these so-called "end users" done for Linux? Fuck them! The end users of Linux and Free Software have always been the same people who created it: the developers. If you "just want it to work", and you can't even be arsed to file a bug report, fuck you. It works for us; you should be grateful we decided to share the source with you.

Ranting aside, this really cuts to the heart of why I use Free Software: even if it wasn't technologically superior (which it is, precisely because it is Free Software), I would still use it because it is the only software I can modify and use however I want without having to say the equivalent of "please sir, may I have another?" to some big corp. whose only concern is next quarter's earnings.

Free Software doesn't have end users; that's kind of the point. You can either participate and contribute a little something back or you can pay some company to tell you what you are not allowed to do. We welcome your input, but if all you can do is say "your software sucks! do what I say or I'll use someone else's software!" then all I can say is farewell and peace be to you.

Books

Journal Journal: John Taylor Gatto's "Underground History of Education" 5

So I've been reading The Underground History of American Education by John Taylor Gatto. I've found it very enlightening, and up until Chapter 14 I was pretty much agreeing with the author. Now, I'm not one who dismisses everything I disagree with out of hand; on the contrary, I try to seek out opinions different from mine and test them (and mine) against evidence to better understand the world. But when Gatto starts citing court rulings such as the "posting the ten commandments in school rooms is unconstitutional" as bad and anti-religious, I start to grow a bit skeptical.

As I had been reading TUHoAE I detected faint warning signs that Gatto might have a religious bias. Little things that warned me, such as his antipathy towards what he called "scientific teaching". I could excuse some of these as perhaps merely terminology differences; what Gatto meant by "scientific thinking" did not match up with what I meant by it.

But then the other shoe dropped: Gatto demonstrates his bias against science which contradicts his religion:

Instead of remaining merely a useful description of how things work, great synthesizing theories like Big Bang or Natural Selection purport to explain the origin of the universe or how life best progresses. Yet by their nature, these things are beyond proof or disproof. Few laymen understand that the synthesizing theories of Science are religious revelations in disguise.

How are the big bang theory and the theory of natural selection not useful descriptions of how things work? How could someone who is obviously so erudite and had researched previous chapters so well fall into the trap of baseless assertions and a basic misunderstanding of the way science works? Is he not aware that natural selection has been demonstrated many times?

Another quote that left me gaping:

We are hidden by more than a half-century from the shock and numbness this new doctrine of "separation of church and state" occasioned, a great bewilderment caused in part by the absence of any hint of such a separation doctrine in the Declaration, Constitution, or the Bill of Rights.

Did he seriously just ignore the first amendment? Is he completely oblivious to the treaty of Tripoli, which by the constitution is legally binding as law of the land, and it states "the Government of the United States of America is not, in any sense, founded on the Christian religion" and it was signed over two centuries ago?

Another question I had was why had no one noticed this? In all the reviews I read, no one had mentioned that Gatto turned into a raving young-earth creationist midway through his book. Had these people read the book all the way through? Where are all the people who should be calling him out on his kookiness? The chapter I'm currently reading is his least footnoted and therefore least supported by evidence one so far, although I notice that previous chapters had been slowly but steadily heading towards less references.

Gatto tries to sound unbiased and reasonable, but the way he phrases things makes it obvious he thinks that there can be no morality without religion. Through the court decisions he throws out as "anti-religious" the reader can also tell he is one of those christians who believes he is being persecuted because he isn't allowed to force his religion on others.

I will try to make myself finish the book, grating though Gatto's unfounded assertions may be. I do value the eye-opening, well-researched and well-documented information that was contained in the earlier part of the book. It had always seemed to me that there was something wrong with public school education in America; I'm grateful to have substantiating evidence to back that assertion up. But I think that Gatto has misdiagnosed the cause and blamed the wrong driving forces for the downfall of education in this country. While I will not deny that anything can be taken dogmatically, I don't believe that returning to one dogma (religion) in place of another (so called unscientific "scientism") is the answer. I believe the answer is to become more rational, less biased and more pragmatic towards how people learn.

What do you think?

Programming

Journal Journal: read() vs recv() vs recvfrom() for UDP sockets

This may be old news to network programming gurus out there, and those dealing with saner programming languages and libraries probably won't have to worry about it, but I figured I'd post it for all those newbs (like me) to hopefully save them some time.

In C, it turns out that read() and recv() don't work on unconnected sockets (at least UDP, in this, my little story). The man pages do say something along the lines of "recv() is normally used on connected sockets", but to me that seems a bit subtle. Sometimes, some of us need a good *WHACK* on the head with a cluestick, and I finally got one when I was perusing the netcat source code to see what they did that made read() work with UDP. Turns out you have to call recvfrom() at least once on a UDP socket before you can call read() or recv() on it and get it work "reliably". I say "reliably" because what took me so long on this was that I figured UDP meant UNRELIABLE datagram protocol, so that's why I wasn't getting all the data. Turns out sending data over loopback shouldn't drop any packets, even when you use UDP.

And just for reference: no, select() accept() and listen() don't seem to work on UDP sockets either. Bummer :(

The Military

Journal Journal: Working for the Government 1

So, reading this, it got me to thinking (again): why do I work for the government?

First, a little background: I graduated with a BS in CS in 2000. I then went to work for a startup started by my most challenging professor (I figure I'd learn more working for him than going to graduate school). Half a year later, they lay me off (but toss me some contracting work) because they can't afford to pay me (dotcom bust and all that). Somewhat naively, I decide to try consulting. Seven months later, after burning through all my savings, I'm desperate for a job. I mention this to a college friend who had gone back to his home town to work for DoD, and he manages to get me a site visit. A month later (surprisingly fast for the Feds!), I'm working at a Navy base in the middle of the desert.

So here I am, seven years later, wondering "why do I still work for the government?". A lot has changed since then. I love the town. It's small, dry and hot year round (I grew up in very similar desert). Plenty of very tall and fun to climb/hike/camp in mountains are within a days drive. I play alto sax in the local big band (and occasionally orchestra). I just got sworn in to the local mountain rescue group. The one thing that sucks is the job, and even it's not that bad.

You know how everyone says government employees don't work? It's true, but not for reasons you think. Sure, there are many who are just plain lazy. But out here at a place where we are far from the bureaucracy, where the majority of employees are engineers and scientists who are passionate about their work, the real reasons work doesn't get done are also the major reasons people quit or just gripe about their jobs. Security, while necessary, is a significant speed bump on the road to productivity. Having to get hardware you need to test out of a safe that only one person (not you) has access to, only to find out that you need a computer (that's in another safe that you don't have access to), can be demoralizing.

Not being allowed to install software that you need to do your job is another frustrating thing. It seems that even though someone may be trustable with state secrets, and be an expert in their field, they aren't allowed to decide what the best tool for the job is.

There are numerous other reasons: waiting for months for parts, having to pay credit card bills out of pocket for travel expenses (because vouchers in the travel system are 3 months behind), being forced to "standardize" on one software platform at a place where by definition, standards may be in flux (we are a research center, after all), or the standards are not the best tool for the job and were only chosen for political reasons.

So why do I still work here? I have been tempted, quite often to quit. However, there is nowhere else in this town where I could get the same salary for the same job. It's too small a town. I don't want to move; I love the town. I could try consulting/contracting, but that's iffy. And despite all it's drawbacks, it's not really all that bad. I work with smart people, and the one good thing about bureaucracy is that it's slow, so I haven't been completely cut off yet in the self-administration arena (I have a development machine that I alone am root on; the network admins and I have an understanding: I never call for help or create a problem, and they keep my connection up). All those times when I can't get real work done because I'm waiting for something? I've been playing with different programming languages, toolkits, development tools, etc. Recently, I've been going through "The Pragmatic Programmer" and quite enjoying it. I have some qualms about helping a corrupt government design weapons, but I don't blame Smith and Wesson for gun deaths.

I would jump at an opportunity to get paid to work on open source software from home (telecommute), but I'm not moving. I'm married and my wife also works on base. She has complaints about her job as well, but we both like the town; relocation is out of the question unless we could both get comparable jobs somewhere else in a similar environment.

Now, if they try to make me into a manager who doesn't get to write any code, then I will be looking for a new job.

Programming

Journal Journal: Re-usable vs Re-editable 1

Given the recent controversy over Prof. Knuth's comments, I'm left pondering one (of many) questions: why can't code be both re-usable and re-editable? (no, I haven't read the article)

Are re-usable and re-editable code fundamentally opposed? Would it just take too much time (or genius) to make code both re-usable and re-editable? Or is Knuth just calling designing for re-usability a waste of time like unit tests?

To me, the re-usableness and re-editableness of code seem orthogonal. You can write code that is re-usable but completely un-editable (it's usually unreadable at that point). If you're writing code properly, it seems to me that re-editable is a given. Re-usable is more difficult to do, but it seems like a good idea, and a best practice that most programmers should try to get into the habit of (just like writing re-editable code).

Maybe I'm just missing something here, but I always aim to make my code re-editable and re-usable, and one doesn't seem to get in the way of the other, other than competing for my time. Thoughts?

Programming

Journal Journal: Heap Debugging in C 5

I learned some very interesting things today. First, valgrind kicks ass. Second, don't always believe what you read in man pages. Take this little snippet I'd been working on for the past 1.5 days:

uint16_t
checksum (packet)
    struct re_packet *packet;
{
  int i = 0;
  uint16_t checksum = 0, temporary = 0;
  unsigned int intermediate = 0;
  unsigned int *binary_blob = calloc (1, sizeof (struct re_header) + packet->header.size);
  if (binary_blob == NULL)
    return (-1);
 
/// Necessary because checksum doesn't include itself.
  temporary = packet->header.checksum;
  packet->header.checksum = 0;
 
  if (!memcpy (binary_blob, &packet->header, sizeof (struct re_header)))
    return (-1);
  if (!memcpy (binary_blob + sizeof (struct re_header),
          packet->data, packet->header.size))
    return (-1);
 
  for (i = 0; i < (packet->header.size + sizeof (struct re_header)) / 4 ;
      ++i)
    intermediate ^= binary_blob[i];
 
// Restore original checksum.
  packet->header.checksum = temporary;
 
  checksum = intermediate ^ (intermediate >> 16);
 
  SAFE_FREE (binary_blob);
 
  return (checksum);
}

Now, you see, the "binary_blob" has to be a pointer to an array of unsigned integers because that's what we're using for the checksum. So far so good, right? Except that the program using this little snippet has weird memory errors, and when I say weird, I mean segfaulting in calloc(). A sure sign of heap corruption. But where could it be coming from. Just as a check, I insert some code to print out how many iterations it goes through before it crashes; ends up being just one. Hmm, heap corruption usually takes longer than that.

As I watch the compile for the zillionth time, it occurs to me that wxWidgets is being linked because this project has a separate GUI; but this little piece of code doesn't need it. Perhaps wxWidgets is doing some weird initialization stuff that is really not necessary in a piece of C code. So I also quickly learn how to turn that off in automake, and recompile. Still the same error in the same place. Hmmm . . .

Go learn how to use valgrind with memory checking. It says that "binary_blob" is being written beyond it's end. Huh? It's exactly as big the writes *should* be. Double-check memcpy(3); nope, it clearly says the third argument "n" is the number of bytes.

After banging my head against this for another couple of hours, I finally get the crazy idea, "hey let's just try casting to see if that fixes it, even though it shouldn't make a difference." Et voila! It works:

uint16_t
checksum (packet)
    struct re_packet *packet;
{
  int i = 0;
  uint16_t checksum = 0, temporary = 0;
  unsigned int intermediate = 0;
  unsigned int *binary_blob = calloc (1, sizeof (struct re_header) + packet->header.size);
  if (binary_blob == NULL)
    return (-1);
 
/// Necessary because checksum doesn't include itself.
  temporary = packet->header.checksum;
  packet->header.checksum = 0;
 
  if (!memcpy ((void *) binary_blob, &packet->header,
    sizeof (struct re_header)))
    return (-1);
  if (!memcpy ((void *) binary_blob + sizeof (struct re_header),
          packet->data, packet->header.size))
    return (-1);
 
  for (i = 0; i < (packet->header.size + sizeof (struct re_header)) / 4 ;
      ++i)
    intermediate ^= binary_blob[i];
 
// Restore original checksum.
  packet->header.checksum = temporary;
 
  checksum = intermediate ^ (intermediate >> 16);
 
  SAFE_FREE (binary_blob);
 
  return (checksum);
}

Apparently, memcpy(3) does some hinky where it actually looks at the type of the dest you pass it and decides that, no, "n" doesn't actually mean bytes, it means the number of elements you want to write to in that array. At least that's my theory.

(Posting this here because I don't have my own blog and I'm too lazy to look up the C newsgroup on groups.google.com; excuse the incoherence; I'm really frazzled and just happy it works (for now)).

GNU is Not Unix

Journal Journal: Webcam(s) for GNU/Linux 1

(posting this under "GNU is Not Unix" as there doesn't appear to be a "Linux" category or an "Ask Slashdot" category; WTF is up with slashdot these days? Are they forgetting their roots? Anyways, that's another rant . . . ).

I've been looking for a webcam that works with Linux for a long time. I've tried ordering Logitech QuickCams of the 3000 and 4000 variety once I was fairly certain they would work with Linux only to be told that they were "out of stock" or "back-ordered" (another rant: why can't these retards keep their online inventory up to date? it really should be prosecutable under false-advertising to list a product as available when it isn't; I found a blog posting along these lines somewhere, but can't find it now; I can't remember if I found it linked from slashdot.org or planet.debian.org; if anyone has a link to it, let me know).

Here's my question: is there a webcam that I can use with Ekiga and MythTV's MythPhone plugin? Where can I buy it? Please note that getting used hardware off of eBay is not an option; binary only drivers are also not preferred. I'm just looking for a simple webcam that I can do videoconferencing with under Linux. I know that many people will say "just search google", but believe me, I've tried and every time I find that the webcams supported by Linux aren't for sale anymore.

Slashdot Top Deals

A morsel of genuine history is a thing so rare as to be always valuable. -- Thomas Jefferson

Working...