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.

Is there any good way to define the square of a function in M9?

Usually one can write e.g. f[z_] = 3*Sin[z] etc etc.

But is it possible to define g^2[z_] = 2*Cos[z] for example?

share|improve this question

closed as off-topic by Yves Klett, Sjoerd C. de Vries, Öskå, ciao, RunnyKine Jun 9 '14 at 22:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, Sjoerd C. de Vries, Öskå, ciao, RunnyKine
If this question can be reworded to fit the rules in the help center, please edit the question.

g[z_]:=f[z]^2? I don't get it. –  Öskå Jun 9 '14 at 16:22
For example define a function g^2 = x or something. –  Love Learning Jun 9 '14 at 16:23
You can't use ^2 in the function name (left side), it has to be in the right side of the definition. Use the method by Oska above. –  Nasser Jun 9 '14 at 16:31
Ok Nasser thanks. –  Love Learning Jun 9 '14 at 16:32
Why not g[z_]:=Sqrt[2*Cos[z]]? Or do you not want g defined, only its square? If that's the case, maybe go at it as a replacement rult, say expr /. g[z]^2->2*Cos[z]. –  Daniel Lichtblau Jun 9 '14 at 22:27

2 Answers 2

You can do something like this more safely using up-values:

g /: g[x_]^2 := g2[x]

(* g[x] *)

(* g2[x] *)

Note that this definition is a structural transfomation rule and has no mathematical meaning. The system won't know that g[x] is Sqrt[g2[x]], nor that g[x]^4 is g2[x]^2.

It does seem from your question that you're not on the right path here though. It would be better to explain what your actual problem is that prompted you to want to do this.

share|improve this answer

What you can do in principal anyway is redefine the Power function for the specific case:

 g[x_] = Sin[x];
 Power[g[x_], 2] = 1-Cos[x]^2;

1 - Cos[y]^2 + Sin[y]

This strikes me as asking for trouble though..note for one thing this redefines Sin[]^2 not just g[]^2 ..

share|improve this answer
It's good to mention that redefining builtins is generally a very bad idea and might cause problems in unexpected ways. (Especially since the OP is a beginner.) Theoretical scenario: suppose some package or builtin uses Block[{g}, ... g[x]^2 ...]. –  Szabolcs Jun 9 '14 at 20:23

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