1

I am currently working on library that keeps functions in unevaluated form, e.g.

In: a = RhoVariable[s]
Out: RhoVariable[s]

RhoVariable has some definition, namely (2-Sqrt[4-s])/(2+Sqrt[4-s]) and I want to be able to convert it to that form using Normal[ (*some object containing RhoVariable*) ]. Well, I can do that defining and UpValue for it:

RhoVariable/:Normal[RhoVariable[z_]] := (2-Sqrt[4-z])/(2+Sqrt[4-z]);

which works fine if I put it very explicitly

In: Normal[RhoVariable[t]]
Out: (2-Sqrt[4-t])/(2+Sqrt[4-t]);

but not in any other case

In: Normal[2 RhoVariable[t]]
Out: 2 RhoVariable[t]

and specifying Head does not change anything, not to mention is kinda not what I want, cos the usage of all Heads I'll have to convert later in development.

In: Normal[2 RhoVariable[t], RhoVariable]
Out: 2 RhoVariable[t]

How to make it behave like I expect (= change any occurrence of RhoVariable to expression inside normal blocks, kinda like instead of every Normal[ (*expressions with RhoVariables*) ] I'd invoke (*expressions with RhoVariables*) /. RhoVariable[z_]] :> (2-Sqrt[4-z])/(2+Sqrt[4-z]);

fqrt
  • 84
  • 2
  • 3
    I find it much easier to use some combination of Hold and ReleaseHold. You could also add arguments to your function definition that trigger evaluation. You could also create two (differently named) versions of the function and just replace the heads when you want to trigger evaluation. – lericr Mar 15 '22 at 16:53
  • 3
    But without knowing why you're doing this, I can't formulate a detailed answer. Like, how are you currently "keep[ing] functions in unevaluated form"? What determines when you want to see the unevaluated form and when you want to see the evaluated form? Will there be any composition/nesting (and do the same rules always apply simultaneously to all of these fancy expressions)? – lericr Mar 15 '22 at 16:54
  • Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory [tour] now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Mar 15 '22 at 17:22
  • @lericrc function RhoVariable has no definition. There will be loads of compositions and my goal was overloading "Normal" to make it possible to get maths formulas from these expression when possible. – fqrt Mar 16 '22 at 10:57
  • 1
    @fqrt it would be sooo much easier to just replace the value of RhoVariable in the final expression though! So do all your symbolic computation with undefined RhoVariable then, when you are done, use finalResult /. RhoVariable[t_] :> (2-Sqrt[4-t])/(2+Sqrt[4-t])? You could package that up in a named transformation rule if you like. – MarcoB Mar 16 '22 at 15:48

0 Answers0