## A wierd way to invoke the compiler

November 18, 2016 C/C++ development and debugging. ,

Did you know that you can run the compiler from sources specified in stdin?  Here’s an example:

// m.c
#include <stdio.h>

int main()
{
int x = 3;
printf("%d\n", x);
return 0;
}


You have to specify the language for the code explicitly, since that can’t be inferred from the filename when that file data is coming from stdin:

$cat m.c | clang -g -x c - -o f$ ./f
3


This fact came up in conversation the other day. The result is something that is completely undebuggable, but you can do it! I’m curious if there’s actually a use case for this?

## Kiva, a fun alternative to standard charitable donations.

November 17, 2016 Incoherent ramblings , ,

This year, when I was still at IBM, I had opted out of the IBM Employee Charitable Fund (ECF) and chose to put cash into Kiva microloans instead.  I was tired of just blindly dumping money into some big charity like United Way.  I don’t really have any idea what United Way or any other charity is doing with the money I provide.

At the time I had no idea that I wouldn’t be working at IBM for much longer.  When I left IBM and started working as a contractor for LzLabs in the spring, having made this switch already made it easy for me to continue doing so.  The only change I made I was increase the amount that I was making for my monthly Kiva funds topups.  I’m now an employee of LzLabs Canada (3 of 5 at the time of the incorporation), but since we don’t have an IBM style employee charitable fund, I am still putting funds into Kiva that I used to put into explicit registered charities.

I quite like Kiva as an alternative to standard charity. For one, it isn’t straight up charity, since I can choose to fund people who look like they are trying to improve their condition.  That’s a sustainability difference that I think is very important, and part of the trap of the welfare system.  If you reward welfare recipients by providing it unconditionally as we do, you create the welfare state.  Most of my Kiva loans have specifically targeted individuals who have some sort of business venture that they are trying to improve.  My preference has been for people that want tools or livestock (example: a milk producing cow) that will continue to provide value long after the loan is paid.

Its fun to be able to specifically choose who my funds are going to, and when the loan repayments come in, I can often recycle those repayments directly into a new loan without even waiting for my next top up period.

I don’t think that the default overhead that Kiva wants for each loan is reasonable ($3 on$25), so I lower that significantly each time.  Note that if the loan that you try to fund doesn’t get the backers required, Kiva keeps that overhead donation amount.  When I recycled funds after such an unfunded loan into a new loan, I explicitly set the corresponding Kiva donation amount to zero.

Unfortunately, I don’t think I can get a Canadian charitable tax credit for the sort of permanently recycling Kiva loans that I am doing, but it’s fun enough to see my loan portfolio grow that I don’t care too much about the tiny little tax kickback Uncle Trudeau and Aunt Wynne “give me” in exchange for me financing their spending sprees.

## Transverse gauge

Jackson [1] has an interesting presentation of the transverse gauge. I’d like to walk through the details of this, but first want to translate the preliminaries to SI units (if I had the 3rd edition I’d not have to do this translation step).

### Gauge freedom

The starting point is noting that $$\spacegrad \cdot \BB = 0$$ the magnetic field can be expressed as a curl

\label{eqn:transverseGauge:20}

Faraday’s law now takes the form
\label{eqn:transverseGauge:40}
\begin{aligned}
0
&= \spacegrad \cross \BE + \PD{t}{\BB} \\
&= \spacegrad \cross \BE + \PD{t}{} \lr{ \spacegrad \cross \BA } \\
&= \spacegrad \cross \lr{ \BE + \PD{t}{\BA} }.
\end{aligned}

Because this curl is zero, the interior sum can be expressed as a gradient

\label{eqn:transverseGauge:60}
\BE + \PD{t}{\BA} \equiv -\spacegrad \Phi.

This can now be substituted into the remaining two Maxwell’s equations.

\label{eqn:transverseGauge:80}
\begin{aligned}
\spacegrad \cdot \BD &= \rho_v \\
\spacegrad \cross \BH &= \BJ + \PD{t}{\BD} \\
\end{aligned}

For Gauss’s law, in simple media, we have

\label{eqn:transverseGauge:140}
\begin{aligned}
\rho_v
&=
&=
\end{aligned}

For simple media again, the Ampere-Maxwell equation is

\label{eqn:transverseGauge:100}
\inv{\mu} \spacegrad \cross \lr{ \spacegrad \cross \BA } = \BJ + \epsilon \PD{t}{} \lr{ -\spacegrad \Phi – \PD{t}{\BA} }.

