0
$\begingroup$

My Medical Researchers very much prefer me to use TraditionalForm for symbols and algorithms. However, using TraditionalForm on left hand side of an assignment will introduce run-time headaches if I forget to shadow the built-in function names. Or there is the other headache of "loosing" the visual TraditionalForm when consolidating code into Paclet .wl files where I have to convert from TraditionalForm into "Input Text" form.

Is the Notation Paclet the solution? How would the TraditionalForm generators in this sample be converted using Notation`?

enter image description here

(the above is also posted on community.wolfram.com)

$\endgroup$
0

1 Answer 1

0
$\begingroup$

I was only able to get a partial solution to your problem. I encountered an obstacle which I wasn't able to get past. I decided to post it anyway to point out where the difficulty lies. Perhaps you or someone else can find a workaround.

Load the notation package.

In[1]:= << Notation`

Perform some initializations for the data you're processing.

In[2]:= dataset = 
  Dataset[{<|"Name" -> "Alice", "Age" -> 25, "Country" -> "USA"|>, <|
     "Name" -> "Bob", "Age" -> 30, "Country" -> "Canada"|>, <|
     "Name" -> "Charlie", "Age" -> 35, "Country" -> "UK"|>}];
In[3]:= columnNames = DeleteDuplicates@Flatten@Keys@dataset;
n = Length@dataset;

For convenience, I'm going to define some functions to implement the expressions you want to compute. Your users won't see these, since they'll interact with the code via the notations. For the same reason, there's no need to typeset the computations, though you can if you want to. You can also just inline the code on the rhs of the notations if desired.

In[4]:= generateAccessors[
   i_] := (Subscript[(ToExpression@#), i] := dataset[i, #]) & /@ 
   columnNames;

By the way, I think you have the indices swapped in your code; 'i' goes below, 'n' goes above.

In[5]:= computeMean[k_] := Sum[Subscript[k, i], {i, n}]/n;
In[6]:= computeSigma[k_] := 
  With[{m = computeMean[k]}, Sqrt[Sum[(k - m)^2, {i, n}]/n]];

When you load the Notation package, a palette should appear on your screen. You need to use it to enter notations because what you see isn't what you get. There are things like TemplateBox etc. which the package uses, so trying to type things in by hand is unlikely to work unless you know exactly what the package does behind the scenes. Choose the second button on the palette. You want the display form to map to an internal form, but not vice-versa, so use the choice with "==>".

When you create a notation, you want pattern variables on the lhs to map into the same pattern variables on the rhs. If they don't match, you'll get an error message.

You didn't indicate any way for users to call your accessor. I decided to just use "script A", with a subscript i_. When the user calls this, the notation I define then plugs the value entered into the 'i_' into the generateAccessors function.

In[7]:= Notation[
 NotationTemplateTag[
  Subscript[\[ScriptCapitalA], i_]] \[DoubleLongRightArrow] 
  NotationTemplateTag[generateAccessors[i_]]]

Test it:

In[16]:= Table[Subscript[\[ScriptCapitalA], i], {i, n}];

In[17]:= Subscript[Age, 1]
(*Out[17]= 25 *)

In[18]:= Subscript[Name, 1]
(*Out[18]= "Alice"*)
 
In[19]:= Subscript[Age, 3]
(*Out[19]= 35*)

Now set up the notation for the mean.

In[13]:= Notation[NotationTemplateTag[
\!\(\*OverscriptBox[\(k_\), \(_\)]\)] \[DoubleLongRightArrow] 
  NotationTemplateTag[computeMean[k_]]]

Test it:

In[20]:= 
\!\(\*OverscriptBox[\(Age\), \(_\)]\)
(*Out[20]= 30*)

In[21]:= 
\!\(\*OverscriptBox[\(Name\), \(_\)]\)
(*Out[21]= 1/3 ("Alice" + "Bob" + "Charlie")*)

Now set up the notation for sigma.

In[22]:= Notation[
 NotationTemplateTag[Subscript[\[Sigma], k_]] \[DoubleLongRightArrow] 
  NotationTemplateTag[computeSigma[k_]]]

This is where I ran into a problem.

In[23]:= Subscript[\[Sigma], Age]
(*Out[23]= Sqrt[(-30 + Age)^2]*)
 
Subscript[\[Sigma], 
\!\(\*OverscriptBox[\(Age\), \(_\)]\)]
(*Out[26]= Sqrt[(30 + 
  1/3 (-Subscript[30, 1] - Subscript[30, 2] - Subscript[30, 3]))^2]*)

In[25]:= computeSigma[
\!\(\*OverscriptBox[\(Age\), \(_\)]\)]
(*Out[25]= Sqrt[(30 + 
      1/3 (-Subscript[30, 1] - Subscript[30, 2] - Subscript[30, 3]))^2]*)

The problem is that although the pattern "k_" and the pattern "Overscript[k_, _]" look similar, they have different box representations. (Use "Show Expression" in the cell menu or Shift+Ctrl+E to toggle back and forth to get these.)

Cell[BoxData["k_"], "Input",
 CellChangeTimes->{{3.904560204635746*^9, 3.904560205059948*^9}}]

Cell[BoxData[
 OverscriptBox["k_", "_"]], "Input",
 CellChangeTimes->{{3.904560222532483*^9, 3.9045602277364197`*^9}}]

Because the box representations don't match, and the notation won't pass both the k_ and Overscript[k_, _] over to the function. I tried various things, but I either broke the sigma function, the mean function, or failed to create a notation due to internal errors.

So I don't have a full solution, only a partial one.

$\endgroup$
1
  • $\begingroup$ Thank you for investing time here -- even with a partial solution! $\endgroup$ Commented Sep 24, 2023 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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