Skip to content

Instantly share code, notes, and snippets.

@tueda
tueda / LoadVariable.m
Created September 23, 2013 12:40
LoadVariable[var, env, default] tries to load a variable from the environment variables, if the variable is undefined.
(*
* Defines the given symbol if it has not been defined. For the value of the
* symbol, this function tries to load it from the given environment variable.
* The optional third argument is the default value which is used when the
* environment variable is undefined. This function returns the value of the
* symbol if defined, otherwise returns $Failed.
*)
LoadVariable[symbol_, env_String, default_:Null] := (
If[!ValueQ[symbol],
If[Environment[env] =!= $Failed,
@tueda
tueda / ExtGCD.frm
Created September 23, 2013 12:49
An implementation of the extended euclidean algorithm.
**
* The extended euclidean algorithm, which gives x and y such that
* a*x + b*y = gcd(a, b) >= 0.
* Input: Integers $a and $b.
* Output: Integers $x and $y.
*
#procedure ExtGCD(a,b,x,y,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6)
#define aa "`tmp1'"
#define bb "`tmp2'"
#define lastx "`tmp3'"
@tueda
tueda / abs_dirname.sh
Last active December 23, 2015 17:39
abs_dirname is similar to dirname but prints the directory name in the absolute path.
#! /bin/sh
# Prints the absolute path to the directory containing the given path.
# The directory must exist.
abs_dirname() {(
cd "`dirname \"$1\"`" && pwd
)}
# Examples
@tueda
tueda / mktemp_d.sh
Last active December 23, 2015 17:39
mktemp_d makes a temporary directory and prints the path of it.
#! /bin/sh
# Wraps "mktemp -d $1XXXXXXXXXX"
mktemp_d() {(
umask 077
{
# Use mktemp if available.
dir=`mktemp -d "$1XXXXXXXXXX" 2>/dev/null` && [ -d "$dir" ]
} || {
# Fall back on mkdir.
@tueda
tueda / Time.m
Last active December 23, 2015 19:29
Time[expr] evaluates the given expression with printing the timing of the evaluation in the real time.
(*
* Evaluates the given expression with printing the timing in the real time.
*)
Time[expr_] := Module[{t, r},
{t, r} = AbsoluteTiming[expr];
Print["Elapsed time: ", t, " seconds."];
r
];
SetAttributes[Time, HoldFirst];
@tueda
tueda / path-manip.bash
Last active June 16, 2016 18:46
Append/prepend/remove a path to/from an environment variable. Assumed no spaces in the path. #bash
# append_path VAR PATH - adds PATH to the end of PATH
append_path() {
[ $# -lt 2 ] && return
[ ! -d "$2" ] && echo "Warning: path $2 is not found on `hostname`" >&2
remove_path $1 $2
if eval '[ -z "$'$1'" ]'; then
eval $1=$2
else
eval $1='$'$1:$2
fi
@tueda
tueda / expand-den.frm
Created October 1, 2013 15:24
Series expansions of denominators.
CF den;
S x;
S a,j,n,x1,x2;
S a,b,c;
L F1 = den(1-x);
L F2 = den(1-x)^2 / x;
L F3 = den(a+b*x );
L F4 = den( +b*x+c*x^2);
@tueda
tueda / scoped_save.cc
Last active December 25, 2015 18:49
Let a variable to a value temporarily.
/*
* This snippet uses C++11 features. For C++03, see the previous revision.
*
* Replace idioms like
*
* struct Foo {
* int a;
* };
*
* void func(Foo *f) {
@tueda
tueda / Makefile
Last active December 25, 2015 20:09
A template for Makefile for C/C++/Fortran. #make #c #c++ #fortran
CC = gcc
CFLAGS = -g -O2 -Wall -Wextra -std=c90 -pedantic
CXX = g++
CXXFLAGS = -g -O2 -Wall -Wextra -std=c++98 -pedantic
F77 = gfortran
FFLAGS = -g -O2 -Wall -Wextra -std=f95 -pedantic
FC = gfortran
FCFLAGS = -g -O2 -Wall -Wextra -std=f95 -pedantic
CPPFLAGS =
DEFS =
@tueda
tueda / filesize.sh
Created October 21, 2013 09:13
filesize <filename> prints the size of the given file.
filesize() {
if [ -f "$1" ]; then
wc -c "$1" | awk '{print $1}'
else
echo "filesize: file $1 doesn't exist" >&2
return 1
fi
}