16
$\begingroup$

I'm a physics student and I try to simplify my day with Mathematica. Every time I use Mathematica I need Div[], Grad[], Curl[], etc., so I use Needs["VectorAnalysis`"].

My question is: Is there any way to tell Mathematica that the package VectorAnalysis` should always be loaded? It would simplify my day enormously.

By the way, I tried

DeclarePackage["VectorAnalysis`", {"Div", "Grad", "Curl"}]

from the Mathematica tutorial on Automatic Loading of Packages, which says:

When you first use Grad, Mathematica automatically loads the package that defines it.

Unfortunately, nothing happens. The functions are still unknown, until I use Needs["VectorAnalysis`"]

What am I doing wrong, and how do I fix my problem?

$\endgroup$
1

2 Answers 2

12
$\begingroup$

As J.M. pointed out, this can be achieved by using an init.m file.

init.m files are loaded on startup. The documentation can be found here.

To load the VectorAnalysis package on each startup, you can write the init file using Export. The snippet below will append a Needs["VectorAnalysis`"] to the kernel init.m file:

initFileName = FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}];

(* Make a back-up *)
CopyFile[initFileName, initFileName <> "_" <>
   StringReplace[ToString@Round[DateList[]], " " -> ""] <> ".bak"];

exprs = Import[initFileName, {"Package", "HeldExpressions"}];

Export[initFileName,
    Append[exprs, HoldComplete[Needs["VectorAnalysis`"]]],
    {"Package", "HeldExpressions"}
]
$\endgroup$
3
  • 4
    $\begingroup$ Warning. This routine hosed my init.m file, completely emptying it. (Running Mma 7 on Win 7.) $\endgroup$ Jul 12, 2012 at 9:39
  • 3
    $\begingroup$ I´d also opine that modifying important init files by hand would be the safer alternative than scripting (even with backup) in most cases $\endgroup$
    – Yves Klett
    Jul 12, 2012 at 15:08
  • $\begingroup$ THANKS MAN!!!! U MADE MY DAY!! Thats exacly what i want....nice I'm new in this board, but actually now i think one of the most efficient boards...thanks again...maybe u'll hear about me with another question, but i hope next time, i can help :) $\endgroup$
    – Max Hacki
    Jul 12, 2012 at 21:39
4
$\begingroup$

On Mathematica 7 (at least) DeclarePackage doesn't work first time on this Curl example:

DeclarePackage["VectorAnalysis`", {"Div", "Grad", "Curl"}]

v := {3 Xx^2 + 2 Yy + (a  - 3 b)  Zz,
   (b - 7 a) Xx - 5 Yy - Zz,
   2 Xx + c a Yy + 7 Zz}

Curl[v]

{0, 0, 0}

But strangely when v is defined again and run it works:

v := {3 Xx^2 + 2 Yy + (a  - 3 b)  Zz,
   (b - 7 a) Xx - 5 Yy - Zz,
   2 Xx + c a Yy + 7 Zz}

Curl[v]

{1 + a c, -2 + a - 3 b, -2 - 7 a + b}

On the other hand Needs works first time:

Quit

Needs["VectorAnalysis`"]

v := {3 Xx^2 + 2 Yy + (a  - 3 b)  Zz,
   (b - 7 a) Xx - 5 Yy - Zz,
   2 Xx + c a Yy + 7 Zz}

Curl[v]

{1 + a c, -2 + a - 3 b, -2 - 7 a + b}

You could manually add Needs["VectorAnalysis`"] to the init file referred to by Malte, found at FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}].

$\endgroup$
2
  • 2
    $\begingroup$ DeclarePackage fails here because you create a shadowing problem by first using Xx etc., before Curl. To fix this you need to include these DeclarePackage so that they are created in the correct Context: DeclarePackage["VectorAnalysis`", {"Div", "Grad", "Curl", "Uu", "Vv", "Xx", "Xxi", "Yy", "Zz"}] $\endgroup$
    – Mr.Wizard
    Jul 24, 2012 at 10:00
  • $\begingroup$ Quit needs a bracket pair. $\endgroup$ Jul 24, 2012 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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