Tag Info

Hot answers tagged

51

The main change since that time seems to be that the modern way of using options is associated with OptionsPattern[] - OptionValue commands. A typical way of defining a function would be: Options[f] = { FirstOption -> 1, SecondOption ->2 } f[x_,y_,opts:OptionsPattern[]]:= Print[{x,y,OptionValue[FirstOption],OptionValue[SecondOption]}] ...


47

The documentation is wrong. It should have been fixed, but AbsoluteOptions does not work with ViewMatrix (on all platforms). M- introduced interactive 3D graphics since V6, and after that getting values through AbsoluteOptions (which is an old function) becomes very tricky since the Kernel (who evaluates the option) cannot fully know what is happening on ...


29

For this purpose, I wrote a small Symbol Information Palette. This palette let's you quickly look up usages, options and attributes of symbols and was tested on Mac OSX and Linux. Installation The source code is hosted on my GitHub site but to preview or install the palette you only have to evaluate this: Get["http://goo.gl/QPywk"] The link is just ...


26

Here is a practical example from a StackOverflow question. I hope that it gives a good overview of the basic methods. Question What would be the best way to make a function out of the below code ? It would take a dataList as well as some graphical options (such as colors) as arguments and return a customized tabular representation as shown ...


25

You could plot the curve twice, with two different styles: Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, PlotStyle -> {Directive[Thickness[0.03], White], Black}] Changing the background to gray: Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, PlotStyle -> {Directive[Thickness[0.03], White], Black}, Background -> Gray]


24

One way would be to use ColorConvert to convert the RGB or Hue values to gray scale. Here's an example: Plot[{Sin[x], Cos[x], Exp[-x^2], Sinc[π x]}, {x, 0, π}] /. x : _RGBColor | _Hue | _CMYKColor :> ColorConvert[x, "Grayscale"] For 2D plots that accept a ColorFunction, you can simply use GrayLevel to get the plot in grayscale as: DensityPlot[ ...


23

It got a bit out of hand, but here's a way to construct a ViewMatrix pair from the triple ViewVector, ViewAngle, and ViewVertical. The left figure is the Graphics3D object using ViewVector, ViewAngle, and ViewVertical and the right is the one using ViewMatrix. If you rotate the left figure or scale it (by dragging the figure while keeping Alt depressed), the ...


23

I doubt you can find a chart for all options, but take a look at this: For this and other insights two courses by Yu-Sung are a must (there are notebooks and videos there): Graphics Language Quick Start Visualization: Advanced 3D Graphics The above chart is from the 1st one. The one @Kuba links in the comment to your question is from the 2nd - I ...


20

