Wolfram Blog News, views, & ideas from the front lines at Wolfram Research 2015-03-02T14:05:58Z http://blog.wolfram.com/feed/atom/ WordPress Vitaliy Kaurov <![CDATA[Find Waldo Faster]]> http://blog.internal.wolfram.com/?p=24123 2015-03-02T14:05:58Z 2015-02-27T19:48:57Z Martin Handford can spend weeks creating a single Where’s Waldo puzzle hiding a tiny red and white striped character wearing Lennon glasses and a bobble hat among an ocean of cartoon figures that are immersed in amusing activities. Finding Waldo is the puzzle’s objective, so hiding him well, perhaps, is even more challenging. Martin once said, “As I work my way through a picture, I add Wally when I come to what I feel is a good place to hide him.” Aware of this, Ben Blatt from Slate magazine wondered if it’s possible “to master Where’s Waldo by mapping Handford’s patterns?” Ben devised a simple trick to speed up a Waldo search. In a sense, it’s the same observation that allowed Jon McLoone to write an algorithm that can beat a human in a Rock-Paper-Scissors game. As Jon puts it, “we can rely on the fact that humans are not very good at being random.”

Readers with a sharp eye can see that the set of points in the two images below is the same, so the images are just different visualizations of the same data. The data shows the 68 spots where Waldo hides in the special edition of seven classic Waldo books. Big thanks go to Ben Blatt, who sat for three hours in a Barnes & Noble bookstore with a measuring tape painstakingly constructing this fabulous dataset. Ben’s study had a follow up by Randal Olson from Michigan State University, who suggested a different approach based on the density of Waldo spots and a shortest path algorithm. In this post, I will go over both of these approaches and suggest alternatives—these are the final results:

Waldo plot path

Getting the Data

I start with getting coordinates for Waldo’s hideouts from all seven classic puzzles. Ben Blatt indicated where they are on a single image. After importing the data, you will see a schematic of a dual-page of an open puzzle book with all hideouts marked with red spots. Each spot has text inside labeling the book and the page of the puzzle it belongs to. The text is not important to us and, moreover, needs to be removed in order to obtain the coordinates.

i = Import["https://wolfr.am/3hI4OSh8"]

Waldo's hideout in two-page layouts

With Wolfram Language image processing tools, it’s easy to find the coordinates of the red spots. Some spots overlap, so we need robust segmentation to account for each spot separately. There are different approaches, and the code below is just one of them. I’ve arranged it so you can see the effects, step-by-step, of applications of image processing functions. Binarize is used to reduce complex RGB image data to a simple matrix of 0s and 1s (binary image). Then ColorNegate followed by FillingTransform heal the small cracks inside the spots due to text labels. Erosion with DiskMatrix diminish and separate overlapping spots. Finally, SelectComponents picks only the spots, and filters out the other white pixels by specifying the rough number of pixels in a spot.

Waldo input2

Waldo SelectComponents

Getting spots’ centers in the image coordinate system is done with ComponentMeasurements. We also verify that we have exactly 68 coordinate pairs:

spots = ComponentMeasurements[Last[%], "Centroid"][[All, 2]]; Short[spots]

The precision of our routine is illustrated by placing blue circles at the coordinates we found—they all encompass the hideouts pretty accurately, even the overlapping ones:

