14

Examples

A convenient shorthand that I use frequently is the "double slash" notation to string commands together. For example, I can write:

x - 1 + x^2 //TraditionalForm

And receive back:

x^2 + x - 1

Or another example:

(x - 1)(x + 2) //Expand //TraditionalForm

Yields:

x^2 + x - 2

However, I have never been able to find a way to input multiple arguments into a function using this notation. That is, I cannot call something like:

x^2 + 9x + 5, 3 //PolynomialMod

Obviously the above can easily be written as:

PolynomialMod[x^2 + 9x + 5, 3]

(but what's the fun in that?)

Question

Is there a way to input multiple parameters using the shorthand postfix operator?

See also: http://reference.wolfram.com/language/ref/Postfix.html

therealrootuser
  • 251
  • 2
  • 6

5 Answers5

14

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_, n_: 3] := PolynomialMod[expr, n]

now

x^2 + 9 x + 5 // f
2 + x^2

In this case you will have fixed value for Optional Arguments which is 3 and if you need to use this method with other value than 3 you need to change 3 in the definition of f above. However, an easy way to do it is as follows:

x^2 + 9 x + 5 // PolynomialMod[#, 3] &
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
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 own "postfix" operator like

Colon[list_, f_] := f @@ list

Then you can write

{x^2 + 9x + 5, 3} ∶ PolynomialMod

That's the \[Colon] character, also entered as Esc:Esc, or you could use any other infix operator with no built-in meaning.

Simon Rochester
  • 6,211
  • 1
  • 28
  • 40
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}
Ernst Stelzer
  • 2,055
  • 12
  • 24
5

You can do (but only for two arguments)

x^2 + 9x + 5 ~ PolynomialMod ~ 3

SEngstrom
  • 1,711
  • 12
  • 14
  • Using the infix notation is an interesting idea, but as you pointed out, it only seems to work on two arguments. And it is a side-step to being able to do this with postfix notation, which is what I want. – therealrootuser Oct 07 '14 at 03:11
  • While there are several huge infixionados around and infix is useful stuff , this is not actually answering the question... – Yves Klett Oct 07 '14 at 04:32
  • Interesting, but hard to read, as infixes tend to be, except perhaps in the case of Join: a ~ Join ~ b. – asterix314 Oct 07 '14 at 06:24
  • @asterix314 ... well... 1+1 is easy to parse :-) – Yves Klett Oct 07 '14 at 11:51
2

Sometimes you can use new Mathematica operator forms (V10+)

{-5, -3, -1, 2, 4, 6} // SortBy[Abs]

{-1, 2, -3, 4, -5, 6}

ybeltukov
  • 43,673
  • 5
  • 108
  • 212