amr
  • Member for 9 years, 9 months
  • Last seen more than 3 years ago
Creating a simulation of our Solar System
Accepted answer
40 votes

Update June 2015 Here is an updated version of the program. I've made it compatible with newer Mathematica versions (AstronomicalData returns Quantity structures in newer versions, which wrangled ...

View answer
Debugging Mathematica Code
31 votes

I found a curious style of debugging that I call "Epicsauce Debugging Level 2." First you type this: x = 0; While[True, Pause[1]; (*Dynamic[x]*) x++ ]; Then you highlight the Dynamic[x] and ...

View answer
How to generate a random snowflake
21 votes

Well I guess one more couldn't hurt. Using an iterated matrix-replacement scheme and some fancy opacity: powzerz = 2; width = 550; primitive = Scale[Cuboid[], 0.99999]; matrix0 = {{{1}}}; matrixT = ...

View answer
Equivalent of MATLAB's "hold on" function
Accepted answer
16 votes

My take: toward[p1_, p2_, v_: .05] := p1 + v Normalize[p2 - p1]; {n, r} = {4, 3}; DynamicModule[{pts, history}, pts = r {Cos[#], Sin[#]} & /@ Range[2 Pi/n, 2 Pi, 2 Pi/n]; history = {pts}; ...

View answer
Building a continued fraction
16 votes

Well, the smartass answer would be ContinuedFraction[E - 2, n] So your approximation function (if I understood correctly) would be FromContinuedFraction[ContinuedFraction[E - 2, n]] But the way I ...

View answer
When should I, and when should I not, set the HoldAll attribute on a function I define?
16 votes

The Hold functions enable Mathematica's version of what some other languages call "macros." You can use them for a lot of things, but the essential point is that they preserve the structure of the ...

View answer
Functions that remember their values
12 votes

In some languages Print behaves like the identity function. But as noted, Print in Mathematica returns Null. I want to make clear what is going on with the memoization idiom, for those who might be ...

View answer
Applying an ArrayPlot as a texture to the surface of a cylinder
12 votes

I made a program of this kind before and the most efficient solution I found was Cuboid. Or perhaps it was the best-looking solution. The rendering code is: render[stack_, iterations_, color_, ...

View answer
How to make each node of a graph a button?
Accepted answer
12 votes

It looks like you can use VertexShapeFunction to do it (also take a look at the other options for Graph). Modifying one of the examples from the documentation: Graph[{1 -> 2, 2 -> "bob", "bob" -...

View answer
How can I shorten this code to rotate a line segment around its center?
Accepted answer
11 votes

Something along the lines of Rotate[Line[pts], angle, Mean[pts]]: g = Graphics[Line[{{1, 1}, {2, 2}}]]; rot = l : Line[pts_] :> Rotate[l, Pi/2, Mean[pts]]; Show[g, g /. rot] I believe that ...

View answer
Understanding Pie Chart Annulus generation and alternate style using Disk[]
11 votes

Have you looked at SectorChart? Here's one approach using RegionPlot. Note that because of the range of values ArcTan returns, you should specify angles between -Pi and Pi: SegmentPlot[segmentList_, ...

View answer
Visualizing directories
Accepted answer
10 votes

Here's a relatively straightforward "first version": ellipsizeMax = 8; ellipsize[str_] := If[StringLength[str] > ellipsizeMax, StringTake[str, ellipsizeMax] <> "\[Ellipsis]", str]; readDir[...

View answer
another way to do this mapping of a two argument function
9 votes

For educational purposes, here's a couple other ways to do this: Power @@@ {{1, 2}, {2, 2}, {3, 2}} Power[Sequence @@ #] & /@ {{1, 2}, {2, 2}, {3, 2}} Cases[{{1, 2}, {2, 2}, {3, 2}}, List[x__] :...

View answer
What's the analogue of UML in Mathematica land?
9 votes

The answer to the more general question of how necessary "software architecturizationing" is in Mathematica is, in short: Not that necessary. The reason is basically 1) lists 2) dynamic typing and 3) ...

View answer
Mathematica for teaching orthographic projection
8 votes

Take a look at the output of PolyhedronData[{"Prism", 3}, "Faces"]}, which may help. Note this isn't exactly a direct answer, but I think the optimal solution here is a tool that would let you... ...

View answer
Embedding Mathematica Console into a Java/.NET Application
Accepted answer
8 votes

This is not necessarily an answer, but the Mathematica system is very flexible and I think you might be able to accomplish what you're after with Mathematica itself. Look at this code here: nbWindow =...

View answer
Convert a string to hex number?
8 votes

stringToHex[str_] := ToExpression["16^^" <> str]; This is just a way of automating the normal notation you would use, which is 16^^6b (check here for the documentation).

View answer
Extracting sequences from a list?
7 votes

Here's a plain pattern approach, I'm not quite sure how robust it is: ReplaceList[expr, {___, "Open", x : Except["Close"] ..., "Close", ___} :> {x}] Also take a look at Longest and Shortest, ...

View answer
Help with 1-dimensional random walk
7 votes

Not a direct answer to your question, but I've found RandomChoice is generally the most "intentful" of the random functions. With RandomChoice, your code would be something like: randomWalk[n_, p_: ....

View answer
distance between two curves
7 votes

I can't tell what you're doing either, but here's an idea using Nearest: l1 = Table[{x, 1 + x^2}, {x, -2, 2, .005}]; l2 = Table[{x, 1/2 x^2}, {x, -2, 2, .005}]; d = {#[[1]], EuclideanDistance[#, ...

View answer
Sinusoid modelling
Accepted answer
7 votes

Here's a "compositional" approach. If you take things piece-by-piece it is not too hard to build up more complicalated demonstrations. Animate[Module[ {spazzyP, scrollingPaper, scrollingSine, ...

View answer
Why doesn't Mathematica use uniform criteria for validating Options?
7 votes

I think this particular scenario has to do with how you can create your own Import/Export filters: Developing an Import Converter Regarding 'verification' as in the Plot[Sin[x], {x, -Pi, Pi}, Frame -&...

View answer
How to represent a list as a cycle
7 votes

A solution using the built-in Cycles comparison: numbering = MapIndexed[#1 -> #2[[1]] &, Sort[set[[1]]]]; DeleteDuplicates[set, Cycles[{#1}] == Cycles[{#2}] /. numbering &]

View answer
Indexed replacement
7 votes

Here's an approach that uses a more explicit rule replacement: MapIndexed[{##} /. {coeff_ sym_ | sym_, {index_}} :> index^2 sym &, expr]

View answer
How can I rewrite my game without using Manipulate?
Accepted answer
6 votes

I think you've actually done a good job considering you are only using Manipulate. Here's a simplified snake game using Dynamic: snake = {{0, 0}, {0, 1}, {0, 2}}; dir = {0, -1}; directions = { "...

View answer
Temporarily redirect the output of Print[ ] to a second file
Accepted answer
6 votes

From the documentation for Print: "Print sends its output to the channel $Output." Therefore you can just do this: $Output = OpenWrite["filename " <> DateList["Year", "Month", "Day"] <> "...

View answer
How do you draw a straight line with PolarPlot?
6 votes

My friend, what you ask for is madness. Assuming you're completely aware of just doing this, or similar: Show[ PolarPlot[{4/Cos[theta], 4/Sin[theta]}, {theta, -6, 6}], Graphics[Rotate[Line[500 {{-1,...

View answer
How draw dynamic empty rectangle over a bitmap?
Accepted answer
6 votes

Combine the background and the Graphics with Show, so that you can supply both of them as the second argument to LocatorPane: background = Image[Graphics[{LightGray, Rectangle[]}]]; DynamicModule[{...

View answer
How to plot Error Bars in a 3D scatter plot
6 votes

It doesn't look like there's a built-in way to do it. Here's a custom approach: data = {{1, 4, 2}, {2, 2, 1}, {1, 1, 3}, {3, 1, 3}, {1, 2, 1}}; ErrorBar3D[point_, error_] := Line[{point - {0, 0, ...

View answer
Tabulating Numeric Approximation
Accepted answer
6 votes

I recommend you look at RecurrenceTable as J. M. suggested. However to give you an idea of how you would implement subscripts (by which I think you mean recursion) in a "normal" way, here's a simple ...

View answer