C++ Lambda Functions with std::sort -- A Brief Tutorial

  • Posted on: 17 August 2014

C++11 added Lambda Functions to C++, a feature many developers felt was long overdue.

There are a lot of tutorials on the web already about using Lambdas in C++. These tutorials cover all the wide variety of ways which Lambdas can be used in C++, as well as possible reasons for doing so.

I thought I'd throw my hat in the mix, and try to provide an ultra-short, ultra-simple example, adapted from code you may have seen before. We're just going to use a Lambda function to extend the functionality of std::sort.

First, I'm going to use this code for a starting point for this tutorial. So make sure that that's fully digested before heading over here.

Mitered Bend Calculator for RF Microstrip Circuits

  • Posted on: 17 August 2014

In RF layout and design, the 90 degree microstrip mitered bend (or miter bend) is a way to change the direction of a trace quickly, while maintaining the line impedance.

Here are the design equations, which I took from Microwaves101, which they took from the original work. Follow the link for a wealth of additional pertinent information. Actually I just suggest perusing Microwaves101 every once in a while -- you're sure to learn something new. Anyway, here are the equations:

D = W* SQRT(2) (the diagonal of a "square" miter)
X = D* (0.52 + 0.65 e ^ (-1.35 * (W/H)))
A = ( X- D/2) * SQRT(2)

Here's a picture to describe the variables used in the equations above:

A C++ (complex) Polynomial Root Finder -- We'll Do It Fast, We'll use Eigen

  • Posted on: 11 July 2014

Ever wonder what's behind MATLAB's roots() function?

Well, it's not really a root finding algorithm at all. It uses (I believe), the idea of Companion Matrix to cast the root finding problem into an eigenvalue problem. I won't go into the proof, but if you're familiar with the eigenvalue problem, and by extension, a matrix's characteristic polynomial, you could guess some connection might exist between polynomial root finding and the eigenvalue problem.

So, let's cook up a C++ root finding algorithm based on the companion matrix.

Once again, we're really just solving an eigenvalue problem, so there's no need to re-invent an eigensolver. Instead, we'll use Eigen to the computation. This leaves us with very little work to do. In fact, let's just skip to the code:

#include <Core>

An Experiment in Converting MatLab to C++ -- Getting the Matrix Notation / Indexing Right

  • Posted on: 7 June 2014

Where I'm presently employed, I often proceed according to the following workflow:

  1. Get a Matlab / GNU Octave code of a numerical algorithm from boss, or elsewhere.
  2. Convert to C++ as part of my place of work's software packages.
  3. Over time, optimize it if it becomes oft-used, or turns out to be a bottleneck.

Obviously, Converting from Matlab code to C++ varies in difficulty based on the code. Sometimes it's easy, sometimes it's painful. The list below attempts to break down the differences between Matlab / GNU Octave and C++ into three categories. This is relatively superficial and non-technical, but here goes:

    Idea For Fast Rotation (or Vector Rotation) in Javascript -- Expanding the Rotation Matrix

    • Posted on: 28 May 2014

    Say you want to rotate a point, or a vector, through some angle. This transformation is a popular one for games, scientific applications, and much more. In fact, I used it a whole bunch when I was programming my game, Not Asteroids!. All I needed to do was right-multiply a 2d vector (x,y) by the rotation matrix shown here:

    ..and out pops the transformed vector. If you're unfamiliar with this idea, check out the links below to get up to speed. Trust me, it's not bad.

    Kwon 3d
    Dr. Math
    xkcd (just kidding)

    Easy, right?

    Okay, let's take a look at some javascript that will rotate a vector (x,y) by an angle theta, and return a 2 element array containing the rotated vector (x2,y2).:

    Block LU Decomposition Source Code (including back substitution, in MatLab)

    • Posted on: 27 May 2014

    In my last, post, I made mention of a page which contained a matlab-esque pseudo-code version of a block LU decomposition algorithm. But then, less than 24 hours after I posted it, the page was removed! That's life, I suppose.

    Luckily, I had already drafted up a MatLab version of the pseudo-code, at least to the pre-debugging point. A few hours later, I was able to create a functional version, where the results matched that of a native GNU Octave matrix solution.

    In case you didn't read the last post, and you're asking, what's the point of having my very own LU decomposition source code, here's the same response: I see your point. Libraries like LAPACK have a working, highly optimized version that's practically unbeatable on most common platforms. But what if you want to implement block LU on a new platform? Trust me, you don't want to dig through the LAPACK source -- at least not for a new version.

    Block LU Decomposition -- 1990s Web Meets Numerical Algorithms, Volume 1.

    • Posted on: 20 May 2014

    Every once in a while, I come across the following paradox: A well-written and/or extremely informative webpage about a numerical technique or method, that happens to be buried on, oh, say the fourth or fifth page of the relevant Google search result.

    Many of these pages scream 90s internet. Think bellsnwhistles.com. Think Microsoft Frontpage Express (that's what it was called, right)? Then sprinkle in some LaTex images, add in the mathspeak, and you're pretty close to the type of page I'm taking about. I suspect something in that old-school development workflow is what keeps these pages buried in search results.

    Hopefully, I can un-earth a few of these excellent pages. And hopefully, you agree they're undervalued in the eyes of Google and friends.

    To start things off, here's a great webpage about Block LU Decomposition, by somebody at Indiana University.