Suggestions on syntax to express mathematical formula concisely

前端 未结 13 1144
予麋鹿
予麋鹿 2021-02-02 08:46

I am developing functional domain specific embedded language within C++ to translate formulas into working code as concisely and accurately as possible.

I posted a proto

13条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 09:08

    If you're looking for simplicity you should take the implicitness of the loops even further. E.g., something like this

    T( i < j , j < N ) = ( T(i,j) - T(j,i) )/e(i+j)
    

    will work if you rewrite the assignment operator = to behave normally for something like a(i) = b(i) + c(i) but behave like a summation for a(i<5) = b(i) + c(i). Assume summation starts from 0 unless the lower limit is specified, e.g. a(3, check for symbolic upper/lower limits that appear as summation indices and make double sums as necessary. If you want the syntax to force explicitness you could define a separate sum operator s=

    T( i < j , j < N ) s= ( T(i,j) - T(j,i) )/e(i+j)
    

    I don't think you can get any cleaner than this and still have some general purpose usability. As for your short term goal, using the notion of specifying the summation index at the same time that the index first appears you could write.

    E_MP2 s= EV( i < n1 , j < n2 , a < n3 , b < n4 ) *
             2 ( EV(a,b,i,j) - EV(a,b,j,i) ) / ( e(i)+e(j)-e(a)-e(b) )
    

    where you explicitly state that this is a sum (using s=) making that operator then take the summation indices and limiting values from the first instance an index appears. Specifically you could also use a syntax like (assuming now a,b fixed and i,j as per your example)

    E_MP2 s=(i

    which is quite clear notationally.

    You could then go on and take this concept even further by, e.g., defining an integration operator i= that does the same thing. I.e. it looks for instances of variables that are marked down with limits and then proceeds to integrate the expression with respect to those variables

    F i=(0

    similarly to the summation you could specify the limit when x first occurs

    F i= x[0,Pi]^-1 * exp(-I x^2)
    

    where the square brackets serve to differentiate the notation from summation, so that you don't have to use i= or s= and can use both summation and integration at the same time:

    F(i) = G(i,j<10) * x[0,inf]^-1 * H(i,j,x)
    

提交回复
热议问题