Graph Object Wishlist

From WeBWorK_wiki
Revision as of 21:24, 28 September 2008 by Sam (talk | contribs)
Jump to navigation Jump to search

Graph synopsis

# an empty graph
$g = Graph();
$g = Graph({options});

# graph with one function, with and without various options
$g = Graph($f);
$g = Graph({options}, $f);
$g = Graph($f=>{options});
$g = Graph({options}, $f=>{options});

# graph with multiple functions, with and without various options
$g = Graph($f, $f2, $f3, ...);
$g = Graph({graph}, $f, $f2, $f3, ...);
$g = Graph($f=>{options}, $f2=>{options}, $f3=>{options}, ...);
$g = Graph({options}, $f=>{options}, $f2=>{options}, $f3=>{options}, ...);

# convenience method of Formula to generate a graph
$g = $f->graph;
$g = $f->graph + $f2->graph + $f3->graph;

# with options
$g = $f->graph(options);
$g = $f->graph(options) + $f2->graph(options) + $f3->graph(options);

# should support some other types similarly, like Point ($p)
$g = $p->graph;
$g = Graph($p, $p2, $p3);

# graph elements (Formulas, Points, etc.) should have an intermediate form
# to provide an API for easily adding novel types. call it Graph::Element.
# consist primarily of a plot subroutine which actually draws the graph
$ge = GraphElement(sub {...}, {options});

# when the graph needs to plot the element, it calls the plot function.
# passes it the graph object, so it can actually do the plotting, and the
# {options} structure (which is opaque to everyone but the plot function)
$plotfn->($g, {options});

# example for Point:
sub Point::graph {
    my ($self, @options) = @_;
    return Graph($self->graph_element(@options));
sub Point::graph_element {
    my ($self, @options) = @_;
    return Graph::Element(sub { ... }, {options});

# consider using infrastructure on top of GD, such as GD::Graph


  • A Formula object $f has associated with it a graph object, which can be instantiated using
$g = $f->graph(options)

with all of the options having default values.

  • Multiple functions can be graphed simultaneously using
Graph($f->graph(options), $g->graph(options),...) 

or if the defaults are desired

  • The + operator is also overloaded for combining graphs, as in $f->graph(options)+$g->graph(options).
  • As another option Graph() can be given an expression as in Graph("3x^2+8") in which case the defaults are used, which could also be used in conjunction with plus: Graph("3x^2+8")+Graph("x^4+sin(x)").
  • Information about what is to be graphed (i.e. a function of one variable versus a parametric curve in 3-space) is contained in the Formula object, so the graph function does much of the work in differentiating what information is appropriate, creating uniformity in how the graph is called.
  • The constructor Graph() takes any number of MathObjects or strings which can become MathObjects(originally, the objects understood are Formulas, but eventually it should understand Intervals, Sets, Points, Vectors, Complex Numbers, Reals) and options (specified as key value pairs), including
    • Line color (default red for a single graph, when multiple functions are graphed the functions have different color by default, the colors to be determined by the programmers),
    • Line weight (pixels; default: 1),
    • Line style (solid, dashed, dotted, etc.; default: solid),
      • There are properties associated to functions (such as color and style) and properties associated to the graph as a whole (such as display and grid).
    • viewing area is displayed using display. For a 2d graph display=>[1,2] would specify the domain as [1,2] and use defaults for the range. Similarly for a 2d graph dislay=>[[1,2],[3,4]] would do what it should. In 3d graphs display=>[[1,2],[3,4]] and display=>[[1,2],[3,4],[5,6]] would do what one would expect. Display is a global option and is a property of a graph, not the property of a function. (This option should be called something else, 'display is too common', perhaps window=>[1,2])
    • For a parametric function (of one or two variables) one must use something like parametric_range=>[[1,2],[3,4]] and parametric_vars=>[s,t] to give the variables and ranges for these. Similarly, parametric_range=>[1,2], parametric_vars=>t should work for a function of one parametric variable.
  • In the case of a call of the form Graph(“3x^2+8a”) the variables are taken alphabetically. But, the variables can be ordered explicity (which is important for 3d plots so that the correct orientation of the axes is correct). This can be done by vars=>[x,t].
  • If no display is given defaults are used ( [[-10,10], [-10,10]]). BUT, if functions have a domain that is an interval then display is set to be that interval (this is done whenever it can make sense).
  • [-10,10] is the default value for each display range. If range is not specified it should determined, using the domain, by determining maximum and minimum values for the function.
  • By default a grid is not displayed. The grid can be turned on using

grid => [spacing1,spacing2] on a two dimensional axis or grid =>[spacing1,spacing2,spacing3] for a three dimensional axis. Spacing specifies the amount space between grid lines. Grid options can be specified by gridopts=> (need more here)

  • An alternative version of grid uses a string of values specifying where each gridline is to be placed, allowing non-uniformly spaced grids to be constructed. If specified gridlines lie outside the domain or range of the graph they are expanded.

ex. grid=>[“1,2,5”,”2,3”].

Since grid is used as a switch to turn the gridlines on default values are not specified.

By default the grid lines have weight 1, are grey and are dotted, the defaults can be overridden using options.

ex. $f->graph(grid =>[2,3], gridcolor=>black,gridweight=>3]);

Graph(“e^x”, display=>[[1,5],[-4,4]] , grid=>[“0,1,3,5”,”2,7”] ,gridcolor=>red]) 

