• Stars
    star
    197
  • Rank 197,722 (Top 4 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created almost 5 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Document mathematical Go code beautifully

mathfmt

Document mathematical Go code beautifully.

  • Write mathematical formulae in a LaTeX-ish syntax
  • Super/subscripts formatted with Unicode characters: 2^32 becomes 2³² and x_{i+1} becomes xᵢ₊₁
  • Comprehensive symbol library: \zeta(s) = \sum 1/n^{s} becomes ζ(s) = ∑ 1/nˢ

Inspired by Filippo Valsorda's literate Go implementation of Poly1305, which can be reproduced using mathfmt.

Usage

Install mathfmt with:

go get -u github.com/mmcloughlin/mathfmt

Apply to files just like you would with gofmt.

mathfmt -w file.go

Example

Here's our variance function in Go, documented with LaTeX-ish equations in comments.

// Variance computes the population variance of the population x_{i} of size N.
// Specifically, it computes \sigma^2 where
//
//		\sigma^2 = \sum (x_{i} - \mu)^2 / N
//
// See also: https://en.wikipedia.org/wiki/Variance.
func Variance(X []float64) float64 {
	// Compute the average \mu.
	mu := Mean(X)

	// Compute the sum \sum (x_{i} - \mu)^2.
	ss := 0.0
	for _, x := range X {
		ss += (x - mu) * (x - mu) // (x_{i} - \mu)^2
	}

	// Final divide by N to produce \sigma^2.
	return ss / float64(len(X))
}

Run it through mathfmt and voila!

// Variance computes the population variance of the population xᵢ of size N.
// Specifically, it computes σ² where
//
//		σ² = ∑ (xᵢ - μ)² / N
//
// See also: https://en.wikipedia.org/wiki/Variance.
func Variance(X []float64) float64 {
	// Compute the average μ.
	mu := Mean(X)

	// Compute the sum ∑ (xᵢ - μ)².
	ss := 0.0
	for _, x := range X {
		ss += (x - mu) * (x - mu) // (xᵢ - μ)²
	}

	// Final divide by N to produce σ².
	return ss / float64(len(X))
}

Syntax

First a warning: mathfmt does not have a rigorous grammar, it's a combination of string replacement and regular expressions that appears to work most of time. However you may run into some thorny edge cases.

Source

mathfmt only works on Go source code. Every comment in the file is processed, both single- and multi-line.

Symbols

mathfmt recognizes a huge symbol table that is almost entirely borrowed from LaTeX packages. Every symbol macro in comment text will be replaced with its corresponding Unicode character. In addition to LaTeX symbol macros, mathfmt supports a limited set of "aliases" for character combinations commonly used to represent mathematical symbols.

Super/subscripts

Like LaTeX, superscripts use the ^ character and subscripts use _. If the super/subscript consists entirely of digits, then no braces are required: for example 2^128 or x_13. Otherwise braces must be used to surround the super/subscript, for example 2^{i} or x_{i+j}.

Note that Unicode support for super/subscripts is limited, and in particular does not support the full alphabet. Therefore, if there is not a corresponding super/subscript character available for any character in braces {...}, mathfmt will not perform any substition at all. For example there is no superscript q, so mathfmt will not be able to process 2^{q}, and likewise with x_{K}.

Credits

Thank you to Günter Milde for the exhaustive unimathsymbols database of Unicode symbols and corresponding LaTeX math mode commands.

License

mathfmt is available under the BSD 3-Clause License.

More Repositories

1

avo

Generate x86 Assembly with Go
Go
2,704
star
2

globe

Globe wireframe visualizations in Golang
Go
1,591
star
3

geohash

Golang geohash library
Go
525
star
4

addchain

Cryptographic Addition Chain Generation in Go
Go
184
star
5

meow

Meow hash for Golang
Go
124
star
6

profile

Simple profiling for Go
Go
76
star
7

pearl

Tor relay implementation in Golang
Go
75
star
8

cryptofuzz

Fuzzing Go crypto
Go
73
star
9

finsky

Google Play API for Python
Python
57
star
10

ec3

Elliptic Curve Cryptography Compiler: an incomplete experiment in code-generation for elliptic curves in Go
Go
55
star
11

luhn

Generate and verify Luhn check digits
Python
43
star
12

reprotobuf

Reverse engineer protobuf from javanano
Python
29
star
13

professor

Safer interface to Golang net/http/pprof
Go
23
star
14

spherand

Random points on a sphere in Golang
Go
22
star
15

trunnel

Code generator for binary parsing
Go
19
star
16

hellollvm

Basic LLVM passes
C++
17
star
17

md4

Assembly-optimized MD4 hash algorithm in Go
Go
17
star
18

problems

Programming problems
Go
15
star
19

starbucks

All starbucks locations in the world
Python
15
star
20

torcerts

Tor relay certificate downloader
Python
11
star
21

gophercon

Notes from Gophercon
11
star
22

deconstructedgeohash

Supporting code for Geohash Assembly blog post
C++
10
star
23

geohashbench

Benchmarks to compare golang geohash implementations
Go
10
star
24

cpudb

CPUID database derived from InstLatx64
Go
10
star
25

supercomputing

Notes from Supercomputing Conference
10
star
26

garble

Randomize your data
Python
9
star
27

databundler

Embed CSV data in a Golang package
Go
9
star
28

openflights

OpenFlights data in Golang format
Go
9
star
29

iacago

Intel IACA Markers for Golang Assembly
Makefile
9
star
30

goperf

Continuous Benchmarking for the Go compiler
Go
8
star
31

ghr

GitHub recruitment scraper
Go
8
star
32

podium

Convert Golang present talks to PDF
Go
8
star
33

reveal

Reveal source code line-by-line in LaTeX presentations
Python
8
star
34

keepaneyeon

Monitor URLs for changes
Python
8
star
35

aesnix

Experiments with AES-NI performance in Golang
Assembly
7
star
36

ssarules

SSA rules description language in the Go compiler
Go
6
star
37

cite

Cite snippets in your godoc
Go
5
star
38

cuptisamples

NVIDIA CUPTI samples mirror.
Shell
4
star
39

bib

BibTeX references for your Go source code
Go
3
star
40

talks

Talks I've given
Python
3
star
41

cpuid

Interface to intel's cpuid instruction.
C
3
star
42

x

Experimental.
Go
2
star
43

adorn

Generate function and decorator types for Golang interfaces
Go
2
star
44

rqc

Redis query compiler
Go
2
star
45

godepex

Exclude packages from godep
Go
2
star
46

random

Utilities to complement Golang's math/rand package
Go
2
star
47

toroulette

Run a random version of tor
Makefile
2
star
48

bugsalsa

Investigating a 32-bit overflow bug in SUPERCOP-derived salsa20 implementations
Go
2
star
49

usgsmaps

Fetch topo maps from USGS
Python
2
star
50

doclint

Experimental C++ Documentation Linter based on Clang LibTooling
C++
1
star