开发者

What does // mean in Mathematica?

Examples:

  • In

    CT = Table[Prepend[10^4*x[range2] /.
         NDSolve[{...series of equations here...}, {t, range1, range2}, 
            MaxSteps -> 10000,
            PrecisionGoal -> 11], delay], 
            {delay, delaymin, delaymax, 0.1}]; // Timing
    

what does it mean this // Timing after the semicolon?

  • In

    Dρ = -I*((H0 + V).ρ - ρ.(H0 + V)) - Γ*ρ // Simplify;
    

And this //开发者_如何转开发 Simplify here?

I can't find this explanation anywhere!

Thanks in advance, Thiago


This is Mathematica's postfix notation. Basically x//f is the same as f[x]


Yes, argument // function is postfix function application.

Useful about it is that it has a different, lower binding power relative to prefix application (f @ x).

In fact it is lower than most other things (exceptions include CompoundExpression ; and Set =), and therefore it can often be considered as "apply to everything before this."


You say: "I can't find this explanation anywhere!". I assume this means you are not aware of the documentation center that's right under your fingertips whenever you're using Mathematica.

All you have to do is to place your cursor on the // and press F1 and you'll get some sort of explanation, or a list with relevant (hopefully) matches. In this case the PostFix page, which is not extremely helpful. However, it has some links at the bottom (assuming you have versions 6, 7 or 8) that provide more insight, among which a link to the syntax overview page (click the Mathematica syntax link, or enter "guide/Syntax" in the search box).


expr // f is essentially equivalent to f[expr]. Sometimes, it's called postfix notation. I read expr // f as "pass the expression expr to the function f".


a // f

is, I believe, the same thing as

f[a]

(which incidentally, any sane mathematician I know would write as

f(a)

just as it is done in most computer languages.)


As others have mentioned, // is the postfix notation and expr//f means f[expr] in mathematica and f(expr) in math.

Although there might be more subtleties involved, my usage of // has often been in cases where I've started writing out an expression and then realized I wanted to operate a function on it. So instead of moving the cursor all the way back to type f@expr or f[expr], I can simply finish typing what I had in mind, and use expr//f.

Example:

Plot[Sin[x],{x,0,Pi}]
%//Export["test.pdf",#]&

The graphics is passed to the export function and is saved as test.pdf.


As your question has already got very good answers, I want to add just a clarification on usage.

The three expressions

Sin[x]
Sin@x
x // Sin

Are equivalent.

Although, to my knowledge, the last two can't be used with functions with more than one argument. So

Plot[Sin[x], {x, 0, Pi}]   

Can't be invoked in prefix or postfix notation without tricks like

Sin[x] // Plot[#, {x, 0, Pi}] &  

or

Plot[#, {x, 0, Pi}] &@Sin[x]

The prefix notation is usually seen when using simple functions like Sin@x or Sort@list, while most uses of the postfix involve a reasoning like "and now do whatever with this thing I got", for example

(Sin@x+ ...) // Timing

where you decided what to calculate, and then you also want it timed.

One more note:

Really there is much more under the scenes, as the priority of each of these functional constructs is different, but I think that is a much deeper subject and you have to experiment a little before going for subtleties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