14
$\begingroup$

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

$\endgroup$
0

5 Answers 5

14
$\begingroup$

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] &
$\endgroup$
5
  • 7
    $\begingroup$ I'd say the last line is the definite answer to the question (i.e. using a pure function with multiple arguments). $\endgroup$
    – Yves Klett
    Oct 7, 2014 at 4:27
  • $\begingroup$ The last line is exactly what I was looking for, thanks! $\endgroup$ Oct 7, 2014 at 6:39
  • $\begingroup$ @YvesKlett, it was the definitive answer until V10 operator forms. $\endgroup$ Oct 7, 2014 at 17:57
  • $\begingroup$ @alancalvitti why not post another answer to that regard? $\endgroup$
    – Yves Klett
    Oct 8, 2014 at 3:42
  • $\begingroup$ @YvesKlett, I'll post it as a question as op form doesn't currentyl support variadics for example. I don't have a ready answer. $\endgroup$ Oct 8, 2014 at 4:09
6
$\begingroup$

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.

$\endgroup$
6
$\begingroup$

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}
$\endgroup$
5
$\begingroup$

You can do (but only for two arguments)

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

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ Oct 7, 2014 at 3:11
  • $\begingroup$ While there are several huge infixionados around and infix is useful stuff , this is not actually answering the question... $\endgroup$
    – Yves Klett
    Oct 7, 2014 at 4:32
  • $\begingroup$ Interesting, but hard to read, as infixes tend to be, except perhaps in the case of Join: a ~ Join ~ b. $\endgroup$
    – asterix314
    Oct 7, 2014 at 6:24
  • $\begingroup$ @asterix314 ... well... 1+1 is easy to parse :-) $\endgroup$
    – Yves Klett
    Oct 7, 2014 at 11:51
2
$\begingroup$

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

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

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

$\endgroup$
1
  • $\begingroup$ But operator forms have similar limitation as // in terms of handling variable number of arguments. $\endgroup$ Oct 7, 2014 at 17:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.