In this case, since the domain and range are specified the graph would plot over the domain [1,5] and range [-4,4]. Graph(“e^x”, grid=>[“0,1,3,5”,”2,7”] ,gridcolor=>red]) would graph over the range [[0,5],[2,7]].

Note to programmers: this would be easy to implement by constructing a separate graph for each gridline, which would automatically expand the domain and range.

    * Axes: Styles For 2D graphs you can choose between none, normal and boxed. 3D graphs have the option of none, normal, boxed. The default is normal. ex. axes => “boxed” Tickmarks Tickmarks for each axis can be specified either as the number of total ticks on the axis or as a list of values. Axes Labels The labels for the axes can be specified using the labels option, the default is [x,y] or [x,y,z] depending on the situation. To specify no axes labels use empty brackets. ex. labels=> ['p','r'] labels=> [,] Axes Font The font type and size for the axes labels. Default is to be filled along with a list of possible fonts and sizes. ex. axesfont=>[“Arial”,12] Title The default is no title. ex. title => [“Hello”] * The output dimensions of the graph (in pixels, points, inches, etc.) are given by the size option with default units of pixels. Defaults will be specified. ex. size => [200,300] size => [2,3,inches] * need specs for the tex and output dimensions * labeling of a function * piecewise function objects, where the domain of each piece will be specified as an interval object, will have special graphing properties: 1) Whether the endpoint of the interval is open or closed will determine whether a dot or open circle is plotted. * whether the x and y (and z) axes have the same scaling * viewing angle (for 3d graphs) * graphing engine * other options depending on the graphing engine . . . * Like formula objects geometric objects will have graphs associated with them. For example a square with given dimensions can be plotted in an xy axis with lower left corner at a given point and given line colors, etc. Graphs know from the Context() when they are created whether they are 2-d or 3-d (or, eventually 1-d, for intervals and sets). They smoothly upgrade to higher dimension when that is reasonable, for example, when a 3-d function is added to a 2-d graph. Example 1: $g = Graph($f1,$f2,'x^2 + 2','(3,2)'); Graph takes '(3,2)' to be a point in 2-space. '

    Example 2:

    DOCUMENT(); # This should be the first executable line in the problem.
    $showPartialCorrectAnswers = 1;
    $f = Compute('x^2');
    $g = Graph($f,{ymin=>-10,ymax=>100});
    $g2 = Graph('x^2/4',{ymin=>-1,ymax=>7,xmin=>3,xmax=>5});
    Here should be the graph of \(y = $f\): $BR
    \{ $g->plot \} | \{ $g2->plot \} $PAR
    ENDDOCUMENT(); # This should be the last executable line in the problem.


    $graph = $f->graph(xmin=>-2, xmax=>7, color=>'red', weight=>2); 
    $g1 = Compute('2x - 1')->graph; 


    The Graph object has two methods:

     * $g->add($f2, color=>'blue'); 

    $g->add('3xy - y^2'); The ->add method allows new MathOjbects<a href=""> ? </a>to be added to an existing graph.

     * $g->plot; 

    The ->plot method produces a plot in the format appropriate to the situation. It may produce a PNG file and an <img> tag pointing to it. It may produce a .m file and an <applet> tag which uses it. It may produce TeX code (perhaps an eepic or tikz picture or an \includegraphic). Graph objects stringify by calling the ->plot method.

    The . operator serves to 'join' two graphs -- the x, y and z ranges of the resulting graph are the smallest intervals which contain the ranges of the original graphs. The objects which were displayed on the original graphs are displayed on the new graph.


    $g = $g1 . $g2; 
    $g3 .= $f->graph; # This is equivalent to $g3->add($f); 
    $g4 = $f->graph . $f2->graph . $f3->graph . Compute('x+2')->graph; 

    For the future

    Eventually, Graph objects should also be able to produce

    • implicit plots
    • contour plots
    • parametric equations
    • scatter plots (and various other statistical plots)
    • differential equations plots.

    Function options (perhaps we should use the word 'formula' here instead, since that is what they are):

    * color
    * style (dotted, solid, etc)

    Graph Options:

    * display
    * bgcolor
    * grid

    A geometric class of objects that can be graphed, for example rectangles.