5
$\begingroup$

In Mathematica, is it possible to define a graphic variable/function? Something like $$\text{"A drawing of a pentagon''}[x\_]:=ax^5+bx^4?$$

$\endgroup$
  • $\begingroup$ What exactly are you expecting the output to be here? What do you mean that the graphic should be a function of x? And what does "drawing of a pentagon" have to do with the function that you defined there? We need more details, with an explanation of your desired output, please. $\endgroup$ – march Aug 7 '17 at 18:47
  • $\begingroup$ @march, just an ordinary function f[x_]:=ax^5+bx^4, with f replaced by the picture of a pentagon. $\endgroup$ – Jia Yiyang Aug 7 '17 at 19:02
  • $\begingroup$ Okay, that's not what I expected. Why would you want to do something like this? $\endgroup$ – march Aug 7 '17 at 19:03
  • $\begingroup$ @march, because I have a graph theory problem which needs me to assign different functions to different graphs, and I can't find a systematic and neat way of naming all the graphs. What I asked is nothing vital to my purpose, it's just that it would be nice book keeping. $\endgroup$ – Jia Yiyang Aug 7 '17 at 19:07
10
$\begingroup$

You may use the Notation, Symbolize, and InfixNotation tutorial to define your own notation. This method has the added bonus of not unprotecting built-in symbols.

Below is a screenshot of the notebook followed by the code. The screenshot is added because the InputForm of the templates from the Notation` package are not very easy to read.

Mathematica graphics

Graphics[Polygon@CirclePoints@5, ImageSize -> 30]

Needs["Notation`"]

Paste the following to a new cell and convert the cell to StandardForm from the Cell | Convert To menu. This is the template from the Notation package palette that is not easy to read in input form.

Notation[ParsedBoxWrapper[
    RowBox[{GraphicsBox[PolygonBox[NCache[
         {{Sqrt[5/8 - Sqrt[5]/8], (-1 - Sqrt[5])/4}, 
          {Sqrt[5/8 + Sqrt[5]/8], (-1 + Sqrt[5])/4}, {0, 1}, 
          {-Sqrt[5/8 + Sqrt[5]/8], (-1 + Sqrt[5])/4}, 
          {-Sqrt[5/8 - Sqrt[5]/8], (-1 - Sqrt[5])/4}}, 
         {{0.5877852522924731, -0.8090169943749475}, 
          {0.9510565162951535, 0.30901699437494745}, {0, 1}, 
          {-0.9510565162951535, 0.30901699437494745}, 
          {-0.5877852522924731, -0.8090169943749475}}]], 
       ImageSize -> 30], "[", "x_", "]"}]] \[DoubleLongLeftRightArrow] 
   ParsedBoxWrapper[RowBox[{"poly", "[", "x_", "]"}]]]

In fact the remainder of the code will be just as unwieldy to read as it requires graphics in InputForm so I am going to skip adding it.

Basically you just paste the polygon into an input cell and use it as a symbol of a function with one parameter.

<pasted graphic>[value]

By assigning a definition to poly the graphic symbol will resolve to that function and evaluate.

poly[x_] := a x^5 + b x^4

Further graphic symbols can be created in the same manner by creating a new notation with the templates from the notation palette. Of course, each graphic will need its own function to map to.

Hope this helps.

$\endgroup$
  • $\begingroup$ Thanks. What's the use of Needs["Notation`"]? $\endgroup$ – Jia Yiyang Aug 9 '17 at 2:35
  • $\begingroup$ @JiaYiyang It loads the Notation` package tat comes with Mathematica. $\endgroup$ – Edmund Aug 9 '17 at 10:52
9
$\begingroup$

What I think you want is indeed possible since Mathematica 6. We need to unprotect Graphics to allow the rule to be attached, but then this works:

enter image description here

I copied and pasted the output of the first line into the definition on the third line, and the application on the fourth line.

$\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.