Skip to content

Instantly share code, notes, and snippets.

View simonjtyler's full-sized avatar

Simon Tyler simonjtyler

  • FHS
  • Melbourne, Australia
View GitHub Profile
@simonjtyler
simonjtyler / Position4SparseArray.m
Created August 10, 2010 10:51
This code extends Mathematica's Position function to work on SparseArrays.
(* This code extends Mathematica's Position function to work on SparseArrays. *)
(* Note that Position and Cases have the same optional arguments, so the code is quite simple *)
Unprotect[Position];
Position[A_SparseArray, pat_, others___] := Cases[ArrayRules[A], HoldPattern[_->pat], others][[All,1]]
Protect[Position];
@simonjtyler
simonjtyler / H30+
Created October 5, 2010 05:36
Cell expressions for different ways of writing text superscripts in Mathematica: http://stackoverflow.com/q/3860574/421225
Cell[TextData[{
"the chemical formula for hydronium is H",
Cell[BoxData[
FormBox[
SubscriptBox["\[Null]", "3"], TraditionalForm]],
FormatType->"TraditionalForm"],
"O",
Cell[BoxData[
FormBox[
SuperscriptBox["\[Null]", "+"], TraditionalForm]],
@simonjtyler
simonjtyler / GraphPlotHighlight
Created November 5, 2010 00:05
GraphPlotHighlight: A modification of Mathematica's GraphPlot -- An attempted answer to http://stackoverflow.com/q/4091728/421225
Needs["GraphUtilities`"];
Clear[GraphPlotHighlight]
Protect[HighlightColor, HighlightThickness];
Options[GraphPlotHighlight] = Join[Options[GraphPlot],
{HighlightColor -> LightBlue, HighlightThickness -> .15}];
GraphPlotHighlight[edges:{((_ -> _) | (List|Tooltip)[_ -> _, _])..},
hl:{___}:{}, opts:OptionsPattern[]] :=
@simonjtyler
simonjtyler / IntersectQ
Created November 12, 2010 01:03
IntersectQ: See if a line intersects with a point up to some precision
(* Written for http://stackoverflow.com/q/4126473/421225 *)
(* SlowIntersectQ is too slow to use in EdgeRenderingFunction *)
SlowIntersectQ[{start_,end_},pt_,r_?Positive]/;Length[start]==Length[end]==Length[pt]:=Module[{t},
TrueQ[NMinValue[{Norm[((1.-t)start+t end)-pt],0<t<1},t,WorkingPrecision->$MachinePrecision]<r]]
(* Since we only care about straight lines, here's a faster implementation *)
(* from: http://stackoverflow.com/q/849211/421225 *)
@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;]];]
@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&.
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 \,.$$
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.
(*
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[
(*
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]