Suggestions on syntax to express mathematical formula concisely

前端 未结 13 1124
予麋鹿
予麋鹿 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:15

    You may want to look at APLish languages for inspiration. By working with the matrix/array as a whole, you can get the brevity down quite a bit. I'm going to use Q below, and unless I misread your post, your second statement can be written as:

    sum raze (T-flip T) div e
    

    To understand T minus flip T, you need to look at an example. Imagine we have T set to the following matrix:

    0 1 2
    3 4 5
    6 7 8
    

    flip T swaps the top two dimensions, which results in:

    0 3 6
    1 4 7
    2 5 8
    

    So T-flip T is basically T(i,j)-T(j,i) but a lot less typing, and thus a lot less error-prone.

    The raze reduces an array to a single dimension, so razing this:

    0 3 6
    1 4 7
    2 5 8
    

    turns it into this:

    0 3 6 1 4 7 2 5 8
    

    Finally, the sum is sum-over; it basically adds up all of the members of its list, so

    sum 0 3 6 1 4 7 2 5 8
    

    means:

    0+3+6+1+4+7+2+5+8
    

    Implementing this kind of thing in C++ isn't that difficult; you could:

    sum(raze((T-flip(T))/e))
    

    with the right level of operator overloading.

    By the way, K is even more brief:

    +/,/(T-+:T)%e
    

提交回复
热议问题