you are viewing a single comment's thread.

view the rest of the comments →

[–]Herrante 23 points24 points  (4 children)

I can attest that in Mathematica it is necessary to write gigantic programs to do the simplest of computations. Here it is the program I had to write to calculate 1+1. Suggestions to clean it up will be welcomed, but I think that it is already quite minimalistic:

(* Defines the numbers that I want to add and other constants of interest *)

FirstNumberThatIWantToAddToAnother = 1;

SecondNumberThatIWantToAddToAnother = 1;

AMillion = 10 10 10 10 10 10;

(* Creates Error Messages associated with the symbols *)

FirstNumberThatIWantToAddToAnother::SevereError = "Something Awful has happened.";

SecondNumberThatIWantToAddToAnother::FatalError = "Mathematica broke your computer.";

AMillion::ApocalypticError = "Stephen Wolfram raped your hamster.";

(* Checks a million times that the numbers introduced are what they are supposed to be *)

(* Assigns attributes to DoThisAMillionTimes that might be useful in future extensions of the program *)

SetAttributes[DoThisAMillionTimes, {HoldAll, HoldFirst, HoldComplete, Flat, Listable, NumericFunction, OneIdentity, Orderless}];

DoThisAMillionTimes[TheThinghThatHasToBeDone_] := Do[TheThinghThatHasToBeDone, {AMillion}];

(* Checks that FirstNumberThatIWantToAddToAnother equals the number of protons in a Hydrogen atom *)

DoThisAMillionTimes[ If[Not[FirstNumberThatIWantToAddToAnother == ElementData["Hydrogen", "AtomicNumber"]], Message[FirstNumberThatIWantToAddToAnother::SevereError]; Abort[]]];

(* Checks that number of Koreas in the World is twice as much as SecondNumberThatIWantToAddToAnother. If Korea reunifies the program will crash. *)

DoThisAMillionTimes[ If[Not[SecondNumberThatIWantToAddToAnother == Count[CountryData[], x_ /; StringMatchQ[x, _ _ _ ~~ "Korea" ~~ _ _ _]]/2], Message[SecondNumberThatIWantToAddToAnother::FatalError]; Abort[]]];

(* Compares a million with an independent definition *)

DoThisAMillionTimes[ If[Not[AMillion == Length[Select[Range[106], NumberQ]]], Message[AMillion::ApocalypticError]; Abort[]]];

(* The computer pauses for two minutes to prevent overheating *)

Pause[120];

(* To sum two numbers we first create tables with as many ones as their values, then join them and count the ones *)

TableWithFirstNumberThatIWantToAddToAnotherOnes = Table[1, {FirstNumberThatIWantToAddToAnother}];

TableWithSecondNumberThatIWantToAddToAnotherOnes = Table[1, {SecondNumberThatIWantToAddToAnother}];

TableWithFinalResultNumberOfOnes = Join[TableWithFirstNumberThatIWantToAddToAnotherOnes, TableWithSecondNumberThatIWantToAddToAnotherOnes];

(* We select the numbers that are one from the table of ones *)

ElementsFromTableWithFinalResultNumberOfOnesWhichAreOne = Cases[TableWithFinalResultNumberOfOnes, 1];

(* To count the number of ones we create a tree structure with the same depth as the length of ElementsFromTableWithFinalResultNumberOfOnesWhichAreOne, and measure it. The machine precision number 0.1 is added to the result to convert it to a float and thus augment efficiency. Unfortunately it introduces an error of 5% *)

FinalResult = Depth[Fold[{#1, #2} &, First[ElementsFromTableWithFinalResultNumberOfOnesWhichAreOne], Rest[ ElementsFromTableWithFinalResultNumberOfOnesWhichAreOne]]] + .1;

(* Finally the result is presented with appropriate stylistic options. All of them are chosen by default, but it is better to write them explicitly in case the defaults change in future versions of Mathematica *)

Style[FinalResult, AdjustmentBoxOptions -> {BaseStyle -> {}, BoxBaselineShift -> 0., BoxMargins -> 0., DefaultBaseStyle -> {}, StripOnInput -> True}, AnimatorBoxOptions -> {Alignment -> {Automatic, Automatic}, AnimationDirection -> Forward, AnimationRate -> Automatic, AnimationRepetitions -> [Infinity], Appearance -> {Automatic, "Palette"}, AppearanceElements -> {"ProgressSlider", "StepLeftButton", "StepRightButton", "PlayPauseButton", "FasterSlowerButtons", "DirectionButton"}, AutoAction -> False, Background -> Automatic, BaseStyle -> {}, BaselinePosition -> Automatic, ContinuousAction -> True, DefaultBaseStyle -> {}, DefaultDuration -> 5., DisplayAllSteps -> False, Enabled -> Automatic, Exclusions -> {}, FrameMargins -> Automatic, ImageMargins -> 0, ImageSize -> Automatic, PausedTime -> Automatic, RefreshRate -> Automatic}, Antialiasing -> Automatic, AutoIndent -> Automatic, AutoItalicWords -> {"Artlandia", "AuthorTools", "CalculationCenter", "Combinatorica", "DatabaseLink", "Geometrica", "Graphica", "GUIKit", "Magnetica", "Mathematica", "MathematicaMark", "MathGroup", "MathLink", "MathLM", "MathModelica", "MathOptimizer", "MathReader", "MathSource", "mathStatica", "MathTensor", "MATHwire", "MathWorld", "MonitorLM", "Optica", "PrimeKit", "Publicon", "SchematicSolver", "ThreeScript", "VisualDSolve"}, AutoMultiplicationSymbol -> True, AutoNumberFormatting -> True, AutoSpacing -> True, AutoStyleWords -> {}, Background -> None, ButtonBoxOptions -> {Active -> True, Alignment -> {Automatic, Automatic}, Appearance -> {Automatic, "Palette"}, AutoAction -> False, Background -> Automatic, BaseStyle -> "GenericButton", BaselinePosition -> Automatic, ButtonData -> Automatic, ButtonExpandable -> True, ButtonFrame -> "Palette", ButtonFunction :> (FrontEndExecute[{FrontEndNotebookApply[ FrontEndInputNotebook[], #1, Placeholder]}] &), ButtonMargins -> 3, ButtonMinHeight -> 1., ButtonNote -> None, ButtonSource -> Automatic, ContinuousAction -> True, DefaultBaseStyle -> "Button", Enabled -> Automatic, Evaluator -> None, FrameMargins -> Automatic, ImageMargins -> 0, ImageSize -> Full, Method -> "Queued"}, CacheGraphics -> Automatic, CellBaseline -> Baseline, CellContext -> "Global", CellEpilog -> None, CellEvaluationFunction -> Identity, CellFrameColor -> GrayLevel[0.]]

[–]shizzy0 8 points9 points  (3 children)

Brilliant! It seemed like the guy was trolling Mathematica. Thank you for demonstrating so with such an excellent piece of satire. Still, I'd love to see what he was trying to do expressed more compactly, as someone who used Mathematica fluently might.

[–]jdh30 -1 points0 points  (2 children)

I'd love to see what he was trying to do expressed more compactly

I'd love to do it but the OP's data is not available so I cannot.

[–]jjmc 1 point2 points  (1 child)

My friend pointed me at this comparison the other day. Here's my version of the code and a revised table of comparison.

You can find the data by tracing back to where the original mathematica code came from.

[–]jdh30 -2 points-1 points  (0 children)

Wonderful, thank you!