Message Boards Message Boards

0
|
2376 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Same unit and symbol variable name

Posted 2 years ago

Hi there

I have created some variables to represent units that I use a lot.

Example:

s = Quantity[1, “Second”];
A = Quantity[1, “Ampere”];

The issue I’m having occurs when symbol names interferes with the unit variables. As an example we use “s” as a symbol for distance, here where I study. I have a +50 units defined, so I often run into this problem. I could of course use a different symbol name each time the problem occurs, but I was wondering if I could do something smarter here. I’m new at Mathematica, and the basic knowledge I have, gives me no obvious answer. I’m thinking some sort of namespace, but has no clue if that’s the way to go.

Any inputs are much appreciated.

POSTED BY: Jak SD
2 Replies
Posted 2 years ago

Hi Eric

Thanks for getting back to me. I used the unit definitions from this "tutorial" youtube tutorial with a link to the notebook unit notebook So far I have no problems with name collision amongst units.

It's not so much about keystrokes, but more about readability. This post from mathematica.stackexchange.com explains the use of Context, but like you wrote, it's a bit counterproductive.

Perhaps your suggestion about three-letter convention is the way to go. Thank you very much.

POSTED BY: Jak SD
Posted 2 years ago

If you're wanting to save keystrokes (e.g. you want to type something like 5 s and have it transform to Quantity[5, "Seconds"]), then using a namespace (i.e. a Context) will be counterproductive.

How are you avoiding name collisions among your 50 units? I would think that that's more of a problem than colliding with single-letter System symbols.

You could just use more letters. If you used a three-letter convention, you could define sec = Quantity[1, "Seconds"] and probably avoid collisions.

You could also use InputAliases, cf http://reference.wolfram.com/language/ref/InputAliases.html . You can also access this with the option inspector (via menu, Format -> Option Inspector). This would avoid name collisions entirely, but you'd have to use the escape key.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract