Out of the box you can't do something like that but Stardog makes it easy to write user defined functions that could do something like that. See this example.
Your query would need to look a little bit different like the following:
I was working on writing a user defined function that would execute javascript expressions that would be similar to this but haven't gotten around to finishing it. I might be motivated to finish it if it was something that you'd find useful
PS I'm assuming you're looking for something generic and want to use some additional logic for selecting the formula and/or have a large number of formulas. You can evaluate basic arithmetic expressions directly in SPARQL.