# Tag Info

37

Point #1 Part always wraps element sequences with the original head of the expression. expr = Hold[1 + 1, 2 + 2, 3 + 3, 4 + 4, 5 + 5]; expr[[{2, 3}]] Hold[2 + 2, 3 + 3] This applies at every level of the extraction. For example, compare these outputs: expr = g[h[1, 2], i[3, 4]]; expr[[ 2 , 1 ]] expr[[ {2}, 1 ]] expr[[ 2 ,{1} ]] expr[[ {2},{1} ...

32

One convenient way to think of Flatten with the second argument is that it performs something like Transpose for ragged (irregular) lists. Here is a simple example: In[63]:= Flatten[{{1,2,3},{4,5},{6,7},{8,9,10}},{{2},{1}}] Out[63]= {{1,4,6,8},{2,5,7,9},{3,10}} What happens is that elements which constituted level 1 in the original list are now ...

27

It is used in TraditionalForm output, e.g. here: TraditionalForm[ Hypergeometric2F1[a,b,c,x] ] Without \[InvisibleApplication] it would probably be hard for Mathematica to parse it back to InputForm. Probably it is used in more places internally. In order to get rid of it: Locate the file UnicodeCharacters.tr in ...

25

square = Function[x, x^2]; square1 = #^2 &; square2[x_] := x^2; DownValues[square] DownValues[square1] DownValues[square2] {} {} {HoldPattern[square2[x_]] :> x^2} Two differences that immediately come to mind are that: 1) functions with down values won't autocompile when you use them in Table, Map, Nest etc. so therefore are less efficient when ...

20

In addition to Brett's counter-example, it might be helpful to view this from Mathematica's philosophy, which is "everything is an expression". In this framework, you're not really indexing a 1D/2D array, but you're extracting a Part from an expression. Indeed, you can use the ⟦ ⟧ notation on any expression, not just lists/matrices. For example: Sin[x + ...

20

It is a good habit to get into because you can often get tripped up by precedence rules (no one remembers everything!). For instance, PatternTest binds very tightly. See the difference between these two definitions: Clear@f f[_?(# == 2 &)] := Print@"foo" f[_] := Print@"bar" f[2] (* "foo" *) Clear@g g[_?# == 2 &] := Print@"foo" g[_] := Print@"bar" ...

19

I suggest an approach based on creating lexical and / or dynamic environments (custom scoping constructs if you wish), inside which the rules of our "universe" will be altered. I will illustrate with a dynamic environment: ClearAll[withStringManipulations]; SetAttributes[withStringManipulations, HoldAll]; withStringManipulations[code_] := ...

18

The default value of $NumberMarks Automatic means that  should by default be used in arbitrary-precision but not machine-precision numbers. Arbitrary-precision numbers can contain an arbitrary number of digits e.g. : Sqrt[321] == 1.73205080756887729353 Machine numbers contain the same number of digits and maintain no information on their ... 18 To understand what's happening, the difference between evaluation and parsing needs to be made clear: parsing means taking the string (the text) input to Mathematica and converting it to some internal representation of a Mathematica expression evaluation means taking a Mathematica expression and transforming it according to some rules the evaluator knows ... 17 Probably the most common use of Unique is in situations when you need a large number of local variables (and sometimes a variable number of local variables) so using Module is either inconvenient or impossible. In that case you can use the construction: vars= Table[Unique[x],{n}] or something of this kind. You can find a few examples in the archives of the ... 17 To programmatically find the internal representation of the shortforms, you can use MakeExpression, which gives the result wrapped in HoldComplete. Here's an example: MakeExpression@"?name" (* HoldComplete[Information["name", LongForm -> False]] *) MakeExpression@"??name" (* HoldComplete[Information["name", LongForm -> True]] *) 17 It is probably debatable to what extent it has built-in object oriented features. In any case, this answer is not intended to lead you to try to emulate object oriented programming, which is in general a bad idea. (see @Leonid 's answer) However, it is not debatable that Mathematica is tremendously flexible (as to style and notation at least, the evaluation ... 16 I don't think that invisible characters have any internal uses. They are probably just to make expressions look nice to us humans. The place I can think of using \[InvisibleApplication] is as some group action g x = g[x], but there are sure to be other places. As for making invisible characters visible. You can make all special characters explicit using ... 16 No. For example, functions do not have to be atomic. It can be possible to extract parts from them (although it's generally not recommended.) In[1]:= if=Interpolation[Range[10]^2] Out[1]= InterpolatingFunction[{{1,10}},<>] In[2]:= if[3] Out[2]= 9 In[3]:= if[[3]] Out[3]= {{1,2,3,4,5,6,7,8,9,10}} 16 I like to use Ctrl+. to discover how it's grouped. For example, in this example: Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction -> Hue[#] &] Putting your cursor position after & and pressing Ctrl+. two times, you will get all expression ColorFunction -> Hue[#] marked, so it's wrong, and you need to put () like this: Plot3D[Sin[x y], ... 15 As the error message indicates Clear does not work that way. There are several assignment forms that automatically create a definition to something other than a raw symbol: x[5] = 1; Subscript[x, 1] = 2; x /: Subscript[x, 2] = 3; N[x] = 3.14159; DownValues[x] DownValues[Subscript] UpValues[x] NValues[x] {HoldPattern[x[5]] :> 1} ... 15 A second list argument to Flatten serves two purposes. First, it specifies the order in which indices will be iterated when gathering elements. Second, it describes list flattening in the final result. Let's look at each of these capabilities in turn. Iteration Order Consider the following matrix:$m = Array[Subscript[m, Row[{##}]]&, {4, 3, 2}]; $m ... 15 Functions are more concise and generally faster but patterns are a lot more expressive. When you don't need the expressive power of patterns you should probably use functions. I use down values more to set up the high level structure of my program and functions to implement the algorithms. But often I am lazy and use down values out of habit. When I am in ... 15 You can do this by using Alternatives to have the pattern accept either an integer or Infinity as an argument, like so: PillsyFoo[x : (_Integer | Infinity)] := Module[{limit = Min[x, 10], i = 1}, While[i <= limit, ++i]; i - 1] It helps that Mathematica functions like Min and Max usually do the right thing with Infinity as an argument. 15 As explained by Michael Pilat you cannot create your own compound operators with custom precedence. (You could conceivably write your own parser as Leonid has worked on, or attempt to coerce the Box form with CellEvaluationFunction.) You can however use an existing operator with the desired precedence. Looking at the table Colon appears to be a good ... 15 At the risk of repeating myself, I would like to stress that one has to be critical towards the superficial flexibility offered by Mathematica, when (particularly mutable) data structures are concerned. Using mutable data structures assumes a programming style for which Mathematica is not optimized. It can emulate it, yes, and we have seen a number of such ... 14 These two forms may be similar on the surface, but they are very different in terms of the underlying mechanisms invloved. In a sense, Function represents the only true (but leaky) functional abstraction in Mathematica. Functions based on rules are not really functions at all, they are global versions of replacement rules, which look like function calls. ... 14 The backtick is a short-hand to mark the precision of your output. If it is not followed by any number, it denotes machine precision. You can denote arbitrary precision by including a number, as for example, 0.320. By default, these are not displayed in InputForm, which is why you see them only when copying. You can show them with NumberMarks -> True. ... 14 You can see that the base never survives to the evaluation stage by trying for example 16^^98 // Unevaluated // AtomQ True 16^^98 // Unevaluated // Head Integer Trace[16^^98, TraceInternal -> True] {} It's more or less like a box formatting rule. The Front End sends the literal structure to the kernel, it first builds up the ... 14 /: is the short-hand notation for TagSetDelayed, which is creating UpValues. It's useful for over-loading how a particular function behaves with a specific head. For example: In[1]:= h /: Plus[x : h[arg1_, arg2_], y : h[arg3_, arg4_]] := Plus[arg1, arg2, arg3, arg4] In[2]:= h[1, 2] + h[3, 4] Out[2]= 10 The benefit being you don't have to ... 14 There is no need to play around with ReplaceAll, Rule, Block, Module or whatever using D, since you have an oparator Derivative really fulfilling your needs while you need not bother if the arguments were defined, so I recommend it to find symbolic derivatives of your function. Remember of shorthands f', f'' to represent first and second derivatives of ... 14 OK, the verbal description isn't very easy, but I'll try: This is a simulation showing how the attractive forces between 21 spheres cause them to aggregate. Instead of simulating the equations of motion, the Dynamic approximates the physics of the attractive force and the repulsive core in the somewhat artificial body of Function[{x}, ...]. Its argument x ... 13 Your code reveals exactly why Clear complains: Subscript[x, r] is not a Symbol nor a String. When you assign a value to it, you're setting a DownValue not an OwnValue; in other words, you're setting the value of a function not a variable. To use$x_r\$ as a symbol, use the Notation` package's function, Symbolize. I'd recommend using it from the palette ...

13

TracePrint will show you what happens: PreIncrement takes it's argument x, evaluates it (let's call the result result), then evaluates x = result+1. Note that PreIncrement has HoldFirst. Now ++(++x) evaluates ++x first yielding 2, then evaluates (++x) = 2+1 resulting in an error (trying to assign to PreIncrement) and returning 3. This also explains why ...

12

Using Trace, Trace[x = Range[10]; { i, x[[i]] } /. i -> 5] we will see that the error comes from Mathematica trying to evaluate {1,2,3,4,5,6,7,8,9,10}[[i]] As you pointed out, the error message is harmless in this case. If you want Mathematica to substitute first, you can use Hold and ReleaseHold: x = Range[10]; ReleaseHold[Hold[{i, x[[i]]}] /. i ...

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