Group Abstract Group Abstract

Message Boards Message Boards

Alternative notation to [[i]], [[i,j]], etc. for the elements of a Table?

GROUPS:

Hi,

since I am finding the notation {...}[[i]] or {{...},...{...}}[[i,j]], etc, to address the elements of Tables very cumbersome, I am looking for an alternative. Isn't there a possibility to enter the indices in a 2-D math way, as a subscript? All these square brackets make expressions often hard to overlook. I am wasting a lot of time hunting down bracket errors. This would be so much better in a 2-D notation.

Does anyone know such a way of notation? Thanks!

POSTED BY: Gernot H
Answer
11 months ago
Hi,

you can try to generalize the following idea:
f[arg_] := Module[{}, arg /. Subscript[a_, b_] :>  data[[b]]];

x_\[CircleDot]y_ := f[y];
data = {a, b, c};
f\[CircleDot]Subscript[x, 1]
f\[CircleDot]Subscript[x, 2]
f\[CircleDot]Subscript[x, 3]

I.M.
POSTED BY: Ivan Morozov
Answer
11 months ago
A word of caution, you may end up replacing your wasted time hunting down bracket errors with an even greater amount of wasted time hunting down subscript problems. There is even a tutorial that someone wrote to try to explain how to avoid some of the problems with subscripts

http://forums.wolfram.com/student-support/filedownload.cgi?CommentAttachment=1376428543-30335-4493.nb&CommentAttachmentName=SubscriptedVariables101.nb

and some have even advised not using these at all.

Subscripted variables are not "real variables", that has nothing to do with Real versus Complex numbers. I don't think anyone has ever really given the history of these, but my guess was that these were sort of glued onto the system after a lot of other incompatible and barely compatible decisions had been made and could not be changed.

Sometimes they work. In a complicated piece of code filled with subscripted variables that actually works I am always very surprised. In a complicated piece of code filled with subscripted variables that doesn't work the first thing I do is try to eliminate all the subscripts and see if the code works then.
POSTED BY: Bill Simpson
Answer
11 months ago
Completely agree with Bill's argument, such manipulations should be generally avoided.
I'm just curious, why the code below fails to work?
 In[4]:= data = {a, b, c};
 
 f[arg_] := Module[{}, arg /. Subscript[a_, b_] :> data[[b]]];
 Subscript[x_, y_] := Apply[f, {Subscript[x, y]}];
 Subscript[x, 1]
 
 During evaluation of In[4]:= $RecursionLimit::reclim: Recursion depth of 256 exceeded. >>
 During evaluation of In[4]:= $RecursionLimit::reclim: Recursion depth of 256 exceeded. >>
 During evaluation of In[4]:= $RecursionLimit::reclim: Recursion depth of 256 exceeded. >>
During evaluation of In[4]:= General::stop: Further output of $RecursionLimit::reclim will be suppressed during this calculation. >>
...
Out[7]= $Aborted
POSTED BY: Ivan Morozov
Answer
11 months ago
1. Evaluate Subscript[x,1]
2. which evaluates Subscript[x_,y_]
3. which evaluates Apply[f,{Subscript[x,y]}]
which starts step 2 all over again.

That doesn't even really rise to the level of criticizing Subscript, you need to find a completely incomprehensible Subscript failure to qualify for that.
POSTED BY: Bill Simpson
Answer
11 months ago
Thank you,  Bill, now it's clear to me
POSTED BY: Ivan Morozov
Answer
11 months ago
You can type it in many ways: You really need more??

POSTED BY: Sander Huisman
Answer
11 months ago
Hi everyone, and sorry for my late response! Somehow I did not get an Email notification that my question has been answered.)

Thank you all very much!

@ Bill Simpson: I will certainly have a look at this tutorial. Thanks!

@ Sander Huisman: I didn't know about all these ways yet! The one with the doubled "[" as a subskript works best for me, as this way the indices are well distinguishable from argument brackets! Thank you very much!
POSTED BY: Gernot H
Answer
10 months ago
The very first thing I do after installing a new version of Mathematica is setting up keyboard shortcuts for typing the double brackets you get with ESC [[ ESC.

You can find the file named KeyEventTranslations.tr in the installation directory and add the following after the line EventTranslations[{
 Item[KeyEvent["[", Modifiers -> {Command}], FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], "\[LeftDoubleBracket]", After]}]],
 Item[KeyEvent["]", Modifiers -> {Command}], FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], "\[RightDoubleBracket]", After]}]], 

