Skip to content

Instantly share code, notes, and snippets.

@matt-graham
Created April 15, 2021 15:26
Show Gist options
  • Save matt-graham/220a0350e02cb8c92d48cc35d6d33036 to your computer and use it in GitHub Desktop.
Save matt-graham/220a0350e02cb8c92d48cc35d6d33036 to your computer and use it in GitHub Desktop.
Print formatted Markdown list of all NAG subroutines in a source tree
"""Print formatted Markdown list of all NAG subroutines in a source tree"""
import re
import argparse
import pathlib
from itertools import groupby
parser = argparse.ArgumentParser(
description="Generate Markdown list of NAG subroutines used in source tree."
)
parser.add_argument(
"--root-directory", type=pathlib.Path, help="Root directory of source tree"
)
parser.add_argument(
"--filename-pattern",
default="*.f90",
help="Pattern for source filenames to search in",
)
parser.add_argument(
"--nag-subroutine-pattern",
default="([A-Z][0-9]{2}[A-Z]{3})",
help="regex pattern for matching NAG subroutines in source files",
)
parser.add_argument(
"--root-nagdoc-url",
default="https://www.nag.com/numeric/fl/nagdoc_latest/html",
help="Root URL of NAG documentation to generate subroutine doc links from",
)
args = parser.parse_args()
nag_subroutines = set()
for filepath in args.root_directory.rglob(args.filename_pattern):
with open(filepath.resolve(), "r") as f:
file_text = f.read()
matches = re.findall(args.nag_subroutine_pattern, file_text)
nag_subroutines = nag_subroutines | set(matches)
nag_subroutines = sorted(list(nag_subroutines))
nag_doc_urls = {}
for shortname in nag_subroutines:
nag_doc_urls[
shortname
] = f"{args.root_nagdoc_url}/{shortname[:3].lower()}/{shortname.lower()}.html"
nag_chapter_key_text = """A00 – Library Identification
A02 – Complex Arithmetic
C02 – Zeros of Polynomials
C05 – Roots of One or More Transcendental Equations
C06 – Summation of Series
C09 – Wavelet Transforms
D01 – Quadrature
D02 – Ordinary Differential Equations
D03 – Partial Differential Equations
D04 – Numerical Differentiation
D05 – Integral Equations
D06 – Mesh Generation
E01 – Interpolation
E02 – Curve and Surface Fitting
E04 – Minimizing or Maximizing a Function
E05 – Global Optimization of a Function
F – Linear Algebra
F01 – Matrix Operations, Including Inversion
F02 – Eigenvalues and Eigenvectors
F03 – Determinants
F04 – Simultaneous Linear Equations
F05 – Orthogonalization
F06 – Linear Algebra Support Routines
F07 – Linear Equations (LAPACK)
F08 – Least Squares and Eigenvalue Problems (LAPACK)
F11 – Large Scale Linear Systems
F12 – Large Scale Eigenproblems
F16 – Further Linear Algebra Support Routines
G01 – Simple Calculations on Statistical Data
G02 – Correlation and Regression Analysis
G03 – Multivariate Methods
G04 – Analysis of Variance
G05 – Random Number Generators
G07 – Univariate Estimation
G08 – Nonparametric Statistics
G10 – Smoothing in Statistics
G11 – Contingency Table Analysis
G12 – Survival Analysis
G13 – Time Series Analysis
G22 – Linear Model Specification
H – Operations Research
M01 – Sorting and Searching
S – Approximations of Special Functions
X01 – Mathematical Constants
X02 – Machine Constants
X03 – Inner Products
X04 – Input/Output Utilities
X05 – Date and Time Utilities
X06 – OpenMP Utilities
X07 – IEEE Arithmetic
X10 – Automatic Differentiation Utilities
"""
nag_chapter_key = {
line.split(" – ")[0]: line.split(" – ")[1]
for line in nag_chapter_key_text.split("\n")
if " – " in line
}
for key, subroutine_group in groupby(nag_subroutines, key=lambda s: s[:3]):
chapter_title = nag_chapter_key[key] if key in nag_chapter_key else nag_chapter_key[key[0]]
subroutine_list = ", ".join([f"[{shortname}]({nag_doc_urls[shortname]})" for shortname in subroutine_group])
print(f" * [ ] {key.upper()} {chapter_title} ({subroutine_list})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment