Skip to content

Instantly share code, notes, and snippets.

#include <cstdint>
#include <string>
using String = std::string;
#define ASSERT_MSG(cond, msg) \
{ \
if (!(cond)) \
return Err{"Assert (" #cond ") failed at " + IntToStr(__LINE__) + "! Msg '" + Str(msg) + "'."}; \
}
@polkovnikov
polkovnikov / integrate_simpson.py
Created September 18, 2020 05:21
Integrate function numerically using Simpson's Rule
def IntegrateSimpson(x, y, *, dtype = 'float64'):
# Integrates function f represented by (x, y) samples using Simpson's Rule.
# See https://en.wikipedia.org/wiki/Simpson%27s_rule
# Returns array a[i] = "integral on samples (x[0], y[0])..(x[i], y[i])".
# Hence a[-1] (last value) equals to integral on full range.
# First two values are approximated by Trapezoid Rule, as Simpson's Rule needs at least 3 points.
# Needs: python -m pip install numpy
import numpy as np
@polkovnikov
polkovnikov / progress_bar_iterator_wrapper_for_enlighten.py
Last active September 16, 2020 16:18
Progress Bar Iterator-Wrapping Helper Function for enlighten library
# Iterator-Wrapping Helper Function for enlighten library
# Needs: python -m pip install enlighten
def pit(it, *pargs, **nargs):
import enlighten
global __pit_man__
try:
__pit_man__
except NameError:
__pit_man__ = enlighten.get_manager()
@polkovnikov
polkovnikov / shp_to_json.py
Last active September 14, 2020 04:09
Converts ESRI Shapefile (.shp) to GeoJSON (.json, see http://geojson.org/)
# Converts ESRI Shapefile (.shp) to GeoJSON (.json, see http://geojson.org/).
# Usage: python shp_to_json.py SHAPEFILE_NAME.shp
import sys, os, json
# python -m pip install pyshp
import shapefile
ifn = sys.argv[1]
assert ifn.lower().endswith('.shp'), ifn