Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How might I construct a function of the form Rep[defs,code], graphic[code] and display[code] Such that the following outputs can occur?

Rep[
 y[a__] := Graphics[{Circle[],
    Inset[
     graphic[a]
     , Scaled[{0.498, 0.5020000000000002}], Center, 
     Scaled[{0.5, 0.5}]]}, ImagePadding -> 0,
   PlotRangePadding -> 0];
 Times[a_, b_] := Graphics[{RGBColor[1, 0, 0], Rectangle[{0, 0}],
    Black, Inset[
     display[a*b]
     , Scaled[{0.498, 0.5020000000000002}], Center, Scaled[{0.5, 0.5}]]
    }, ImagePadding -> 0, ImageSize -> {85., Automatic}, 
   PlotRangePadding -> 0];
 , graphic[y[y[a*b]]]
 ]

enter image description here

Rep[ 
 Sum[f_, {i_, max_}] :=
  Graphics[{Circle[],
    Inset[
     Sum[f, {i, max}]
     , Scaled[{0.498, 0.5020000000000002}], Center,
     Scaled[{0.5, 0.5}]]}, ImagePadding -> 0,
   PlotRangePadding -> 0],
 graphic[Sum[x^2 , {x, 10}]]
 ]

enter image description here

Here is my current attempt of creating Rep[funs,code]. The biggest problem that I have currently, is that I am trying to define custom functions on top of System attributes(Times & Plus). In addition I was originally going to add a namespace Gr to the passed in variables but I feel that there is likely a better way.

Questions are welcome around the current implementation or a slightly better/different implementation.

My question: How can I construct the functions above such that they satisfy the graphic outputs?

share|improve this question

closed as unclear what you're asking by Kuba, Artes, halirutan, rm -rf Aug 30 '13 at 11:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Related: mathematica.stackexchange.com/q/4343/5 –  rm -rf Aug 18 '13 at 1:42
    
@rm-rf Thank you for the link. I think it different(correct me if wrong) because my centers around the issue of being unable to re declare System` values. I attempted to temporary modify System` values with the chat with Mr.Wizard here mathematica.stackexchange.com/q/29779 But ultimately it seems values like Times and Plus can't be reassigned it scenarios like this 2*3. That has lead me to replacing all values to slightly modified forms to get around this issue. –  Liam William Aug 18 '13 at 1:49
2  
I feel that it has not received enough attention due to the lack of description of what you want to create. –  Kuba Aug 21 '13 at 6:26
3  
@Kuba I still don't understand what this question is about. Sometimes code is more descriptive than text but in this case I have no idea what the purpose is. –  Mr.Wizard Aug 22 '13 at 4:50
1  
I agree with Mr Wizard, this question would benefit from an introductory paragraph explaining the purpose. –  Simon Woods Aug 22 '13 at 11:00
show 4 more comments

1 Answer 1

So it looks like what you want to do is apply the temporary definitions in defs to code, then show the output. Here's what I came up with.

SetAttributes[Rep, HoldAll];

Rep[defs_, code_] := Module[{symsTrans, downVals},
    symsTrans = Union[Cases[Hold[defs], 
        HoldPattern[SetDelayed][f_[___], _] :> (f -> "changeMe"[f]), {0, \[Infinity]}]];

    downVals = Union[Cases[Hold[defs], HoldPattern[SetDelayed][f_[a___], b_] :> 
        RuleDelayed[HoldPattern["changeMe"[f][a]], b], {0, \[Infinity]}]];

    ReleaseHold[Hold[code] /. symsTrans //. downVals]
]

(* flatten all graphics into one *)
graphic[obj_] := Module[{ops},
    ops = Sequence @@ Flatten[Cases[obj, Graphics[l_, op___] :> {op}, {0, \[Infinity]}]];

    Graphics[{obj}, ops] //. {
        Graphics[{a___, Graphics[l_, ___], b___}, o___] :> Graphics[{a, l, b}, o]}
]

(* not sure what this should be *)
display[expr_] := Text[expr]

Here, symsTrans will only allow your custom down values to be applied to the expressions only visible through code i.e. since your custom Sum contains Sum, we'd be in an infinite loop applying this definition.

Here are the outputs:

Rep[
 y[a__] := Graphics[{Circle[],
    Inset[
     graphic[a]
     , Scaled[{0.498, 0.5020000000000002}], Center, 
     Scaled[{0.5, 0.5}]]}, ImagePadding -> 0,
   PlotRangePadding -> 0];
 Times[a_, b_] := Graphics[{RGBColor[1, 0, 0], Rectangle[{0, 0}],
    Black, Inset[
     display[a*b]
     , Scaled[{0.498, 0.5020000000000002}], Center, Scaled[{0.5, 0.5}]]
    }, ImagePadding -> 0, ImageSize -> {85., Automatic}, 
   PlotRangePadding -> 0];
 , graphic[y[y[a*b]]]
 ]

enter image description here

Rep[ 
 Sum[f_, {i_, max_}] :=
  Graphics[{Circle[],
    Inset[
     Sum[f, {i, max}]
     , Scaled[{0.498, 0.5020000000000002}], Center,
     Scaled[{0.5, 0.5}]]}, ImagePadding -> 0,
   PlotRangePadding -> 0],
 graphic[Sum[x^2 , {x, 10}]]
 ]

enter image description here

Let me know if this is what you want (It wasn't very clear to me) or if this doesn't work for other examples you may have.

share|improve this answer
add comment

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