Expanding $$\spacegrad \cross \lr{ \spacegrad \cross \BA } = -\spacegrad^2 \BA + \spacegrad \lr{ \spacegrad \cdot \BA }$$ gives
\label{eqn:transverseGauge:120}

Maxwell’s equations are now reduced to
\label{eqn:transverseGauge:180}
\boxed{
\begin{aligned}
\spacegrad^2 \BA – \spacegrad \lr{ \spacegrad \cdot \BA + \epsilon \mu \PD{t}{\Phi}} – \epsilon \mu \PDSq{t}{\BA} &= -\mu \BJ \\
\end{aligned}
}

There are two obvious constraints that we can impose
\label{eqn:transverseGauge:200}
\spacegrad \cdot \BA – \epsilon \mu \PD{t}{\Phi} = 0,

or
\label{eqn:transverseGauge:220}

The first constraint is the Lorentz gauge, which I’ve played with previously. It happens to be really nice in a relativistic context since, in vacuum with a four-vector potential $$A = (\Phi/c, \BA)$$, that is a requirement that the four-divergence of the four-potential vanishes ($$\partial_\mu A^\mu = 0$$).

### Transverse gauge

Jackson identifies the latter constraint as the transverse gauge, which I’m less familiar with. With this gauge selection, we have

\label{eqn:transverseGauge:260}
\spacegrad^2 \BA – \epsilon \mu \PDSq{t}{\BA} = -\mu \BJ + \epsilon\mu \spacegrad \PD{t}{\Phi}

\label{eqn:transverseGauge:280}

What’s not obvious is the fact that the irrotational (zero curl) contribution due to $$\Phi$$ in \ref{eqn:transverseGauge:260} cancels the corresponding irrotational term from the current. Jackson uses a transverse and longitudinal decomposition of the current, related to the Helmholtz theorem to allude to this.

That decomposition follows from expanding $$\spacegrad^2 J/R$$ in two ways using the delta function $$-4 \pi \delta(\Bx – \Bx’) = \spacegrad^2 1/R$$ representation, as well as directly

\label{eqn:transverseGauge:300}
\begin{aligned}
– 4 \pi \BJ(\Bx)
&=
\int \spacegrad^2 \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’ \\
&=
\int \spacegrad \cdot \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’
+
\int \spacegrad \wedge \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’ \\
&=
\int \BJ(\Bx’) \cdot \spacegrad’ \inv{\Abs{\Bx – \Bx’}} d^3 x’
+
\int \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’
} \\
&=
\int \spacegrad’ \cdot \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’
\int \frac{\spacegrad’ \cdot \BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’

\int \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’
}
\end{aligned}

The first term can be converted to a surface integral

\label{eqn:transverseGauge:320}
\int \spacegrad’ \cdot \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’
=
\int d\BA’ \cdot \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}},

so provided the currents are either localized or $$\Abs{\BJ}/R \rightarrow 0$$ on an infinite sphere, we can make the identification

\label{eqn:transverseGauge:340}
\BJ(\Bx)
=
+
\spacegrad \cross \spacegrad \cross \inv{4 \pi} \int \frac{\BJ(\Bx’)}{\Abs{\Bx – \Bx’}} d^3 x’
\equiv
\BJ_l +
\BJ_t,

where $$\spacegrad \cross \BJ_l = 0$$ (irrotational, or longitudinal), whereas $$\spacegrad \cdot \BJ_t = 0$$ (solenoidal or transverse). The irrotational property is clear from inspection, and the transverse property can be verified readily

\label{eqn:transverseGauge:360}
\begin{aligned}
&=
&=
&=
&= 0.
\end{aligned}

Since

\label{eqn:transverseGauge:380}
\Phi(\Bx, t)
=
\inv{4 \pi \epsilon} \int \frac{\rho_v(\Bx’, t)}{\Abs{\Bx – \Bx’}} d^3 x’,

we have

\label{eqn:transverseGauge:400}
\begin{aligned}
&=
\inv{4 \pi \epsilon} \spacegrad \int \frac{\partial_t \rho_v(\Bx’, t)}{\Abs{\Bx – \Bx’}} d^3 x’ \\
&=
\inv{4 \pi \epsilon} \spacegrad \int \frac{-\spacegrad’ \cdot \BJ}{\Abs{\Bx – \Bx’}} d^3 x’ \\
&=
\frac{\BJ_l}{\epsilon}.
\end{aligned}

