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
(* 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]; |
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
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]], |
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
Needs["GraphUtilities`"]; | |
Clear[GraphPlotHighlight] | |
Protect[HighlightColor, HighlightThickness]; | |
Options[GraphPlotHighlight] = Join[Options[GraphPlot], | |
{HighlightColor -> LightBlue, HighlightThickness -> .15}]; | |
GraphPlotHighlight[edges:{((_ -> _) | (List|Tooltip)[_ -> _, _])..}, | |
hl:{___}:{}, opts:OptionsPattern[]] := |
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
(* 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 *) |
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;]];] |
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
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
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
(* | |
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
(* | |
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] |
OlderNewer