I think you can actually see (most of) what Mathematica is doing by using Trace[..., TraceInternal -> True]. For example, Select[Flatten[ Trace[NDSolve[y'[x] == x && y[0] == 0, y, {x, 0, 6}], TraceInternal -> True]], ! FreeQ[#, Method | NDSolve`MethodData] &] shows the DE was evaluated using NDSolve`LSODA and Newton's method. (I ...


20

Mathematica has to be able to tell that the default arguments can't be rules. So, for some special cases, you could do Options[f] = {"g" -> Identity}; f[x_, y_Integer: 2, z_Integer: 3, OptionsPattern[]]:= OptionValue["g"][x + y + z] Testing: f[1, 2, 3, "g" -> (#^2 &)] 36 f[1] 6 f[1, "g" -> (#^2 &)] 36


20

Here is another approach, based on Filling option : Plot[{Sin[x] - 0.02, Sin[x], Sin[x] + 0.02}, {x, 0, 2 Pi}, PlotStyle -> {Gray, Black, Gray}, Filling -> {1 -> {{3}, Yellow}}] One problem may appear here, namely if a given function has a big absolute value of the derivative, then the strip becomes too thin. We can avoid this by ...


18

Edit: better answer below. I voted for Rojo's answer. If for some reason you cannot be that specific about your arguments you might use the converse: nr = Except[_?OptionQ]; f[x_, y : nr : 2, z : nr : 3, OptionsPattern[]] := OptionValue["g"][x + y + z] If for some further reason you need the optional arguments to be rules themselves, you could filter ...


17

A guess My guess is that you have just run into the details of OptionValue implementation, which are also responsible for its "magical" behavior. OptionValue has to somehow know which function it is in, and tracing the execution of f4[] shows that apparently the following expansion is happening before any evaluation is attempted for the r.h.s.: ...


15

A bit late-to-the-party post, and complementary to the other solutions. Several answers addressed the question quite well IMO. I had my shot on a similar one here, with a solution similar to the one by @Mr.Wizard. But now I just want to stress one subtle point missed by other answers: using OptionQ will leak evaluation for functions which are HoldAll and ...


15

Short answer: You supply a pure function to an option when you want to override the built-in options. In this case, "Diamond" and 0.2 resolve to certain functions or are used as values in certain functions internally which is then used for the respective option. The short names are merely a convenient way for you to remember and enter the option. Longer ...


15

A minor tweak to Markus' method, you may find utility in CapForm: Table[ Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, PlotStyle -> {Directive[AbsoluteThickness[15], CapForm[c], White], Directive[AbsoluteThickness[7], Blue]}, ImageSize -> 400, PlotRangePadding -> 0.2, Frame -> True, Prolog -> ...


15

The problem we encounter here is an instance of rather unexpected limitations of equation solving functionality (i.e. Modulus option in Reduce), e.g. this question : Strange behaviour of Reduce for Mod[x,1] provides another example which has been fixed in the newest version (9.0) of Mathematica. Since Modulus unexpectedly doesn't work here we can take ...


14

All of the polynomial functions, have an option Modulus which allows you to specify an integer field, like $\mathbb{Z}_5$. In particular, Factor works on your example polynomial Factor[x^2+4, Modulus -> 5] (* (1 + x) (4 + x) *) Additionally, IrreduciblePolynomialQ works to determine irreducibility of $x^2+2 $, as follows IrreduciblePolynomialQ[x^2 + ...


14

There are two problems I see with this code. The first one, and the one which gives you immediate problem you reported, is that, as it looks, short (magical) form of OptionValue does not work with SubValues - based definitions. So, you should replace OptionValue["type"] with OptionValue[{opts}, "type"] Or, if you wish to associate the option with pump ...


13

Here is another method that I learned through reading Inside the Mathematica Pattern Matcher: Options[f] = {"g" -> Identity}; f[x_, Shortest[y_: 2, 1], Shortest[z_: 3, 2], OptionsPattern[] ] := OptionValue["g"][x + y + z] From the documentation for Shortest: Shortest[p, pri] is given priority pri to be the shortest sequence. Matches for ...


13

The answer given by @halirutan lead me to a thought that we can use Pane[..] as elements of Grid[...] to obtain desired behavior. Panel[...] can work too, but it introduces extra boundaries. Grid@Partition[MapThread[Pane[Graphics[Rectangle[], ImageSize -> #1], ImageSize -> {60, 60}, Alignment -> #2] &,{{20, 30, 40, 50}, {{1, -1}, {-1, -1}, ...


13

It seems that the handling of the Alignment option is not consistent for all functions using it. Panel for instance seems to support numeric values for this option Manipulate[ Panel["\[Times]", ImageSize -> {100, 50}, Alignment -> {x, y}], {x, -1, 1}, {y, -1, 1} ] while with Grid this is not supported. Knowing this, you could check functions ...


13

You can define your own function for FrameTicks : ticks[min_, max_] := {#, NumberForm[#, 20]} & /@ N[FindDivisions[{min, max}, 5]] ListPlot[{RandomReal[#] + 10^4, RandomReal[#]} & /@ (Range[100] 10^-10), Frame -> True, FrameTicks -> {{Automatic, None}, {ticks, None}}] Just choose your own preferred presentation format of the ...


13

AbsoluteOptions is known as very buggy function and the bug in determining the true PlotRange has very long history... You could try my Ticks-based workaround for getting the complete PlotRange (with PlotRangePadding added): completePlotRange[plot:(_Graphics|_Graphics3D|_Graph)] := Quiet@Last@ Last@Reap[ Rasterize[ Show[plot, Axes -> ...


13

Interactive illustration of the most fundamental properties: Manipulate[ Column[{ Overlay[{ Framed[Graphics[{ LightYellow, Rectangle[{-plotRangePadding - plotRange, -plotRangePadding - plotRange}, {plotRangePadding + plotRange, plotRangePadding + plotRange}], LightGreen, ...


12

One can also define a KMB number format using NumberForm and its options as follows: g[a_] := Switch[a, "3", "K", "6", "M", "9", "B", "12", "T", _, ""]; kmbtForm[num_?NumericQ, digits_?IntegerQ] := StringReplace[#, "." ~~ x : ("K" | "M" | "B" | "T") -> x] &@ ToString@ NumberForm[N@#1, #2, ExponentFunction -> (If[0 >= #, 0, 3 ...


12

This information is given in the tutorial Setting Up Functions with Optional Arguments. Just catch the options given to your function in a variable and use FilterRules odeplot[de_, y_, {x_, x0_, x1_}, opts : OptionsPattern[]] := Module[{sol}, sol = NDSolve[de, y, {x, x0, x1}, FilterRules[{opts}, Options[NDSolve]]]; If[Head[sol] === NDSolve, ...


12

The main points of this answer are that,first, it seems rather difficult to have a fully universal mechanism for option-validation, and second, such a mechanism is not currently available in Mathematica on the language level (meaning automation of complete option-checking, including both the option's name and value). In the particular case in question, ...


11

First a little background: All of Mathematica's plotting functions produce a Graphics expression (or Graphics3D, but let's talk about Graphics now). The Graphics expression is simply a representation of what you see in the graphic. You can look at it by converting the output cell to InputForm (Ctrl-Shift-I). For example, Plot will produce Graphics with ...



Only top voted, non community-wiki answers of a minimum length are eligible