1

I would like to know how I to define a function with a nested set of parameters (or whatever you would like to call it). For example, how would I get the following function A,

M = m1 + m2;
\[Eta] = m1*m2/(m1 + m2)^2
A[t_]:= M (\[Eta] / M * t)^(1/4);

Such that if I just evaluated A[5.0] for example it would return,

1.49535 M Power[\[Eta]/M, (4)^-1]

(i.e. a function of M and \[Eta]) or something similar, but not A as a function of m1, m2,

1.49535 Power[(m1 m2)/(m1+m2)^2, (4)^-1] (m1+m2)

But, at the same time, maintain a form that I could replace either m1 and m2 or M and \[Eta], such that both

A[5.0] /. m1 -> 1 /. m2 -> 1
A[5.0] /. M -> 2 /. \[Eta] -> 1/4

would return 1.77828.

ShaunH
  • 807
  • 7
  • 12

2 Answers2

2

You could define (heeding @belisarius comment) :

bigM[m1_, m2_] = m1 + m2;
\[Eta][m1_, m2_] = m1*m2/(m1 + m2)^2
bigA[m_, eta_, t_] := m (eta/m*t)^(1/4);

Then :

bigA[m, eta, 5.0]
(* 1.49535 (eta/m)^(1/4) m *)

bigA[bigM[1, 1], \[Eta][1, 1], 5.0]
(* 1.77828 *)
b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84
  • Thanks @b.gatessucks, I guess that would work. But in the latter example, I would have to input the m1, m2 data twice. – ShaunH Sep 14 '12 at 06:50
2

As belisarius comments it is best to avoid using Symbol names that are or start with capital letters to avoid collisions with existing or future system functions. Nevertheless I will preserve your usage for clarity.

Taking the question at face value you might use something like this:

def[_, value_?NumericQ] := value
MakeBoxes[def[name_, value_], _] := name

M = def["M", m1 + m2];
\[Eta] = def["\[Eta]", m1*m2/(m1 + m2)^2];
A[t_] := M (\[Eta]/M*t)^(1/4);

A[5.0]
1.49535 M (\[Eta]/M)^(1/4)
A[5.0] /. m1 -> 1 /. m2 -> 1
A[5.0] /. M -> 2 /. \[Eta] -> 1/4
1.77828

1.77828

If this does not work in practice you will need to provide some additional context for your question.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371