www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - BackMath: a compile time symbolic math lib.

reply BCS <ao pathlink.com> writes:
I have written a library that allows for all of the following as native syntax:

A + B - D = C;
A - B - D = C;
A - D - B = C;
(B - A) - D = C;
A - D - B - D = C;
A - B - (D + D) = C;
C = A + B - D;
C = B * A;
C = B * A + B * D;
A / B  + D / B = C;

In all of these cases the assignment is to B.

The library is works by using term re-writing to reverse evaluate the
expression. 
The fun part is that the term re-writing is done with the type system and 
template expressions. This results in there being little inherent runtime 
cost.
I hope to expand it to work with as wide a variety of equation forma as I 
can. I wold love to be able to get it to even solve system of equations like

a*B + c*D = e;
f*B + g*D = h;

where B and D are both unknown.

Now, full disclosure:
-it's incomplete, there a lots of cases it won't handle (but I plan on
improving 
that)
-it is not thread safe, this is because the templates need alias to variables 

-It's unproven, I can formally prove all the "single sided" rules, but haven't 
figured out how to prove the "double sided" rules, yet like the last 2
examples, 
%
-No, I didn't bang out 400 lines of static if's, the "business logic" is 
generated using a lisp program that is harder to read than what it produces ^

Now for the challenge. Is anyone willing to try and generate a C++ equivalent? 
I think it would be really cool to have a side by side comparison of the 
two libs. I think the difference in readability say a lot.

hosted at:
   http://www.dsource.org/projects/scrapple/browser/trunk/backmath

* Walter can we please get this fixed??

% I'm using ACL2 to do the proofs. http://www.cs.utexas.edu/users/moore/acl2/
^ I'll clean it up and post it as well soon. (finals week is coming soon 
so don't hold your breath or anything)
Dec 04 2007
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:ce0a3343268928ca04931959c536 news.digitalmars.com...

 the only variables that can be aliased are globals.
Template alias parameters can be locals.
Dec 04 2007
prev sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS wrote:
 I have written a library that allows for all of the following as native 
 syntax:
 
 A + B - D = C;
 A - B - D = C;
 A - D - B = C;
 (B - A) - D = C;
 A - D - B - D = C;
 A - B - (D + D) = C;
 C = A + B - D;
 C = B * A;
 C = B * A + B * D;
 A / B  + D / B = C;
 
 In all of these cases the assignment is to B.
 
 The library is works by using term re-writing to reverse evaluate the 
 expression. The fun part is that the term re-writing is done with the 
 type system and template expressions. This results in there being little 
 inherent runtime cost.
 I hope to expand it to work with as wide a variety of equation forma as 
 I can. I wold love to be able to get it to even solve system of 
 equations like
 
 a*B + c*D = e;
 f*B + g*D = h;
 
 where B and D are both unknown.
 
 Now, full disclosure:
 -it's incomplete, there a lots of cases it won't handle (but I plan on 
 improving that)
 -it is not thread safe, this is because the templates need alias to 
 variables to work correctly and the only variables that can be aliased 

 -It's unproven, I can formally prove all the "single sided" rules, but 
 haven't figured out how to prove the "double sided" rules, yet like the 
 last 2 examples, %
 -No, I didn't bang out 400 lines of static if's, the "business logic" is 
 generated using a lisp program that is harder to read than what it 
 produces ^
 
 Now for the challenge. Is anyone willing to try and generate a C++ 
 equivalent? I think it would be really cool to have a side by side 
 comparison of the two libs. I think the difference in readability say a 
 lot.
 
 hosted at:
   http://www.dsource.org/projects/scrapple/browser/trunk/backmath
 
 * Walter can we please get this fixed??

 % I'm using ACL2 to do the proofs. 
 http://www.cs.utexas.edu/users/moore/acl2/
 ^ I'll clean it up and post it as well soon. (finals week is coming soon 
 so don't hold your breath or anything)
 
 
Where was this lib when I was doing Algebra I in middle school :-)? Very cool stuff; I'm always amazed at what D can do with metaprogramming. FWIW, I think local variables can be template alias parameters, I'm using just that fact to get the names of the functions a particular template is instantiated in. I'm not sure if this is what you meant, though.
Dec 04 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Robert,

 Where was this lib when I was doing Algebra I in middle school :-)?
 
 Very cool stuff; I'm always amazed at what D can do with
 metaprogramming.
thanks :)
 FWIW, I think local variables can be template alias
 parameters, I'm using just that fact to get the names of the functions
 a particular template is instantiated in. I'm not sure if this is what
 you meant, though.
 
what I need is something that would work like this: struct S(alias r) { real Get(){return r;} } void Do() { real a; S!(a) s; real b = s.Get(); } the issue is that the struct doesn't have access to the function. There is no reason this should be impossible, but the semantics of how to make it work would be a major project in and of it's self. If anyone can find a workaround I'm open to suggestions.
Dec 04 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:ce0a33432689c8ca049e1da7fc2e news.digitalmars.com...
 what I need is something that would work like this:

 struct S(alias r)
 {
   real Get(){return r;}
 }

 void Do()
 {
   real a;
   S!(a) s;
   real b = s.Get();
 }

 the issue is that the struct doesn't have access to the function. There is 
 no reason this should be impossible, but the semantics of how to make it 
 work would be a major project in and of it's self.

 If anyone can find a workaround I'm open to suggestions.
Ooh, I see. Actually I think templated _functions_ with alias parameters can be called with aliases to local variables -- in which case, IIRC, the templated function will actually be instantiated as a nested function within that function, giving it access to the calling function's stack frame. But you're right, this is incredibly difficult to do in the general case.
Dec 04 2007