# Tag Info

39

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] For this purpose a single part e.g. 1 is not a sequence but {1} and 1 ;; 1 are: expr[[1]] expr[[{1}]] expr[[1 ;; 1]] 2 Hold[1 + 1] Hold[1 + 1] This applies at every level ...

35

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 ...

30

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 ...

29

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 ...

26

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 ...

24

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" ...

20

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_] := ...

20

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 ...

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

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], ...

19

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 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} ... 18 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 ... 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 ... 18 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]] *) 18 Here is my version using injector pattern: ClearAll[myWith]; SetAttributes[myWith,HoldAll]; myWith[pars_=vals_,body_]:= Apply[Set,Hold[Evaluate[Transpose[{pars,vals}]]],{2}]/. Hold[vars_]:>With[vars,body] This code assumes that pars evaluate to a list of symbols. For example, myWith[params=vals,a+b+c+d] (* 10 *) 17 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. ... 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 ... 17 Maybe I miss the point here, but FullForm[x ↗ y] gives UpperRightArrow[x,y]. This is described in the documentation to UpperRightArrow and since this symbol is not protected and has not built-in meaning, you can just define it the way you like: UpperRightArrow[x_, y_] := FooBar[x, y] and this instantly gives you Update: As answer to Jacobs ... 16 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 ... 16 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 ... 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 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. ... 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 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 ... 16 It is Kampé de Fériet function, introduced in Joseph Kampé de Fériet, "La fonction hypergéométrique.", Mémorial des sciences mathématiques, Paris, Gauthier-Villars. Its definition is given on Notations page: and, in an alternative form, in Wikipedia:$\${}^{p+q}f_{r+s}\left( \begin{matrix} a_1,\cdots,a_p\colon b_1,b_1{}';\cdots;b_q,b_q{}'; \\ ...

15

You can do this by using Alternatives to have the pattern accept either an integer or Infinity as an argument, like so: Pillsy`Foo[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

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 ...

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 ...

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