Skip to main content
Tweeted twitter.com/#!/StackMma/status/571094295131918336
added 5 characters in body; edited title
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

How can a function know in wichwhich function it was evaluated?

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wichwhich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange]myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dontdoesn't work in generallgeneral for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; h[2] 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

How can a function know in wich function it was evaluated?

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; h[2] 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

How can a function know in which function it was evaluated?

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in which Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that doesn't work in general for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; h[2] 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

added 12 characters in body
Source Link
sacratus
  • 1.6k
  • 11
  • 18

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]];   h[2] 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]];   h[2] 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

added 11 characters in body
Source Link
sacratus
  • 1.6k
  • 11
  • 18

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{},   If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize] OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{}, If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

Hello my second question here might be very hard.

I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function f?

To be more precise: Suppose there is a unknown number of functions {f1,f2,f3,...}, how can g know in wich Function it was evaluated?

Pseudocode:

f1[g] "--inside-of-g-->" f1 f3[g] "--inside-of-g-->" f3 

I have an Idear how to work on this problem, but it works not in every case.

EDIT:

ok, i wanted to reduce my problem to the relevant point, but i can also explain you, what i exactly want to do with it.

I want to build up a function myTicks[plotRange] that generates Axes-Ticks for a Plot the way i want. My first function works like this:

Plot[Sin[x], {x,0,10}, Ticks->{myTicks[{0,10}, StepSize-> 0.5], Automatic}] 

Now i want to change the plotRange of myTicks to an optional argument like:

MyTicks[plotRange_:Automatic, opt:OptionsPattern[]] := Module[{},  If[plotRange =!= Automatic, Range[plotRange〚1〛, plotRange〚2〛, OptionValue[StepSize]], (* magical lookup for the PlotRange of the Plot *)]] 

So now it should be possible to call myTicks without giving the actual PlotRange of the outer Plotfunction.

I hope that i did not confuse you more.

EDIT2: here goes my simple Solution that dont work in generall for me:

My simple Solution is a little hackish and works not in every case:

In:

g[arg_: "default"] := Module[{read, expr}, read = NotebookRead[EvaluationCell[]]; expr = ToExpression[ReplaceAll[read, Cell[x__, y__] :> x], StandardForm, Hold]; Print[expr]; Sin[arg] ] f[g[2]] 

Out:

Hold[f[g[2]]] f[Sin[2]] 

In this Case expr holds enough Information to work with, so that one could filter out the functionname f, but there is a big problem with this solution:

It only works, if the function g appears explicitly in the EvaluationCell. something like:

h[a_] := a*f[g[2]]; 

wont work (expr == Hold[h[2]])

A second problem is, that when the EvaluationCell holds more than one line, it gets very complicated to filter out in wich line the function was called. Just think about if the function g is called more than once in the EvaluationCell.

So my solution is not very satisfying in general.

A good example of a functionality like this, is the build-in-Mathematica function OptionValue. So there must be a better way to do this!

added 51 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
edited tags
Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
i added my own solution to the question because it did not fully answer my question
Source Link
sacratus
  • 1.6k
  • 11
  • 18
Loading
Added my precise Problem, that led me ask the question
Source Link
sacratus
  • 1.6k
  • 11
  • 18
Loading
Added my precise Problem, that led me ask the question
Source Link
sacratus
  • 1.6k
  • 11
  • 18
Loading
added 84 characters in body
Source Link
sacratus
  • 1.6k
  • 11
  • 18
Loading
Source Link
sacratus
  • 1.6k
  • 11
  • 18
Loading