This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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\"]\)\!\(\* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
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[ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \,.$$ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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&. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;]];] |
NewerOlder