Message Boards Message Boards

Calculate a hessian matrix?

GROUPS:

Hello, I have to work on Mathematica for studies reasons but i'm not very familiar with it. I explain what we are trying to do with Mathematica: We want to calculate a hessian matrix but we want to keep the calculus theoretical as long as possible. So we have to determinate the size a the matrix : n and there is where my problems starts . I have some script written by a professor and we have to use them. Here is his instruction : Run the Mathematica script “double_chain.m” with the parameter n, for example in the terminal like this:

<path-to-Mathematica>/WolframKernel -nopromt -run “<<double_chain.m" n

So what i understand is to run the script from the command in windows but it doesn't work, Mathematica does not find my file. Moreover the first lines of the script are :

input=$CommandLine;
Print["n="<>input[[5]]]
n=ToExpression[input[[5]]];

So I then tried to replace "$CommandLine" by pretty much everything that makes sens but nothing works.

When i cancel the first 2 lanes and put a number after n=, the program does give me a satisfying result. So my problem is to enter this "n parameter". Thank you for your help.

POSTED BY: jack gredart
Answer
2 months ago

I suspect that you just need to be more clear on the paths to the files. The input "<<doublechain.m" is telling the kernel to load the file "doublechain.m". If that file isn't in your current folder (or on your path), the kernel likely won't find it.

On my windows machine, I created a file (demo.m) in my temp folder. From my command prompt, I call run the file as follows:

C:\Users\chadk\temp>"\Program Files\Wolfram Research\Mathematica\11.1\wolfram.exe" -noprompt -run "<<demo.m" n

