4
$\begingroup$

Several functions work with symbols in MMA: Take for example Part:

l1=l2=Range[4];
l1[[3]]
(*3*)
l2[[4]]
(*4*)

I want to make my own function work in the same way.

I can do some thing like this:

p[l_,x_]:=l[[x]]
p[l1,3]
(*3*)
p[l2,4]
(*4*)

Now I want to use a symbol for example assign [[[]]]] or [*@] (or any other symbol) to my p function so that I can do something like this:

l1[[[3]]]
(*3*)
l2[[[4]]]
(*4*)

or

l1[*@3]
(*3*)
l2[*@4]
(*4*)
$\endgroup$
  • $\begingroup$ What would you like your operator to do though, and what syntax do you have in mind for it? As a possible starting point, you might want to take a look at the list of existing operators without built-in meanings and try to assign your operation to one of those. $\endgroup$ – MarcoB May 26 '15 at 18:00
  • $\begingroup$ I don't think you can define operator symbols yourself, but you can use undefined ones like CirclePlus. $\endgroup$ – Sjoerd C. de Vries May 26 '15 at 18:02
  • $\begingroup$ @SjoerdC.deVries how to do that? I don't know even how to assigned any operator symbols to function. $\endgroup$ – Algohi May 26 '15 at 18:04
6
$\begingroup$

I'll give you a simple example of one operator without built-in meaning that I have re-purposed.

I recently found myself plotting functions or evaluating integrals on a square domain centered on the origin that could be represented by Rectangle[{-a, -a}, {a, a}]. Tired of typing in the Rectangle expression, I noticed that there is a Square expression and operator (esc+sq+esc, $\square$) that is defined but has no meaning, so I assigned it one as follows:

Clear[Square]
Square[x_ /; x > 0] := Rectangle[{-x, -x}, {x, x}]

I can now write expressions like the following:

ContourPlot[Cos[(x^2 - y^2)/3], {x, y} \[Element] \[Square](2 Pi), PlotPoints -> 50]

which will look like this in the notebook:

ContourPlot expression

ContourPlot output

... or like the following:

Integrate[Cos[(x^2 - y^2)/3], {x, y} \[Element] \[Square]3]

which will look like this:

integral expression

integral output

$\endgroup$
4
$\begingroup$

This solution doesn't provide quite the flexibility that you've given as an example but may still be satisfactory:

Have a look at: https://reference.wolfram.com/language/tutorial/OperatorsWithoutBuiltInMeanings.html and How to define custom operators

That'll allow you to assign a function to an unused (or used) in-fix operator. Operator precedence may be important to consider if you do this (https://reference.wolfram.com/language/tutorial/OperatorInputForms.html). I don't remember if you can / how to redefine operator preference to suit your needs.

$\endgroup$
2
$\begingroup$
l1 = l2 = Range[4];
p[l_, x_] := l[[x]]

This is a example of MakeExpression. You might need to make a new expression like this.

MakeExpression[RowBox[{l_, "[", 
      RowBox[{"[", RowBox[{"[", x_, "]"}], "]"}], "]"}], StandardForm] := 
 MakeExpression[RowBox[{"p", "[",RowBox[{l, ",", x}], "]"}], StandardForm]

l1[[[3]]]
(*3*)

And other example.

MakeExpression[RowBox[{l_, "[", 
      RowBox[{"*", RowBox[{"@", x_}]}], "]"}], StandardForm] := 
 MakeExpression[RowBox[{"p", "[", RowBox[{l, ",", x}], "]"}], StandardForm]

l1[*@3]
(*3*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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