Skip to content

Commit

Permalink
Add support for subscript
Browse files Browse the repository at this point in the history
Example:

Input:
┌───────────────────────────┐
│S_n = u_1 + u_2 + ... + u_n│
└───────────────────────────┘
Output:
┌───────────────────────┐
│S  = u  + u  + ... + u │
│ n    1    2          n│
└───────────────────────┘

This fixes issue:
#1
  • Loading branch information
ArthurSonzogni committed May 18, 2020
1 parent e726975 commit 4df472a
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Expand Up @@ -8,8 +8,6 @@ script:
- cd build
- cmake ..
- cmake --build .
- cat ./CMakeFiles/CMakeOutput.log
- cat ./CMakeFiles/CMakeError.log

notifications:
email: false
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Expand Up @@ -45,7 +45,7 @@ if (Web)
string(APPEND CMAKE_CXX_FLAGS " -s ASSERTIONS=2")
string(APPEND CMAKE_CXX_FLAGS " --closure 1")
string(APPEND CMAKE_CXX_FLAGS " --no-heap-copy")
string(APPEND CMAKE_CXX_FLAGS " -s EXPORTED_FUNCTIONS='[\"_translate\"]'")
string(APPEND CMAKE_CXX_FLAGS " -s EXPORTED_FUNCTIONS='[\"_translate\", \"_main\"]'")
string(APPEND CMAKE_CXX_FLAGS " -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\",\"cwrap\"]'")

