This notebook is a tutorial on using the KnotTheory` package to retrieve data from third party sources. As an example, I'm using Marc Culler's table of A-polynomials, available online at http://www.math.uic.edu/~culler/Apolynomials/index.html

To get started, you'll need to download a copy of the KnotTheory` package, from http://katlas.math.toronto.edu/
Once you've got that, and unzipped it, modify the path in the following line, and load KnotTheory`

\(\(KnotTheoryPath\  = \ "c:\scott\projects\svn-checkouts\KnotTheory\trunk\";\)\)
AppendTo[$Path,  KnotTheoryPath];
<<  KnotTheory`

Quickly verify that it's working:

HOMFLYPT[Knot[8, 19]][a, z]

Now verify that the KnotTheory` package can successfully retrieve data from online:

RetrieveInvariants[{"Khovanov s-Invariant"}, 
        AllKnots[{3, 6}], "KnotInfo"] // TableForm

Okay, now we can get started. We're going to need to tell KnotTheory` where to find the knot invariants online, and then how to parse the data it finds there. We construct the URL for a given knot, first writing a function which translates KnotTheory`'s syntax for knots into Culler's notation.

CullerKnotName[Knot[n_, k_]] := "L1" <> 
        If[n < 10, "0", ""] <> ToString[n] <> 
        If[k < 100, "0", ""] <> If[k < 10, "0", ""] <> 
        ToString[k]

CullerKnotName[Knot[5, 1]]

CullerKnotName[Knot[10, 128]]

With this, it's easy to construct the URL. We need to override the function "KnotInvariantURL". It takes two arguments, a string naming the invariant (in this case, I've chosen "A-polynomial"), and the knot.

KnotInvariantURL["A-polynomial", 
        K_] := "http://www.math.uic.edu/~culler/Apolynomials/apolys/" <> 
        CullerKnotName[K] <> ".apoly"

Let's not bother doing any parsing at first, and just see what KnotTheory` can do. We use the function RetrieveInvariants, with third argument "url".

RetrieveInvariants[{"A-polynomial"}, AllKnots[5], "url"]  // 
      TableForm

We need to strip off the initial and final parts of the string.  To do this, we override the function ParseKnotInvariantFromURL. A little experimentation indicates we should use:

ParseKnotInvariantFromURL["A-polynomial",  K_,  data_] := 
      StringTake[data, {14, -3}]

RetrieveInvariants[{"A-polynomial"}, AllKnots[5], "url"] // 
      TableForm

This isn't quite right, however; ParseKnotInvariantFromURL should return a Mathematica expression, not just a string. We need to wrap the result with a call to ToExpression.

(ToExpression[string, StandardForm] does a reasonable attempt at interpreting polynomials. Often you may want to use ToExpression[string, TeXForm] for fancier input.)

ParseKnotInvariantFromURL["A-polynomial",  K_,  data_] := 
      ToExpression[StringTake[data, {14, -3}], StandardForm]

RetrieveInvariants[{"A-polynomial"}, AllKnots[5], "url"]  // 
      TableForm

Not bad! Let's just make some slight improvements, for safety's sake. The first change below ensures that we don't try anything silly if we can't work out the Culler notation for the given knot. The second change does some basic checking that the downloaded data looks like what we expect.

\(KnotInvariantURL["A-polynomial", K_] := 
      Module[{knotname = 
            CullerKnotName[K]}, 
If[\(! MatchQ[
              knotname, \ _String]\), 
          Return[$Failed]]; "http://www.math.uic.edu/~culler/Apolynomials/apolys/" <> 
          knotname <> ".apoly"
]\)

\(ParseKnotInvariantFromURL["A-polynomial", \ K_, \ data_String] := 
      Module[{results}, 
results = 
          StringCases[
            data, "A_" ~~ \(CullerKnotName[
                    K] ~~ \(" :=
" ~~ \(polynomial__ ~~ ";
"\)\)\) :> polynomial]; 
If[Length[results] == 0, 
          Return[$Failed]]; 
ToExpression[
          results[[1]], 
          StandardForm]
]\)

Now let's extract all the data Culler has for the Rolfsen table, storing it in the variable "apolynomialdata".

(apolynomialdata = 
          RetrieveInvariants[{"A-polynomial"}, 
            AllKnots[{3, 10}], "url"])  // TableForm

Okay, that's all very well. What can we do with it? Best of all is to write the data into the KnotAtlas. This again requires a few steps, but shouldn't be too hard. First, we should go to the KnotAtlas wiki, and tell it a little about the new invariant we're adding. Go to http://katlas.math.toronto.edu/wiki/Invariant_Definition_Table and have a quick look. If you're following this tutorial, you can ignore the big warning message :-).

Click edit, scroll down, and add a new row to the table; something like

<tr>
<!-- Invariant name --> <td>A-polynomial</td>
<!-- WikiPage =            --> <td>A-polynomial</td>
</tr>

That's all! Save your changes, and tell the KnotTheory` package to reload the table.

LoadInvariantRules["Invariant_Definition_Table"];

To write data to the KnotAtlas, you need to be logged in. If you haven't already created an account there, please do so now. If you're going to upload lots of data, you might like to create a 'robot' account, like mine, "ScottDataRobot". This lets us keep track of changes more easily.

\(CreateWikiConnection[
"http://katlas.math.toronto.edu/w/index.php", 
"ScottDataRobot", 
InputString["Enter Your Password"]
]\)

You can check that the login worked properly using "WikiUserName[]".

WikiUserName[]

Now upload all the data. StoreInvariants returns a list of failures, so if it returns an empty list, all is well.

StoreInvariants[apolynomialdata, "KnotAtlas"]

RetrieveInvariants[{"A-polynomial"}, 
      AllKnots[{3, 9}], "KnotAtlas"]