0
$\begingroup$

I am new to Mathematica package development and I have been trying to write and run some simple packages to get a feel for it. I wrote the following package.

My aim is to define a package which has a function named FusionCoef whose argument is a square matrix. The function then returns a list of lists.

BeginPackage["Fusion`"]

FusionCoef::usage = "Argument is the S-matrix. It returns fusion coeffients as a
list of lists"

Begin["`Private`"]
FusionCoef[K_] := Module[{n = 1, M, S = IdentityMatrix[3]},
   S = K;
   n = Length[S[[1]]];
   M = Table[Sum[(S[[i, m]] S[[j, m]] S[[k, m]])/(S[[1, m]]), {m,1,n}], {i, 1, n}, {j, 1, n}, {k, 1, 
  n}];
   M
  ];

End[]

EndPackage[]

While running this package I am getting several errors like

<< Fusion`

Part::partd: Part specification S[[1]] is longer than depth of object.

Part::partd: Part specification S[[1,1]] is longer than depth of object.

Part::partd: Part specification S[[1,1]] is longer than depth of object.

General::stop: Further output of Part::partd will be suppressed during this calculation.

Part::pkspec1: The expression k cannot be used as a part specification.

Part::pkspec1: The expression k cannot be used as a part specification.

Part::pkspec1: The expression k cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation 

I believe that the problem is with the way in which I have defined a function with matrix valued arguments. Any help would be appreciated.

$\endgroup$
  • 1
    $\begingroup$ Restart the kernel, make sure that the contents of your package are really what you show here, and try again. I do not see anything wrong with the package. $\endgroup$ – Szabolcs Nov 18 '18 at 14:10
  • 1
    $\begingroup$ Then you are not loading this package, but something else. Use FindFile["Fusion`"] to find out what. $\endgroup$ – Szabolcs Nov 18 '18 at 14:57
  • 1
    $\begingroup$ Does the output of FindFile["Fusion`"] match your expectation? $\endgroup$ – Lukas Lang Nov 18 '18 at 14:57
  • 2
    $\begingroup$ If we cannot reproduce the issue, then it is very difficult to help. All I can say is that the code you quote cannot possibly cause these errors. $\endgroup$ – Szabolcs Nov 18 '18 at 15:21
  • 1
    $\begingroup$ No, I do not think it has anything to do with Ubuntu. You are certainly not running (only) the code you show here. To solve this issue, triple check that you are indeed running this code, and find the difference. $\endgroup$ – Szabolcs Nov 18 '18 at 22:30
1
$\begingroup$

Before trying to include it in a package, a function should be validated as a ordinary function. I think your definition of FusionCoef needs to be corrected to something like this:

Clear[FusionCoef, squareMatrixQ]
squareMatrixQ = (MatrixQ[#] && With[{d = Dimensions[#]}, d[[1]] == d[[2]]] &);
FusionCoef[sq_?squareMatrixQ] :=
  Module[{n},
    n = Length[sq[[1]]];
    Table[
      Sum[(sq[[i, m]] sq[[j, m]] sq[[k, m]])/(sq[[1, m]]), 
      {m, 1, n}], {i, 1, n}, {j, 1, n}, {k, 1, n}]]

Then

m = FusionCoef[Array[s, {3, 3}]];
Dimensions @ m

{3, 3, 3}

Column[MatrixForm /@ m]

matrix

Since I am not familiar with your problem domain, I am not sure this is really the function you need, but it is certainly an improvement over your version and, at least, I hope it will help you to move forward with your package building.

Note: I put the above definitions into the private section of your package code and ran the package and it performed without any errors.

$\endgroup$
  • 2
    $\begingroup$ Fun note, SquareMatrixQ is a built-in $\endgroup$ – b3m2a1 Nov 18 '18 at 19:49
  • $\begingroup$ @b3m2a1. That's embarrassing. I swear I checked the docs for it before writing my own version and can't understand how I missed it. Am editing the answer to used the built-in. $\endgroup$ – m_goldberg Nov 19 '18 at 14:12

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.