MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: compact notation for NonCommutativeMultiply that supports cut and paste

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127727] Re: compact notation for NonCommutativeMultiply that supports cut and paste
  • From: Tobias Hagge <tobiashagge at gmail.com>
  • Date: Fri, 17 Aug 2012 03:43:44 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <8525141.75636.1344674234471.JavaMail.root@m06> <k07hjo$cu8$1@smc.vnet.net>

David, thank you for your interesting solution. It does not allow copy/pasting of just part of a noncommutative product, which is a feature for some applications, but not quite what I need. After some experimentation I was able to find two ways to accomplish more precisely what I was looking for, which I'm posting below.

Contrary to what I wrote (sorry), what I really want is SMALL (rather than no) spaces between the symbols, just like Times. I ran into these problems:

1) Executing this:
<< Notation`
InfixNotation[ParsedBoxWrapper["foo"], NonCommutativeMultiply]
NonCommutativeMultiply[a, b, c]

and then copy/pasting the output gives "afoobfoo c".

2) Evaluating this:
<< Notation`
Unprotect[NonCommutativeMultiply];
InfixNotation[ParsedBoxWrapper["\[Null]"], NonCommutativeMultiply];
NonCommutativeMultiply /:
    MakeBoxes[NonCommutativeMultiply[x__], StandardForm] :=
    RowBox[Riffle[(MakeBoxes[#, StandardForm]) & /@ {x},
     ConstantArray[
      "\[InvisibleSpace]\[Null]\[InvisibleSpace]",
      Length[{x}] - 1]]];

NonCommutativeMultiply[a,b,c]

and then copy/pasting gives input which evaluates correctly, however the spacing of output before copy/pasting still does not match the spacing after.

For whatever reason, the following code does not produce the problematic whitespace. It gives a StandardForm output for NonCommutativeMultiply which looks like times, but copy/pastes to a form which evaluates as NonCommutativeMultiply.

Unprotect[NonCommutativeMultiply];
InfixNotation[ParsedBoxWrapper["\[Null]"], NonCommutativeMultiply];
NonCommutativeMultiply /:
  MakeBoxes[NonCommutativeMultiply[x__], StandardForm] :=
  RowBox[Flatten[
    Riffle[(MakeBoxes[#, StandardForm]) & /@ {x},
     ConstantArray[{"\[VeryThinSpace]", "\[Null]",
       "\[VeryThinSpace]"}, Length[{x}] - 1]]
    , 1]
   ];

\[VeryThinSpace] can be replaced with \[InvisibleSpace] if no spacing between factors is desired. I tried replacing \[Null] with "|" (to get something like your notation), but unfortunately, replacing \[VeryThinSpace] with \[NegativeVeryThinSpace] causes the bad whitespace to reoccur, so this code won't give a notation like yours which is as compact.

A second solution is:

InfixNotation[ParsedBoxWrapper["\[InvisibleApplication]"], NonCommutativeMultiply]

It is simpler but has two slight disadvantages:

1) Invisible function application no longer works.
2) When copy/pasting part of a long product, there is no visible indication whether the end of the selection is a symbol or the noncommutative operator.

Best,
Tobias

On Sunday, August 12, 2012 1:14:16 AM UTC-5, djmpark wrote:
> I think you are saying that you don't like the x**y**z space taking infix
>
> operator of NonComutativeMultiple and want something more compact because of
>
> long multiplication sequences. I think it is a bit confusing to have no
>
> infix separator (Is every argument guaranteed to be a single character
>
> symbol?). The following uses a Vertical line and negative spaces to make a
>
> pretty compact form.
>
>
>
> Unprotect[NonCommutativeMultiply];
>
> NonCommutativeMultiply /:
>
>  MakeBoxes[NonCommutativeMultiply[x__ /; Length[{x}] > 1],
>
>   form : (StandardForm | TraditionalForm)] :=
>
>  Module[{displayFunc, interpretationFunc, work1, work2, work3,
>
>    n = Length[{x}], f},
>
>  
>
>   work1 =
>
>    Riffle[Table[Slot[i], {i, n}],
>
>     RowBox[{"\[NegativeThinSpace]", "\[NegativeMediumSpace]", "|",
>
>       "\[NegativeMediumSpace]", "\[NegativeThinSpace]"}]];
>
>   work2 = RowBox@work1;
>
>   displayFunc = (Evaluate@work2) &;
>
>  
>
>   work3 = Riffle[Table[Slot[i], {i, n}], ","];
>
>   interpretationFunc =
>
>    f[(RowBox[{"NonCommutativeMultiply", "[", Sequence @@ work3,
>
>         "]"}])] /. f -> Function;
>
>  
>
>   TemplateBox[MakeBoxes[#, form] & /@ {x}, "NonCommutativeMultiply",
>
>    DisplayFunction -> displayFunc,
>
>    InterpretationFunction -> interpretationFunc]
>
>   ]
>
>
>
> NonCommutativeMultiply[a, b, T, dd, F]
>
> % // FullForm
>
> % // InputForm
>
>
>
> TemplateBox is an undocumented, but rather nifty construction, that I got
>
> from WRI support some time ago. It can be copied and pasted. You can edit
>
> the variables but not other parts of the expression. Someone from WRI might
>
> give a neater formulation of this.
>
>
>
>
>
> David Park
>
>
>
>
> From: tobiashagge
>
>
> Hello,
>
>
>
> I am doing some group theory computations that have output of the form
>
>
>
> := NonCommutativeMultiply[very-long-sequence-of-letters]
>
>
>
> I want the StandardForm output to display as a string of symbols without
>
> spaces, just like the output of Times. I want to be able to copy and paste
>
> that output into an input line and have it be correctly interpreted.
>
> Finally, I want "a b" to evaluate as Times[a,b].
>
>
>
> I decided to define the null character as an operator, piecing together bits
>
> of code from the internet.
>
>
>
> Unprotect[NonCommutativeMultiply];
>
> InfixNotation[ParsedBoxWrapper["\[Null]"], NonCommutativeMultiply];
>
>
>
>
>
> I'm having two issues:
>
> 1) The special characters are not visible in the notebook, which makes the
>
> code a bit hard to read.
>
> 2) Letters appear spaced apart in the output. If you copy and paste the
>
> output, the spacing disappears (so pushing the letters together in the
>
> output using negative space will make copy/paste awkward).
>
>
>
> Using AlignmentMarker instead of Null fixes the spacing issue. However, for
>
> reasons I do not understand, the InfixNotation command does not work in this
>
> case.
>
>
>
> Any help or helpful advice is appreciated.
>
>
>
> Best,
>
> Tobias



  • Prev by Date: Re: V8 slow like a snail
  • Next by Date: Re: Importing xlsx spreadsheet data into Mathematica,
  • Previous by thread: Re: compact notation for NonCommutativeMultiply that supports cut and paste
  • Next by thread: Series Simplification - how to truncate the result ?