0
$\begingroup$

I'm sure this is a pretty common question, but I have been unable to find anything that explains what I am trying to do. Say I have some code I use quite frequently:

Clear[σDATA, ΔσDATA];
σDATA = {};
ΔσDATA = {};

Do[
    PartitionedData = Partition[DATA, j];
    Clear[MeanParitionedData];
    MeanParitionedData = {};
    Clear[MeanDiffList];
    MeanDiffList = {};
    Do[
        AppendTo[MeanParitionedData, Mean[PartitionedData[[i]]]];,
        {i, 1, Length[PartitionedData]}
    ];
    Do[
        AppendTo[
    MeanDiffList, (MeanParitionedData[[k + 1]] - 
      MeanParitionedData[[k]])^2];,
        {k, 1, Length[MeanParitionedData] - 1}
    ];
    σ = (1/(Length[MeanParitionedData] - 1)*
    Total[MeanDiffList[[1 ;;]]])^(1/2);
    AppendTo[σDATA, σ];
    AppendTo[ΔσDATA, σ/(2 (Length[
        MeanParitionedData] - 1))^(1/2)];,
    {j, 1, NumberPoints/4}
 ]

Where DATA is my input and σDATA,ΔσDATA are my outputs.

How can I save this routine such that I can call it with a short line like

RoutineName[DATA]

and this would output my two lists, σDATA and ΔσDATA, which I can then plot or do whatever I want with.

Note: I am aware that the routine is probably not so optimised in Mathematica but I need to keep it in this structure for a few reasons.

$\endgroup$
  • 1
    $\begingroup$ How can I save this routine such that I can call it with a short line E.g. RoutineName[DATA] one way might be to make it a Module and then call it each time you need to use it. And better not use UpperCaseFirstLetter for function names, as it confuses these names with Mathematica own functions. $\endgroup$ – Nasser Dec 7 '17 at 15:00
  • $\begingroup$ @Nasser Thanks for the reply. Can I store Modules in a separate notebook or file and call them, rather than having to put it in the same notebook each time. Essentially is there a user library or something? $\endgroup$ – Q.P. Dec 7 '17 at 15:15
  • $\begingroup$ Check out packages. $\endgroup$ – chuy Dec 7 '17 at 15:19
  • $\begingroup$ Can I store Modules in a separate notebook or file and call them Yes. That is what I do. Another option it to use packages. Here is a question on first option. code-reuse-between-files-in-the-same-directory please see if this question answers what you want. Just put all the functions (i.e. definitions) in one notebook, and evaluate that notebook. Then put the calls to these functions in a separate notebook. This keeps definitions separate from actual use. $\endgroup$ – Nasser Dec 7 '17 at 15:21
  • 1
    $\begingroup$ There is one more option. You may store just your data and/or the output in a separate notebook. The data may be large. In such a case in order to make it comfortable I make a notebook with a Section and eventually a Subsection containing an input cell in which I write Data= ; and then between = and ; I copy-paste the list Data. The same I make with [Sigma]DATA and [CapitalDelta][Sigma]DATA. Then I select this cell, go to Menu/Cell/Cell Properties/Initialization Cell. Then Save the notebook. $\endgroup$ – Alexei Boulbitch Dec 7 '17 at 15:33
2
$\begingroup$

You might try something like this:

sigmaizer[data_] :=
  Module[
      {nPts, σDATA, ΔσDATA, PartitionedData, MeanParitionedData, MeanDiffList, σ},
    nPts = Length[data]; (* this is a guess *)
    σDATA = {};
    ΔσDATA = {};
    Do[
      PartitionedData = Partition[DATA, j];
      MeanParitionedData = {};
      MeanDiffList = {};
      Do[
        AppendTo[MeanParitionedData, Mean[PartitionedData[[i]]]], 
        {i, 1, Length[PartitionedData]}];
      Do[
        AppendTo[
          MeanDiffList, 
          (MeanParitionedData[[k + 1]] - MeanParitionedData[[k]])^2], 
        {k, 1, Length[MeanParitionedData] - 1}];
      σ = (1/(Length[MeanParitionedData] - 1)*Total[MeanDiffList])^(1/2);
      AppendTo[σDATA, σ];
      AppendTo[ΔσDATA, σ/(2 (Length[MeanParitionedData] - 1))^(1/2)], 
      {j, 1, nPts/4}];
    {σDATA, ΔσDATA}]

You would call it like this:

{σ, Δσ} = sigmaizer[DATA]

Notes

  • I have made a guess concerning your undefined variable NumberPoints. If my guess is wrong, the code will need to be modified.
  • There may be bugs — I could not test the code because you did not supply sample data.
  • The code localizes all your intermediate variables so you don't have to worry about clearing values from them.
$\endgroup$
  • $\begingroup$ Perfect, yes you were correct about the NumberPoints. This is exactly what I was after. $\endgroup$ – Q.P. Dec 7 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.