Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A friend of mine made a tattoo on his chest of some chemical substance formula.

He challenged us to identify which substance is that. I thought that perhaps a combination of:

  • Mathematica image processing capabilities
  • Wolfram|Alpha chemical data
  • Manual intervention

could do the trick. Here is the picture:

enter image description here

I am looking for pointers on how to attack the problem (like keywords of Mathematica functions, etc).

EDIT 1: I suppose the following need to be done:

  • Extract the formula from the picture as basic (2D) structure diagram
  • Define a distance metric for images or use existing one (e.g., ImageDistance[])
  • Calculate the distance of our element's diagram against all ChemicalData[] elements
  • Pick the few first and do a visual verification

At the moment 1st step seems like the most challenging.

EDIT 2: As shown by Sjoerd it is much easier to search for a formula as string rather than as image!

share|improve this question
Anyone know a quick way to import all chemical data? I ran ChemicalData/@ChemicalData[] but it connects to wolfram servers once for each entry, so some 40k times. Had to quit the kernel to get it to stop. (v8) – ssch Sep 4 '13 at 18:38
@ssch Perhaps this is useful?… – Zet Sep 4 '13 at 18:41
I don't want to be a party pooper, so here's the spoiler alert. – István Zachar Sep 4 '13 at 19:29
All I can say is drat - why was I giving a lecture when this question showed up?!?!? – bobthechemist Sep 5 '13 at 1:00
up vote 26 down vote accepted

Preload all chemical data:

ChemicalData[All, "Preload"];
RebuildPacletData[]; (* the latter should not really be necessary *)

Get all names:

cd = ChemicalData[];

Get their molecular formulae:

l = ChemicalData[#, "MolecularFormulaString"] & /@ cd;

By counting the Cs, Os and Hs in the tattooed diagram we know we have to find $\rm{C_{19}H_{28}O_{2}}$. Looking for all molecules that have that molecular formula:

p = Position[l, "C19H28O2"];

Column[Labeled[ChemicalData[#, "StructureDiagram"], #] & /@ Extract[cd, p]]

Mathematica graphics

Looks like it is Testosterone.


In 10.4 one can call various external services dealing with chemical and/or pharmaceutical data, such as PubChem, ChemSpider and OpenPHACTS. If one knows how to code the chemical diagram into a SMILES string this gives an alternative approach to identifying this compound:

pubchem = ServiceConnect["PubChem"];

][1, "Synonym"] //Normal // Column

Mathematica graphics

Among all the synonyms we see testosterone pop up.

share|improve this answer
Nicely done Sjoerd! Searching for a formula as string is much easier than as image! – Zet Sep 4 '13 at 20:38
@zet My pleasure. It took me a bit longer than necessary because I initially miscounted the number of Hs. – Sjoerd C. de Vries Sep 4 '13 at 20:48
Nice solution, +1! It also thins down results if you have some additional chemistry knowledge, like the molecule has sterol structure (name should include "ster"), an alcohol (with a hydroxyl group -OH), with a carbonyl group at the other end (-C=0), i.e. it is also a ketone (name should end with "-one"). The friend's gender helps to figure out the "testo-" part... – István Zachar Sep 5 '13 at 9:51
I will leave the question open for a couple of days and then I'll accept your answer @SjoerdC.deVries. Thanks again. – Zet Sep 5 '13 at 16:06
@zet no problem. – Sjoerd C. de Vries Sep 5 '13 at 20:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.