1
$\begingroup$

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.

$\endgroup$
1
  • 2
    $\begingroup$ Don't use capital letters for the starting char of your vars/functions. You'll collide with Mma internal symbols $\endgroup$ Commented Sep 14, 2012 at 6:51

2 Answers 2

2
$\begingroup$

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 *) 
$\endgroup$
1
  • $\begingroup$ Thanks @b.gatessucks, I guess that would work. But in the latter example, I would have to input the m1, m2 data twice. $\endgroup$ Commented Sep 14, 2012 at 6:50
2
$\begingroup$

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.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.