option(ADD_GOOGLE_ANALYTICS "Build the static library" ON)
Expand Down
35 changes: 24 additions & 11 deletions src/translator/math/Math.cpp
Expand Up @@ -36,15 +36,16 @@ std::vector<Translator::Example> Math::Examples() {
{"1-fraction", "f(x) = 1 + x / (1 + x)"},
{"2-square-root", "sqrt(1+sqrt(1+x/2))"},
{"3-power", "f(x) = 1 + x^2 + x^3 + x^(1+1/2)"},
{"4-summation", "sum(i^2,i=0,n) = n^3/2+n^2/2+n/6"},
{"5-integral", "int(x^2 * dx ,0,1) = n^3/3"},
{"6-product",
{"4-subscript", "S_n = u_1 + u_2 + ... + u_n"},
{"5-summation", "sum(i^2,i=0,n) = n^3/2+n^2/2+n/6"},
{"6-integral", "int(x^2 * dx ,0,1) = n^3/3"},
{"7-product",
"mult(i^2,i=1,n) = (mult(i,i=1,n))^2\n\n\n\nmult(1/2,1,100) = "
"7.8886091e-31"},
{"7-vector", "[a;b] + [c;d] = [a+c; b+d]"},
{"8-matrix", "[1,2;3,4] * [x;y] = [1*x+2*y; 3*x+4*y]"},
{"9-factorial", "[n;k] = n! / (k! *(n-k)!)"},
{"10-Math-symbols",
{"8-vector", "[a;b] + [c;d] = [a+c; b+d]"},
{"9-matrix", "[1,2;3,4] * [x;y] = [1*x+2*y; 3*x+4*y]"},
{"10-factorial", "[n;k] = n! / (k! *(n-k)!)"},
{"11-Math-symbols",
"Alpha + alpha + Digamma + digamma + Kappa + kappa + Omicron \n"
"omicron + Upsilon + upsilon + Beta + beta + Zeta + zeta + Lambda \n"
"lambda + Pi + pi + Phi + phi + Gamma + gamma + Eta + eta + Mu + mu \n"
Expand Down Expand Up @@ -86,8 +87,7 @@ void Draw::Resize(int new_dim_x, int new_dim_y) {
dim_x = new_dim_x;
dim_y = new_dim_y;

content.resize(dim_y);
for (auto& line : content) {
content.resize(dim_y); for (auto& line : content) {
line.resize(dim_x, L' ');
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ Draw ComposeVertical(const Draw& top, const Draw& down, int spaces) {
return composition;
}

Draw ComposeDiagonal(const Draw& A, const Draw& B) {
Draw ComposeDiagonalUp(const Draw& A, const Draw& B) {
Draw composition;
composition.Append(A, 0, B.dim_y);
composition.Append(B, A.dim_x, 0);
Expand All @@ -129,6 +129,17 @@ Draw ComposeDiagonal(const Draw& A, const Draw& B) {
return composition;
}

Draw ComposeDiagonalDown(const Draw& A, const Draw& B) {
Draw composition;
composition.Append(A, 0, 0);
composition.Append(B, A.dim_x, A.dim_y);

composition.center_x = composition.dim_x / 2;
composition.center_y = A.center_y;

return composition;
}

Draw WrapWithParenthesis(const Draw& element, Style* style) {
Draw draw;
draw.Resize(element.dim_x + 2, element.dim_y);
Expand Down Expand Up @@ -253,7 +264,9 @@ Draw Parse(MathParser::FactorContext* context,
suppress_parenthesis &= (context->valueBang().size() == 1);
Draw draw = Parse(context->valueBang(0), style, suppress_parenthesis);
for (int i = 1; i < context->valueBang().size(); ++i) {
draw = ComposeDiagonal(draw, Parse(context->valueBang(i), style, false));
auto* compose =
context->powop(i - 1)->POW() ? ComposeDiagonalUp : ComposeDiagonalDown;
draw = compose(draw, Parse(context->valueBang(i), style, false));
}
return draw;
}
Expand Down
6 changes: 4 additions & 2 deletions src/translator/math/Math.g4
Expand Up @@ -45,7 +45,7 @@ newlines : EOL+;
equation : expression (relop expression)* ;
expression : term (addop term)* ;
term : factor (mulop factor)* ;
factor : valueBang (POW valueBang)* ;
factor : valueBang (powop valueBang)* ;
valueBang: value | valueBang BANG;
value : (PLUS | MINUS)? atom ;
atom : scientific | variable | function | matrix | LPAREN expression RPAREN ;
Expand All @@ -57,9 +57,10 @@ matrixLine : expression ( ',' expression ) *;
relop : EQ | GT | LT | GE | LE;
addop : PLUS | MINUS ;
mulop : TIMES | DIV ;
powop : POW | SUBSCRIPT ;
VARIABLE : VALID_ID_START VALID_ID_CHAR* | '...' ;

fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_';
fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') ;
fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;
SCIENTIFIC_NUMBER : NUMBER (E SIGN? NUMBER)? ;

Expand All @@ -85,6 +86,7 @@ LE : '<=' ;
EQ : '=' ;
POINT : '.' ;
POW : '^' ;
SUBSCRIPT: '_' ;
EOL : '\r\n' | '\n' ;
WS : [ \t] + -> skip ;

Expand Down
3 changes: 3 additions & 0 deletions test/Math/mixed_pow_subscript/input
@@ -0,0 +1,3 @@
A_2^3

A^2_3
7 changes: 7 additions & 0 deletions test/Math/mixed_pow_subscript/output
@@ -0,0 +1,7 @@
3
A
2

2
A
3
11 changes: 11 additions & 0 deletions test/Math/pow/input
@@ -0,0 +1,11 @@
A^2

A^(1/2)^sqrt(2)

(A^(1/2))^sqrt(2)

A^((1/2)^sqrt(2))

A^421 + 1/2

A^421/2
32 changes: 32 additions & 0 deletions test/Math/pow/output
@@ -0,0 +1,32 @@
2
A

_
╲╱2
⎛1⎞
⎜─⎟
⎝2⎠
A

_
╲╱2
⎛ ⎛1⎞⎞
⎜ ⎜─⎟⎟
⎜ ⎝2⎠⎟
⎝A ⎠

⎛ _⎞
⎜ ╲╱2⎟
⎜⎛1⎞ ⎟
⎜⎜─⎟ ⎟
⎝⎝2⎠ ⎠
A

421 1
A + ─
2

421
A
────
2
11 changes: 11 additions & 0 deletions test/Math/subscript/input
@@ -0,0 +1,11 @@
A_2

A_(1/2)_sqrt(2)

(A_(1/2))_sqrt(2)

A_((1/2)_sqrt(2))

A_421 + 1/2

A_421/2
32 changes: 32 additions & 0 deletions test/Math/subscript/output
@@ -0,0 +1,32 @@
A
2

A
⎛1⎞
⎜─⎟
⎝2⎠
_
╲╱2

⎛A ⎞
⎜ ⎛1⎞⎟
⎜ ⎜─⎟⎟
⎝ ⎝2⎠⎠
_
╲╱2

A
⎛⎛1⎞ ⎞
⎜⎜─⎟ ⎟
⎜⎝2⎠ ⎟
⎜ _⎟
⎝ ╲╱2⎠

1
A + ─
421 2

A
421
────
2

0 comments on commit 4df472a

Please sign in to comment.