C++ compiler diagnostic gone horribly wrong: error: explicit specialization in non-namespace scope

September 23, 2022 C/C++ development and debugging. , , , , , , , ,

Here is a g++ error message that took me an embarrassingly long time to figure out:

In file included from /home/llvm-project/llvm/lib/IR/Constants.cpp:15:
/home/llvm-project/llvm/lib/IR/LLVMContextImpl.h:447:11: error: explicit specialization in non-namespace scope ‘struct llvm::MDNodeKeyImpl<llvm::DIBasicType>’
 template <> struct MDNodeKeyImpl<DIStringType> {
           ^

This is the code:

template <> struct MDNodeKeyImpl<DIStringType> {
  unsigned Tag;
  MDString *Name;
  Metadata *StringLength;
  Metadata *StringLengthExp;
  Metadata *StringLocationExp;
  uint64_t SizeInBits;
  uint32_t AlignInBits;
  unsigned Encoding;

This specialization isn’t materially different than the one that preceded it:

template <> struct MDNodeKeyImpl<DIBasicType> {
  unsigned Tag;
  MDString *Name;
  MDString *PictureString;
  uint64_t SizeInBits;
  uint32_t AlignInBits;
  unsigned Encoding;
  unsigned Flags;
  Optional<DIBasicType::DecimalInfo> DecimalAttrInfo;

  MDNodeKeyImpl(unsigned Tag, MDString *Name, MDString *PictureString,
               uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
                unsigned Flags,
                Optional<DIBasicType::DecimalInfo> DecimalAttrInfo)
      : Tag(Tag), Name(Name), PictureString(PictureString),
        SizeInBits(SizeInBits), AlignInBits(AlignInBits), Encoding(Encoding),
        Flags(Flags), DecimalAttrInfo(DecimalAttrInfo) {}
  MDNodeKeyImpl(const DIBasicType *N)
      : Tag(N->getTag()), Name(N->getRawName()), PictureString(N->getRawPictureString()), SizeInBits(N->getSizeInBits()),
        AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()),
        Flags(N->getFlags(), DecimalAttrInfo(N->getDecimalInfo()) {}

  bool isKeyOf(const DIBasicType *RHS) const {
    return Tag == RHS->getTag() && Name == RHS->getRawName() &&
           PictureString == RHS->getRawPictureString() &&
           SizeInBits == RHS->getSizeInBits() &&
           AlignInBits == RHS->getAlignInBits() &&
           Encoding == RHS->getEncoding() && Flags == RHS->getFlags() &&
           DecimalAttrInfo == RHS->getDecimalInfo();
  }

  unsigned getHashValue() const {
    return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
  }
};

However, there is an error hiding above it on this line:

        Flags(N->getFlags(), DecimalAttrInfo(N->getDecimalInfo()) {}

i.e. a single missing brace in the initializer for the Flags member, a consequence of a cut and paste during rebase that clobbered that one character, when adding a comma after it.

It turns out that the compiler was giving me a hint that something was wrong before this in the message:

error: explicit specialization in non-namespace scope

as it states that the scope is:

‘struct llvm::MDNodeKeyImpl

which is the previous class definition. Inspection of the code made me think that the scope was ‘namespace llvm {…}’, and I’d gone looking for a rebase error that would have incorrectly terminated that llvm namespace scope. This is a classic example of not paying enough attention to what is in front of you, and going off looking based on hunches instead. I didn’t understand the compiler message, but in retrospect, non-namespace scope meant that something in that scope was incomplete. The compiler wasn’t smart enough to tell me that the previous specialization was completed due to the missing brace, but it did tell me that something was wrong in that previous specialization (which was explicitly named), and I didn’t look at that because of my “what the hell does that mean” reaction to the compilation error message.

In this case, I was building on RHEL8.3, which uses an ancient GCC toolchain. I wonder if newer versions of g++ fare better (i.e.: a message like “possibly unterminated brace on line …” would have been much nicer)? I wasn’t able to try with clang++ as I was building llvm+clang+lldb (V14), and had uninstalled all of the llvm related toolchain to avoid interference.

More “interesting” electrical.

September 17, 2022 Home renos

We naively thought we could swap a couple of sconce lights, but this is what we found

Neither of the existing lights was mounted in a box, instead there was a “clever” surface mounting method used, with the wires and marrettes tucked into little cavities.  I see why this was done, especially on the exterior wall, since there is not enough space for a standard size octagon box between the brick and the drywall.  We have about a one inch gap, then lathe, then the drywall.

I’ll see if I can find and install a shallow octagon box instead.  It will be tricky to do so, because there are no studs to connect to, and not enough space to retrofit any into the wall.  As Sofia said, “nothing is ever easy, is it.”

Angular momentum bivector in cylindrical and spherical bases.

September 15, 2022 math and physics play , , , , , , ,

[Click here for a PDF version of this post]

Motivation

In a discord thread on the bivector group (a geometric algebra group chat), MoneyKills posts about trouble he has calculating the correct expression for the angular momentum bivector or it’s dual.

This blog post is a more long winded answer than my bivector response and includes this calculation using both cylindrical and spherical coordinates.

Cylindrical coordinates.

The position vector for any point on a plane can be expressed as
\begin{equation}\label{eqn:amomentum:20}
\Br = r \rcap,
\end{equation}
where \( \rcap = \rcap(\phi) \) encodes all the angular dependence of the position vector, and \( r \) is the length along that direction to our point, as illustrated in fig. 1.

fig. 1. Cylindrical coordinates position vector.

The radial unit vector has a compact GA representation
\begin{equation}\label{eqn:amomentum:40}
\rcap = \Be_1 e^{i\phi},
\end{equation}
where \( i = \Be_1 \Be_2 \).

The velocity (or momentum) will have both \( \rcap \) and \( \phicap \) dependence. By chain rule, that velocity is
\begin{equation}\label{eqn:amomentum:60}
\Bv = \dot{r} \rcap + r \dot{\rcap},
\end{equation}
where
\begin{equation}\label{eqn:amomentum:80}
\begin{aligned}
\dot{\rcap}
&= \Be_1 i e^{i\phi} \dot{\phi} \\
&= \Be_2 e^{i\phi} \dot{\phi} \\
&= \phicap \dot{\phi}.
\end{aligned}
\end{equation}
It is left to the reader to show that the vector designated \( \phicap \), is a unit vector and perpendicular to \( \rcap \) (Hint: compute the grade-0 selection of the product of the two to show that they are perpendicular.)

We can now compute the momentum, which is
\begin{equation}\label{eqn:amomentum:100}
\Bp = m \Bv = m \lr{ \dot{r} \rcap + r \dot{\phi} \phicap },
\end{equation}
and the angular momentum bivector
\begin{equation}\label{eqn:amomentum:120}
\begin{aligned}
L
&= \Br \wedge \Bp \\
&= m \lr{ r \rcap } \wedge \lr{ \dot{r} \rcap + r \dot{\phi} \phicap } \\
&= m r^2 \dot{\phi} \rcap \phicap.
\end{aligned}
\end{equation}

This has the \( m r^2 \dot{\phi} \) magnitude that the OP was seeking.

Spherical coordinates.

In spherical coordinates, our position vector is
\begin{equation}\label{eqn:amomentum:140}
\Br = r \lr{ \Be_1 \sin\theta \cos\phi + \Be_2 \sin\theta \sin\phi + \Be_3 \cos\theta },
\end{equation}
as sketched in fig. 2.

fig. 2. Spherical coordinates.

We can factor this into a more compact representation
\begin{equation}\label{eqn:amomentum:160}
\begin{aligned}
\Br
&= r \lr{ \sin\theta \Be_1 (\cos\phi + \Be_{12} \sin\phi ) + \Be_3 \cos\theta } \\
&= r \lr{ \sin\theta \Be_1 e^{\Be_{12} \phi } + \Be_3 \cos\theta } \\
&= r \Be_3 \lr{ \cos\theta + \sin\theta \Be_3 \Be_1 e^{\Be_{12} \phi } }.
\end{aligned}
\end{equation}

It is useful to name two of the bivector terms above, first, we write \( i \) for the azimuthal plane bivector sketched in fig. 3.

Spherical coordinates, azimuthal plane.

\begin{equation}\label{eqn:amomentum:180}
i = \Be_{12},
\end{equation}
and introduce a bivector \( j \) that encodes the \( \Be_3, \rcap \) plane as sketched in fig. 4.

Spherical coordinates, “j-plane”.

\begin{equation}\label{eqn:amomentum:200}
j = \Be_{31} e^{i \phi}.
\end{equation}

Having done so, we now have a compact representation for our position vector
\begin{equation}\label{eqn:amomentum:220}
\begin{aligned}
\Br
&= r \Be_3 \lr{ \cos\theta + j \sin\theta } \\
&= r \Be_3 e^{j \theta}.
\end{aligned}
\end{equation}

This provides us with a nice compact representation of the radial unit vector
\begin{equation}\label{eqn:amomentum:240}
\rcap = \Be_3 e^{j \theta}.
\end{equation}

Just as was the case in cylindrical coordinates, our azimuthal plane unit vector is
\begin{equation}\label{eqn:amomentum:280}
\phicap = \Be_2 e^{i\phi}.
\end{equation}

Now we want to compute the velocity vector. As was the case in cylindrical coordinates, we have
\begin{equation}\label{eqn:amomentum:300}
\Bv = \dot{r} \rcap + r \dot{\rcap},
\end{equation}
but now we need the spherical representation for the \( \rcap \) derivative, which is
\begin{equation}\label{eqn:amomentum:320}
\begin{aligned}
\dot{\rcap}
&=
\PD{\theta}{\rcap} \dot{\theta} + \PD{\phi}{\rcap} \dot{\phi} \\
&=
\Be_3 e^{j\theta} j \dot{\theta} + \Be_3 \sin\theta \PD{\phi}{j} \dot{\phi} \\
&=
\rcap j \dot{\theta} + \Be_3 \sin\theta j i \dot{\phi}.
\end{aligned}
\end{equation}
We can reduce the second multivector term without too much work
\begin{equation}\label{eqn:amomentum:340}
\begin{aligned}
\Be_3 j i
&=
\Be_3 \Be_{31} e^{i\phi} i \\
&=
\Be_3 \Be_{31} i e^{i\phi} \\
&=
\Be_{33112} e^{i\phi} \\
&=
\Be_{2} e^{i\phi} \\
&= \phicap,
\end{aligned}
\end{equation}
so we have
\begin{equation}\label{eqn:amomentum:360}
\dot{\rcap}
=
\rcap j \dot{\theta} + \sin\theta \phicap \dot{\phi}.
\end{equation}

The velocity is
\begin{equation}\label{eqn:amomentum:380}
\Bv = \dot{r} \rcap + r \lr{ \rcap j \dot{\theta} + \sin\theta \phicap \dot{\phi} }.
\end{equation}

Now we can finally compute the angular momentum bivector, which is
\begin{equation}\label{eqn:amomentum:400}
\begin{aligned}
L &=
\Br \wedge \Bp \\
&=
m r \rcap \wedge \lr{ \dot{r} \rcap + r \lr{ \rcap j \dot{\theta} + \sin\theta \phicap \dot{\phi} } } \\
&=
m r^2 \rcap \wedge \lr{ \rcap j \dot{\theta} + \sin\theta \phicap \dot{\phi} } \\
&=
m r^2 \gpgradetwo{ \rcap \lr{ \rcap j \dot{\theta} + \sin\theta \phicap \dot{\phi} } },
\end{aligned}
\end{equation}
which is just
\begin{equation}\label{eqn:amomentum:420}
L =
m r^2 \lr{ j \dot{\theta} + \sin\theta \rcap \phicap \dot{\phi} }.
\end{equation}

I was slightly surprised by this result, as I naively expected the cylindrical coordinate result. We have a \( m r^2 \rcap \phicap \dot{\phi} \) term, as was the case in cylindrical coordinates, but scaled down with a \( \sin\theta \) factor. However, this result does make sense. Consider for example, some fixed circular motion with \( \theta = \mathrm{constant} \), as sketched in fig. 5.

fig. 5. Circular motion for constant theta

The radius of this circle is actually \( r \sin\theta \), so the total angular momentum for that motion is scaled down to \( m r^2 \sin\theta \dot{\phi} \), smaller than the maximum circular angular momentum of \( m r^2 \dot{\phi} \) which occurs in the \( \theta = \pi/2 \) azimuthal plane. Similarly, if we have circular motion in the “j-plane”, sketched in fig. 6.

fig. 6. Circular motion for constant phi.

where \( \phi = \mathrm{constant} \), then our angular momentum is \( L = m r^2 j \dot{\theta} \).

Scary electrical… What could go wrong?

September 4, 2022 Home renos

Unplugged an A/C adapter and found this hiding behind it.

Why not just open the box and connect the wire properly?  This is right under the electrical panel, so the shutoff for the circuit isn’t far away.

Interior angles of a regular n-sided polygon: a strange way to find them.

August 8, 2022 math and physics play , , , , , ,

[Click here for a PDF version of this post]

Fig 1. Regular polygon, interior angles.

For reasons that I can’t explain, I woke up this morning dreaming about the interior angles of regular polygons. i.e. the angles \( \pi – \theta \), as illustrated in fig. 1.

The logical way to calculate that angle would be to slice the polygon up into triangles from the center, since each slice would have an interior angle would be \( 2 \pi / N \), and then the problem is just trigonometric. However, in my dream, I was going around the outside, each time rotating by a constant angle, until reaching the original starting point. This was a vector algebra problem, instead of a trigonometric problem, as illustrated in
fig. 2.

fig. 2. Polygon vertex iteration.

I didn’t have the computational power in my dream to solve the problem, and had to write it down when I woke up, to do so. The problem has the structure of a recurrence relation:
\begin{equation}\label{eqn:regularNgon:20}
\Bp_k = \Bp_{k-1} + a \Be_1 \lr{ e^{i\theta} }^{k-1},
\end{equation}
where
\begin{equation}\label{eqn:regularNgon:40}
\Bp_N = \Bp_0.
\end{equation}

We can write these out explicitly for the first few \( k \) to see the pattern
\begin{equation}\label{eqn:regularNgon:60}
\begin{aligned}
\Bp_2
&= \Bp_{1} + a \Be_1 \lr{ e^{i\theta} }^{2-1} \\
&= \Bp_{0} + a \Be_1 \lr{ e^{i\theta} }^{1-1} + a \Be_1 \lr{ e^{i\theta} }^{2-1} \\
&= \Bp_{0} + a \Be_1 \lr{ 1 + \lr{ e^{i\theta} }^{2-1} },
\end{aligned}
\end{equation}
or
\begin{equation}\label{eqn:regularNgon:80}
\Bp_k = \Bp_{0} + a \Be_1 \lr{ 1 + e^{i\theta} + \lr{ e^{i\theta} }^{2-1} + \cdots + \lr{ e^{i\theta} }^{k-1} },
\end{equation}
so the equation to solve (for \(\theta\)) is
\begin{equation}\label{eqn:regularNgon:100}
\Bp_N = \Bp_0 + a \Be_1 \lr{ 1 + \cdots + \lr{ e^{i\theta} }^{N-1} } = \Bp_0,
\end{equation}
or
\begin{equation}\label{eqn:regularNgon:120}
1 + \cdots + \lr{ e^{i\theta} }^{N-1} = 0.
\end{equation}
The LHS is a geometric series of the form
\begin{equation}\label{eqn:regularNgon:140}
S_N = 1 + \alpha + \cdots \alpha^{N-1}.
\end{equation}
Recall that the trick to solve this is noting that
\begin{equation}\label{eqn:regularNgon:160}
\alpha S_N = \alpha + \cdots \alpha^{N-1} + \alpha^N,
\end{equation}
so
\begin{equation}\label{eqn:regularNgon:180}
\alpha S_N – S_N = \alpha^N – 1,
\end{equation}
or
\begin{equation}\label{eqn:regularNgon:200}
S_N = \frac{\alpha^N – 1}{\alpha – 1}.
\end{equation}
For our polygon, we seek a zero numerator, that is
\begin{equation}\label{eqn:regularNgon:220}
e^{N i \theta} = 1,
\end{equation}
and the smallest \( \theta \) solution to this equation is
\begin{equation}\label{eqn:regularNgon:240}
N \theta = 2 \pi,
\end{equation}
or
\begin{equation}\label{eqn:regularNgon:260}
\theta = \frac{2 \pi}{N}.
\end{equation}
The interior angle is the complement of this, since we are going around the outside edge. That is
\begin{equation}\label{eqn:regularNgon:280}
\begin{aligned}
\pi – \theta &= \pi – \frac{2 \pi}{N} \\ &= \frac{ N – 2 }{N} \pi,
\end{aligned}
\end{equation}
and the sum of all the interior angles is
\begin{equation}\label{eqn:regularNgon:300}
N \lr{ \pi – \theta } = \lr{N – 2 } \pi.
\end{equation}

Plugging in some specific values, for \( N = 3, 4, 5, 6 \), we find that the interior angles are \( \pi/3, \pi/2, 3 \pi/5, 4 \pi/6 \), and the respective sums of these interior angles for the entire polygons are \( \pi, 2 \pi, 3 \pi, 4 \pi \).

Like, I said, this isn’t the simplest way to solve this problem. Instead, we could solve for \( 2 \mu \) with respect to interior triangle illustrated in
fig. 3, where

fig. 3. Polygon interior geometry.

\begin{equation}\label{eqn:regularNgon:320}
2 \mu + \frac{ 2\pi}{N} = \pi,
\end{equation}
or
\begin{equation}\label{eqn:regularNgon:340}
2 \mu = \frac{N – 2}{N} \pi,
\end{equation}
as found the hard way. The hard way was kind of fun though.

The toughest problem to solve would be “why on earth was my brain pondering this in the early morning?” I didn’t even go to bed thinking about anything math or geometry related (we finished the night with the brain-dead activity of watching an episode of “Stranger things”.)