Alternatively, you could specify the path using ... -run "Get[\"\Users\chadk\temp\demo.m\"]" (where I have used escape characters for the folders as well as the String for the file name. ('<<' is synactic sugar for 'Get').

POSTED BY: Chad Knutson
Answer
2 months ago

Thanks for you answer, I tried but it does not work, either when i lauch Mathematica.exe it says that the file "n" does not exist. And when I launch it with WolframKernel.exe there is nothing on it, like just white screen on the program. I really don't understand.

POSTED BY: jack gredart
Answer
2 months ago

I'm not sure the best way to run the script at launch of Mathematica, and I wouldn't recommend that approach. As you have shown, you can load the file in Mathematica (using 'Get'), but you would need to change how the value of 'n' is specified in that file.

Using WolframKernel, you do not see a prompt because you passed the flag -noprompt. However, if you remove that flag, you'll have to change the code because 'n' will now be in the 4th position of \ $CommandLine (not the 5th). The simplest change to make would be to set 'n' to the last element in \$CommandLine. Now it will work regardless of the flags you pass to the kernel:

input=$CommandLine;
n=ToExpression[Last[input]];
POSTED BY: Chad Knutson
Answer
2 months ago

okay I understand, and i have done exactly what you said and here is what i got : in my commands i type : D:\ProgrammeFiles\mathematica\WolframKernel -run "double_chain.m" m

Then wolfram lauch and all i got is : l[1]=

I'm sorry i'm really not friendly with that software but i can't understand ****

POSTED BY: jack gredart
Answer
2 months ago

Your input is missing the '<<'. Try this instead:

D:\ProgrammeFiles\mathematica\WolframKernel -run "<<double_chain.m" m

Or, if the double_chain file is in a different folder:

D:\ProgrammeFiles\mathematica\WolframKernel -run "Get[\"\\Path\\to\\the\\file\\double_chain.m\"]" m

where I am using escape keys on double quotes and backslashes.

I can understand your frustration! This is not trivial usage of the Wolfram kernel. I think that your instructor could have made this code much easier for students to use (for example, loading the package in a Mathematica notebook).

POSTED BY: Chad Knutson
Answer
2 months ago

Hey we are progressing (I guess ?). Now I got these messages:

Syntax::stresc: Unknown string escape \U.

Syntax::stresc: Unknown string escape \j.

Syntax::stresc: Unknown string escape \D.

Syntax::stresc: Unknown string escape \s.

Syntax::stresc: Unknown string escape \d.

ToExpression::sntxi: Incomplete expression; more input is needed .

I'm sorry I keep asking, indeed it doesn't seem very trivial. I'm very thankful to you for answering.

POSTED BY: jack gredart
Answer
2 months ago

I can't tell at which point in your code these errors are coming from.
The error messages indicate that there is a ToExpression wrapped around a string. Here is an input that gives the same error:

In[6]:= ToExpression["\U"]
During evaluation of In[6]:= Syntax::stresc: Unknown string escape \U.
Out[6]= $Failed

So perhaps this is in your -run "Get[ path to file]" code??? On windows machines, you have to be very careful with the path names because "\" is an escape character in Mathematica. A safer way to specify the path is to use FilenameJoin, e.g.,

In[7]:= FileNameJoin[{"Users","chadk","temp","demo.m"}]
Out[7]= Users\chadk\temp\demo.m

Then your command line input would look something like this:

C:\Users\chadk> "\Program Files\Wolfram Research\Mathematica\11.1\WolframKernel.exe" -run "Get[FileNameJoin[{\"Users\",\"chadk\",\"temp\",\"demo.m\"}]]" n

where the escape character (/) is used for the strings inside the FileName

POSTED BY: Chad Knutson
Answer
2 months ago

Okay so I managed to remove all the : Syntax::stresc: Unknown string escape \ errors.

The only one remaining is ToExpression::sntxi: Incomplete expression; more input is needed . And in my script I can't see what's wrong with the ToExpression : n=ToExpression[input[[4]]];

I think we are almost there (can't thank you enough).

POSTED BY: Updating Name
Answer
2 months ago

Progress! I suspect that the problem is in your command line input. Here is a simple example that gives the same error message:

C:\Users\chadk\temp>"\Program Files\Wolfram Research\Mathematica\11.1\WolframKernel.exe" -run "1+"

The kernel tries to evaluate ToExpression["1+"], and gives that error message.

POSTED BY: Chad Knutson
Answer
2 months ago

Honestly I can't see, "D:\ProgrammeFiles\mathematica\WolframKernel" -run "Get[\" 'Users' 'jack' 'desktop' 'script mathmatica' 'double_chain.m' "]" n

Can't make this work.

POSTED BY: jack gredart
Answer
2 months ago

Okay so I managed to launch it (FINALLY) But (it would have been to easy) I have now these errors

Table::iterb: Iterator {i, 2, n} does not have appropriate bounds.

Table::iterb: Iterator {i, 1 + n, -1 + 2 n} does not have appropriate bounds.

Table::iterb: Iterator {i, 1 + n, -1 + 2 n} does not have appropriate bounds.

General::stop: Further output of Table::iterb will be suppressed during this calculation.

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

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

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

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

NSum::nslim: Limit of summation -1. + 2. n is not a number.

NSum::nslim: Limit of summation -1. + 2. n is not a number.

NSum::nslim: Limit of summation -1. + 2. n is not a number.

General::stop: Further output of NSum::nslim will be suppressed during this calculation.

Do::iterb: Iterator {i, 1, dim} does not have appropriate bounds.

I don't where is that coming from, I mean to me it seems like the script itself have a problem. But I really don't think my professor would set me a wrong script. Could it be like in C language and I have to give kernel more ram ?

POSTED BY: jack gredart
Answer
2 months ago

Recall from your initial post that your professor's code takes the last part of the $CommandLine script and sets that value to 'n'. So, if you are typing 'n' in that position, Mathematica will set n=n, which is an acceptable expression at that point. However, the functions inside the script are clearly expecting n to be an integer. Try running the code again with '3' instead of 'n'. I suspect it will run without errors.

I don't know what your professor is expecting you to learn from this exercise, so I have no idea what the value of 'n' should be.

POSTED BY: Chad Knutson
Answer
2 months ago

Group Abstract Group Abstract