Tag Info

Hot answers tagged

42

Prelude The items in this post are not generally regressions; they are simply changes and enhancements that may break code or cause problems in moving from one version to another. The bullet points are offered as specific solutions to instances of incompatibility, and not as recommendations of general practice. For example, Plot Themes are a powerful tool ...


26

Clearly the @ notation is inspired by the usual mathematical notation for function composition. f@g[x] looks very similar to the mathematical notation $(f\circ g)(x)$. But it is important to understand that @ does not denote function composition. In mathematical notation $f\circ g$ is also a function. In Mathematica f@x is simply a different way to ...


19

Try this: Map[If[#==1,Unevaluated@Sequence[],#]&,{1,2,3}] Note the output. The 1 is gone. That's because Unevaluated@Sequence[] puts the empty sequence there, that is, "nothing". ##&[] is a shorthand that can be used in most places for same - ## is the sequence of arguments, & makes it a function to apply to something, [] is that something - ...


13

As djp explains parentheses are unnecessary in the FullForm of an expression; it is logical for superfluous information to be removed. However if you want parentheses to persist you could use something like this: $PreRead = # /. RowBox[{"(", body___, ")"}] :> RowBox[{"paren", "[", body, "]"}] &; MakeBoxes[paren[body___], form_] := ...


13

So recently I've learned from John Fultz that RawBoxes are kind of verbatim indicator for MakeBoxes which is not well stressed out in documentation. This or I've missed the point but it doesn't matter, here we have handy way to do this: x = 5; ToExpression @ MakeBoxes[RawBoxes["x"] = 123]; x 123


11

We already have some answers explaining the issue. Let me give a solution to your problem. Let's say you have the code RegularExpression["((re)*)"] that you want to comment out. Since we have nested comments in Mathematica, just use a pair of (* to prevent your issue: Although I don't know the internal implementation of Mathematicas parser, the ...


11

You can have a single rule using Alternatives (|)... {a, b, c, d, e, f, g, h} /. x : (a | c | e | f) -> 12 Furthermore you can construct the rule on the fly... {a, b, c, d, e, f, g, h} /. x : Alternatives@@{a, c, e, f} -> 12 As noted by @Kuba below there is no requirement for the pattern to have a name (x) so... {a, b, c, d, e, f, g, h} /. (a | ...


10

This behaviour is very common, possibly near universal, in programming languages with matchfix comment syntax. The reason is that the contents of a comment sequence is presumed not to be code. Usually that presumption is correct, but not in this case. The general rule is You should be able to put anything inside a comment and the only special tokens ...


10

The behaviour we see is due to the precedence of &, which is much lower than the precedence of /@. As a consequence, the expression Line /@ (Print[#]; #) is bound tightly together by the high precedence /@ infix operator, yielding the single argument to the low precedence & postfix operator. This means that the second expression is interpreted as ...


10

A lot of functions in MMA have default values for Optional Arguments, for Example Flatten. It can take Flatten[expr] which means Flatten[expr, Infinity] Some functions don't have such option and you need to feed the Optional Arguments but you can go around by building your own function for your example, you can do this kind of trick like this: f[expr_, ...


9

... why introduce Composition as a new feature? Composition is used to create a new anonymous function that can be used in all the standard ways such as Map and Apply etc. To achieve the same thing without it one needs a Function. Much like operator forms the use of Composition allows one to eliminate extraneous Function constructs which can make code ...


9

The Notation package is not necessary to use an infix form of \[Star] as that is handled automatically. Also I recommend PadRight for constructing your expression (reference Generating a matrix using sublists A and B n times). SetAttributes[Star, HoldFirst] Star[a_List, n_Integer] := PadRight[a, n*Length@a, a] {1, 2}⋆5 (* ⋆ is \[Star] *) {1, 2, ...


8

In Mathematica version 10, you can also use Inactive to allow the Symbol to be created before doing the assignment. Here is an example: Clear["x"]; Activate[Inactive[Set][Symbol["x"], 3]] (* ==> 3 *) x (* ==> 3 *)


8

Also PlotRange[plot] PlotRange /. AbsoluteOptions[plot] Last @@ AbsoluteOptions[plot, PlotRange] PlotRange /. plot[[2]] all give (* {{0.,10.},{-0.999999,1.}} *) Note: Regarding usage of PlotRange as a function, it is undocumented, and the earliest reference I could find on this site is this answer dated Oct 11, 2012: The same range on each plot in a ...


8

You can make them an expression if you want. Let par[x.....] represent (x....). For example: (x+y)*z The FullForm would be: Times[par[Plus[x,y]], z] But in every such expression, the par[..] would only ever have on argument (in the example, Plus[x,y]). It would never modify the meaning of the argument. So in FullForm, there would be no point having ...


8

The documentation you linked to is more about how Infix is used to define output formats. You can find more information on how to use functions in infix form in the tutorials Special Ways to Input Expressions and Operator Input Forms. If you really want to use Partition in the infix form with an offset, you can use samplelist ~Partition~ Sequence[3, 2] ...


7

tree = Function[x, Defer @ FullForm @ x, HoldAll]; Now: 2 + 2 // tree Plus[2, 2] I used Defer to allow the output to be evaluated. If you do not prefer this replace it with HoldForm. For some explanation of the mechanics of this code see: Why doesn't "Defer" work with "TableForm"? See also my standard methods for ...


7

Differences /@ B or, if you don't like runes, Map[Differences, B] In v10, there is an operator form of Map which can be very useful, Map[Differences] @ B which allows you to slip a Map operation into a sequence of transformations without worrying about precedence issues.


7

As already explained, this happens because MatrixForm act as a wrapper. The answer to the question about how this behavior is implemented and how can eventually be reproduced is contained in the Informmation of the system symbol $OutputForms. Indeed ?? $OutputForms returns: $OutputForms is a list of the formatting functions that get stripped off ...


7

I believe it is important to get a fundamental understanding of what Pure Functions are that goes beyond the understanding using of a syntax. Hereafter an non-exhaustif summary of a few key understandings: 1) Pure Functions have they roots in Lambda calculus that forms the basis of functional programming paradigm implemented in Mathematica. 2) In ...


7

Unevaluated@Sequence[1, 2]~ConstantArray~10 $\ $ {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2} Or using Notation << Notation` Notation[ParsedBoxWrapper[ RowBox[{ RowBox[{"[", "const_", "]"}], "\[Star]", "reps_"}]] \[DoubleLongRightArrow] ParsedBoxWrapper[ RowBox[{ RowBox[{"Unevaluated", "@", RowBox[{"Sequence", "[", "const_", ...


6

You should use Sequence PolynomialMod[x^2 + 9 x + 5, 3] PolynomialMod@Sequence[x^2 + 9 x + 5, 3] Sequence[x^2 + 9 x + 5, 3] // PolynomialMod 2 + x^2 all produce the same output. Works with any function Sequence[i, {i, 10}] // Table {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


6

Algohi's answer is the most appropriate if one of the function's arguments is primary and the others secondary. However, you can get closer to the syntax that you suggested in your question using {x^2 + 9 x + 5, 3} // Apply @ PolynomialMod which works in version 10 using the operator form of Apply. Or, to include earlier versions, you could define your ...


6

Here's another way to achieve the same thing: Differences[{##}] & @@@ B {{1, 1}, {}, {1, 1, 1}} OR for fun Replace[B, x : {__} :> Differences[x], 1]


6

If you cannot resolve the keyboard behavior try an alternate input form; I propose: EscdinttEsc To input: Then use Tab to move between the Placeholder fields.


6

Range[10] - Make a list of integers from 1 to 10. /. - Replace all occurrences of {x_ - anything, to which we will refer to as x /; - so long as the test PrimeQ[x] - for primality yields True -> - with x^2} - the square of itself.


6

Fixed code: r[t_] := Piecewise[{{{7 t, 0, 4 (1 + Cos[t])}, 0 <= t <= π}, {{5 Cos[t - 3 π/2] + 7*π, 3 Sin[t - 3 π/2] + 3, 0}, π < t <= 2 π}, {{7*π + 3*Cos[t - 3 π/2], 2*3 - 3 + 3*Sin[t - 3 π/2], 2/(3 π) (t - 2 π)^2}, 2 π < t <= 4 π}, {{7 π - 5 (t - 4 π), 6 + 4 ((t - 4 π)/π)^3, 2 - 1/π t^2 + 10 ...


6

Brief? How about this. Define: c = ConstantArray; Now you can get what you want using the infix notation: "a"~c~7 and 10~c~7 With lists {1, 2}~c~7 you'll need to Flatten.


5

Graphics3D[Cylinder[{{#1, #2, #3}, {#4, #5, #6}}, #7] & @@@ listed] Or Graphics3D[Cylinder[ArrayReshape[{##}, {2, 3}], #7] & @@@ listed] Or MapThread[Cylinder[ArrayReshape[{##}, {2, 3}], #7] &, Transpose@listed] // Graphics3D Or Graphics3D[Cylinder[ArrayReshape[#, {2, 3}], #[[-1]]] & /@ listed]


5

This answer is in the same spirit as Kuba's answer, but it avoids conversion between boxes and expressions. Here is our workhorse mark::usage = "Use in combination with markedExpression"; markedExpressify::usage = "Use mark to mark strings for conversion to expressions"; markedExpressify[expr_] := Module[{pos, hCExpr, ext, thr, rLH, expresser}, ...



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