Tag Info

Hot answers tagged

5

We can replace something, in all definitions associated with a symbol, using function like this: ClearAll[replaceExtendedDefinition] SetAttributes[replaceExtendedDefinition, HoldFirst] replaceExtendedDefinition[sym_, rules_] := Replace[ Language`ExtendedDefinition[sym] , (rule:Rule | RuleDelayed)[lhs_, rhs_] :> ...


5

If you define Diff in a private context, like BeginPackage["Diff`"] Diff::usage = "" Begin["`Private`"] Diff[yy_] := Module[{cc}, cc = D[yy, x[1]]; cc]; End[] EndPackage[] then you will get << Diff` Diff[x[1]^2] (* 0 *) That's because the x referred to in the package is in the Diff`Private` context: Definition@Diff // InputForm (* ...


5

A Mathematica package is just a file with the .m or .wl extension that contains Mathematica expressions. Get will simply evaluate these expressions. The "Run Package" button will simply evaluate everything in the package file. It's equivalent to Get["/path/to/mypakcage.m"]. The "Update" button rebuilds the Function and Section menus just left of this ...


4

Only initialization cells will be saved as uncommented code. Check Cell -> Cell Properties -> Initialization Cell and Initialization Group. You can also use Code cells (instead of Input), which have the Initialization Cell property by default. Alt-8 or Command-8 inserts a code cell. You can check that it is an initialization cell by looking at the top ...


4

I found, in the "Applications" section of the documentation of ClearAll, this authoritative recommendation: Unprotect and clear all symbols in a package, to allow it to be read twice: Begin["`Private`"]; Unprotect["`*"]; ClearAll["`*"]; f[x_] := x^2 Protect[f]; End[]; I don't see this advice in the guide here for Setting up Wolfram Packages, ...


3

This is messy and imperfect, but it will work in simple cases: Import the package contents: pack = ImportString[data, {"Package", "HeldExpressions"}] (* {HoldComplete[BeginPackage["Test`"]], HoldComplete[testFunction::usage = "-";], HoldComplete[Begin["`Private`"]], HoldComplete[testFunction[] := {123, explicitvalue, Hold[explicitvalue]};], ...


3

When using automatically generated packages I am selective about which Cells I mark for Initialization and therefore transference to the package file. I don't find the need to include ClearAll in such cases. I do use a Unprotect / Protect paring which at least prevents additional definitions from accidentally being made elsewhere. A case where ClearAll ...


3

Another possibility with built-in functions: (* Define the process *) proc = ItoProcess[\[DifferentialD]s[t] == \[Mu] s[t] \[DifferentialD]t + \[Sigma] s[t] \[DifferentialD]w[t], s[t], {s, s0}, {t, 0}, w \[Distributed] WienerProcess[]] Now the process can be used as: Expectation[z, z \[Distributed] proc[t]] (* E^(t \[Mu]) s0 *) ...


2

A comment from @Diogo brought me on the right track: In[1]:= <<ItosLemma` In[2]:= dS=ItoMake[S[t],\[Mu] *S,\[Sigma]*S] Out[2]= dt S \[Mu]+S \[Sigma] Subscript[dB, 1] In[3]:= ItoD[Log[S[t]]] Out[3]= dt (\[Mu]-\[Sigma]^2/2)+\[Sigma] Subscript[dB, 1]



Only top voted, non community-wiki answers of a minimum length are eligible