WOLFRAM

Best of Blog

Best of Blog

Code Length Measured in 14 Languages

Update: See our latest post on How the Wolfram Language Measures Up. I stumbled upon a nice project called Rosetta Code. Their stated aim is "to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another." After amusing myself by contributing a few solutions (Flood filling, Mean angle, and Sum digits of an integer being some of mine), I realized that the data hidden in the site provided an opportunity to quantify a claim that I have often made over the years—that Mathematica code tends to be shorter than equivalent code in other languages. This is due to both its high-level nature and built-in computational knowledge. Here is what I found. Mathematica code is typically less than a third of the length of the same tasks written in other languages, and often much better.
Best of Blog

Automating xkcd Diagrams: Transforming Serious to Funny

On early Monday morning I noticed an interesting question posted on Mathematica Stack Exchange titled quite innocently "xkcd-style graphs." Due to the popularity of Randall Munroe's xkcd web comic, I expected a bit more than average of about ten or so up-votes, a few bookmarks. Little did I know. Spontaneously emerging viral events are hard to predict, so if you are lucky to catch one, it is fascinating to watch its propagation across the web and the growth of its ranks. In a matter of two days, this post received more than 100,000 views, 200 up-votes, and 150 bookmarks; produced responses and similar posts across other Stack Exchange communities; triggered a small tornado on Twitter; and was discussed on Hacker News and reddit. For convenience, I repeat Amatya's original post and example xkcd image here: "I received an email to which I wanted to respond with a xkcd-style graph, but I couldn't manage it. Everything I drew looked perfect, and I don't have enough command over Plot Legends to have these pieces of text floating around. Any tips on how one can create xkcd-style graphs? Where things look hand-drawn and imprecise. I guess drawing weird curves must be especially hard in Mathematica."
Best of Blog

Designing Jigsaw Puzzles with Mathematica

I was browsing Mathematica user communities for original projects and came by the following question: "How can I calculate a jigsaw puzzle cut path?" Such creative problems are in abundance at our user forums, the Wolfram Demonstrations Project, and The Mathematica Journal, which tells me that Mathematica, at its heart, is a conduit for creativity: it's a programing language that likes the challenge of convoluted problems and inspires elegant, often unexpected solutions. In its essence, a jigsaw puzzle is a tiling or tessellation, which means there are no gaps or overlaps between the pieces. Moreover, every piece is unique and has a definite place in the puzzle. Uniqueness is achieved by making a piece be a part of a large image, have a specific shape, or a combination of these. There are many approaches to producing such patterns. For instance, this single mathematical formula from our graphics examples produces a beautiful tessellation: However, since this question was asked on Mathematica Stack Exchange, a young, modern technical Q&A site, it was very specific in accordance with the community rules. Here are the author's requirements: All pieces must be unique to preclude placing a piece in the wrong spot. Pieces must be interlocking such that each piece is held by adjacent pieces. It must be possible to generate different paths (sets) for a specific shape that are not merely rotations or reflections of the first. Additionally, the author of the question notes that he seeks original designs alternative to typical mass-produced shapes.
Best of Blog

Analyzing Your Email with Mathematica

In Stephen Wolfram's recent blog post about personal analytics, he showed a number of plots generated by analyzing his archive of personal data. One of the most common pieces of feedback we received was that people wanted to know how they could perform the same kind of analysis on their own data. So in this blog post I'm going to show you how to analyze your email the same way Stephen Wolfram did. Naturally, we did all the data cleaning and analysis for Stephen's data in Mathematica, so we'll be using Mathematica for everything here as well. All the code can be downloaded here. Let's start with that really cool diurnal plot Stephen did of his outgoing email. This plot shows the date and time each email was sent, with years running along the x axis and times of day on the y axis:
Best of Blog

The Longest Word Ladder Puzzle Ever

UPDATE: The solution to the puzzle and more comments from Jon have been added at the bottom of the post. On the long flight to the recent Wolfram Technology Conference, I ended up on the puzzle page of a newspaper. My attention was drawn to a word ladder puzzle, where you must fill in a sequence of words from clues, but each word differs from the previous by only a single letter. Here, for example, is a simple puzzle already solved:
best from a position of superiority or authority
bast strong woody fibers obtained especially from the phloem of from various plants
bash a vigorous blow
bath a vessel containing liquid in which something is immersed (as to process it or to maintain it at a constant temperature or to lubricate it)
math a science (or group of related sciences) dealing with the logic of quantity and shape and arrangement
I wasn't going to do a blog entry on this, as it is a very similar task to my "Exploring Synonym Chains" post that I wrote some time ago, but that changed with a chance conversation at the (excellent) Technology Conference. Proving that one never stops learning, Charles Pooh, one of our graph theory developers, pointed out to me that my synonyms item could have been done much better. I had broken one of the very rules that I wrote about in my "10 Tips for Fast Mathematica Code" entry—"Use built-in functions." I had effectively re-implemented the built-in Mathematica commands GraphPeriphery and GraphDiameter. So, armed with these two new functions, let's find the longest word ladder puzzle that can be made using Mathematica's English dictionary.
Best of Blog

10 Tips for Writing Fast Mathematica Code

When people tell me that Mathematica isn't fast enough, I usually ask to see the offending code and often find that the problem isn't a lack in Mathematica's performance, but sub-optimal use of Mathematica. I thought I would share the list of things that I look for first when trying to optimize Mathematica code. 1. Use floating-point numbers if you can, and use them early. Of the most common issues that I see when I review slow code is that the programmer has inadvertently asked Mathematica to do things more carefully than needed. Unnecessary use of exact arithmetic is the most common case. In most numerical software, there is no such thing as exact arithmetic. 1/3 is the same thing as 0.33333333333333. That difference can be pretty important when you hit nasty, numerically unstable problems, but in the majority of tasks, floating-point numbers are good enough and, importantly, much faster. In Mathematica any number with a decimal point and less than 16 digits of input is automatically treated as a machine float, so always use the decimal point if you want speed ahead of accuracy (e.g. enter a third as 1./3.). Here is a simple example where working with floating-point numbers is nearly 50.6 times faster than doing the computation exactly and then converting the result to a decimal afterward. And in this case it gets the same result.
Best of Blog

Building a Microscopy Application in Mathematica

As a change from my usual recreational content, today I thought I would describe a real Mathematica application that I wrote. The project came from my most important Mathematica user—not because she spends a lot of money with Wolfram Research, but because I am married to her! Her company, Particle Therapeutics, works on needle-free injection devices that fire powdered drug particles into the skin on a supersonic gas shock wave. She was trying to analyze the penetration characteristics on a test medium by photographing thin slices of a target under a microscope and measuring the locations of the particles. The problem was that her expensive image processing software was doing a poor job of identifying overlapping particles and gave her no manual override for its mistakes. Faced with the alternative of holding rulers up to her screen and recording each value by hand, I promised that I could do better in Mathematica, with the added advantage that now her image processing tool would be integrated into her analysis code to go from image file to report document in a single workflow.
Best of Blog

How I Made Wine Glasses from Sunflowers

Eons ago, plants worked out the secret of arranging equal-size seeds in an ever-expanding pattern around a central point so that regardless of the size of the arrangement, the seeds pack evenly. The sunflower is a well-known example of such a "spiral phyllotaxis" pattern: It's really magical that this works at all, since the spatial relationship of each seed to its neighbors is unique, changing constantly as the pattern expands outwardly—unlike, say, the cells in a honeycomb, which are all equivalent. I wondered if the same magic could be applied to surfaces that are not flat, like spheres, toruses, or wine glasses. It's an interesting question from an aesthetic point of view, but also a practical one: the answer has applications in space exploration and modern architecture.
Best of Blog

What Shall We Do with the Drunken Sailor? Make Him Walk the Plank!

Back in 1988 when Mathematica was just a year old and no one in my university had heard of it, I was forced to learn Fortran. My end-of-term project was this problem: "A drunken sailor returns to his ship via a plank 15 paces long and 7 paces wide. With each step he has an equal chance of stepping forward, left, right, or standing still. What is the probability that he returns safely to his ship?" I wrote a page or so of ugly code, passed the course, and never wrote Fortran again. Today I thought I would revisit the problem. We can code the logic of the sailor's walk quite easily using separate rules for each case. Firstly, if he is ever on the 16th step or already on the ship, then he is safely on the ship the next time.