0
$\begingroup$

I came across the following way of defining complex conjugate in mathematica [a]:

enter image description here

SuperStar[y_] := y /. Complex[u_, v_] -> Complex[u, -v]

I am able to run this program, but I don't understand the way of defining it.

For example, let us define a function f as

f[x_] := x^2 + 1

Then I define (instead of *, I am using n):

f_^n := f /. t_ -> t + 1

Evaluating this results in following output:

"Tag Power in f_^n is Protected. "

However, for * in the superscript, it works fine.

I want to understand how they have defined the function? Does it use pattern matching? What is special with "*" in superscript position?

[a]: Patric Tam: A physicist guide's to mathematica

$\endgroup$
2
$\begingroup$

It looks like if you define a function such as

$g\_^+ := /.g \rightarrow -g$

where the + is entered using a superscript, then $5^+$ gets you -5. So it seems that it doesn't like numbers or variables, but other kinds of symbols are okay. I think when you do f_^n, it replaces it with Power[f_, n] := f/.t_ -> t+1. Power is protected so that you don't accidentally change it and all of a sudden break your ability to calculate powers properly.

It looks to me like it is using pattern matching to find any instance where you have some thing with a superscript star and replaces it. Similarly with my + example, you can do $(a b c)^+$ and get $-abc$.

It looks like you're mostly interested in understanding function definitions and pattern matching, so you probably already know this but I thought I'd mention it just in case, MMA does have a built-in conjugate symbol that can be entered as Esc conj Esc.

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