Wednesday, January 22, 2014

Give us your best (Mathematica) one-liner

Here’s another guest post from Allison Taylor at Wolfram, which is part of a series that will help smooth your introduction to Mathematica. Thanks Allison: over to you!

At our 2010 and 2011 Wolfram Technology Conferences, attendees were challenged to come up with their best one-liners—pieces of code written in less than one line, or under 140 characters. The outputs were then judged by their complexity, usefulness, and general cool-factor. The submissions turned out to be quite diverse, thought provoking, and most of all, pretty nifty illustrations of the efficiency of the Wolfram Language.

So it only seemed fitting to tailor some of these small bundles of powerful code to the equally small-but-mighty Raspberry Pi. (To run these, you must first open the front-end version of Mathematica on your Pi by clicking the Mathematica icon.)

Echidnahedron (AKA, the Mathematica Spikey), by Radko Kriz

We couldn’t help but start off with teaching you how to make a cool, geometric logo similar to the one that has been representing Mathematica and Wolfram Research for over 25 years! The echidnahedron is a “stellation” of the icosahedron—basically, it takes the faces of the icosahedron and turns them into infinitely extending spikes. Or Spikey, as we like to call it.

Graphics3D[{Opacity[.8],Glow[RGBColor[1,0,0]],EdgeForm[White],Lighting ->  None,PolyhedronData["Echidnahedron","Faces"]}]

The Ulam Spiral, by Dave Lawrence

Along a similar mathy theme, the Ulam Spiral is a beautiful way to visualize prime numbers. The spiral is contructed on an integer coordinate plane, starting at (0,0) and spiraling outward through every point that is prime.

s = {Re@#, Im@#} & /@   
  Fold[Join[#1, Last@#1 + I^#2 Range@#2/2] &, {0},    
   Range@140]; ListPlot[s, Joined -> True,  
 Epilog -> {Point /@ s[[Prime@Range@PrimePi@Length@s]]}]

Not the biggest bang Stanislaw Ulam ever made, but neat nonetheless

Test If an Integer Is a Prime Number, by Sascha Kratky

And while we’re on the subject of prime numbers, there are tons of math and computation examples where this little gem of code will likely come in handy. Run this function with any integer, and it will return True if it is prime and False if it is not.
RegExPrimeQ[n_Integer] := !   
  StringMatchQ[StringJoin[Table["1", {Abs[n]}]],    
   RegularExpression["^1?$|^(11+?)\\1+$"]] RegExPrimeQ[17]

Make Your Own Borg Cube, by Alex Hirsbrunner

You know you’ve always wanted to make one. By tiling a bunch of randomly generated cubes of random sizes and colors, this code will create your very own one-of-a-kind Borg cube to assimilate all your friends into the hive mind.
p := Random[];
r = RotateLeft;
Graphics3D[  
 Table[{RGBColor[.2, p, .2],   
  Cuboid[l =    
     r[{z, p, p}, a], l +    
     r[p/      
       8 {Sign[z - .5], p, p}, a]]}, \ {z, {0, 1}}, {a, 0, 2}, {6!}]]

Your biological and technological distinctiveness will be added to our own.

Fractals, by Stephan Leibbrandt

Make a “heart-shaped box of wires and springs” with this nifty line of code!
Image@Compile[{},    
   Block[{i, x, p},     
    Table[i = 0; x = 0. I; p = r + I c;      
     While[Abs@x
     Tanh[Power[i/9^3, (7)^-1]], {c, -1, 1, .01}, {r, -2, 1,.01}]]][]

Rubber Duck, by Paul Abbott

And we’ll leave you with this cute little creation, made using a series of spherical functions, because it’s just adorable, isn’t it?
SphericalPlot3D[  
 Log[\[Theta]] + Sin[\[CurlyPhi]], {\[Theta], 0,   
  2 \[Pi]}, {\[CurlyPhi], 0, 2 \[Pi]}, Boxed -> False, Axes -> False,  
 PlotStyle -> Yellow, Mesh -> None, PlotPoints -> 30]

Inspired? Show us what Raspberry Pi oriented one-liners you can come up with!

No comments:

Post a Comment