This means that the Ampere-Maxwell equation takes the form

\label{eqn:transverseGauge:420}
\spacegrad^2 \BA – \epsilon \mu \PDSq{t}{\BA}
= -\mu \BJ + \mu \BJ_l
= -\mu \BJ_t.

This justifies the transverse in the label transverse gauge.

# References

[1] JD Jackson. Classical Electrodynamics. John Wiley and Sons, 2nd edition, 1975.

## Continuity equation and Ampere’s law

### Q:

Show that without the displacement current $$\PDi{t}{\BD}$$, Maxwell’s equations will not satisfy conservation relations.

### A:

Without the displacement current, Maxwell’s equations are
\label{eqn:continuityDisplacement:20}
\begin{aligned}
\spacegrad \cross \BE( \Br, t ) &= – \PD{t}{\BB}(\Br, t) \\
\spacegrad \cross \BH( \Br, t ) &= \BJ \\
\spacegrad \cdot \BD(\Br, t) &= \rho_{\mathrm{v}}(\Br, t) \\
\spacegrad \cdot \BB(\Br, t) &= 0.
\end{aligned}

Assuming that the continuity equation must hold, we have
\label{eqn:continuityDisplacement:40}
\begin{aligned}
0
&= \spacegrad \cdot \BJ + \PD{t}{\rho_\mathrm{v}} \\
&= \PD{t}{} (\spacegrad \cdot \BD) \\
&\ne 0.
\end{aligned}

This shows that the current in Ampere’s law must be transformed to

\label{eqn:continuityDisplacement:60}
\BJ \rightarrow \BJ + \PD{t}{\BD},

should we wish the continuity equation to be satisfied. With such an addition we have

\label{eqn:continuityDisplacement:80}
\begin{aligned}
0
&= \spacegrad \cdot \BJ + \PD{t}{\rho_\mathrm{v}} \\
\end{aligned}

The first term is zero (assuming sufficient continity of $$\BH$$) and the second two terms cancel when the space and time derivatives of one are commuted.

## Calculating the magnetostatic field from the moment

The vector potential, to first order, for a magnetostatic localized current distribution was found to be

\label{eqn:magneticFieldFromMoment:20}
\BA(\Bx) = \frac{\mu_0}{4 \pi} \frac{\Bm \cross \Bx}{\Abs{\Bx}^3}.

Initially, I tried to calculate the magnetic field from this, but ran into trouble. Here’s a new try.

\label{eqn:magneticFieldFromMoment:40}
\begin{aligned}
\BB
&=
\frac{\mu_0}{4 \pi}
\spacegrad \cross \lr{ \Bm \cross \frac{\Bx}{r^3} } \\
&=
-\frac{\mu_0}{4 \pi}
\spacegrad \cdot \lr{ \Bm \wedge \frac{\Bx}{r^3} } \\
&=
-\frac{\mu_0}{4 \pi}
\lr{
} \\
&=
\frac{\mu_0}{4 \pi}
\lr{
– \lr{ \Bm \cdot \lr{\spacegrad \inv{r^3} }} \Bx
+\Bm \lr{\spacegrad \inv{r^3} } \cdot \Bx
}.
\end{aligned}

Here I’ve used $$\Ba \cross \lr{ \Bb \cross \Bc } = -\Ba \cdot \lr{ \Bb \wedge \Bc }$$, and then expanded that with $$\Ba \cdot \lr{ \Bb \wedge \Bc } = (\Ba \cdot \Bb) \Bc – (\Ba \cdot \Bc) \Bb$$. Since one of these vectors is the gradient, care must be taken to have it operate on the appropriate terms in such an expansion.

Since we have $$\spacegrad \cdot \Bx = 3$$, $$(\Bm \cdot \spacegrad) \Bx = \Bm$$, and $$\spacegrad 1/r^n = -n \Bx/r^{n+2}$$, this reduces to

\label{eqn:magneticFieldFromMoment:60}
\begin{aligned}
\BB
&=
\frac{\mu_0}{4 \pi}
\lr{
– \frac{\Bm}{r^3}
+ 3 \frac{(\Bm \cdot \Bx) \Bx}{r^5} %
+ 3 \Bm \inv{r^3}
-3 \Bm \frac{\Bx}{r^5} \cdot \Bx
} \\
&=
\frac{\mu_0}{4 \pi}
\frac{3 (\Bm \cdot \ncap) \ncap -\Bm}{r^3},
\end{aligned}

which is the desired result.