0
$\begingroup$

I made the following package file :

BeginPackage["thermotoolpackage`"]

LLog[x_] := If[x != 0, Log[x], 0]

H2[x_] := (-x)*LLog[x] - (1 - x)*LLog[1 - x]

InvH2BrancheDroite[x_] := 1 - InverseFunction[H2][x]
EntropyMat[M_] := Module[{eigenValues, entropy},
  eigenValues = Eigenvalues[M];
  entropy = -Sum[
     If[eigenValues[[i]] != 0, eigenValues[[i]]*Log[eigenValues[[i]]],
       0], {i, Length[eigenValues]}]
  ]
VectToDensityMat[v_] := Module[{rho},
  rho = Array[v[[#1]]*Conjugate[v[[#2]]] &, {Length[v], Length[v]}]
  ]

test[x_] := x + 1
EndPackage[]

The cell of this package is parametrised as "initialization cell".

In my code, I first load the package :

<< thermopackage`

But all functions excepted test are recognized. If I do test[2] it does not recognize it a a function (whereas LLog, H2 etc are recognized).

I actually just reopened this old package I made in the past and wanted to add functions (like the example test), and the recent functions I added to my package aren't recognized, and I don't get why.

They are only recognized if I first open the package and run it.

How to make it work ? Mathematica seems again very complicated for a very basic task.

$\endgroup$
  • $\begingroup$ Mathematica does not need to be complicated. What is in you actual package file? You do not say it explciitly, but it seems that you are saving a notebook as a package, and only show us what you see in the notebook. There is often no reason to do this, as package files can be edited as if they were a notebook. Almost certainly, your package file does not contain what you believe it does. $\endgroup$ – Szabolcs May 8 at 17:12
  • $\begingroup$ @Szabolcs hmmm I'm not sure to totally get what you mean, but my package is saved as a .wl file (when I open it with mathematica there is the icon "run package" for example that doesn't shows up for an usual notebook). $\endgroup$ – StarBucK May 8 at 17:18
  • 3
    $\begingroup$ OK, that clears up one bit, but I still don't understand fully: are you saying that << thermopackage` and pressing Run Package have different effects? Then << thermopackage` does not load the file you think it is loading. Evaluate FindFile["thermopackage`"] to see what it loads. $\endgroup$ – Szabolcs May 8 at 17:37
  • $\begingroup$ @Szabolcs you are right. It is totally my mistake I loaded another file actually. Thank you very much. $\endgroup$ – StarBucK May 9 at 8:50
  • $\begingroup$ You can use Get["/path/to/file"] to load a specific file. Speaking of complicated things, file lookup is complicated. mathematica.stackexchange.com/a/133275/12 I think it's a better idea to only use the Get["context`"] syntax with packages that are properly installed in a standard location (such as $UserBaseDirctory/Applications). $\endgroup$ – Szabolcs May 9 at 8:53
2
$\begingroup$

I put your code in a initialization cell and saved it to my desktop as thermopkg.wl. I quit my local and restarted it fresh notebook. I load your package with

Needs["thermotoolpackage`", FileNameJoin[{$HomeDirectory, "Desktop", "thermopkg.wl"}]]

I then evaluated

?? test

and got

into.png

So it looked like test was loaded OK, and I tried

test[42]

43

I infer from this that there nothing intrinsically wrong with your pacakage and your problems problems reside from trying to load the package into a notebook that had conflicting symbols defined.

BTW, it is better to write packages in the package editor that in a notebook. You can do that by opening a new package editor window with File > New > Package.

$\endgroup$
  • $\begingroup$ When did the package editor get introduced? $\endgroup$ – CA Trevillian May 9 at 2:37
  • 1
    $\begingroup$ @CATrevillian. I really don't know. I believe Its current form goes back to V11.0.. $\endgroup$ – m_goldberg May 9 at 22:38
  • $\begingroup$ thank you! I ask because I had to piece together a custom packager (from older code not updated in a few versions) in a notebook that autowrites to .m upon saving. I know that is older and .wl is newer, but I wasn’t able to have it function at the time. Maybe now I’ll use this editor! :) $\endgroup$ – CA Trevillian May 10 at 1:51

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.