Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
5  
Look up init.m in the docs. –  J. M. Jul 12 '12 at 0:07
add comment

2 Answers

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"}
]
share|improve this answer
3  
Warning. This routine hosed my init.m file, completely emptying it. (Running Mma 7 on Win 7.) –  Chris Degnen Jul 12 '12 at 9:39
3  
I´d also opine that modifying important init files by hand would be the safer alternative than scripting (even with backup) in most cases –  Yves Klett Jul 12 '12 at 15:08
    
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 :) –  Max Hacki Jul 12 '12 at 21:39
add comment

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"}].

share|improve this answer
1  
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"}] –  Mr.Wizard Jul 24 '12 at 10:00
    
Quit needs a bracket pair. –  Sjoerd C. de Vries Jul 24 '12 at 12:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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