Show[i, Graphics[{Blue, Thickness[.005], Circle[#, 15] & /@ spots}]]

The hideout locations are spot on

Note a sizable textbox in the top-left corner, which is always present in the original puzzles too, deforming the data a bit. I will transition from the image coordinate system to the actual book one. A single Waldo puzzle takes a double-page spread measuring 20 by 12.5 inches:

book = spots 20/First[ImageDimensions[i]]

Ben Blatt’s Approach

Quoting Ben: “53 percent of the time Waldo is hiding within one of two 1.5-inch tall bands, one starting three inches from the bottom of the page and another one starting seven inches from the bottom, stretching across the spread…. The probability of any two 1.5-inch bands containing at least 50 percent of all Waldo’s is remarkably slim, less than 0.3 percent. In other words, these findings aren’t a coincidence.” This is a keen observation because it is not obvious from just looking at the scattered dataset. I can illustrate by defining a geometric region equal to a single 1.5-inch strip (note the handy new ImplicitRegion function):

strip[a_] := ImplicitRegion[a < y <= a + 1.5 && 0 < x < 20, {x, y}]

To see which bands contain the most Waldos, I scan the double-page spread vertically in small steps, counting the percentage of points contained in the band. The percentage is shown in the left plot while the corresponding band position is shown in the right one. Clearly there are sharp peaks at 3 and 7 inches, which would amount to almost 50% if two identical bands were placed at the maxima.

scan = Table[{n, 100 Count[RegionMember[strip[n], book], True]/68.}, {n, 0, 11, .2}];

Animation of band scanning

Widening the bands or adding more of them would raise the percentage, but also add more area to cover during the search, so it would increase the search time too. The total percentage from two bands at the maxima amounts to about 47%, while Ben claimed 53%, which I attribute to errors in quite approximate data acquisition:

Total[100 Count[RegionMember[strip[#], book], True]/68. & /@ {3, 7}]

Musing on Randy Olson’s Approach

Randy had a deeper statistical and algorithmic take on the Waldo data. First he visualized the density of points with a smooth function. The higher the density, the greater the probability is of finding Waldo in the surrounding area. The density plot has a quite irregular pattern, very different from Ben’s two simple bands. The irregular pattern is more precise as a description, but harder to remember as a strategy. Then, Randy “decided to approach this problem as a traveling salesman problem: We need to check every possible location that Waldo could be at while taking as little time as possible. That means we need to cover as much ground as possible without any backtracking.”

I can get a similar plot of the density points right away with SmoothDensityHistogram. To that, I’ll add two more things. The first is a shortest tour through all the points (i.e., a traveling salesman problem solution) found with FindShortestTour:

tour = FindShortestTour[book]

It lists the order of consecutive locations to visit so the length of the tour is minimal (the first number is the tour’s length). This “tour” is a closed loop and is different from Randy’s open “path” from A to B. If we could remember the tour exactly, then we could always follow it and find Waldo quickly. But it’s too complex, so to better comprehend its shape, we can smooth it out with a spline curve:

order = book[[Last[tour]]];

Shortest Waldo-finding tour smoothed with a spline curve

If you compare the visualization above with the original plain scattered plot of points, you may get the feeling that there are more points in the scattered plot. This is an optical illusion, which, I suspect, is related to the different dimensionality of these two visualizations. Points along a curve are perceived as a one-dimensional object covering much less area than a scatter plot of points perceived as two-dimensional and covering almost all the area.

Such visualizations help to give a first impression of the data, the overall distribution, and patterns:

  1. Background density is a visual guide to remembering where finding Waldo is more probable.
  2. The line through the points is a guide to the optimal trajectory when you scan the puzzle with your eyes.

But these two must be related somehow, right? And indeed, if we make the probability density background “more precise” or “local and sharp,” it will sort of wrap around our tour (see image below). SmoothDensityHistogram is based on SmoothKernelDistribution, which is pretty easy to grasp. It is based on the formula

Formula for SmoothKernelDistribution

where kernel k(x) can be a function of our choice and h defines effective “sharpness” or “locality” of the resulting density function. You see below that by making our distribution sharper by decreasing h, we localize the density more around the shortest tour. I have also chosen a different kernel “Cosine” function and changed several minor visual settings (for the tour, spline, and density) to show flexible options for tuning the overall appearance to anyone’s liking.

SmoothDensityHistogram[book, {1.5, "Cosine"}, ...


By getting the spline from the tour and widening it, we could get closer to the pattern of the density (the regions of thick-enough spline would overlap where the density is higher). And vice versa, by localizing the density kernel, we can get the density to envelope the spline and the tour. It is quite marvelous to see one mathematical structure mimic another!

Well, while nice looking, this image is not terribly useful unless you have a good visual memory. Let’s think of potential improvements. Currently I have a closed loop for the shortest tour, and this means going once from the left to right page and then returning back. Could I find a one-way shortest path that traverses once from left to right? This is what Randy Olson did with a genetic algorithm, and what I will do with a modified FindShortestTour that artificially sets the distance between the start and end points to zero. Thus, FindShortestTour is forced to draw a link between the start and end and then traverse all the rest of the points. Finally, the fake zero-length start-end link is removed, and I get a disconnected start-to-end point path.

thruway[p_List][k_Integer, m_Integer] := ...

I will also get rid of the single outlier point in the top-left corner and sort points in order of increasing horizontal coordinate. The outlier does not influence much, and even when it’s the solution, it can be easily checked because the area above the textbox is small.

bookS = Rest[Sort[book]]; tourS = thruway[bookS][1, 67]; orderS = bookS[[Last[tourS]]];

Next I will find a shortest path from the bottom-left to bottom-right corners. And again, I will choose different visual options to provide an alternative appearance.

SmoothDensityHistogram[bookS, {1.5, "Cosine"},  Mesh ...


Randy used a similar but different path to devise a less-detailed schematic trajectory and instructions for eye-scanning the puzzle.

The Grand Unification

When you have a dual-page book spread, isn’t it natural to think in terms of pages? People habitually examine pages separately. Looking at the very first density plot I made, one can see that there’s a big density maximum on the left and another one stretched toward the right. Perhaps it’s worth looking at the same analysis done for each page independently. Let’s split our points into two sets belonging to different pages:

pages = GatherBy[bookS, First[#] ...


We walk through the same steps, slightly changing visual details:

porder = #[[thruway[#][1, Length[#]][[2]]]] & /@ pages;


And indeed, a simple pattern emerges. Can you see the following two characters on the left and right pages correspondingly: a slanted capital L or math angle “∠” and Cyrillic “И”?

Row[Style[#, #2, Bold, FontFamily ...

I would suggest the following strategy:

  1. Avoid the very top and very bottom of both pages.
  2. Avoid the top-left corner of the left page.
  3. Start from the left page and walk a capital slanted “L” with your eyes, starting with its bottom and ending up crossing diagonally from the bottom left to top right.
  4. Continue on the right page and walk the capital Cyrillic “И”, starting at the top left and ending at the bottom right via the И-zigzag.
  5. If Waldo is not found, then visit areas in steps 1) and 2).

These two approaches of density and shortest path make a full turn and lead me back to the original simpler band idea. Those who prefer something simpler might notice that in the density plot above, there are two maxima on each page along the diagonals mirroring each other across the vertical page split. There are quite a few points along the diagonals, and we could attempt to build two bands of the following geometry:

W = 2; S = .85; R = 2; diag = ImplicitRegion[(S x - W + R < y || S (20 - x) - W + R < y) && y <= S x + W + R && y <= S (20 - x) + W + R && 0 < x < 20 && 1.5 < y < 11, {x, y}];

I compare this region to the original Slate design using RegionMeasure. We see that my pattern is 6% larger in area, but it also covers 7.4% more of Waldo’s positions than Blatt’s region, and it provides a simpler and shorter one-way scanning of the puzzle instead of walking from left to right twice along two bands.

band = ImplicitRegion[(3 ...


So, concerning the strategy, imagine two squares inscribed in each page and walk their diagonals, scanning bands about 2.8 inches thick. This strategy not only tells you where to look for Waldo first, it tells you where to look if you failed to find him: along the other two diagonals. This can be seen from our split-page density plot above and my final visualization below showing diagonal bands covering all four maxima:

sdhg[d_] := SmoothDensityHistogram[d, MeshShading ...


For the Die-Hard Shortest-Path Buffs

The traveling salesman problem is NP-complete; thus, it is possible that the worst-case running time for any algorithm solving it grows superpolynomially (even exponentially) with the number of points. While FindShortestTour can work with large datasets, the way we adapted FindShortestTour (which by default returns a “loop” path) to a shortest disconnected path between two given points is a bit dodgy. We can force FindShortestTour to work properly on the dataset by considering all Waldo positions as vertices of a Graph.

In general, FindShortestTour on a set of points in n-dimensional space essentially finds the shortest tour in a CompleteGraph. Now if I want such a path with the additional constraint of start and end points, I can augment the graph with an extra vertex that joins the given start and end points in question. The distance to the extra vertex does not really matter. FindShortestTour will be forced to pass through the section start-extra-end points. This works with any connected graph, not necessarily a complete graph, as shown below:

Augmented FindShortestTour graph

After finding the tour on the augmented graph, I remove the extra vertex and its edges, and so the shortest tour disconnects and goes from the start to end points.

I will build a symmetric WeightedAdjacencyMatrix by computing the EuclideanDistance between every two points. Then I’ll simply build a WeightedAdjacencyGraph based on the matrix and add an extra vertex with fake unit distances. Before I start, here is a side note to show how patterns of data can appear even in such an abstract object as WeightedAdjacencyMatrix. Old and new (sorted) orders of points are visually drastically different, while the matrix in both cases has the same properties and would generate the same graph.

ArrayPlot[Outer[EuclideanDistance, #, #, 1], ColorFunction...


On the left, points are unsorted, and on the right, points are sorted according to the shortest tour. The brighter colors are pairs of most remote points. Now let’s define the whole function that will return and visualize a shortest tour based on a network approach:

shortestTourPath[pts_, s_, t_] := ...

Applying this to the Waldo dataset, we get a beautiful short path visual through a complete graph:

shortestTourPath[orderS, 1, 67]

Short path visual through a complete graph


By the way, the latest Wolfram Language release has the state-of-the-art FindShortestTour algorithm. To illustrate, I’ll remind you of a famous art form when a drawing is made of single line with no interruption or a sculpture from a single wire piece. The oldest examples I know are simultaneously the largest ones—mysterious Nazca Lines from Peru that could measure hundreds of feet in span. Here is, for instance, an aerial image of a 310-foot-long hummingbird (Nazca Lines cannot be perceived from the ground—they are that large). Adjacent to it is a one-line drawing by Pablo Picasso.

Nazca Lines: aerial image of a 310-foot-hummingbird drawingA single-line drawing by Picasso

Picasso’s art is more complex, but it’s pretty easy to guess that humans will have a harder and harder time drawing a subject as the line gets longer and more intricate. But as it turns out, this is exactly where FindShortestTour excels. Here is a portrait computed by FindShortestTour spanning a single line through more than 30,000 points to redraw the Mona Lisa. The idea is quite simple: an image is reduced to only two colors with ColorQuantize, and then FindShortestTour draws a single line through the coordinates of only dark pixels.

So why isn’t it simply a chaos of lines rather than a recognizable image? Because in a shortest tour, connecting closer neighbors is often less costly. Thus, in the Mona Lisa one-liner, there are no segments spanning across large white spaces.

Concluding Remarks

My gratitude goes to Martin Handford, Ben Blatt, and Randy Olson, who inspired me to explore the wonderful Waldo patterns, and also to many creative people at Wolfram Research who helped me along the way. Both Ben and Randy claimed to verify the efficiency of their strategies by actually solving the puzzles and measuring the time. I do not have the books, but hope to do this in future. I also believe we would need many people for consistent benchmarks, especially accounting for different ways people perceive visually and work through riddles. And most of all, I am certain we should not take this seriously.

We did attest that humans are bad at being random. But that is not a bad thing. One who is good at exhibiting patterns is likely apt at detecting them. I would speculate further and say that perhaps a sense of deviation from randomness is at the root of our intelligence and perception of beauty.

Download this post as a Wolfram notebook.

Hector Zenil http:// <![CDATA[Why Alan Turing Has Already Won, No Matter How The Imitation Game Does at the Oscars]]> http://blog.internal.wolfram.com/?p=23997 2015-02-21T15:50:26Z 2015-02-20T20:39:06Z When I was invited to join the Turing Centenary Advisory Committee in 2008 by Professor Barry Cooper to prepare for the Alan Turing Year in 2012, I would have never imagined that just a few years later, Turing’s life and work would have gained sufficient public attention to become the subject of a Hollywood-style feature film, nor that said movie would go on to earn eight Oscar nominations.

Imitation game nominations using Wolfram|Alpha

The Imitation Game is in essence a rather accurate summation of the life and brilliant work of Alan Turing, even while some aspects of the movie are clearly fictional. Turing was in many ways a victim of his great achievements—he was forbidden from telling of his accomplishments during the war to anyone, including his family. Imagine you have the chance to save the world—and you do so—but you are required to keep it confidential; that was the case of Turing and his colleagues at Bletchley Park. He was also prosecuted toward the end of his life in an unfair turn of events. Nevertheless, Turing was never engulfed by his circumstances and remained interested in what can be considered the greatest questions in science and beyond, from what intelligence is in both humans and machines to what mechanisms nature harnesses to produce forms and shapes, and ultimately life itself.

At Bletchley Park, Turing, together with other mathematicians and engineers of great talent, made longstanding contributions to science and to human history by building the first automated machines to decipher encrypted messages. His work and the work at Bletchley led to what is today one of the greatest intelligence agencies—the UK Government Communications Headquarters (GCHQ). One can play with a computer program online to see the inner workings of the famous Enigma machine used by the Germans to encode their messages, which Turing and his team were able to crack. The example below from the Wolfram Demonstrations Project shows the inner workings of such an encryption machine that used cylinders to scramble and conceal the contents of messages during World War II.


It is worth taking a minute to see how computers, and ultimately the human race, have come this far, with Turing himself playing a central role. Turing approached several scientific endeavors with incredible idiosyncrasy. When tackling what would become his most seminal work, which gave rise to the digital computer, Turing was actually not thinking of computers but of humans. Indeed, he asked how humans calculate and how much of that could be captured by mechanical calculation. By pursuing this idea to its final consequences, Turing revealed two incredibly powerful concepts of priceless intellectual value.

Turing first showed that either a human or computer with the same resources would find a fundamental limit on the sort of things that could be proven to be true or false about a theory. In other words, there are formally unanswerable questions whose answers are nevertheless known to be true or false; hence, truth and proof are separate concepts. Indeed, in this very technical formal approach, not all truths can be proven by mechanical means.

At the same time, Turing also found that he could build a single machine to do the work of any other computing machine; this is what we call a “computer” today. Computers that are capable of emulating any other computer are called “universal,” because they can do whatever any other machine can do. You can check email, watch videos, and play video games all on the same computer, as it can be reprogrammed to perform multiple tasks. That’s obvious to us now, but it wasn’t at all obvious to someone living in the 1930s.

An “a” machine, as Turing would first call it (for “automatic”), today known as a Turing machine, is the most basic unit of study in the field of theoretical computer science. The concept not only gave birth to the whole field but remains the object of reference for most purposes in computer science. For example, when you want to know how much memory one theoretically needs to perform a computation, a universal Turing machine is always the point of reference. An immediate follow-up question is whether this kind of “universal” computation occurs naturally in the world and is responsible for the complexity and structure we find in the universe.

One way to answer this question is to look at how many resources are required to build such universal computers. If it turns out that this type of computation is very easy to implement because it requires only a few simple resources, one might be persuaded that nature makes use of this computational resource to produce the shapes and patterns we find around us. And indeed, it turns out that the resources needed to implement a universal computing machine are so incredibly simple that they are available everywhere in nature. This was illustrated by a competition sponsored by Stephen Wolfram to prove whether an exceptionally simple Turing machine that he had found in his exploration of these computers in his book, A New Kind of Science, was actually capable of universal computation. The contest started and ended in a heated but rich discussion on the necessary and sufficient initial conditions for a system to become and be considered universal.

small turing machine

The machine above starts with an empty “tape” on which a reading “head” is placed; the head moves to the right or left and writes or deletes colors over time following the rules displayed at the bottom. While this small Turing machine requires a special initial condition to perform as a universal computer, it does not violate any previous result in the field, and thus meets the conditions for a system to be universal. An excellent place to learn about this rich computing model can be found here, and more about the smallest universal Turing machine here.

Another question related to Turing’s main interests is about the shapes and forms that emerge in nature all around us, especially in connection to life. By looking at another, but equivalent, type of computing program that Stephen Wolfram introduced in the 1980s called 1-dimensional cellular automata, one can quickly see that computer programs are incredibly rich in behavior, and that even the simplest computer programs can display an incredibly complex range of behavior. Below (see pyramidic figure) is a minimalistic computer program of this type with only three colors—an example of Wolfram’s 1-dimensional cellular automata—that starts from the simplest possible initial condition (a single dark cell at the top) and replaces the cell according to a simple set of rules (see top figure) that looks only at the neighbor’s colors to determine the color of the next central cell.

Pyramid celllular automaton

The program produces all sorts of simple and complex patterns that interact without destroying each other. There is even a small transient at the intersection of the rightmost fractal behavior and the middle one where regions of high complexity emerge just before dying out, whereas on the leftmost side there is a stripe of regular behavior feeding a seemingly never-ending, random-looking region that expands over time. The digital computers that humans produce may be simply imitating the computations that occur naturally throughout the universe, and that are, indeed, the very basis of how the universe functions.

Among the many interests of Alan Turing was the challenge of mathematical notation, which he explored in a manuscript, “Notes on the Foundations of Computer Science,” to be auctioned on April 13 of this year. Wolfram Research has built on Turing’s ideas since its beginning, and the topic of mathematical notation has also been key to the development of Wolfram Mathematica and the Wolfram Language. It is the Wolfram Language that enables Wolfram|Alpha, the world’s only computational knowledge engine, to understand queries in both formal and natural language. Stephen Wolfram has written and spoken extensively on these matters.

Recently Wolfram Research has taken an interesting direction toward pervasive computation (and related to Turing’s seminal idea of universal computation) with Wolfram Programming Cloud, bringing revolutionary technology to the real world for creating and deploying software and applications to anywhere, from anywhere. Also, the Wolfram Connected Devices Project takes us closer to the goal of injecting the power of universal computation even in the simplest devices from the Internet of Things, effectively making any physical device into a universal Turing machine. Devices like the Raspberry Pi mini-computer, already powerful in its own right, is made even more so by being shipped with the Wolfram Language and Mathematica preinstalled, enabling inquiry and exploration for the next generation of scientists and makers.

Much still remains to be discovered in connection to Turing’s main ideas, as it may turn out that the universe itself is some sort of universal Turing machine. In fact, as far-fetched as this might sound, it is exactly what classical physics prescribes: a fully deterministic world governed by computable rules and therefore emulative by a simple computer program such as a Turing machine. When physicists explain their ultimate quest for a theory of everything, they are endorsing just such an idea. Stephen Wolfram has his own approach to tackle this challenge and anticipates this being one of his next major projects as time permits.

Wolfram|Alpha shows a spike in the number of visits to the Wikipedia entry for Alan Turing on several important dates in modern history. First, the official public apology given by British Prime Minister Gordon Brown on September 10, 2009, and then around several academic events in late 2011 and early 2012. We see a great spike exactly on the 100th anniversary of his birthday on June 23, 2012—and by strange coincidence, as Stephen Wolfram notes in recognizing Turing’s 100th birthday, Mathematica’s anniversary also falls on June 23 (1988). Another spike occurs on December 24, 2013, when a posthumous royal pardon was granted for Turing’s 1952 conviction for homosexuality. More recently, there has been a steady increase due to the release of the movie The Imitation Game.

Alan Turing Wikipedia page

Regardless of the outcome of the Oscars awards this Sunday, in a way, Alan Turing has already won. The achievements of Turing, widely considered to be the father of modern computing, are now known worldwide, in ironic contrast to the unfortunate circumstances that led to his death. While he accomplished much in his relatively short lifetime, the best way to remember Turing is by continuing to advance his greatest ideas with the same kind of eagerness that he always had toward scientific inquiry, and by finding answers in unexpected directions. At Wolfram Research, we strive to do exactly that.

Johan Rhodin <![CDATA[Q&A with Michael Tiller, Author of Modelica by Example]]> http://blog.internal.wolfram.com/?p=23499 2015-02-11T16:34:58Z 2015-02-11T16:34:58Z Modelica is the object-oriented modeling language used in SystemModeler to model components and systems. When I first learned Modelica, I read all books available about the language (there are not that many!) and found the book Introduction to Physical Modeling with Modelica by Michael Tiller to be the best out there.

In 2012, when Michael started a Kickstarter campaign to fund the development of a Creative Commons licensed book about Modelica, I was the first person to back it, and Wolfram Research became one of the gold sponsors of the book. A new key feature in SystemModeler 4.0 is the full Modelica by Example book included in the product. This makes it much easier to get started learning Modelica.

I had the opportunity to ask Michael a couple of questions about the new book and Modelica.

Michael and Johan

Michael Tiller (left) and me, at the North American Modelica Users’ Group Conference at Georgia Tech in 2014.

What is your background in Modelica?

I started using Modelica around 1998, I think. At the time, I was already a very big fan of high-level modeling languages. As an undergraduate, I had used the MAST modeling language (in Saber) extensively. When I started working at Ford, I tried to apply modeling languages to modeling efforts in powertrain modeling.

I quickly ran into two issues. The first was that up until that point, the modeling languages I had looked at were mainly focused around modeling of electrical systems. I needed a modeling language that had a broader vision for modeling that included mechanical and, more importantly, fluid system dynamics. The other issue was having a modeling language that was open. Openness could help avoid vendor lock-in. But more importantly to me, I saw the ability to use a modeling language as a medium for capturing high-level knowledge. That knowledge could be used in a variety of different ways. Compiling the code into something that could be simulated was just part of the story. I wanted a language that was open so that people could plug new ideas and tools into the ecosystem and not have to wait for a single vendor to deliver those capabilities.

Both of these issues led me to Modelica. From there I worked with my colleagues at Ford to inject this technology into the modeling efforts that were underway at Ford. Along the way, I participated in the Modelica design group providing feedback from an industrial user’s perspective. I recognized that one big hindrance to the adoption of Modelica was the lack of a book on the topic. So in 2001, I published the first book on Modelica.

After Ford, I worked in engineering consulting. We used Modelica as one technology, among many, for helping customers to realize the advantages of engineering processes that were more model-based. In 2012, I started my current company, Xogeny, with the goal of helping engineering companies leverage all the amazing software technologies that are out there. That means going beyond just Modelica and FMI and expanding into lots of cloud and web technologies as well. My hope is that eventually engineers who develop models will have all the tools they need to make those models accessible to anyone who wants to use them and to really add value to the engineering process along the way.

Why is Modelica important?

Modelica is important because, as I mentioned previously, it captures a representation of people’s models in an open way and in a way that can be reasoned about. This comes from the fact that Modelica is a declarative language, not an imperative language. When you deal with typical programming languages, the imperative constructs (e.g., do this, now do that, now loop until this is true, then overwrite that result) make it nearly impossible to actually extract the high-level intention of the user (at least not without a tremendous amount of discipline). Modelica is not a language of statements and operations, it is a language of relationship: how variables relate to each other through an equation, how the values of a variable are related to a type (and its physical units), how a resistor is related to a capacitor through connections in a schematic diagram. The point is that the structure of the model and all of these relationships are always preserved in Modelica.

This property has been leveraged extensively to create very fast simulations by being able to reason about these relationships. We can see which variables depend on which other variables and use this to efficiently perform calculations, minimize the number of variables involved in solving nonlinear systems, recognize which signals are synchronized with other signals to ensure determinism in results, and so on.

That being said, I think there are other aspects of the engineering process that can benefit from this approach. So I don’t think we’ve fully leveraged the knowledge captured in Modelica models yet.

Why is learning Modelica important?

There are lots of approaches to simulation that kind of mirror computer programming where you start with some known values, then you compute something from those, then you compute something from those. Unfortunately, those kinds of approaches have limitations. But those limitations are very hard to see when you are just starting out. It is very common (both in engineering and in programming) for people to pick technologies that are easy to get started with, but don’t scale very well once you get into larger, more complex problem domains. But you can’t really blame people for taking the path of least resistance when trying to learn technologies. My goal, in writing both of my books, was to provide an easy path for people to learn about Modelica because, in my opinion, Modelica “has legs.” It can get you through those large and complex problems. But you have to learn it first before you can apply it to those kinds of problems.

This is an interesting and challenging question. The real advantages of Modelica are hard to explain to people at first, and often by the time they get to the point where they recognize the limitations of other technologies, they are so far down that road and so invested in them, they don’t have the option to switch technologies. That’s a big driver for me in trying to get people on the Modelica track from the start.

I think there is a bigger picture here, which is the value of standards in general. Many engineering companies want to focus on their core competency and want to “outsource” lots of stuff to vendors. But I feel like companies are more and more appreciating not just the cost in terms of dollars of this approach, but also the cost in terms of agility. They can’t change anything easily because of all the tear-up involved due to of vendor lock-in or just poor planning. The engineering community needs to start putting more focus on software and IT standards (and not just standards for fasteners, materials, etc).

What is the purpose of the book?

As I say in the dedication of the book, “I’m not interested in capitalizing on my knowledge of Modelica, I want to share it with others so they can experience the same joy I do in exploring this subject.” It is as simple as that. The nature of the Kickstarter project might give the appearance that this is some kind of business undertaking. But if that were the case, I wouldn’t be giving the book away on the web for free. The money was just to give me the freedom to really invest time in the project (and allow me to continue to invest time in it as it develops).

Using enumerations
Excerpt from the Modelica by Example book inside SystemModeler.

Any final thoughts?

I hope the book excites people (students in particular) about modeling and simulation. I think Modelica is a great example of a technology that is open, scalable, and industrially proven. It isn’t the only one. For me, that is the most exciting part. There are so many amazing technologies out there that we can apply to engineering, and every day I jump out of bed excited to see how to do just that.

I think in broader terms, my hope is to demonstrate to people in the engineering world the power of open collaboration. Whether that is open source, creative commons, standardization, whatever. Engineering is too “closed.” Paranoia about intellectual property related issues is very strong, and it makes people afraid to collaborate and discuss. I think people are also hesitant to leverage more advanced software and IT technologies. I think that is partly because they haven’t been given enough training to appreciate the value of these technologies and feel competent to participate. But I also think engineering is too segregated from software and IT. For example, when I look at lots of companies, the IT operations are very strongly focused on making sure email works and the networks are functioning and viruses aren’t spreading. All that is important, but what I don’t see a lot of is IT organizations having responsibilities to support the engineering process. Most engineers I talk to feel like every time they go to IT departments, they are told “No” to anything they want to do. The engineers are subservient to the IT departments. I think that is crazy. After all, these are engineering companies. Engineering should be their top priority. So I think that mindset has to change, and the book, my blog, and various other things I’m doing are just trying to make the value for open collaboration and standards (among other things) so valuable that we can hopefully break down some of those barriers.

Jenna Giuffrida <![CDATA[New Wolfram Technologies Books]]> http://blog.internal.wolfram.com/?p=23550 2015-02-11T14:40:39Z 2015-02-09T21:21:19Z We are once again thrilled by the wide variety of topics covered by authors around the world using Wolfram technologies to write their books and explore their disciplines. These latest additions range from covering the basics for students to working within specialties like continuum mechanics.


Computational Explorations in Magnetron Sputtering
Magnetron sputtering is a widely used industrial process for depositing thin films. E. J. McInerney walks you through the physics of magnetron sputtering in a step-by-step fashion using Mathematica syntax and functions. The reader is encouraged to explore this fascinating topic by actively following along with a series of simple computer models. By working through these models, readers can build their intuition of PVD processes and develop a deeper appreciation of the underlying physics.

Continuum Mechanics using Mathematica: Fundamentals, Methods, and Applications, second edition
In this second edition of Continuum Mechanics using Mathematica, Antonio Romano and Addolorata Marasco take a methodological approach that familiarizes readers with the mathematical tools required to correctly define and solve problems in continuum mechanics. It provides a solid basis for a deeper study of more challenging and specialized problems related to nonlinear elasticity, polar continua, mixtures, piezoelectricity, ferroelectricity, magneto-fluid mechanics, and state changes.

Mathematica Data Visualization: Create and Prototype Interactive Data Visualizations Using Mathematica
Nazmus Saquib begins by introducing you to the Mathematica environment and the basics of dataset loading and cleaning. You will then learn about different kinds of widely used datasets, time series, and scientific, statistical, information, and map visualizations. Each topic is demonstrated by walking you through an example project. Along the way, the dynamic interactivity and graphics packages are also introduced. This book teaches you how to build visualizations from scratch, quickly and efficiently.


Interactive Computational Geometry, A Taxonomic Approach
This ebook by Jim Arlow is an interactive introduction to some of the fundamental algorithms of computational geometry. The code base, which is in the Wolfram Language, is integrated into the text and is fully executable. This book is delivered as an interactive CDF (Computable Document Format) file that is viewable in the free CDF Player available from Wolfram, or can be opened in Mathematica. Readers are encouraged to have a copy of Mathematica in order to get the most out of this text.

学生が学ぶMathematica入門 (Introduction to Mathematica for Students)
Souji Otabe provides a quick introduction to Mathematica for students. Engineers and scientists need to know about symbolic computation, but they also need to know numeric computation to analyze experimental data. In this ebook, Mathematica is treated as a general problem-solving tool for science and engineering students. Readers will study basic operations of Mathematica first, and then they will learn how Mathematica can be applied to engineering and science.

Mathematik fur Informatiker (Mathematics for Computer Scientists) Band 2: Analysis und Statistik 3. Auflage
The second edition of Gerald and Susanne Teschl’s textbook introduces mathematical foundations that are concise, yet vivid and easy to follow. They are illustrated using numerous worked-out examples mixed with applications to computer science, historic background, and connections to related areas. The end of every chapter offers a self-test and warmup problems (with detailed solutions), as well as more advanced problems. Complementary sections introduce Mathematica to visualize the subjects taught, thereby fostering comprehension of the material.


Neoclassical Physics
In this introductory text by Mark A. Cunningham, physics concepts are introduced as a means of understanding experimental observations, not as a sequential list of facts to be memorized. Numerous exercises are provided that utilize Mathematica software to help students explore how the language of mathematics is used to describe physical phenomena. Students will obtain much more detailed information about covered topics and will also gain proficiency with Mathematica, a powerful tool with many potential uses in subsequent courses.

Pathways Through Applied and Computational Physics
This book is the collaborative effort of Nicolo Barbero, Matteo Delfino, Carlo Palmisano, and Gianfranco Zosi to illustrate the role that different branches of physics and mathematics play in the execution of actual experiments. The unique feature of the book is that all the subjects addressed are strictly interconnected within the context of the execution of a single experiment with very high accuracy, namely the redetermination of the Avogadro constant NA, one of the fundamental physical constants. Another essential feature is the focus on the role of Mathematica, an invaluable, fully integrated software environment for handling diverse scientific and technical computations.

Probability: An Introduction with Statistical Applications, second edition
Thoroughly updated, John J. Kinney’s second edition of this title features a comprehensive exploration of statistical data analysis as an application of probability. The new edition provides an introduction to statistics with accessible coverage of reliability, acceptance sampling, confidence intervals, hypothesis testing, and simple linear regression. Encouraging readers to develop a deeper intuitive understanding of probability, the author presents illustrative geometrical presentations and arguments without the need for rigorous mathematical proofs. This book features an appendix dedicated to the use of Mathematica and a companion website containing the referenced data sets.

Emily Suess <![CDATA[Sweet Tweets: Valentine’s Day Tweet-a-Program Challenge]]> http://blog.internal.wolfram.com/?p=23934 2015-02-05T21:43:48Z 2015-02-05T16:31:35Z As Valentine’s Day approaches, Wolfram is holding a Tweet-a-Program challenge. To help us celebrate the romantic holiday, tweet us your best Valentine-themed Wolfram Language code. As with our other challenges, we’ll pin, retweet, and share your submissions with our followers—and we’ll use the Wolfram Language to randomly select winning tweets, along with one or two of our favorites. If you’re a lucky winner, we’ll send you a Wolfram T-shirt!

Submissions aren’t limited to heart-themed programs, but check out these examples if you need a little inspiration:

Wolfram Tweet-a-Program ContourPlot3D

Wolfram Tweet-a-Program Cupid Curve

Wolfram Tweet-a-Program MatrixPlot

To win, tweet your submissions to @WolframTaP by 11:59pm PDT on Saturday, February 14. So that you don’t waste valuable code space, we don’t require a hashtag with your submissions. However, we do encourage you to share your code with your friends by retweeting your results with hashtag #WLSweetTweet.

Jenna Giuffrida <![CDATA[On Super Bowls and Puppy Bowls]]> http://blog.internal.wolfram.com/?p=23849 2015-02-02T22:27:48Z 2015-01-30T18:55:19Z This weekend marks the culmination of blood, sweat, and, oh yes, tears (Deflategate, anyone?) from months of struggle: Super Bowl XLIX.

For those of you who are interested, Wolfram|Alpha possesses a wealth of sports stats so that you can get all the cold, hard facts about the Patriots and the Seahawks.

Patriots vs. Seahawks

And if you can’t wait for Sunday to get your next football fix, or find yourself suffering withdrawal afterward, VICTIV is doing very cool things with the Wolfram Language to run a fantasy sports league. Earl Mitchell delves into the step-by-step process for new users on his blog, The Rotoquant.

But some of you are probably just plain old tired of all this “Deflatriots” business and of having your television occupied  by football games, news, talking heads, and commercials from September through February, because after a while, the teams start to blur together. Fortunately, with the help of the Wolfram Language, you can pick your team out of the crowd using this Graph of NFL logos we created by pulling the images from our Wolfram Knowledgebase and using Nearest to organize them by graphical similarity.

NFL logos arranged with Nearest by graphical similarity

If you’re one of those who are weary of all the football hoopla, then let us soothe your soul with a time-honored and longstanding tradition of cuteness: Animal Planet’s Puppy Bowl XI.

Puppy Bowl XI

With celebrities such as Katty Furry performing in the halftime show, it promises to be the most adorable sports game you’ll watch all year. The competition will be fierce, with 57 shelter-donated puppies—all up for adoption!—fighting for the honor to be the Bissel MVP (Most Valuable Puppy).

Past MVPs have included Max and Abigail, both Jack Russell Terriers, and the last MVP, Loren, was a Brittany, a breed not present in 2015′s lineup.

Jack Russell Terrier in Wolfram|Alpha

It’s not unlikely that one of the eight Labrador Retrievers will take home the prize for the first time ever. Again using the Wolfram Language, here’s the breakdown of Puppy Bowl breeds:

Breed representation in this year's Puppy Bowl

But who knows, one of those Beagles could come out of the end zone and snatch the victorious touchdown from right under their wet noses. Are you ready for some puppy ball?

The world's most famous Beagle, in the world's most famous computational knowledge engine

Wolfram Blog Team http:// <![CDATA[MHacks V’s Winning Hack Uses Wolfram Programming Cloud]]> http://blog.internal.wolfram.com/?p=23680 2015-01-22T21:44:39Z 2015-01-22T21:44:39Z Draw Anything, an iOS app designed and created by Olivia Walch and Matt Jacobs, was the winning hack at the recent MHacks V. Utilizing the power of Wolfram Programming Cloud, the two Draw Anything hackers came out on top after a fierce competition between more than 250 talented teams, made up of 1,200 hackers representing over 100 universities. Students from around the world came to learn, network, and “spend 36 hours building anything they can imagine.”

MHacks V winners: Draw Anything team, Olivia Walch and Matt Jacobs

This hackathon was a race to the finish, requiring all the creativity and innovation each team could muster. “Using Wolfram was a no-brainer,” said Walch. “We needed a fast way to do computations off the device, and the Wolfram Language had so much of the functionality we needed built in already: from image processing to computing Fourier coefficients. Making the app in 36 hours would not have been possible without it!”

According to the MHacks project profile, with the use of the Wolfram Language and Wolfram Programming Cloud, “our fabulous new iOS App takes any input image, converts it into a line drawing, and computes its Fourier series expansion using the fast Fourier transform (FFT). Draw Anything creates step by step drawing guides by truncating the Fourier series at different lengths.”
Draw Anything tutorial, showing images created without human guidance

“We ran our computations in the Wolfram Programming Cloud so that they would run quickly and efficiently, and so that the user would not experience any slow down in their device,” said Jacobs. “I am relatively new to programming, but it was incredibly easy for me to pick up the language and use it. I’m really looking forward to coming up with new projects to code in the Wolfram language!”

The designers also included a shout-out on their home page to Wolfram’s Michael Trott for his blog post that inspired the creation of the Draw Anything app.

At MHacks V, which was hosted by the University of Michigan and in part sponsored by Wolfram Research, teams of up to 4 members completed submissions that were judged on the usefulness, originality, technical difficulty, and design of their hacks. Including the winning hack, a total of 14 teams worked on projects involving Wolfram technologies.

The creator of one of those, WolfWeather, had this to say about using Wolfram tech: “…the language itself is something out of a science fiction movie being able to perform one hundred lines of code in two or three lines of code. I wanted to do something simple and fun, so I created WolfWeather. Its goals are straightforward: it gives users current weather updates, the time, date, weekday, the Zodiac year, and their GPS location. It also promotes the Wolfram Language and shows off a bit of the sheer power the language has as a knowledge base.”

The Michigan Daily‘s article on the event includes a brief interview with Jacobs and Walch, who revealed that they plan to continue developing Draw Anything and will be attending future hackathons, including TreeHacks and Seoul Global Hackathon.

Congratulations to team Draw Anything and all participants, and thank you, MHacks, for another unforgettable hackathon!

Got a hackathon coming up? Contact Wolfram to request our participation, or check out the resources on our hackathon page.

Oleksandr Pavlyk http:// <![CDATA[Jacob Bernoulli’s Legacy in Mathematica]]> http://blog.internal.wolfram.com/?p=23585 2015-01-15T18:56:42Z 2015-01-15T18:53:41Z January 16, 2015, marks the 360th birthday anniversary of Jacob Bernoulli (also James, or Jacques).

Input 1 through Output 4

Jacob Bernoulli was the first mathematician in the Bernoulli family, which produced many notable mathematicians of the seventeenth and eighteenth centuries.

Jacob Bernoulli’s mathematical legacy is rich. He introduced Bernoulli numbers, solved the Bernoulli differential equation, studied the Bernoulli trials process, proved the Bernoulli inequality, discovered the number e, and demonstrated the weak law of large numbers (Bernoulli’s theorem).

Jacob Bernoulli's mathematical achievements

Bernoulli’s treatise Ars Conjectandi (i.e. The Art of Conjecturing) was posthumously published in 1713, eight years after his demise, and was written in Latin, science’s lingua franca of the time. It is considered a seminal work of mathematical probability. Its importance is witnessed, in part, by its translations to French by G. Le Roy in 1801, and, recently, to English by E. D. Sylla in 2005.

The Art of Conjecturing comprises four parts. The first part reproduces Christiaan Huygens’ De Ratiociniis in Ludo Aleae (On Reasoning in Games of Chance), with extensive commentary from Bernoulli and detailed solutions of Huygens’ five problems, posed at the end of Huygens’ work with answers, but without derivations. In the first part, Bernoulli also derives the probability that at least m successes will occur in n independent trials with success probability of p:

m successes will occur in n independent trials with success probability of p

The second part, “The Doctrine of Permutations and Combinations,” is devoted to combinatorics and to the study of figurate numbers, i.e. numbers that can be represented by a regular geometrical arrangement of equally spaced points:

Geometrical arrangement of equally spaced points

It is here that Bernoulli introduces Bernoulli numbers. He starts by noting the identity among binomial coefficients nr, namely that Equation.

Bernoulli knew that for a fixed m, binomial coefficient n-1 m is a polynomial in n, namely
nr-1 . This identity allows him to solve for Equation. He gives a table of results for 0≤m≤10.

To reproduce Bernoulli’s table, define a function to construct equations for the sum of powers:

Binomial equation

Solving for the sum of powers:

Sum of powers

Bernoulli writes, “[W]hoever has examined attentively the law of the progression herein can continue the Table further without these digressions for computations” by making the following educated guess:


He notes that coefficients Br+1 do not depend on n, and can be computed recursively by substituting n==1 into the above equation.

Computed recursively

These coefficients are the celebrated Bernoulli numbers, which have found their way into many areas of mathematics [e.g. see mathoverflow.net].

Bernoulli numbers

In the second part of his book, Bernoulli counts the number of permutations, the number of permutations in sets with repetitions, the number of choosing objects from a set, etc., which he later applies to compute probabilities as the ratio of the number of configurations of interest to the total number of configurations.

In part three, Bernoulli applies results from the first two chapters to solve 24 problems related to games of chance. A recurring theme in these problems is a sequence of independent 0 or 1 outcomes, which bears the name of Bernoulli trial, or Bernoulli process. I thought Jacob Bernoulli’s birthday anniversary to be an apt occasion to explore his problems with Mathematica.

For example, problem 9 asks to find the expected payout in a three-player game. Players alternately draw cards without replacement from a pack of twenty cards, of which ten are face cards. When the cards are exhausted, winnings are divided among all those who hold the highest number of face cards.

With c1, c2, and c3 denoting the number of face cards each player has, the payout of the first player is:


After the pack of twenty has been distributed, the first and the second players each receive seven cards, but the third one only receives six. The tally vector of face cards received by each player follows MultivariateHypergeometricDistribution:


This and other problems are stated and solved in the accompanying Mathematica notebook.

The concluding part four of Ars Conjectandi discusses uses of probability in civil, moral, and economic matters. Here Bernoulli argues that the probability reflects our incomplete knowledge of the state of the world, and unlike in a game of chance, where probabilities can be determined by finding the proportion that configurations of interest take in the whole set of possible configurations, the probabilities here cannot be a priori established. Bernoulli argues that these unknown probabilities can be inferred from past outcomes.

He proves the weak law of large numbers, asserting that the observed frequency of successes in n independent trials where the probability of success equals p will converge to p as the number of trials grows. Thus, you can estimate p arbitrarily accurately by running a sufficient number of trials. Specifically, for any δ and ε, there exists a large enough sample size n that:


The demonstration “Simulated Coin Tossing Experiments and the Law of Large Numbers” by Ian McLeod, among others, explores this convergence.

Download this post as a Computable Document Format (CDF).
Download Bernoulli problems as a Mathematica notebook.

Mikael Forsgren <![CDATA[Can AstraZeneca Count on Your Liver? A Mathematical Modeling Approach to Monitoring Liver Function in Drug Trials]]> http://blog.internal.wolfram.com/?p=21624 2015-01-06T15:45:24Z 2015-01-06T15:45:24Z Mathematical modeling is not just used for understanding and designing new products and drugs; modeling can also be used in health care, and in the future, your doctor might examine your liver with a mathematical model just like the one researchers at AstraZeneca have developed.


The liver is a vital organ, and currently there isn’t really a way to compensate for loss of liver function in the long term. The liver performs a wide range of functions, including detoxification, protein synthesis, and secretion of compounds necessary for digestion, just to mention a few. In the US and Europe, up to 15 % of all acute liver failure cases are due to drug-induced liver injury, and the risk of injuring the liver is of major concern in testing new drug candidates. So in order to safely monitor the impact of a new drug candidate on the liver, researchers at the pharmaceutical company AstraZeneca have recently published a method for evaluating liver function that combines magnetic resonance imaging (MRI) and mathematical modeling—potentially allowing for early identification of any reduced liver function in humans.

Last year, Wolfram MathCore and AstraZeneca worked together on a project where we investigated some modifications of AstraZeneca’s modeling framework. We presented the promising results at the ISMRM-ESMRMB Joint Annual Meeting, which is the major international magnetic resonance conference. In this blog post, I’ll show how the Wolfram Language was used to calculate liver function and how more complex models of liver function can be implemented in Wolfram SystemModeler.

A quick introduction to the method

You might be wondering what happens within the liver during the examination using a mathematical model. It all starts after the injection of the MRI contrast agent into the blood, where it spreads and ultimately reaches the liver. Inside the liver (see the figure below) the blood vessel walls are highly permeable, like a coffee filter, allowing for a rapid diffusion of the agent into the extracellular space. The MRI contrast agent accumulates in the liver cells, and finally is excreted into the bile. The accumulation and efflux require that the cells are healthy, have enough energy, and are not overloaded with other work. If the cells are compromised, the transfer rates of the agent will be reduced. A reduced liver function can thus be observed by the calculated transfer rates in the model.

Contrast steps

Okay, now that you have some background on the basics of how liver function can be estimated, let’s move on to the fun part of computation and modeling. I will start by showing examples of the types of data we use.

Extracting data

Data is extracted from the images in regions of interest (ROIs) within the liver as well as the spleen. The latter is used as a good and stable surrogate for measuring the amount of contrast agent within the blood directly, since the splenic cells do not accumulate any contrast agent; this means that our measurement in the spleen is only influenced by the contrast agent in the spleen’s blood vessels. The ROIs can be of any geometry. In Mathematica, I draw and modify ROIs with a custom interactive interface. Of course, you could also select the entire liver or other distinct parts in images using some of the automated algorithms implemented in Mathematica.

Below is an example of the types of images that we used. These two images were acquired about five minutes after the injection of the contrast agent, which is the reason that the liver is so bright (compared to, for instance, the muscles that can be seen on the sides of the images). The images are captured on a coronal imaging plane, which means that the images are what you see when the subject is lying down on its back and you are looking down on the subject from above. Images a) and b) are at different heights from the table, where b) is further from the table; there you can also see a portion of the spleen.

Kidneys, liver, spleen, stomach

If you are familiar with human anatomy, especially in medical imaging, you might have noticed that the images don’t look like the inside of a human. Well, in that case you are right: the images show the inside of a rat, which were the subjects used in the study performed at AstraZeneca.


AstraZeneca has gathered quite a lot of high-quality data on its rats, using the approach mentioned above, and I will not show all of that here. Rather, I’ve been inspired by early TV chefs and prepared some of the data. I will now exemplify the method with three subjects; i) a rat with normal liver function, ii) a rat with slightly reduced liver function, and iii) a rat with severely reduced liver function. The data covers 60 minutes, where the first four minutes are baseline (prior to the injection of the contrast agent) and used for the post-processing, so those values should by definition be equal to zero.

As I mentioned previously, data is extracted from the image series in two different regions. One of these two regions is the liver, and after some post-processing of this data, we get the mean contrast agent concentrations within the liver cells (I will name this data set cHep in the code from here on). You can see what these concentration time series look like for all three subjects in the figure below. I will use this data for model fitting.

Concentration time series for three subjects

The second region from which data is extracted is the spleen, and after the post-processing, we get the mean contrast agent concentration within the extracellular space (I will name this data set cES). This data tells us how much contrast agent is available for accumulation in the liver, and it will be used as input in the models. You can see what this data looks like for all three subjects in the figure below.

Normal, slightly reduced, and severely reduced function

In order to use the measured extracellular concentrations (cES) in the model, the values need to be continuous. So let’s go ahead and generate an interpolating function (intES) based on these values, one for each case. Since there is no contrast in the first four minutes, by experimental design, we set these to zero.

Set to zero

And we can check the agreement with the data. Here I’ll just show the normal case, remembering that we just set the first four points to be identical to zero.

Normal case

Defining the model

The model is defined using an ordinary differential equation, where we solve for the concentration of the contrast agent within the liver cells. The uptake comes from the extracellular space (step 3 in the figure) governed by the kinetic parameter k1. The transfer of this agent into the bile is described using Michaelis–Menten kinetics (step 4 in the figure) using the kinetic parameters Vmax and Km.

Kinetic parameters Subscript[V, max] and Subscript[K, m]

With the initial condition for the concentration in liver cells being:

Initial condition for the concentration in liver cell

In the project, a simplification of the above model was investigated, specifically, the efflux into the bile was described with a linear rate equation. Since Michaelis–Menten kinetics are approximately linear in low substrate concentrations, this simplification can be valid if the concentrations of the contrast agent are low enough.

Depending on kinetic parameters

Now it’s time to solve the two models using ParametricNDSolve. Since we have the interpolating functions (intES), specific for each subject, inside the model, we need to compute a solution specifically for each subject:

Need to compute a solution specifically for each subject

Fitting the model to data

In order to fit the model to the data, we need a target or objective function to guide our optimization algorithm in the correct direction. In this case, I’ve used the Euclidean norm, as a measure of the goodness of model fit:

Euclidean norm

Whenever I use a global optimization algorithm for estimating the parameters, which takes a fair bit of time to complete, I like to see where the algorithm is moving in the parameter space. This way, I can see if it struggles, or maybe it finds a local minima it can’t get out of, or anything else that might be fun and educational to observe. For this purpose, the monitor functionalities are suitable:

Monitor functionalities

In order to improve the optimization, we also include a list of reasonable parameter boundaries and start guesses, which covers a wide range of scenarios.

List of reasonable parameter boundaries and start guesses

Completing the code for the above Block, with the necessary inputs to NMinimize, we get the following compact piece of code that helps us with the answer to: How good is the liver function?

Compact piece of code that helps us with the answer to: How good is the liver function?

If you’re interested in the different global optimization schemes available in Mathematica, there is great tutorial available here.

Once again the TV-chef magic kicks in, and we have a bunch of optimal parameter values already prepared for the three subjects (for both models):

Optimal parameter values already prepared

And we combine the parameter values with the parametric solutions of the models to calculate the model predictions for both models and all three cases:

Combine the parameter values with the parametric solutions of the models


Below you can see the predictions made by the model with the fitted parameter values compared to the data on our three subjects, ranging from normal to severely reduced liver function.

As you can see in the figures, there is a clearly reduced concentration of contrast in the last case. This reduction can be appreciated quantitatively in the table underneath the figures, where the uptake rate is almost a factor 20 lower in the last case. It’s noteworthy that the uptake rate is in all practical aspects identical for both model variants in the three cases, indicating that the use of a linear description of the efflux of contrast into the bile instead of Michaelis–Menten kinetics might be valid. Also, the models are able to predict the data very well; of course, you wouldn’t be reading this unless that was the case (data from humans is much noisier, for various reasons).

In the animation below, I’ve correlated the model predictions for the rat with normal liver function with the acquired images, so that you might better appreciate how the numbers relate to the images. As you might remember from the beginning of this post, the liver is the large organ at the top of the images.

On the horizon

In the original paper by Jose Ulloa et al., where the first model and the data come from, the model parameters were able to separate between the different groups with strong significance. In this project we found that the uptake rate was in practice identical for both model variants, and that the simplified model was also good at separating between the different groups.

These methods that AstraZeneca has developed were evaluated on rats, and the work continues at AstraZeneca, and in other pharmaceutical companies, on refining and ultimately utilizing these methods for investigating liver function in pre-clinical and clinical trials, as well as in the clinic. We are all very excited about these results, and as you read this, both AstraZeneca and Wolfram MathCore are involved in new projects dedicated to evaluating these methodologies further, even applying them to patients suffering from liver disease.

Modeling liver function in Wolfram SystemModeler

In the above calculations, I used Mathematica exclusively; however these models can just as easily be implemented in SystemModeler by using the BioChem library, as shown by the figure below. In this particular case, the model contains so few states that the model implementation is just as fast programmatically in Mathematica, but if this were a larger or hierarchical model, SystemModeler would be my first choice. It’s also worth noting that it if I had implemented the model using SystemModeler, the code for fitting the parameters would have been basically the same. In principle, I would only need to modify to the target function.

BioChem library

Wolfram MathCore collaborates with researchers at Linköping University’s Center for Medical Image Science and Visualization (CMIV) on research aimed toward a comprehensive non-invasive diagnostic MRI-based toolset for patients suffering from liver disease. The collaboration has for example led to the development of a mathematical model for estimating liver function in humans based on, in principle, the same kind of MRI data we have shown in this post. The underlying assumptions for this model and the one used above are very similar. The figure below shows this model implemented in SystemModeler using the BioChem library, and more details on this model can be found on our example pages.

Model implemented in BioChem library

If you want to try the tools I’ve used for yourself, you can get a trial of both Mathematica and SystemModeler and get cracking.

At Wolfram MathCore, we have done numerous consultancy projects for a wide range of customers, from machine dynamics and 3D mechanical systems to thermodynamics and, of course, life science. The results from another life science project we worked on together with MedImmune (a subsidiary of AstraZeneca) were recently published in a daughter journal of Nature. So, if you need to solve tricky problems or want to get your modeling and simulation project up and running quickly with our tools, don’t hesitate to contact us at Wolfram MathCore!

Tom Sherlock <![CDATA[Serial Interface Control of Astronomical Telescopes]]> http://blog.internal.wolfram.com/?p=23425 2014-12-29T16:01:04Z 2014-12-29T16:01:04Z As an amateur astronomer, I’m always interested in ways to use Mathematica in my hobby. In earlier blog posts, I’ve written about how Mathematica can be used to process and improve images taken of planets and nebulae. However, I’d like to be able to control my astronomical hardware directly with the Wolfram Language.

In particular, I’ve been curious about using the Wolfram Language as a way to drive my telescope mount, for the purpose of automating an observing session. There is precedent for this because some amateurs use their computerized telescopes to hunt down transient phenomena like supernovas. Software already exists for performing many of the tasks that astronomers engage in—locating objects, managing data, and performing image processing. However, it would be quite cool to automate all the different tasks associated with an observing session from one notebook.

Mathematica is highly useful because it can perform many of these operations in a unified manner. For example, Mathematica incorporates a vast amount of useful astronomical data, including the celestial coordinates of hundreds of thousands of stars, nebula, galaxies, asteroids, and planets. In addition to this, Mathematica‘s image processing and data handling functionality are extremely useful when processing astronomical data.

Previously I’ve done some work interfacing with telescope mounts using an existing library of functions called ASCOM. Although ASCOM is powerful and can drive many devices associated with astronomy, like domes and filter wheels, it is limited because it only works on PCs and needs to be pre-installed on your computer. I wanted to be able to drive my telescope directly from Mathematica running on any platform, and without any special set up.

Telescope Serial Communication Protocols

I did some research and determined that many telescope mounts obey one of two serial protocols for their control: the Meade LX200 protocol and the Celestron NexStar protocol.

The LX200 protocol is used by Meade telescopes like the LX200 series as well as the ETX series. The LX200 protocol is also used by many non-Meade telescope mounts, like those produced by Losmandy and Astro-Physics.

The NexStar protocol is used by Celestron telescopes and mounts as well as those manufactured by its parent company, Synta, including the Orion Atlas/Sirius family of computerized mounts.

The full details of these protocols can be found in the Meade Telescope Serial Command Protocol PDF and the NexStar Communication Protocol PDF.

A notable exception is the Paramount series of telescope mounts from Software Bisque, which use the RTS2 (Remote Telescope System) protocol for remote control of robotic observatories. The RTS2 standard describes communication across a TCP/IP link and isn’t serial-port based. Support for RTS2 will have to be a future project.

Since Mathematica 10 has added direct serial-port support, it’s possible to implement these protocols directly in top-level Wolfram Language code and have the same code drive different mounts from Mathematica running on different platforms, including Linux, Mac, Windows, and Raspberry Pi.

Example: Slewing the Scope

Here’s an example of opening a connection to a telescope mount obeying the LX200 protocol, setting the target and then slewing to that target.

Open the serial port (“/dev/ttyUSB0″) connected to the telescope:


First we need a simple utility for issuing a command, waiting for a given amount of time (usually a few seconds), and then reading off the single-character response.


These are functions for setting the target right ascension and declination in the LX200 protocol. Here, the right ascension (RA) is specified by a string in the form of HH:MM:SS, and the declination (Dec) by a string in the form of DD:MM:SS.


Now that we have the basics out of the way, in order to slew to a target at coordinates specified by RA and Dec strings, setting the target and then issuing the slew command are combined.


We can also pass in real values as the coordinates, and then convert them to correctly formatted strings for the above function.

ScopeSlewToRADecPrecise cont'd

Now we can point the scope to the great globular cluster in Hercules (Messier 13). Here is an image:

Point the scope

Slew the scope to the Ring Nebula (Messier 57):

Slew the scope

And slew the scope to Saturn:

Slew scope to Saturn

When the observing session is complete, we can close down the serial connection to the scope.


Please be aware that before trying this on your own scope, you should have limits set up with the mount so that the scope doesn’t accidentally crash into things when slewing around. And of course, no astronomical telescope should be operated during the daytime without a proper solar filter in place.

The previous example works with Mathematica 10 on all supported platforms. The only thing that needs to change is the name of the serial port. For example, on a Windows machine, the port may be called “COM8″ or such.

Telescope Control with Raspberry Pi

One interesting platform for telescope control is the Raspberry Pi. This is an inexpensive ($25–$35), low-power-consumption, credit-card-sized computer that runs Linux and is tailor-made for all manner of hackery. Best of all, it comes with a free copy of Mathematica included with the operating system.

Raspberry Pi

Since the Pi is just a Linux box, the Wolfram Language code for serial-port telescope control works on that too. In fact, since the Pi can easily be wirelessly networked, it is possible to connect to it from inside my house, thus solving the number one problem faced by amateur astronomers, namely, how to keep warm when it’s cold outside.

The Pi doesn’t have any direct RS-232 ports in hardware, but an inexpensive USB-to-serial adapter provides a plug-n-play port at /dev/ttyUSB0. In this picture, you can see the small wireless network adapter in the USB socket next to the much larger, blue usb-to-serial adapter.

Serial cable connection

The serial cable connects to the serial port on my computerized equatorial mount. The mount I used was a Losmandy G11, however, any other mount could be used as long as it has a serial input port and obeys the LX200 protocol.


Astrophotography with the Pi

Once I had the Pi controlling the telescope, I wondered if I could use it to take pictures through the scope as well. The Raspberry Pi has an inexpensive camera available for $25, which can take reasonably high-resolution images with a wide variety of exposures.

Raspberry Pi camera

This isn’t as good as a dedicated astronomical camera, because it lacks the active cooling needed to take low-noise images of deep sky objects, but it would be appropriate for capturing images of bright objects like planets, the Moon, or (with proper filtering) the Sun.

It was fairly easy to find the mechanical dimensions of the camera board on the internet, design a telescope adapter…

Mechanical dimensions

…and then build the adapter using my lathe and a few pennies worth of acetal resin (Dupont Delrin®) I had in my scrap box. The normal lens on the Pi camera was unscrewed and removed to expose the CCD chip directly because the telescope itself forms the image.

Build the adapter

Note that this is a pretty fancy adapter, and one nearly as good could have been made out of 1 1/4 plumbing parts or an old film canister; this is a place where many people have exercised considerable ingenuity. I bolted the adapter to the side of the Pi case using some 2-56 screws and insulating stand-offs cut from old spray-bottle tubing.

Bolted the adapter

This is how the PiCam looks plugged into the eyepiece port on the back of my telescope, and also plugged into the serial port of my telescope’s mount. In this picture, the PiCam is the transparent plastic box at the center. The other camera with the gray cable at the top is the guiding camera I use when taking long-exposure astrophotographs.

PiCam plugged into eyepiece port

Remotely Connecting to the PiCam

The Pi is a Linux box, and it can run vncserver to export its desktop. You can then run a vnc client package, like the free TightVNC, on any other computer that is networked to the Pi. This is a screen shot taken from my Windows PC of the TightVNC application displaying the PiCam’s desktop. Here, the PiCam is running Mathematica and has imported a shot of the Moon’s limb from the camera module attached to the telescope via the adapter described above.

Imported shot

It’s hard to read in the above screen shot, but here is the line I used to import the image from the Pi’s camera module directly into Mathematica:


This command invokes the Pi’s raspistill camera utility and captures a 1024 x 1024 image exposed at 1,000 microseconds after a 10-second delay, and then brings the resulting JPEG file into Mathematica.

One problem that I haven’t solved is how to easily focus the telescope remotely, because the PiCam’s preview image doesn’t work over the vnc connection. One interesting possibility would be to have Mathematica take a series of exposures while changing the focus via a servo attached to the focus knob of the telescope. An even better solution would be to have Mathematica use image processing functions to determine when the best focus has been achieved.