Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    215639

Untitled

Sign in to follow this  
Mushu

42 views

  • Random Observation of the Day!
    The correct way to cite one's own drawings in one's own paper is to write "[Created by Author]" at the end of the caption. Yeah, whatever.

    It took the professor a while to find this obscure fact; I had to try my best not to laugh


So yeah. Hopefully that'll raise the grade on the paper up a little (she was being nice and let me pencil it in). Then as I was walking back to my dorm, I realized I had forgotten to "pledge it" (ie, sign that I didn't cheat on it). Which means I'll be lucky if she grades it at all.

Pleh.

Anyway, just finished off the variable part of the equation storage mechanism classes! Just a quickie to go over the usage, like usual -

Given: f(x,y) = x^2 + y^2

Yay a happy happy paraboloid :D

First we typedef ourself a variable table (since it'll get tedious to write out) -

typedef VarTable< float, std::string > vTType;

VarTable is essentially a wrapper for std::map at this point, and with these parameters will create a string-to-float map. Joy!

For convience, lets also typedef ourselves a class to polymorphically point to Values, Expressions, and Statements -

typedef IEvalable< float, vTType >* pEval;

Next we need a Statement object -

Statement< float, vTType > equ;

See how the typedef earlier was helpful? No? Well, you soon shall see.. bwahaha.

Lets set up some values for the body of the equation -

pEval varX = new Variable< float, vTType >( "x" );
pEval varY = new Variable< float, vTType >( "y" );
pEval expX = new Value< float, vTType >( 2 );
pEval expY = new Value< float, vTType >( 2 );


Yeah, that's a real pain in the ass to type. But if you put it into perspective - these functions are going to be used by the parser functions (which will be templated) it gets reduced a lot. Well, to a general constructor and some switch statements. I hate writing parsers.

Moving on, let's raise our x and y to the second power -

pEval exprX2 = new Expression< OP_POW, float, vTType >( varX, expX );
pEval exprY2 = new Expression< OP_POW, float, vTType >( varY, valY );


Finally, we can splice them into our equation using OP_ADDs -

equ.splice< OP_ADD >( exprX2 );
equ.splice< OP_ADD >( exprY2 );


Awesome! Now we've got our equation stored in a Statement structure, so we can call Statement::eval( vTType ) and get values from it -

Err, after we set some values that is. By default an undefined variable query will return 0. So if we call eval now we'll just get 0. Boring.

In fact, lets instance ourselves a table first. Yeah, that'd be a good idea :|

vTType vTable;
vTable.set( "x", 1 );
vTable.set( "y", 3 );


Now we can call eval -

std::cout << equ.eval( vTable ) << std::endl;

Which outputs "10" to our console. Hooray!

Coming next: a parser to do this automagically. Or, more probably, me ranting about how parsers suck and how I'm too stupid to write one to parse correctly yadda yadda. Hooray! :D
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now