Re: Parsing and storing formulas

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: James Curran (JamesCurran_at_mvps.org)
Date: 10/28/04


Date: Thu, 28 Oct 2004 15:13:28 -0400


    You could also use MathML as an intermediate format. Your example of
"2(Y+2X)", in MathML becomes:
<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <mrow>
  <mn>2</mn>
  <mo>&InvisibleTimes;</mo>
  <mrow>
   <mo>(</mo>
   <mrow>
    <mrow>
     <mn>2</mn>
     <mo>&InvisibleTimes;</mo>
     <mi>X</mi>
    </mrow>
    <mo>+</mo>
    <mi>Y</mi>
   </mrow>
   <mo>)</mo>
  </mrow>
 </mrow>
</math>
Tools exist for converting expression strings into MathML and evaluating
MathML files.

-- 
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com       Work: www.njtheater.com
Blog: www.honestillusion.com  Day Job: www.partsearch.com
"Stefan Rosi" <invalid@invalid.com> wrote in message
news:uc0TP5PvEHA.1300@TK2MSFTNGP14.phx.gbl...
> I was wondering how I can parse a mathematical formula in a storable way.
> For example I have two parameters like X and Y. And the user has to
specify
> a formula to calculate the result. Like   2(Y+2X). Then this formula has
to
> be stored for in the database. Each time the user visits a specific page,
> the calculations have to be done according to his own formula.
>
> Till now I have two ideas, the first one is to store the formula in just a
> string and make the variable names identical to their representation
columns
> in the database. So I can just execute the query against the proper table
> and SQL Server will parse the string and return the result. Anyway this
> method has two drawbacks, first I can't use stored procedure anymore, and
> second the mathematical operations can be very complex, which will be a
> problem because sql server is not a mathematic program.
>
>
>
> The second idea is to represent the formula as a tree.  Each inner node
> stands for an operation like multiplication, addition . etc. and the
leaves
> are either variables or constants: to clarify this idea I will attach a
> diagram with this post. When I want to make the calculations I have just
to
> cal a function on the root, and it will call the same function on it
> children and so on recursively. The tree will be serialized and saved as
xml
> in the database.
>
> Anyway this tree will be hard to implement.
>
> So I just wanted to ask if somebody knows a better way to do this task. I
am
> sure I am not the first person faced with this problem.
>
> How can I parse and store an entered formula? Is there any ready component
> to do it?
>
>
>
> Any answer will be appreciated.
>
> Stefan
>
>
>