Is it possible to use Mathematica's manipulate to change variables that have already been declared?
Example:
changeme = 8;
p = SomeSortOfPlot[changeme];
manipulate[Show[p],{changeme,1,10}]
The basic idea is that I want to make a plot with a certain changable value but declare it outside of manipulate.
Any ideas?
One option is to use Dynamic[] and LocalizeVariables -> False.
Example:
changeme = 8;
p[x_] := Plot[Sin[t], {t, 1, x}];
{
Manipulate[p[changeme], {changeme, 2, 9}, LocalizeVariables -> False],
Dynamic[changeme] (* This line is NOT needed, inserted just to see the value *)
}
Evaluating "changeme" after the Manipulate action will retain the last Manipulate value.
HTH!
If you want anything reasonably complicated or flexible, it is best to use Dynamic and DynamicModule instead of Manipulate. The only exception is if you're writing a demonstration.
For example - a very basic way of doing what you want is
(in fact you don't even need the Row and Slider if you want to just change changeme by hand.)
changeme=8;
p[x_]:=Plot[Sin[t],{t,1,x}];
Row[{"x \[Element] (1, ",Dynamic[changeme],") ",Slider[Dynamic[changeme],{2,9}]}]
Dynamic[p[changeme]]
Related
For example, the following rule to create a slider that edits a given variable:
EditorLine[stat_] := {
Labeled[Slider[Dynamic[stat], {1, 20, 1}],
{SymbolName[Unevaluated[stat]]}, {Left}], Dynamic[stat]}
SetAttributes[EditorLine, HoldFirst]
This works fine for EditorLine[x], but using a map - e.g., EditorLine /# {x,y,z}, gives an error because the map function evaluates the variable names and thus they are not held.
This can be done by writing EditorLine /# {Unevaluated[x],Unevaluated[y],Unevaluated[z]} but if I wanted to write repeated function calls like that I would not be using a map!
Is there any better way of doing this?
This will work too:
EditorLine /# Unevaluated[{x, y, z}]
and if you add Listable attribute, even shorter:
EditorLine[{x, y, z}]
I want to make mathematica insensitive to the functions first capital letter. For example, it accepts both "Plot" and "plot" as plotting function.
I agree with george's sentiment: "You don't want to do that." It is common practice to start user Symbols with lowercase letters which both identifies them and prevents collisions with built-ins. Nevertheless you can do this in several ways. One is just to create aliases as george also suggested, e.g.
plot = Plot;
sin = Sin;
plot[sin[x], {x, 0, 6}]
This has the advantage of working even in packages because it does not rely on the Front End. However, because these are not true aliases it will fail in some cases, e.g.:
evaluate = Evaluate;
Hold[evaluate[2 + 2]]
Hold[evaluate[2 + 2]]
Whereas the "real" function behaves like this:
Hold[Evaluate[2 + 2]]
Hold[4]
To get complete equivalence, though only in the Front End, you can use $PreRead. (Example.) You will need to build a list of rules that replace the string form of each lowercase Symbol with the uppercase string. I shall do that only for all Symbols in the System` context.
With[{rules = Thread[ToLowerCase[#] -> #] & # Names["System`*"]},
$PreRead = # /. rules &
];
Now both of these examples work:
plot[sin[x], {x, 0, 6}]
hold[evaluate[2 + 2], 3 + 4]
The latter producing:
Hold[4, 3 + 4]
This is not a direct answer to your question and I strongly advise you against redefining Mathematica functions just for the sake of the letter-case.
Nevertheless, have you seen that there is an option Match case in command completion when you go to Edit -> Preferences -> Interface?
If you turn this off, then you can type plot in the notebook and you get the correct Plot as suggestion from the autocompletion. You only have to hit enter and the correct command is inserted.
Mathematica 8's DictionaryLookup function uses "English" as the language by default. Is there any way to set the default language to "BritishEnglish" or "Spanish"?
Thanks in advance.
There does not appear to be an option for this, but you can modify the definition of DictionaryLookup to suit you.
The method I will use relies on the automatic ordering of DownValues and was written for version 7 so it may need adjustment. You can look at DownValues[DictionaryLookup] to see how the function is written as it is top-level Mathematica code.
$dictionaryLanguage = "Spanish";
Unprotect[DictionaryLookup];
DictionaryLookup[pat : Except[_List], x___] /;
! TrueQ[$dicLang] && ValueQ[$dictionaryLanguage] :=
Block[{$dicLang = True},
DictionaryLookup[{$dictionaryLanguage, pat}, x]
]
DownValues[DictionaryLookup] =
RotateRight # DownValues[DictionaryLookup];
Protect[DictionaryLookup];
With this definition, if $dictionaryLanguage is set that value will be used for the language. You can restore default behavior with $dictionaryLanguage =.. Examples:
$dictionaryLanguage = "Spanish";
DictionaryLookup["*orac*", 3]
{"adoración", "aminoración", "colaboración"}
$dictionaryLanguage =.;
DictionaryLookup["*orac*", 3]
{"coracle", "coracles", "Horace"}
Know that you call also do look-ups outside of DictionaryLookup. You can load the dictionary for a language like this:
DataPaclets`Dictionary`ReloadDictionary["Dutch"]
Which places the data in DataPaclets`Dictionary`$Dictionary. An example search:
Pick[#, # ~StringMatchQ~ "*fzand*"] ~Take~ 4 & # DataPaclets`Dictionary`$Dictionary
{"afzand", "afzandde", "afzandden", "afzanderij"}
The equivalent DictionaryLookup query:
DictionaryLookup[{"Dutch", "*fzand*"}, 4]
{"afzand", "afzandde", "afzandden", "afzanderij"}
If you use these tools often you could them in the context path with:
AppendTo[$ContextPath, "DataPaclets`Dictionary`"]
Then you could use ReloadDictionary and $Dictionary as is, without the context name.
This should be quick to an expert, but I'm relatively new at defining functions with options. Here is a schematic of what I've tried, I'll explain after showing the code:
MyPlotFunction[params_, optionalparameter_List:{1,2,3}, opts:OptionsPattern[]]:=
Plot [ stuff, {x,0,1}, Evaluate#FilterRules[{opts},Options#Plot]];
Options[MyPlotFunction] = { PlotRange->{-5,5}, Frame->True, ... other plot options};
There are four slight subtleties:
I have an optional parameter in my function that needs to be a list of integers.
I want the ability to call the function with any option of Plot, especially using values other than the default values specified in the third line.
I want to have default values for some of the options.
I potentially want to put other options in the function, so it is not guaranteed that all of the options should be passed through to plot.
But what I have above doesn't work. The default options I set are ignored, yet they appear in the ??MyPlotFunction information for my function. I'll give examples if you guys can't spot the error yet.
Edit:
Examples that doesn't work:
SimplePlot[t_,opts:OptionsPattern[{PlotRange->{-4,4},Frame->True}]]:=
Plot[2x+t,{x,0,1},opts];
Fails, the default option is ignored.
SimplePlot[t_,opts:OptionPattern[]]:=
Plot[2x+t],{x,0,1},opts];
Options[SimplePlot] = {PlotRange->{-4,4},Frame->True};
Fails, the default option is ignored.
SimplePlot[t_,opts__:{PlotRange->{-4,4},Frame->True}]:=
Plot[2x+t,{x,0,1},opts];
Default options work with a bare call, but if one of these options or any other plot option is overridden the remaining defaults are lost.
OptionsPattern[] only catches the options that are passed in, so you need to explicitly include any non-default option settings, say by using something like:
FilterRules[{opts, Options[MyPlotFunction]}, Options#Plot]
Here's a simple example:
Options[MyPlotFunction] = {PlotRange -> {-5, 5}, Frame -> True};
MyPlotFunction[params_, optionalparameter_List: {1, 2, 3},
opts : OptionsPattern[MyPlotFunction]] :=
Plot[optionalparameter, {x, 0, 1},
Evaluate#FilterRules[{opts, Options[MyPlotFunction]}, Options#Plot]]
As noted in the comments to Brett's answer, since options given first supersede options given later, and since options to Plot may be given as a list, you can write something like this:
Options[SimplePlot] = {PlotRange -> {-4, 4}, Frame -> True};
SimplePlot[t_, opts : OptionsPattern[]] :=
Plot[2 x + t, {x, 0, 1}, opts, #] & # Options[SimplePlot];
Mathematica provides many functions which are capable of handling Dynamic as an argument.
For example, the function FileNameSetter has the following variant:
FileNameSetter[Dynamic[name]]
uses the dynamically updated current value of name, with the
value of name being reset if a different file is chosen.
I wonder how one goes about defining a function pattern that takes a dynamic expression as an argument. For example, here is one attempt to wrap the dynamic variant of the function LocatorPane:
SinLocatorPane[Dynamic[sinvalue_]] :=
LocatorPane[Dynamic[x, (x = #; sinvalue = Sin[First[#]]) &],
Plot[Sin[x], {x, 0, 10}]]
What is the correct pattern to use for a dynamic expression argument? Are there any caveats with using the dynamic argument inside the function definition?
If you would like to write a function that updates the value of a certain variable, this is like passing a variable by reference. Standard way of achieving this in Mathematica is to use Hold* attributes on your function.
SetAttributes[SinLocatorPane, HoldFirst];
SinLocatorPane[sinvalue_] :=
LocatorPane[Dynamic[x, (x = #; sinvalue = Sin[First[#]]) &],
Plot[Sin[x], {x, 0, 10}]]
Then
{Dynamic[sv], SinLocatorPane[sv]}
would work as your expected. Your code worked because Dynamic has HoldFirst attributed and this allowed your code to update variable sinvalue. Otherwise Dynamic was not really needed