Hot answers tagged script
14
The reason why all the lines are commented is because you are using the frontend to create your script in input cells and saving it as a .m file (equivalent to choosing "Mathematica Package" in the Save As dialog).
Now to create a Mathematica package file (or .m file), the code needs to be in initialization cells or code cells. Content in any other cell is ...
11
You can use the global AutoOpenNotebooks setting to give a list of notebooks that must be opened on startup. The default path where it looks for these notebooks is $UserBasedirectory/SystemFiles/FrontEnd/TextResources.
Now every time you open Mathematica, that notebook will be opened (in my case, tile.nb).
10
You can force the Column to display correctly in text-only script mode by passing it explicitly to OutputForm. For example:
#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script
list = {a, b, c};
Print[Column[list] // OutputForm];
gives the output you expect:
a
b
c
7
It is indeed possible and I use this all the time. Instead of the for ... & done you might want to check out gnu parallel. It's great for running parallel processes from the command line.
parallel can be told how many processes to run in parallel, so you don't run out of licences or memory. Here is an example:
parallel -j4 "math -noprompt -script ...
7
It is possible, and I do it routinely. The limitation is the number of kernels your license allows you to run simultaneously. This I believe is reported by $MaxLicenseProcesses:
Of course you need to be careful not to write to the same files etc!
6
If you look at this
#!/usr/local/bin/math8.0.4/MathematicaScript -script
$HistoryLength=0;
$pwf=FileNameJoin[{NotebookDirectory[],FileBaseName[NotebookFileName[]]}];
$pwf = "test";
$parameterfile=StringJoin[$pwf,".dat"];
Print[$pwf];
Print[$parameterfile];
Print[$InputFileName];
you will see that the Print statements work just fine - What requires the ...
6
What I do is the following.
Save the following code as a text file in a permanent location under the name MathematicaLauncher.scpt:
tell application "System Events"
try
get process "Mathematica"
on error -- Not running, launch and run
launch application "Mathematica"
-- May need to wait until application finishes launching
...
6
I think this is a bug in the script execution under version 9. You can work around it by using a wrapper for your script, which loads it using the -run option.
For example, save the following as an executable file:
/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "commandLineArg={\"$1\"}; <<dividendsForSymbol; Exit[]"
Then in ...
5
Generally, it is better to use softer than harder error-reporting mechanisms. Calling Quit[] seems too radical to me. What normally happens is that functions where the error occur are not in the position to make right decisions about what to do with the error, since they are likely too low-level. This is exactly the reason why exceptions exist. They ...
4
If you just need to run a block of Mathematica code then this questions seems to
be equivalent to, How do I run an operating system command/script from SQL?
Using the operating system command line invocation MathKernel -script myFile.m you can call your Mathematica script by whichever mechanism your version of SQL supports for accessing OS commands.
Your ...
3
It works OK for me here on Mac OS X Mountain Lion, Mathematica 9, given a few minor changes:
Mathematica Script saved as math-script.math:
#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script
Print["loading"]
UsingFrontEnd[NotebookEvaluate["/tmp/test-notebook.nb"]]
Print["quitting"]
Quit[]
Mathematica Notebook saved as ...
3
If you need more interactivity, take a look at mash. From the README:
This is how "math -script" should work. Namely, the same way that the
perl/python/ruby/etc interpreters do:
take a mathematica source file as its first argument
(or from stdin if no arguments),
make all the arguments available to the mathematica code as an
array (list) ...
3
You get your desired result by invoking the SetDelayed option for your functions. To output an Integer, remove the N[] function.
#!/usr/local/bin/MathematicaScript -script
SetOptions[$Output, FormatType -> OutputForm];
foo[bar_?IntegerQ, baz_?IntegerQ] := bar;
M[g_?IntegerQ] := Sum[foo[i, g], {i, 1, 4}];
Print[M[1]]
(*10*)
2
Do I understand it correctly that you have several CPU cores in your machine (say, four of them), and you wish to run as many instances of the simulation as the number of cores, to make maximum use of the available computing power?
In this case you can just start four separate instances of the Mathematica kernel on the command line. Simply run MathKernel ...
2
You can obtain kernel IDs with either, Kernels[] or ParallelEvaluate[$KernelID].
ids=ParallelEvaluate[$KernelID]
{1, 2, 3, 4}
kobjs=Kernels[]
{KernelObject[1, "local"], KernelObject[2, "local"],
KernelObject[3, "local"], KernelObject[4, "local"]}
These can then be used with ParallelEvaluate to specify which kernel you wish to run your code ...
2
No, this is not possible. Manipulate only works in a notebook, opened in the Front End, because it relies on Front End functionality. GUIRunModal uses Java to display a GUI, not the Front End.
It is not possible to use Manipulate without the Front End (or CDFPlayer).
2
In the end, I just changed my script to a bash script that feeds a here-document to MathKernel:
#!/bin/bash
export symbol="$1"
/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt <<\EOF | grep -v ^StringForm
exportableDate[date_] := 10000 date[[1]] + 100 date[[2]] + date[[3]];
dividendTable = ...
2
You print by having a Print[] on the expression you want to print. End all other statements with ;.
fooToPrint = bar[...]; Print[fooToPrint];
fooNoPrint = bar[...];
e.g.,
a = 1 + 2;
Print[a];
b = 3 + 4;
a = b;
Print[a];
1
Running this script (using the command line ./m.m 10):
#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script
Print["$CommandLine ",$CommandLine]
Print["$ScriptCommandLine ",$ScriptCommandLine]
num = ToExpression[$ScriptCommandLine[[2]]];
Print /@ RandomVariate[
MixtureDistribution[
{1, 2},
{NormalDistribution[1, 0.2],
...
1
I found out how to solve this problem in SQL Sever. I used PsExec together with xp_Cmdshell to do the job. You have to install the PsExec in Windows in the system32 directory of the server machine. You can find the installer here. After that, I created this procedure, that can execute a file in a remote server:
CREATE PROCEDURE [dbo].[MAT_EXEC_SCRIPT]
...
Only top voted, non community-wiki answers of a minimum length are eligible


