SeExpr
Functions | Variables
tutorial.txt File Reference

Functions

<!--CopyrightDisneyEnterprises,
Inc.Allrightsreserved.LicensedundertheApacheLicense,
Version2.0(the"License");youmaynotusethisfileexceptincompliancewiththeLicenseandthefollowingmodificationtoit:Section6Trademarks.deletedandreplacedwith:6.Trademarks.ThisLicensedoesnotgrantpermissiontousethetradenames,
trademarks, servicemarks,
orproductnamesoftheLicensoranditsaffiliates,
exceptasrequiredforreproducingthecontentoftheNOTICEfile.YoumayobtainacopyoftheLicenseathttp:-->
< h2 > Programmer Tutorial</h2 >
< p > Getting started with
SeExpr is relatively easy
SeExpr gives you a way to
evaluate one or many
evaluations of an expression
What changes between different
applications of expressions is
mainly the particular
variables that are 
accessible (sometimes also the set of functions).Each application of expressions generally has it's own subclass of Expression that gets instantiated.To get started we're going to go through a simple application that is an ascii graphing calculator.This is located in the src/demos/asciiGraph.cpp part of the source tree.< p >< h2 >Problem Overview</h2 > We are going to write a function grapher that displays in ASCII.In particular for a given f(x) we can evaluate it at all the x's in a window and draw the resulting y's.For example if the user ran our program< pre >./asciiGraph"val
 
sin (val)/val" </pre> we would get <pre> | | | | | </pre> or if we did <pre> ./asciiGraph "x-3" </pre> we'd get <pre> | | ------------------------------|----------------- | | | | | </pre> <h2>Implement the subclass</h2> First we subclass Expression and give it a const ructor
 
</pre > Once we have this we
need an instance to store our
variable and provide a
reference to that We make it
because 
resolveVar () is const .One does not need to store a variable reference in a given expression.In fact
 
</pre >< h3 > Variable setting
</h3 > Next we need to make a
way of setting the variable As
the controlling code will use
the expression it will
repeatedly alternate between
setting the independent
variables that are used and
calling 
evaluate ().What it has to do depends very much on the application.In this case we only need to set the independent variable x as
 
</pre >< h2 > Evaluating
expressions</h2 > Evaluating
an expression is pretty easy
But before we can do that we
need to make an instance< pre >
GrapherExpr 
expr ("x+x^2")
 
</pre > there might be errors
in the expression you must
check with 
isValid () before you can evaluate.Then you can print a parse error as well< pre > if(!expr.isValid())
 
 for (int i=0;i &lt;w;i++)
 

Variables

</pre >< h3 > A simple
variable reference</h3 > This
is not a very interesting
subclass of expression until
we add some additional
variables Variables on some
applications may be very
dynamic In this 
case
 
</pre >< h3 > A simple
variable reference</h3 > This
is not a very interesting
subclass of expression until
we add some additional
variables Variables on some
applications may be very
dynamic In this 
however
 
</pre >< h3 > A simple
variable reference</h3 > This
is not a very interesting
subclass of expression until
we add some additional
variables Variables on some
applications may be very
dynamic In this we only need 
x
 
</pre > Once we have this we
need an instance to store our
variable and provide a
reference to that We make it 
mutable
 
</pre > Once we have this we
need an instance to store our
variable and provide a
reference to that We make it
because it may be useful to
use the same ExprVarRef from
multiple expressions!For 
example
 
</pre > Once we have this we
need an instance to store our
variable and provide a
reference to that We make it
because it may be useful to
use the same ExprVarRef from
multiple expressions!For if
you have expressions that all
have access to the same 
variables
 
</pre >< h3 > Binding our
variable reference</h3 > If we
now tried to use 
expressions
 
</pre >< h3 > Binding our
variable reference</h3 > If we
now tried to use the variable
would still not be found by
our expressions To make it
bindable we need to override
the resolveVar() function as
follows 
return
 
</pre >< h3 > Variable setting
</h3 > Next we need to make a
way of setting the variable As
the controlling code will use
the expression 
evaluation
 
</pre > However
 
</pre > Finally
 
</pre > we can loop through
all the x points in the graph
and find out the y value as 
follows
 
const double one_over_samples_per_pixel =1./samplesPerPixel
 

Function Documentation

<!--CopyrightDisneyEnterprises,Inc.Allrightsreserved.LicensedundertheApacheLicense,Version2.0(the"License");youmaynotusethisfileexceptincompliancewiththeLicenseandthefollowingmodificationtoit:Section6Trademarks.deletedandreplacedwith:6.Trademarks.ThisLicensedoesnotgrantpermissiontousethetradenames,trademarks,servicemarks,orproductnamesoftheLicensoranditsaffiliates,exceptasrequiredforreproducingthecontentoftheNOTICEfile.YoumayobtainacopyoftheLicenseathttp:--><h2> Programmer Tutorial</h2><p> Getting started with SeExpr is relatively easy SeExpr gives you a way to evaluate one or many evaluations of an expression What changes between different applications of expressions is mainly the particular variables that are accessible ( sometimes also the set of  functions)
</pre><h3> Variable setting</h3> Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate ( )

Definition at line 157 of file tutorial.txt.

</pre><h2> Evaluating expressions</h2> Evaluating an expression is pretty easy But before we can do that we need to make an instance<pre> GrapherExpr expr ( "x+x^2"  )
for ( int  i = 0;i&lt;w;i++)

Definition at line 193 of file tutorial.txt.

Referenced by SeExpr2::PrintFuncX::eval().

</pre> there might be errors in the expression you must check with isValid ( )

Definition at line 177 of file tutorial.txt.

Referenced by SeExpr2::ExprPrototypeNode::prep().

</pre> Once we have this we need an instance to store our variable and provide a reference to that We make it because resolveVar ( ) const
* sin ( val  ) const

Variable Documentation

</pre><h3> A simple variable reference</h3> This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this case

Definition at line 108 of file tutorial.txt.

</pre><h3> Variable setting</h3> Next we need to make a way of setting the variable As the controlling code will use the expression evaluation

Definition at line 151 of file tutorial.txt.

</pre> Once we have this we need an instance to store our variable and provide a reference to that We make it because it may be useful to use the same ExprVarRef from multiple expressions ! For example

Definition at line 128 of file tutorial.txt.

</pre><h3> Binding our variable reference</h3> If we now tried to use expressions

Definition at line 140 of file tutorial.txt.

</pre> Finally

Definition at line 185 of file tutorial.txt.

</pre> we can loop through all the x points in the graph and find out the y value as follows

Definition at line 185 of file tutorial.txt.

</pre><h3> A simple variable reference</h3> This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this however

Definition at line 108 of file tutorial.txt.

</pre> However

Definition at line 177 of file tutorial.txt.

</pre> Once we have this we need an instance to store our variable and provide a reference to that We make it mutable

Definition at line 126 of file tutorial.txt.

const double one_over_samples_per_pixel =1./samplesPerPixel

Definition at line 192 of file tutorial.txt.

Referenced by main().

</pre><h3> Binding our variable reference</h3> If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar () function as follows return

Definition at line 141 of file tutorial.txt.

Referenced by ExprTextEdit::keyPressEvent().

</pre> Once we have this we need an instance to store our variable and provide a reference to that We make it because it may be useful to use the same ExprVarRef from multiple expressions ! For if you have expressions that all have access to the same variables

Definition at line 128 of file tutorial.txt.

</pre><h3> A simple variable reference</h3> This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x