# Tag Info

17

Let's look at this with a simple example without considering complicated indexing and levels. Consider the list (the colours are merely for visual guidance): A = Array[Subscript[a, ##] &, {2, 3, 4}] Dimensions@A (* {2, 3, 4} *) This is a list containing 2 lists, each of which contains 3 sublists, which in turn contain 4 elements of the array. ...

16

Everything depends on what you try to do with this. First you could use Subsuperscript and get rid of one level which is introduced by your Power Subscript[e,3]^2//FullForm (* Power[Subscript[e,3],2] *) Subsuperscript[e,3,2]//FullForm (* Subsuperscript[e,3,2] *) Both forms look equally in the front-end, but now you can use TagSet to transform all e with ...

15

What the first part of the variable declaration does Manipulate initializes complexparts to {Re[#], Im[#]} & when it executes. (In general, a declaration of the form {{var, expr},...} in a Manipulate results in the local variable var being initialized to expr.) To use complexparts outside of the Manipulate, do this: complexparts = {Re[#], Im[#]} ...

13

# is a placeholder for an expression. If you want to define a function, $y(x)=x^2$, you just could do: f = #^2 & The & "pumps in" the expression into the # sign. That is important for pairing & and # when you have nested functions. f[2] (* 4 *) If you have a function operating on two variables, you could do: f = #1 + #2 & So ...

11

The problem is just name collisions, that isn't at all abstract and will happen in any programing language, so it would be odd to claim that it's impossible due to the way Mathematica works. The solution is simply to name your parameters when you write your functions so they don't collide, so you write for instance: RegionFunction -> Function[{a1, b1}, ...

11

I'm afraid my comment was too obscure to be noticed. Further, I disagree with one premise somewhere in the commentary, and I wish to make a fuller explanation to see if I understand correctly or incorrectly. Finally, I think the question is answered in the documentation on the Standard Evaluation Procedure: Evaluate the head of the expression. ...

8

While the tutorial will undoubtedly explain better that I could the entire topic of pure functions, which is what Slot, or # has to do with, I'll answer the specific question at hand. The Slot is treated as the argument in an anonymous function. Specifically, the code #^2 & // FullForm Reveals that what is actually going on is ...

8

Here is another way: you can fool the depth-1 tag rule for UpValues with a few temporary symbols. Here is an example: ClearAll[e]; e /: Subscript[e, i_?IntegerQ] := e /: Subscript[e, i] = Module[{el}, el /: el^p_ := el /: el^p = Module[{elp}, elp /: NumericQ[elp] = True; Format[elp] := TraditionalForm[Subscript["e", i]^p]; ...

8

WordData can give you the IPA form of a word: Gather[ WordData[#, "PhoneticForm"] & /@ {"pray", "prey", "wade", "weighed"} ] (* {{"pr'ey", "pr'ey"}, {"w'eyd", "w'eyd"}} *) EDIT It seems WordData[word, "PhoneticForm"] no longer provides the proper IPA, however that data is still included in the paclet so we can make a new WordData property for that. ...

7

If I understand the question here are three ways to "nest" functions: f1 = Function[x, (# + x)/2 &]; f2 = With[{x = #}, (# + x)/2 &] &; f3 = # /. x_ :> ((# + x)/2 &) &; All work the same: #@7 & /@ {f1, f2, f3} {(#1 + 7)/2 &, (#1 + 7)/2 &, (#1 + 7)/2 &} Note that with the first form I used the Slot based ...

7

The original complete definition is cfRemainders[x_, iter_: Hold[$IterationLimit]] := NestWhileList[FractionalPart[1/#] &, FractionalPart[x], # != 0 &, 1, ReleaseHold[iter]] The iter_ : Hold[$IterationLimit] makes iter an Optional argument with the default value Hold[$IterationLimit] if the argument is omitted. Secondly, by using Hold, ... 6 You can use Notation package to treat anything like function and then set whatever attributes to this function: The only problem is that Notation don't support test patterns, so to make an expression numeric with only integer indexes: 6 I'm not surprised you're having trouble as {{complexparts, {Re[#], Im[#]} &}, None} is (to the best of my knowledge) a nonstandard "hack" and not part of the documented standard uses. I have used it myself but I still don't recommend it. Usually you can use the Initialization option instead. Evaluating {{complexparts, {Re[#], Im[#]} &}, None} by ... 5 The problem is that if you pass a symbol, it will be created already during the parsing stage, when you pass it, in the current context. Therefore I suggest to pass its string name instead. This function will do the job: ClearAll[f]; f[symbolName_String, value_, context_] := Block[{$ContextPath}, BeginPackage[context]; ToExpression[ ...

5

I'm not certain I understand your goals, though I too wish there were a cleaner way to do this. I presume that you are dissatisfied with the performance of this fairly direct solution: index[expr_] := {Extract[expr, #, HoldComplete], #} & /@ Position[expr, _] Your own method using both Position and Level is a clever way to vectorize this as it were. ...

4

Actually this is more like a comment to Michael E.'s answer than an own answer, but it became too long for a comment. I think it is worth mentioning that $IterationLimit (and also$RecursionLimit and probably some others as well) is somewhat special and thus needs special treatment: For a "normal" variable it would be quite simple to achieve what Michael ...

4

What this does is set a default value for iter, meant for use if cfRemainders is called with only one argument. The default value for iter in this case is $IterationLimit, and the Hold[] enclosing it means cfRemainders will use$IterationLimit symbol for the new rule. If there was no enclosing Hold[], \$IterationLimit would have been replaced with the Integer ...

4

This sort of programming is not my strength and I don't know Python. Reading the comments, perhaps this won't quite be perfect, but it might be good enough. It seems good enough for many purposes, at least in the way I interpret the question. It localizes all Symbols in the code that are in the "Global" context and don't have Ownvalues or DownValues. If ...

3

I think that giving the language we use in Mathematica a name ("W", or whatever), and establishing it as separate from the Mathematica Interface is a step in the right direction. Mathematica is "Visual Wolfram" (arg) or something like that - an interactive interface for TWL. It has a REPL, renders graphics, formats tables, grids, etc.. That's not TWL - ...

3

Plot has Attributes HoldAll, so one possibility to get what you expect is to do just SetOptions[Plot, Evaluated -> True]; at the beginning. Another possibility (better documented) would be to use Evaluate inside Plot: Plot[Evaluate[...], {x, 0, 2}]

3

Solutions with Inner Here is the function I came up with ClearAll[toExprPosLists]; SetAttributes[toExprPosLists, HoldAllComplete] toExprPosLists[expr_] := Inner @@ Function[ Hold@ Evaluate[ List, Unevaluated @@ Level[Unevaluated@expr, {0, Infinity}, #, Heads -> True], Position[Unevaluated@expr, _], # ] ...

3

I believe this is what you want: f[symname_String, value_, context_] := (Begin[context]; With[{s = Symbol[symname]}, Set[s, value]]; End[]) Then use it like this: f["myvar", 4, "MyContext"] Verify: ? MyContext`myvar Hope that helps

2

That code you're pulling out of the Manipulate is just a list. complexparts is a blank symbol -- it's meaningless on its own. You may as well just type {1, 2, 3, 4}. The reason that code acquires meaning is because the Manipulate function chooses to interpret that list in a certain way. In other words, check the documentation for Manipulate to understand ...

2

On my installation of Mathematica 9.01 (Windows 7 x64), with the lightweight grid enabled ii=0; (#-ii++)& /@ Range[1,10] out[1]= {1,1,1,1,1,1,1,1,1,1} and ii=0; Parallelize[(#-ii++)& /@ Range[1,10]] out[2] = {-3, -3, -1, -1, 3, 4, 5, 6 ,7, 8} In fact, I get a different result each time I run the latter variant. One of the delights of ...

2

This is hardly a complete answer but I suspect this is the result of special handling of the symbol Complex, much as there is special handling of packed arrays. Remember that Block only affects things that evaluate, e.g. Block[{a = 1}, Hold[a, b, c]] returns Hold[a, b, c]. I believe that Complex may be passed over when it comes to evaluation. Consider ...

2

You just need to apply the third argument of Outer as rm -rf suggested. With your A and B, Outer[#1 -> #2 &, A, B, 1] gives {{{{1}} -> 8, {{1}} -> 9}, {{{2}} -> 8, {{2}} -> 9}, {{{1}, {2}} -> 8, {{1}, {2}} -> 9}} as you requested. To check (with your X) Outer[#1 -> #2 &, A, B, 1] == X True

1

This is NOT AT ALL answer to this question, but I am just writing my opinion here. Link shown for Maple, counts only number of atomic operations and doesn't inform anything about kernel level instructions which I believe is not more significant than Timing.I tried the following and it gives the same kind of output. All that is needed is to put it together in ...

1

Another important role played by pure functions (those built with slots #) is that in functional programming (and Mathematica follows that paradigm) many times you need to apply a function that doesn't live outside the specific place where you define/evaluate it. In other words, mapping a function to a data set can be done without to save any unneeded ...

1

There are two ways of doing this that are mostly equivalent. First, f[ Sequence @@ l ] (* 10 *) But, the use of Sequence is to many characters, in my opinion, and there is a better way. Essentially, the notation @@ is shorthand for the function Apply which replaces the Head of an expression with another head. In the prior case, Sequence replaced the head ...

1

How about this: A = {{{1}}, {{2}}, {{1}, {2}}}; B = {8, 9}; Table[{i -> j}, {i, A}, {j, B}] which gives me: {{{{{1}} -> 8}, {{{1}} -> 9}}, {{{{2}} -> 8}, {{{2}} -> 9}}, {{{{1}, {2}} -> 8}, {{{1}, {2}} -> 9}}} I've got one too many sets of braces, it looks like.

Only top voted, non community-wiki answers of a minimum length are eligible