The Explore command
The Explore command provides the functionality to quickly and easily construct and insert a collection of embedded components for exploring the results of commands or procedures which return expressions, plots, or images. The automatically constructed components are inserted inside a worksheet table and the exploration is accomplished by moving controllers such as slider components.
Details on all the options provided by the command are available on the Explore help page. This worksheet provides additional examples and commentary on the use of the command.
Tip: The inserted assembly of embedded components will be treated like output by the Graphical User Interface (GUI). Re-executing Explore commands will replace the previously inserted content. To retain the inserted collection of embedded components in a more permanent way, you can copy and paste the whole inserted GUI table which contains them. Once pasted in elsewhere they should function as before, even if the original Explore command that produced them is deleted.
Introduction and basic concepts
The essence of the exploration concept is that an expression may depend upon names which are considered as real-valued independent parameters. The Explore command constructs an interface in which a variety of results can be consecutively displayed as the values of the parameters change according to some interactive controllers. The interactive application which the Explore command inserts will dynamically substitute values for the parameters, evaluate, and display each result.
For example, the following plotting command call contains the name A , upon which it depends. If name does not have a numeric value then evaluation of that plotting command call would generate an empty plot.
plot3d( sin(A*y)*x^2, x = -1..1, y = -Pi..Pi );
The Explore command allows us to construct and interface in which the plot is recomputed with parameter A taking on various numeric values.
Parameters and initialvalues
The parameters optional argument of the Explore command allows you to specify which of the unknown names in an expression of plot are the parameters to be explore via sliders. In addition, the initialvalues option allows you to specify the parameter values to be used in the first evaluation.
Compare the following two examples:
In the first example, a pop-up dialog window will appear, suggesting end points for parameters A and B. Click on the Explore button to accept the suggested values.
The second example allows you to bypass the pop-up dialog altogether, by programmatically specifying the parameters' ranges and initial values.
Explore( plot3d( sin(A*y) * B * x^2, x = -1..1, y = -Pi..Pi, view = -2..2 ) );
A
B
Explore( plot3d( sin(A*y) * B * x^2, x = -1..1, y = -Pi..Pi, view = -2..2 ), parameters = [A = 0..10, B = 0.0..10.0], initialvalues=[A=3, B=4] );
Exploring a mathematical expression
Expressions or function calls which evaluate to other mathematical expressions, following substitution of parameter values, can also be explored.
The component displaying the resulting expressions is a Math Expression Component (or MathContainer).
Tip: Properties such as the autofit, border display, and dimensions of the MathContainer can be altered using the right-click context menu on that component.
With exploration of mathematical expressions it can be useful to see the unevaluated expression alongside each result.The following examples demonstrate several ways in which the result may or may not be displayed as an equation, including as an identity involving the unevaluated expression.
By default the result displays as an equation, with the unevaluated expression echoed as the left-hand-side.
Explore( factor(sum(i^n,i=1..N)), parameters=[n=1..10] );
n
Display of that left hand side can be optionally suppressed.
Explore( factor(sum(i^n,i=1..N)), parameters=[n=1..10], echoexpression=false );
Since an equation can also be explored, a customized identity could be shown instead. In this next example the left hand side of the input expression is within unevaluation quotes, so that the right hand side summation is computed symbolically while the left hand side is not.
Explore( 'sum'(i^n,i=1..N)=factor(sum(i^n,i=1..N)), parameters=[n=1..10], echoexpression=false );
Yet another variation on the above is to use the inert operator Sum rather than the active sum. Note the lighter shade of the pretty-printed summation symbol.
Explore( Sum(i^n,i=1..N)=factor(sum(i^n,i=1..N)), parameters=[n=1..10], echoexpression=false );
Exploring a function call
One powerful way to use the Explore command is to pass it an unevaluated function call to a previously defined procedure. The key here is that the procedure must consistently return the kind of object to be explored, whether that be a plot, an expression, or an image.
At first glance the next example may appear trivial, but the technique is used in several of the more advanced application examples that come later in this worksheet.
f := proc(a) local b; b := a^2; return plot( sin(b*x), x=-2*Pi..2*Pi ); end proc: Explore( f(p), parameters=[p=-2.0 .. 2.0] );
p
Using previously assigned data
The following example illustrates the use of data which has previously been assigned to a name earlier in the session.
data := [[0,2],[1/2,1.5],[0.8,2.5],[1.4,2.6],[1.8,1.5],[2.2,3.1],[2.6,2.4],[3.1,2.2],[3.5,2.2]]:
with(CurveFitting):
Explore(plots:-display( plot([data,Spline(data,v,degree=n)],v=0.0..3.5,view=-1..4, style=[point,line],color=[black,blue],symbol=circle,symbolsize=20), plot(Spline(data,v,degree=n),v=-1.0..0.0,view=-1..4, color=red), plot(Spline(data,v,degree=n),v=3.5..4.5,view=-1..4, color=red)), parameters=[n=1..7]);
Interactive plots
Interactive plots produced by the PlotBuilder command make use of new Explore functionality.
Issuing the following command will bring up the Plot Builder options in the Context Panel.
Select Exploration from the Plot Builder options, and then click Build Exploration.
The result is an exploration comprised of Embedded Components inserted into the current worksheet. The Edit button beside the interactive plot will get you back to the Plot Builder.
PlotBuilder({cos(w),tan(w+z)});
z
Context-sensitive menus
Both Explore and Plot Builder can be accessed in a purely clickable manner, via the context-sensitive menus. Context-sensitive menus are used to apply operations to an expression using only a point-and click method. The menus are available for both 2-D Math input or output.
Execute the command below, then in the Context Panel for the output expression, you can see options for both Explore and Plot Builder.
exp(x)-a*x^3;
ⅇx−a⁢x3
Exploring an animation
The Explore command allows you to create an exploratory interface which can play frames as the instances of various parameter values. The frames are produced on-demand and kept only as they are needed. This contrasts with the animations produced by the animate plotting command, which must generate all frames in advance and retain them together. The frame on-demand approach to playing an exploration is discussed further on in this worksheet, but in this section we can see how the older style of animation is supported within an exploration interface.
After executing the Explore command below, select the plot with the pointer and run the animation using the top menu. By default, the embedded component will show the animation in continuous loop mode. While the animation is playing you can move the slider and see a changing instance of the animation.
Note that the animations are created on-the-fly for each new slider value.
Explore( plots:-animate( plot, [a+sin(b*x), x=-2*Pi..2*Pi, view=[-2*Pi..2*Pi,-5..5]], b=-2.0..2.0 ), parameters = [a=-3.0..3.0] );
a
Placement of controls
The controls for parameters can be placed either to the left, bottom, or right of the component displaying results. All parameter controls can be placed via a single optional argument to the Explore command, as a customized default.
Parameter controls can also be specified individually as an override of any default.
Explore( plot(b+abs((c-2)*(c+2))*cos(a*x),x=-6..6,view=-8..8), parameters=[ c=0.0..2, b=-3..3, [a=1..3.0, placement=bottom] ], placement=right );
c
b
Control types for range parameters
Parameters whose values are specified as a range can have any of several types of variable controller.
The variable controller are: slider, dial, volumegauge, meter, and rotarygauge.
The size of the plotting component can be optionally specified. By default, its size is determined by the size of the initial plot.
Explore( plot(abs((c-2)*(c+2))*sin(a*x), x=-6..6, view=-4..4), parameters=[ [c=0.0..2, controller=volumegauge], [a=1..3.0, controller=dial] ], size=[400,246], placement=left );
ComboBox controls
Parameters whose values are specified as lists are incorporated as Combo Box controls.
Explore( plot(abs((c-2)*(c+2))*sin(a*x), x=-6..6, view=-4..4, color=r, style=s), parameters=[ [r=[red,blue,green],label=color], [s=[line,point],label=style], c=0.0..2, a=1..3.0 ], size=[400,246], placement=right );
redbluegreen
linepoint
Advanced techniques
Animation
The display of the generated frames at each set of parameter values can be animated. Each parameter control can have its own check box which determines whether it will play with the animation. These check boxes can be unselected while the animation is playing.
Note that any parameters whose animation check box is unselected during play can still be interactively controlled.
The animation check boxes can be specified as a single new default option, and this in turn can be overridden on any parameter's individual specification. The animation check boxes can be specified as present and initially selected, present and initially unselected, or absent altogether.
By default, any combo box control of a discrete valued parameter does not get an animation check box unless this is supplied in the parameter's individual specification.
Explore( plot(abs((c-2)*(c+2))*sin(a*x),x=-6..6,view=-4..4,color=r,style=s), parameters=[ [r=[red,blue,green],label=color,animate=true], [s=[line,point,line],label=style,animate=false], c=-2.0..2, [a=1..3.0] ], size=[400,246], animate, placement=left );
linepointline
Loop animation
The loop option allows for looped play of the animated sequence.
The loop control check box can be omitted altogether (default), initially selected, or initially unselected.
Explore( plot(abs((c-2)*(c+2))*sin(a*x),x=-6..6,view=-4..4,color=r,style=s), parameters=[ [r=[red,blue,green],label=color,animate=true], [s=[line,point,line],label=style,animate=false], c=-2.0..2, [a=1..3.0] ], size=[400,246], animate, loop, placement=left );
Numerical ordinary differential equations with parameters
Suppose that you have an Initial Value Problem (IVP) with unassigned symbolic parameters. The interactive functionality of dsolve,numeric provides an efficient mechanism for numerically solving the solution at multiple values of the parameters. You can use the Explore command to interact with such a solution space.
sol := dsolve( {alpha*diff(x(t),t)=0.7*(y(t)-x(t))-sin(x(t))^2, alpha*diff(y(t),t)=0.7*(x(t)-y(t))+z(t), beta*diff(z(t),t)=-y(t), x(0)=3, y(0)=0, z(0)=-3}, {x(t), y(t), z(t)}, numeric, 'parameters'=[alpha,beta], 'output'='listprocedure'):
caller := proc(a,b); sol('parameters'=['alpha'=a,'beta'=b]); sol; end proc:
attract:=proc(A,B) plots:-odeplot( caller(A,B), [x(t), y(t), z(t)], t = 0 .. 500, 'style'=line, 'numpoints'=10000, 'labels'=[`x`,`y`,`z`], 'axes'='box', 'color'='COLOR'('HUE',A/10)); end proc:
You can now produce a plot of the solution at parameter values alpha=0.1 and beta=0.2.
attract(0.1,0.2);
And you can also explore the family of such plots, as the parameter values change. This is accomplished by calling Explore on a function call.
Explore( attract(a,b), 'parameters'=[a=0.5..2.0, b=1.0..10.0] );
Images
Procedures which return images can be explored interactively.
Basic example: float[8] Array
Arrays which are supported by the ImageTools package can be interpreted as images.
The following is a basic example involving a function call which evaluates to a 2-dimensional datatype=float[8] Array which is interpreted as a grayscale image.
Explore( Array(1..100,1..100,(i,j)->evalf(sin(p/10*(i+j))),datatype=float[8]), parameters=[p=2.0..4.0], size = [200, 200] );
Procedures returning an image
A function call to a command or procedure which returns an image can also be explored.
img := ImageTools:-Read(cat(kernelopts(datadir),"/images/excavator.jpg")):
Explore( ImageTools:-Threshold(img, threshold), parameters=[threshold=0.2..0.8], placement=right, size = [320, 210] );
threshold
Exploration of images can also be animated.
with(Fractals:-EscapeTime):
Explore( Mandelbrot(300, -1.78+(-1-1.5*I)/exp(zoom), -1.78+(2.2+1.5*I)/exp(zoom), iterationlimit=a, output=layer1) , parameters=[ a=50..300, zoom=0.0..8.0 ] , animate , placement=left );
zoom
Interactive markers
Pairs of range controls (e.g. sliders) can be coupled as interactive marker controls on 2D plots. Each marker is displayed as a cross in the plot, which may be dragged interactively using the mouse pointer.
By default the sliding controls associated with marker parameters are hidden. These controls can be collectively made to display with the showmarkercontrols option, or individually made to display or not with the optional shown keyword in the individual parameter's specification.
DEplot and initial conditions
deS := diff(x(t),t)=-0.5*x(t)*y(t);
deS ≔ ⅆⅆt⁢x⁡t=−0.5⁢x⁡t⁢y⁡t
deI := diff(y(t),t)=+0.5*x(t)*y(t)-0.15*y(t);
deI ≔ ⅆⅆt⁢y⁡t=0.5⁢x⁡t⁢y⁡t−0.15⁢y⁡t
Explore( DEtools[DEplot]( [deS,deI], [x(t),y(t)], t=0..40, x=0..1, y=0..0.6, [[x(0)=x0,y(0)=y0]], arrows=medium), parameters=[ [x0=0.3..0.99], [y0=0.1..0.5, shown=false] ], showmarkercontrols, markers=[[x0,y0]], placement=left, animate );
x0
Applications
Signal processing: infinite impulse response filter
The following application uses example code from the SignalProcessing package and, after enclosure in a simple procedure, produces an interactive application that dynamically illustrates the effects of the filter parameter value on the resulting modified signal.
First we generate a known signal. Then we construct a reusable container Array C in order to improve performance of the exploration process by leveraging the in-place computational semantics of the InfiniteImpulseResponseFilter command.
Jsignal:=SignalProcessing:-GenerateJaehne( 512, 4095 ): PJsignal:=plots:-listplot( Jsignal, 'title' = "Original Signal" ): Ctemp:=Array(1..512,datatype=float[8]):
filterapp:=proc(frequency,filter) uses SignalProcessing; local PB,taps; taps:=GenerateButterworthTaps(3,frequency,'filtertype'=filter); InfiniteImpulseResponseFilter(Jsignal,taps,'container'=Ctemp); PB:=plots:-listplot(Ctemp,'title'="Filtered Signal"); plots:-display(<<PJsignal;PB>>,'size'=[400,300]); end proc:
Explore(filterapp(frequency,filter), parameters=[frequency=0.05..0.49, [filter=["highpass","lowpass"]]], placement=left, animate, loop, title="Infinite Impulse Response Filter");
frequency
filter
highpasslowpass
RGB color space: an application
The next example uses a self-initializing module to efficiently do in-place updates of an image Array as the parameters change.
RGBapp:=module() local ModuleApply,init,f,sr,sg,sb,n,n1,n2,c,tc,R; init:=proc() n:=200; n1,n2:=trunc(n/4),trunc(n/2); c:=Matrix(n,'datatype'='float[8]', subs(d2=n2, (i,j)->`if`((d2-i)^2+(d2-j)^2<(d2)^2,1,0))); tc:=Matrix(n,'datatype'='float[8]'); R:=Array(1..2*n,1..2*n,1..3,'datatype'='float[8]'); end proc; f:=proc(x) LinearAlgebra:-MatrixAdd(tc,c,.0,x,'inplace'); NULL; end proc; ModuleApply:=proc(tr,tg,tb) if not n::posint then init(); end if; if sr<>tr then f(tr); sr,R[n1..n1+n,n1..n1+n,1]:=tr,tc; end if; if sg<>tg then f(tg); sg,R[n1..n1+n,2*n-5*n1..2*n-n1,2]:=tg,tc; end if; if sb<>tb then f(tb); sb,R[2*n-5*n1..2*n-n1,1+n2..n+n2,3]:=tb,tc; end if; R; end proc; end module:
A single call to F will produce a color image as output, which can be visualized by embedding in the current worksheet.
ImageTools:-Embed( RGBapp(0.7,0.3,0.9) );
An interactive application can now be easily constructed.
Explore( RGBapp( Red, Green, Blue ), parameters = [ Red=0.2..1, Green=0.2..1, Blue=0.2..1], initialvalues = [ Red=.7, Green=.7, Blue=.7 ], placement = right, title="The additive RGB color space" );
Red
Green
Blue
Cobweb plot of the logistic map
The cobweb plot of the logistic map illustrated by iterated application of the formula r*x*(1-x) exhibits emergent chaotic behavior as the parameter r increases from a value of 3 towards the value of 4.
Cobweb:=proc(r, x0, {numpoints::posint:=1})
local f,i,M,N,x;
N:=2*numpoints;
f:=(x,r)->r*x*(1-x):
M:=Matrix(N,2,datatype=float[8]):
M[1,1],M[1,2]:=x0,0;
M[2,1],M[2,2]:=M[1,1],f(M[1,1],r);
for i from 3 to N-1 by 2 do
M[i,1],M[i,2]:=M[i-1,1],f(M[i-1,1],r);
M[i+1,1],M[i+1,2]:=M[i,2],M[i,2];
end do;
plots:-display(plot(x,x=0..1.1,color=black,linestyle="dot"),
plot('f'(x,r),x=0..1,color=black),
plot(M,style=line,color=red),
view=[0..1.1,0..1.1]);
end proc:
The procedure Cobweb defined below will produce a so-called cobweb plot by iterating the process of applying the formula to obtain a y value and taking that computed y value as the next x value in the sequence. This sequence of operations produces (x,y) points which lie alternately on the formula's curve and the x=y line.
Cobweb( 2.8, 0.1, numpoints=10 );
For some values of r convergence to a point or a cycle occurs. As the value r increases towards 4 the chaotic behavior emerges. A dynamic exploratory interface with varying parameters r and x0 are much more impressive mechanisms for illustrating the emergent behavior than are static cobweb plots with fixed values of r and original point x0.
Explore( Cobweb( r, x0, numpoints=n ), parameters=[ r=2.0 .. 4.1, [x0=0.0 .. 1.0, animate=false], [n=1 .. 200, animate=false] ], initialvalues=[r=2.8, x0=0.1, n=100], animate, loop, placement=left, title="Cobweb plot for r*x*(x-1)" );
r
Edge detection
The following application performs edge detection by convolving the data with a pair of 3x3 kernels (Sobel operator), followed by optional thresholding.
The discrete valued action parameter is used to allow the choice between convolution (only) or thresholding (only) after the initial convolution. When the action ComboBox is selected as threshold then the three Sliders control the thresholding effect on the displayed image. When the ComboBox is selected as convolve then the image reverts to the result of the original convolution and the three Sliders have no effect.
img := ImageTools:-Read(cat(kernelopts(datadir),"/images/undercarriage.jpg")):
GetEdges:=module() local ModuleApply,edge,stored; ModuleApply:=proc(img, {action::identical(threshold,convolve):=':-convolve'}, {t::float:=0.5,high::float:=1.0,low::float:=0.0},$) uses ImageTools; if action=':-convolve' then edge := EdgeDetect(img, ':-method' = ':-Sobel'); FitIntensity(edge, ':-inplace'=true); return edge; elif action=':-threshold' then if type(edge,Image) then return Threshold(edge,t,':-high'=high,':-low'=low ); else error "action convolve must precede action threshold"; end if; else return img; end if; end proc: end module:
Explore( GetEdges(img,action=act,t=thresh,high=highval,low=lowval), parameters=[[act=[':-convolve',':-threshold'],label=action], [thresh=0.01..0.3,label=threshold], [highval=0.0..1.0,label=high], [lowval=0.0..1.0,label=low]], initialvalues=[thresh=0.07,highval=0.75,lowval=0.0], placement=right, size=[400,267], title="Edge detection by convolution and thresholding" );
convolvethreshold
Return to Index for Example Worksheets
Download Help Document