Do make a backup of the file before doing any modifications and put the backup outside of the Mathematica installation directory (or at least make sure you don't add extra files with a .tr extension).

On Windows/Linux I use the Control modifier instead of Command.

Now you can use Command-[ and Command-] to type these double brackets.  I'm so used to this at this point that I almost can't work at all with Mathematica without this modification.
POSTED BY: Szabolcs Horvat
Answer
10 months ago

Szabolcs, the above for some reason did not work for me, 10.0.2, windows 7. In the notebook, the left [[ show OK, but not the ]] one. I type Alt-[ and now I see [[ added automatically, but when I type Alt-] the notebook inserts [] instead of ]] I modified the file

C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\FrontEnd\TextResources\Windows Now it looks like this: (had to do it as Admin on windows)

enter image description here

Restarted Mathematica. Type Alt-] and get [] instead of ]]

POSTED BY: Nasser
Answer
6 days ago

Hi Nasser, this is because that shortcut is already defined in V10. See: http://reference.wolfram.com/language/tutorial/KeyboardShortcutListing.html

Try Ctrl instead of Cmd.

Or just use Get@"http://www.mertig.com" which does not modify the system KeyEventTranslations.tr at all.

POSTED BY: Rolf Mertig
Answer
6 days ago

As Rolf said, "Command" is a special key available only on Macs. On Windows I use Control instead of Command.

POSTED BY: Szabolcs Horvat
Answer
6 days ago

As Rolf said, "Command" is a special key available only on Macs. On Windows I use Control instead of Command.

No problem. it works now with Control. But it does say in the file the following:

(* Modifiers can be "Shift", "Control", "Command", "Option"
For Macintosh: "Command" = Command Key, "Option" = Option Key For X11: "Command" = Mod1, "Option" = Mod2

For Windows: "Command" = Alt, "Option" = Alt *)

Notice the last line. It says for windows "Command" = Alt, so that is why I left it as you had it, and used Alt to activate the Command. Any way. it is working now. thanks. Very useful trick.

POSTED BY: Nasser
Answer
6 days ago
Editing  KeyEventTranslations.tr, or any file deep inside Mathematica, is strongly discouraged and not supported.

I understand Mr. Horvat's wanting a shortcut, and it sounds like he is appropriately cautious. 
POSTED BY: Bruce Miller
Answer
10 months ago

But it works just great! Yes, this is not for beginners to write, but it is now extremely easy to run it. I do Mathematica programming since nearly 25 years and I completely agree with Szabolcs that good keyboard shortcuts increase productivity.

POSTED BY: Rolf Mertig
Answer
8 days ago
Well, I would really like to know how to do this without modifying the installation directory.  Actually I asked about it here a while ago.

Please also take this as a feature suggestion/request for adding shortcuts for entering these characters.

Having used these shortcuts for several years, I know from experience that I'm less productive without them, and that having them makes Mathematica significantly more usable (mostly indirectly by allowing me to easily write more readable code).
POSTED BY: Szabolcs Horvat
Answer
10 months ago

Many times you can reduce the [[ ]] abuse by using rules. For example when you use Cases instead of Select.

Besides that I have also made modifications to MenuSetup.tr in order to make () {} [] [[]] easier to type with single key shortcuts, but this is probably discouraged and not supported. For example:

        MenuItem["Matching [[ ]]",FrontEndExecute[{FrontEnd`NotebookApply[FrontEnd`InputNotebook[], "\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]"]}],MenuKey["F8"]],

One more related idea: In many modern editors when you select something and type "(" then the selected expression becomes parentized. I wish Mathematica did that too.

POSTED BY: Gustavo Delfino
Answer
7 days ago

I had no desire to start modifying the key event translations file. I recommend using a programmable gaming mouse and/or keyboard for programming complex key sequences. A programmable mouse/keyboard device make my work flow much more efficient, so they offset the relatively minor cost. They allow you to program single keys or key sequences using macros and automatically activate your custom profile when linked to an executable. Personally, I use the Razer Naga series mouse.

POSTED BY: David Grogg
Answer
7 days ago

The answer to an alternative notation is contained in package Notation`. One can turn subscript into a representation of the [[ i ]] notation. It doesn't make code entry easier but it does make it readable and compact.

(The biggest problem is showing the code in this reply. I keep getting expanded notation for very simple text, I'll use pictures)

enter image description here

enter image description here

Attachments:
POSTED BY: Douglas Kubler
Answer
6 days ago

There are some reasons to use subscripts. For example, if I do symbolic solutions to problems with variables like x[1], x[2], and get rules such as x[1] -> expr1, etc. and then apply N to the rules, x[1] changes to x[1.], etc. That doesn't happen with subscripts.

POSTED BY: Frank Kampas
Answer
6 days ago

I think the OP was referring to a different application of subscripts ...

Regarding your note, I use SetAttributes[x, NHoldAll] for this situation.

POSTED BY: Szabolcs Horvat
Answer
6 days ago