Message Boards Message Boards


New to Mathematica Language - Questions on Context

Posted 10 years ago
2 Replies
1 Total Likes
I am new to Mathematica programming and am trying to develop a package.  My questions are:
  1. I have a small set of functions defined in a package inside of a Begin["`Private`"].  If I call one of my functions from a notebook, I cannot get the numeric value of the function (even if I use //N).  What is the correct way to call a function embedded in a package?  The code of my simple package is below.
     (* Exported symbols added here with SymbolName::usage *)
     averagePowerRC::usage = "averagePowerRC[V, R, C, f] returns the average power of an RC circuit for V volts and f Hertz."
     capacitanceParallelPlate::usage = "capacitanceParallelPlate[A, d, relativePermittivity, freePermittivity] returns the capacitance of a parallel plate condensor neglecting fringe effects."
     reactance::usage = "reactance[C,f] returns capacitive reactance in ohms."
     resistance::usage = "resistance[A, d, resistivity] returns resistance in ohms (Pouillet's law)."
    (* Implementation of the package *)

    x = 1/ (2 Pi f C);
    p = 0.5 V^2 R / (R^2 + x^2)

    capacitanceParallelPlate[A_,d_,relativePermittivity_,freePermittivity:8.854 10^-12]:=
    c = relativePermittivity freePermittivity A / d

    resistance[A_, d_, resistivity_]:=
    r = resistivity d / A

    x= 1/(2 Pi f C)


  2. My call from my notebook is below (I suspect that this is the problem):
    << EKRScaleupPower`
    cap20L = capacitanceParallelPlate[0.02^(2/3), 0.02^(1/3), 50]
  3. The first time I tried to call this from my .nb, I received an error message saying that my function was found in multiple contexts.  If I understand contexts properly, they are similar in idea to the .net namespace.  A logical collection of classes and symbols.  In .net, if I have the same method in multiple namespaces or classes, I can specify the method that I want with a call that uses a Namespace.Class.Method syntax.  For example, FunctionGeneratorsNamespace.Agilent33220a.SquareWave(20, 120) would call the SquareWave method in the Agilent33220a class from the FunctionGeneratorsNamespace.  It looks like the context in Mathematica is similar.  Could anyone point me to a set of example code that clearly demonstrates Mathematica contexts?
POSTED BY: Doug Kimzey
2 Replies
Nasser - Thanks most kindly.  I agree with the note on the using the verbose context name. 
POSTED BY: Doug Kimzey
cap20L = capacitanceParallelPlate[0.02^(2/3), 0.02^(1/3), 50]
You needed to change the optional argument from
freePermittivity:8.854 10^-12
freePermittivity_:8.854 10^-12
A call to a function can also be done by explicitly putting the context name in the call. I myself like to do that when I am using more than one package even though it is not needed, but It helps when I am looking at the code later to remind me from which package the function came from. In your case, the call would now become like this:
cap20L = EKRScaleupPower`capacitanceParallelPlate[0.02^(2/3),0.02^(1/3), 50]
(* 1.20167*10^-10 *)
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract