Skip to main content
added 660 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

The key detail is appending Function @@ {λ, Pstatesalpha[λ]} which will build a function with the evaluated expression Pstatesalpha[λ] as the body and the parameter λ.

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question isSince the above was Accepted (thanks) let me explain a bit ambiguous, but here's a guess at what you want:better.

You hadI use the method fn @@ {args} to evaluate args and pass them to a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like: fn (using Apply) that otherwise holds its arguments (has a Hold attribute).

expr = f[a, b]
a^2 + b/3

AndIn the definition was lost:

Clear[f]

Now you can recovercode above, copied from the definition withquestion, there is a global symbol λ which if assigned a value will break the code. One method to avoid this is to use a Formal Symbol, entered Esc$xEsc where x is any a-z letter. These symbols exist specifically for cases like this. Another method is to use Slot as in the syntax # + 2 & (notethe =FullForm rather than usualof :=# is Slot[1]). That might look like this (in context):

f[a_AppendTo[RankMatrix, b_]{Alpharank, =Evaluate[Pstatesalpha[#]] expr;&}]

TestEvaluate is used instead of Apply and List, but many methods are possible:

f[5Evaluate[Pstatesalpha[#]] &

Function @@ {Pstatesalpha[#]}

With[{body = Pstatesalpha[#]}, 7]body &]

Pstatesalpha[#] /. x_ :> (x &)
82/3

With each of these Pstatesalpha[#] is evaluated outside of Function and then inserted into it.

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

The key detail is appending Function @@ {λ, Pstatesalpha[λ]} which will build a function with the evaluated expression Pstatesalpha[λ] as the body and the parameter λ.

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question is a bit ambiguous, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3

Second try.

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

The key detail is appending Function @@ {λ, Pstatesalpha[λ]} which will build a function with the evaluated expression Pstatesalpha[λ] as the body and the parameter λ.

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

Since the above was Accepted (thanks) let me explain a bit better.

I use the method fn @@ {args} to evaluate args and pass them to a function fn (using Apply) that otherwise holds its arguments (has a Hold attribute).

In the code above, copied from the question, there is a global symbol λ which if assigned a value will break the code. One method to avoid this is to use a Formal Symbol, entered Esc$xEsc where x is any a-z letter. These symbols exist specifically for cases like this. Another method is to use Slot as in the syntax # + 2 & (the FullForm of # is Slot[1]). That might look like this (in context):

AppendTo[RankMatrix, {Alpharank, Evaluate[Pstatesalpha[#]] &}]

Evaluate is used instead of Apply and List, but many methods are possible:

Evaluate[Pstatesalpha[#]] &

Function @@ {Pstatesalpha[#]}

With[{body = Pstatesalpha[#]}, body &]

Pstatesalpha[#] /. x_ :> (x &)

With each of these Pstatesalpha[#] is evaluated outside of Function and then inserted into it.

added 177 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

The key detail is appending Function @@ {λ, Pstatesalpha[λ]} which will build a function with the evaluated expression Pstatesalpha[λ] as the body and the parameter λ.

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question is a bit ambiguous, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question is a bit ambiguous, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

The key detail is appending Function @@ {λ, Pstatesalpha[λ]} which will build a function with the evaluated expression Pstatesalpha[λ] as the body and the parameter λ.

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question is a bit ambiguous, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3
added 537 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question is a bit ambiguiousambiguous, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3

The question is a bit ambiguious, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3

Second try. (old answer preserved below for reference)

In the comments below Spawn's answer you said that you are trying to construct a table of functions. Does this do what you want?:

Pstatesalpha[λ_] = 0;

RankMatrix = {};

For[jsum = 1, jsum <= 3, jsum++,
 Pstatesalpha[λ_] = (delem*a0)^2*SumPstatesalpha[λ] + Pstatesalpha[λ];
 AppendTo[RankMatrix, {Alpharank, Function @@ {λ, Pstatesalpha[λ]}}]
]

Now:

RankMatrix[[1, 2]][17]
a0^2 delem^2 SumPstatesalpha[17]

The question is a bit ambiguous, but here's a guess at what you want:

You had a function:

f[a_, b_] := a^2 + b/3

Somewhere it was transformed like:

expr = f[a, b]
a^2 + b/3

And the definition was lost:

Clear[f]

Now you can recover the definition with (note = rather than usual :=):

f[a_, b_] = expr;

Test:

f[5, 7]
82/3
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading