(XML question) structure for symbols in Astro::Constants

5 views
Skip to first unread message

Boyd Duffee

unread,
May 22, 2018, 6:02:03โ€ฏAM5/22/18
to The Quantified Onion
Hi All,

I took a look at JJ's perl6 module Math::Constants and saw that he'd
included unicode symbols for the constants.
https://github.com/JJ/p6-math-constants
While this may be a bad idea for code maintenance, I think it's cool and
I have a desire to enable the bad boys and girls of science to write
such madness as

$quantum_silliness = -1 * โ„**2 * laplacian($phi) / (2 * MASS_ELECTRON);
                          ๐Ÿกน
                 how cool would this be?  (โ„ is h/2 pi)

An XML file holds all the constants in Astro::Constants on CPAN, which
lends itself to producing modules in Perl5, Perl6, Ruby gems (you get
the idea).  Some constants like Planck's constant are simple ascii, no
fancy stuff.  Right now, I'm stuck with the more involved unicode
symbols, such as โ„ (Planck's Reduced constant) and Mโ˜‰ (solar mass),
which may require alternate representations (LaTeX, HTML, Ascii).

My 2 thoughts for how to add symbols to the XML definition are below:
1) a list of "symbol" entities identified by attributes
2) a complex type with a new entity for each representation

1)
<PhysicalConstant>
    <name type="long">PLANCK</name>
    <symbol>h</name>
</PhysicalConstant>
<PhysicalConstant>
    <name type="long">H_BAR</name>
    <symbol type="unicode">โ„</name>
    <symbol type="description">Latin Small Letter H Bar</name>
    <symbol type="latex">\hbar</name>
    <symbol type="html">&amp;#8463;</name>
    <symbol type="ascii">h_bar</name>
</PhysicalConstant>

which you could unpack with this bit of XML::LibXML code

for my $s ( $constant->getChildrenByTagName('symbol') ) {
    unless ($s->getAttribute('type')) {
        say "\t", $s->textContent();
        next;
    }
    if ($s->getAttribute('type') eq $format) {
        say "\t", $s->textContent();
    }
}


2)
<PhysicalConstant>
    <name type="long">PLANCK</name>
    <symbol>h</symbol>
</PhysicalConstant>
<PhysicalConstant>
    <name type="long">H_BAR</name>
    <symbol>
        <symbolRepresentation>
            <unicode>โ„</unicode>
	    <codepoint>U+210F</codepoint>
            <description>Latin Small Letter H Bar</description>
            <latex>\hbar</latex>
            <html>&amp;#8463;</html>
            <ascii>h_bar</ascii>
        </symbolRepresentation>
    </symbol>
</PhysicalConstant>

which could use this fragment

for my $s ( $constant->getChildrenByTagName('symbol') ) {
    unless ($s->getChildrenByTagName('symbolRepresentation')) {
        say "\t", $s->textContent();
        next;
    }
    my $rep = $s->getElementsByTagName($format)->shift();
    say "\t", $rep->textContent() if $rep;
}


Is there a better representation or extraction method that I'm just not
seeing?  Any comments welcome.

cheers,
Boyd
Reply all
Reply to author
Forward
0 new messages