Skip to content

Instantly share code, notes, and snippets.

View simonjtyler's full-sized avatar

Simon Tyler simonjtyler

  • FHS
  • Melbourne, Australia
View GitHub Profile
from typing import List
class Primes:
"Singleton to dynamically generate primes when required and until out of memory"
_instance = None
_primes: List[int]
_max_num_checked: int
def __new__(cls, init_primes=[2]):
@simonjtyler
simonjtyler / main.dart
Created January 10, 2019 15:17
Memoization to calculate fibonacci numbers using a callable class
void main() {
for (var i = 0; i <= 30; i++) {
print('fibonacci($i) = ${fibonacci(i)}');
}
print('\n'
'Function fibonacci is of type ${fibonacci.runtimeType}'
' and has memoized the values ${fibonacci.vals}');
}
@simonjtyler
simonjtyler / gist:e08397a192b025cce8f8ce03d3fb7d36
Created June 11, 2017 13:40
Average timing in Mathematica
(* Old code, made redundant by RepeatedTiming, but put here for safe keeping *)
(* Also in a AUG 15TH, 2011 pastebin https://pastebin.com/Nv7rH2vE *)
(*http://stackoverflow.com/questions/4198961/what-is-in-your-mathematica-tool-bag/4199042#4199042*)
Unprotect[TimeAv];
TimeAv::usage = "\!\(\*
StyleBox[\"TimeAv\", \"TI\"]\)\!\(\*
StyleBox[\"[\", \"TI\"]\)\!\(\*
StyleBox[\"expr_\", \"TI\"]\)\!\(\*
@simonjtyler
simonjtyler / hue2rgb
Created May 4, 2015 00:08
Hue to RGB Python wrapper
from colorsys import hsv_to_rgb
def hue2rgb(hue, saturation=1, value=0.8, rgb_scale = 1):
"""Given a hue, this function returns a RGB value.
hue is a number between 0 and 1 that represents a color on the color wheel in RGB order.
hue2rgb(0) = red, hue2rgb(1/3) = green, hue2rgb(2/3) = blue"""
return tuple(rgb_scale*c for c in hsv_to_rgb(hue, saturation, value))
(*
A simple modification of ListPlot that takes a list of complex numbers instead of a list of {x,y} coordinates
*)
ComplexListPlot[pts_List, opts : OptionsPattern[]] := ListPlot[Map[{Re@#, Im@#} &, pts, {-1}], opts,
AxesOrigin -> {0, 0}, PlotStyle -> PointSize[Large]]
ComplexListPlot[pt_?NumericQ, opts : OptionsPattern[]] := ComplexListPlot[{pt}, opts]
Save[FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}], ComplexListPlot]
(*
Complete the square for an arbitrary number of variables using a matrix representation.
Output a form that cleanly separates out the different terms.
Other alternatives: http://mathematica.stackexchange.com/q/20051/34
*)
CompleteTheSquare::notquad = "The expression is not quadratic in the variables `1`";
CompleteTheSquare[expr_] := CompleteTheSquare[expr, Variables[expr]]
CompleteTheSquare[expr_, vars_Symbol] := CompleteTheSquare[expr, {vars}]
CompleteTheSquare[expr_, vars : {__Symbol}] := Module[
In[1]:= n = 400;
In[2]:= TimeAv[Plus@@ReplaceList[Product[y[j], {j, 1, n}], y[i_] rest_ :> x[i] rest];]
During evaluation of In[2]:= Total wall time is 0.554032,
total cpu time is 0.530403 and total time spent evaluating the expression is 0.53040
During evaluation of In[2]:= The expression was evaluated 6 times, without any blocking of the runs.
This yields a mean timing of 0.088401 with a standard deviation of 0.0074.
A test of using stackedit
=========================
Some maths
----------
We should note that $1+1=2$ and
$$ f(a) = \frac{1}{2\pi i} \oint_\gamma \frac{f(z)}{z-a}\, dz \,.$$
@simonjtyler
simonjtyler / tableGen.m
Created June 16, 2011 15:05
List comprehension for Mathematica
(* http://stackoverflow.com/questions/6367932/generate-a-list-in-mathematica-with-a-conditional-tested-for-each-element/6368770#6368770 *)
(* The code *)
TableIf::usage = "TableIf[expr,{i,\!\(\*SubscriptBox[\(i\), \(max\)]\)},addif] will \
generate a list of values expr when i runs from 1 to \
\!\(\*SubscriptBox[\(i\), \(max\)]\), only including elements if \
addif[expr] returns true. Note that addif can have dependence on the \
iterator variables.
The default of addif is True&.
@simonjtyler
simonjtyler / Timeit
Created November 13, 2010 04:27
Timeit: A Mathematica function that adds the Timing to the CellLabel of generated Output cells (see http://stackoverflow.com/q/3938827/421225)
SetAttributes[Timeit, HoldAll]
Timeit[x_] := With[{t = Timing[x]}, Module[{out, form},
If[TrueQ[MemberQ[$OutputForms, Head[t[[2]]]]],
out = First[t[[2]]]; form = "//" <> ToString[Head[t[[2]]]],
out = t[[2]]; form = ""];
If[out === Null, Null,
CellPrint[ExpressionCell[t[[2]], "Output",
CellLabel -> StringJoin["(", ToString[t[[1]]], ")",
"Out[", ToString[$Line], "]", form, "="]]];
Unprotect[Out]; Out[$Line] = out; Protect[Out]; out;]];]