Mathematica and Matlab Brush for SyntaxHighlighter
For you Mathematica and Matlab junkies here are brushes for SyntaxHighlighter. These has ALL the functions (over 2200 for Mathematica 6.0 and 1300 for Matlab) which tends to make browsers stall when rendering the source code. To make use of this brush, just use language=’mathematica’ or language=’matlab’.
Download for SyntaxHighlighter 1.51: shBrushMathematicaAll.js, shBrushMatlabAll.js
Download for SyntaxHighlighter 2.0: shBrushMathematica.js, shBrushMatlab.js
Matlab Example
[sourcecode language='matlab']
{% Example Monte Carlo Simulation in Matlab
Function: y = x2^2/x1Generate n samples from a normal distribution
r = ( randn(n,1) * sd ) + mu
mu : mean
sd : standard deviationGenerate n samples from a uniform distribution
r = a + rand(n,1) * (b-a)
a : minimum
b : maximum
%}
n = 100000; % The number of function evaluations
% — Generate vectors of random inputs
% x1 ~ Normal distribution N(mean=100,sd=5)
% x2 ~ Uniform distribution U(a=5,b=15)
x1 = ( randn(n,1) * 5 ) + 100;
x2 = 5 + rand(n,1) * ( 15 – 5 );
% — Run the simulation
% Note the use of element-wise multiplication
y = x2.^2 ./ x1;
% — Create a histogram of the results (50 bins)
hist(y,50);
% — Calculate summary statistics
y_mean = mean(y)
y_std = std(y)
y_median = median(y)
[/sourcecode]
Mathematica Example
[sourcecode language='mathematica']
(* STEP 1 *)
(* The amount of time remaining is timeRem *)
timeRem = 10;
path = {p};
(* STEP 2 *)
(* While there is still time left *)
While[timeRem > 0,
(* Assume for this iteration that m is the midpoint of the mirror the photons path intersects with *)
m = Round[p + 2/3 v];
(* Find the smallest positive root. If s != \[Infinity] then s is equal to the time it takes for the photon to reach the intersection point with the mirror, otherwise the photon does not intersect the mirror *)
s = Min[Cases[t /. Solve[(p + t v - m).(p + t v - m) == 1/9, t], _?Positive]];
(* If the time it takes to reach the next intersection is less than the time remaining then that means there is an intersection *)
If[s < timeRem,
(* p is now set to be the point at which it intersects with the mirror and v is determined by our transformation matrix H *)
p += s v;
v = H[p - m].v,
(* Else there was no intersection, so there are two things that may have happened: (1) The photon stops in mid-flight at timeRem = 10. In this case we let s = timeRem and thus p has reached its final destination, or (2) the photon has sufficient time, but the mirror we chose initially as determined by m was not correct. In this case we let s be 2/3 since we will choose the next mirror in the photon’s path *)
s = Min[timeRem, 2/3];
p += s v
];
(* STEP 3 *)
(* Reduce timeRem by s *)
timeRem -= s;
(* Append p to path *)
path = Append[path, p];
];
Print["The final position of our photon = ", Last[path]];
Print["Distance from p to the origin = ", answer = Norm[p]];
Print["Number of digits Mathematica claims to have correct = ", Precision[answer]]
[/sourcecode]
Nice to find out that there are syntax highlighter brushes available for Matlab and Mathematica too now. Pity that it makes the browser crawl, not sure what we could do about that.
Hope you don’t mind that I included your brushes unaltered in my overview of SyntaxHighlighter brushes. I try to be complete (will never happen) and list all the brushes I can find in one single reference page. If you don’t want your brush listed, please let me know and I’ll remove it
September 23rd, 2009 at 12:44 pmThanks for your matlab brush.
I think there is a small error in this file
http://www.jamesrohal.com/blog/wp-content/uploads/code/shBrushMatlab.js
The multiline comment starts with %{, not {%
FYI,
October 15th, 2009 at 10:15 amThe brush works OK for small programs, but really slows down for longer ones. I went ahead and made a brush that just highlights keywords (not functions) so it is much faster.
It doesn’t look to be following the Syntax.
In the Matlab code everything seems to be green – Commented.
FF 3.5 and Chrome 4.
November 27th, 2009 at 6:59 amVery useful!!!!!!
December 17th, 2009 at 8:38 pm[...] 将Mathematica的Brush文件(shBrushMathematica.js)下载下来(参考链接:http://jamesrohal.com/mathematica-and-matlab-brush-for-syntaxhighlighter/)。然后传到/plugins/syntaxhighlighter/third-party-brushes/下面。编辑/plugins/syntaxhighlighter/syntaxhighlighter.php: [...]
December 30th, 2010 at 1:02 ammost certainly
April 25th, 2011 at 12:47 pmI discovered your blog website on google and verify just a few of your early posts. Continue to keep up the superb operate. I simply extra up your RSS feed to my MSN News Reader. Looking for forward to studying extra from you afterward!…
May 10th, 2011 at 10:47 am