| 327 | | char [] subexprSimplify(char [] expr, int [] rank, char [] mulexpr, char [] indexexpr) |
|---|
| 328 | | { |
|---|
| 329 | | if (expr.length==1) { |
|---|
| 330 | | int r = subexprRank(expr, rank); |
|---|
| 331 | | char [] e = expr; |
|---|
| 332 | | if(indexexpr.length>0) { |
|---|
| 333 | | assert(r>0, "BLADE BUG: MISMATCHED INDEX " ~ expr ~ " " ~ indexexpr ~ " " ~ mulexpr); |
|---|
| 334 | | e = " {" ~ expr ~ indexexpr ~ "} "; |
|---|
| 335 | | } |
|---|
| 336 | | if (mulexpr.length>1) { |
|---|
| 337 | | // in this case, it's worth creating a new variable |
|---|
| 338 | | return "( {" ~ mulexpr ~ "} *" ~ e ~ ")"; |
|---|
| 339 | | } |
|---|
| 340 | | if (mulexpr.length>0) return "(" ~ mulexpr ~ "*" ~ e ~ ")"; |
|---|
| 341 | | return e; |
|---|
| 342 | | } |
|---|
| 343 | | // strip off the parentheses before simplifying |
|---|
| 344 | | return exprSimplify(expr[1..$-1], rank, mulexpr, indexexpr); |
|---|
| 345 | | } |
|---|
| 346 | | |
|---|
| 347 | | /** |
|---|
| 348 | | * Rewrite the expression, taking advantage of distributivity of [] and |
|---|
| 349 | | * associativity of *. Additionally, we group all scalars together, whenever |
|---|
| 350 | | * possible. |
|---|
| 351 | | * |
|---|
| 352 | | * This process creates compound scalars and vectors, delineated by " {" and "} ". |
|---|
| 353 | | * They will be removed in a subsequent step. |
|---|
| 354 | | */ |
|---|
| 355 | | char [] exprSimplify(char [] expr, int [] rank, char [] mulexpr, char [] indexexpr) |
|---|
| 356 | | { |
|---|
| 357 | | // Deal with ++ and --. Only for scalars |
|---|
| 358 | | if (expr.length>2 && (expr[0..2]=="++" || expr[0..2]=="--")) { |
|---|
| 359 | | return expr[0..2] ~ subexprSimplify(expr[2..$], rank, mulexpr, indexexpr); |
|---|
| 360 | | } |
|---|
| 361 | | if (expr.length>2 && (expr[$-2..$]=="++" || expr[$-2..$]=="--")) { |
|---|
| 362 | | return subexprSimplify(expr[0..$-2], rank, mulexpr, indexexpr) ~ expr[$-2..$]; |
|---|
| 363 | | } |
|---|
| 364 | | // Deal with unary operators |
|---|
| 365 | | if (expr[0]=='-') { |
|---|
| 366 | | // Fold unary minus into a multiply, if possible. |
|---|
| 367 | | if (mulexpr.length>0) return subexprSimplify(expr[1..$], rank, "-" ~ mulexpr, indexexpr); |
|---|
| 368 | | return "-" ~ subexprSimplify(expr[1..$], rank, mulexpr, indexexpr); |
|---|
| 369 | | } |
|---|
| 370 | | // Just remove unary plus. |
|---|
| 371 | | if (expr[0]=='+') return subexprSimplify(expr[1..$], rank, mulexpr, indexexpr); |
|---|
| 372 | | |
|---|
| 373 | | int x = exprLength(expr); |
|---|
| 374 | | int y = x+1; |
|---|
| 375 | | assert(y < expr.length, expr); |
|---|
| 376 | | // Deal with shifts, op=, and NCEG operators |
|---|
| 377 | | while (expr[y+1]=='<' || expr[y+1]=='>' || expr[y+1]=='=') ++y; |
|---|
| 378 | | |
|---|
| 379 | | char [] op = expr[x+1..y+1]; |
|---|
| 380 | | char [] left = expr[0..x+1]; |
|---|
| 381 | | char [] right = expr[y+1..$]; |
|---|
| 382 | | if (expr[x+1]=='[') right = expr[y+1..$-1]; // drop off the ']'. |
|---|
| 383 | | int lrank = subexprRank(left, rank); |
|---|
| 384 | | if (op=="[") { |
|---|
| 385 | | // accumulate indexing and slicing operations |
|---|
| 386 | | return subexprSimplify(left, rank, mulexpr, "[" ~ right ~ "]" ~ indexexpr); |
|---|
| 387 | | } |
|---|
| 388 | | int rrank = subexprRank(right, rank); |
|---|
| 389 | | // Fold scalars together |
|---|
| 390 | | if (op=="*") { |
|---|
| 391 | | if (lrank==0) { |
|---|
| 392 | | char [] m = left; |
|---|
| 393 | | if (mulexpr.length>0) m = "(" ~ left ~ "*" ~ mulexpr ~ ")"; |
|---|
| 394 | | if (right.length > 1 && hasScalarMultiply(right[1..$-1], rank)) { |
|---|
| 395 | | // opportunity for scalar folding |
|---|
| 396 | | return subexprSimplify(right[1..$-1], rank, m, indexexpr); |
|---|
| 397 | | } else { |
|---|
| 398 | | if (m.length>1) m = " {" ~ m ~ "} "; |
|---|
| 399 | | return "(" ~ m ~ "*" ~ subexprSimplify(right, rank, "", indexexpr) ~ ")"; |
|---|
| 400 | | } |
|---|
| 401 | | |
|---|
| 402 | | } else if (rrank==0) { |
|---|
| 403 | | char [] m = right; |
|---|
| 404 | | if (mulexpr.length>0) m = "(" ~ mulexpr ~ "*" ~ right ~ ")"; |
|---|
| 405 | | if (left.length> 1 && hasScalarMultiply(left[1..$-1], rank)) { |
|---|
| 406 | | return subexprSimplify(left, rank, m, indexexpr); |
|---|
| 407 | | } else { |
|---|
| 408 | | if (m.length>1) m = " {" ~ m ~ "} "; |
|---|
| 409 | | return "(" ~ m ~ "*" ~ subexprSimplify(left, rank, "", indexexpr) ~ ")"; |
|---|
| 410 | | } |
|---|
| 411 | | } // else it's matrix * matrix |
|---|
| 412 | | } |
|---|
| 413 | | if (op=="*=") { |
|---|
| 414 | | if (rrank==0) { |
|---|
| 415 | | char [] m = right; //subexprSimplify(right, rank, "", ""); |
|---|
| 416 | | if (mulexpr.length>0) m ~= "*" ~ mulexpr; |
|---|
| 417 | | if (m.length>1) m= " {" ~ m ~ "} "; |
|---|
| 418 | | return "(" ~ subexprSimplify(left, rank, "", indexexpr)~ "*=" ~ m ~ ")"; |
|---|
| 419 | | } |
|---|
| 420 | | } |
|---|
| 421 | | return "(" ~ subexprSimplify(left, rank, mulexpr, indexexpr) ~ op ~ subexprSimplify(right, rank, mulexpr, indexexpr) ~ ")"; |
|---|
| 422 | | } |
|---|
| 423 | | |
|---|
| 424 | | struct RevisedExpression { |
|---|
| 425 | | char [] expr; // the revised expression using original variable names |
|---|
| 426 | | char [][] compounds; // the compound variables |
|---|
| 427 | | int [] rank; // rank of all of the compound variables |
|---|
| 428 | | char [] used; // which of the original variables are used. |
|---|
| 429 | | } |
|---|
| 430 | | |
|---|
| 431 | | // revised expression, with unused symbols collapsed. |
|---|
| 432 | | // (so, for example, B+=D*F becomes A+=B*C). |
|---|
| 433 | | char [] removedUnusedSymbolsFromExpression(RevisedExpression e) |
|---|
| 434 | | { |
|---|
| 435 | | char [] m = ""; |
|---|
| 436 | | char knt = 'A'; |
|---|
| 437 | | for (int i=0; i<e.used.length; ++i) { |
|---|
| 438 | | if (e.used[i]!='-') { |
|---|
| 439 | | m~=knt; |
|---|
| 440 | | ++knt; |
|---|
| 441 | | } else m~="@"; |
|---|
| 442 | | } |
|---|
| 443 | | for (int i=0; i<e.compounds.length;++i) { |
|---|
| 444 | | m~=knt; ++knt; |
|---|
| 445 | | } |
|---|
| 446 | | |
|---|
| 447 | | char [] f = ""; |
|---|
| 448 | | for (int i=0; i<e.expr.length; ++i) { |
|---|
| 449 | | char c = e.expr[i]; |
|---|
| 450 | | if (c>='A' && c<='Z') { |
|---|
| 451 | | f ~= m[c-'A']; |
|---|
| 452 | | } else f ~= c; |
|---|
| 453 | | } |
|---|
| 454 | | return f; |
|---|
| 455 | | } |
|---|
| 456 | | |
|---|
| 457 | | int indexRank(char [] s) |
|---|
| 458 | | { |
|---|
| 459 | | int r=0; |
|---|
| 460 | | int numbrack=0; |
|---|
| 461 | | for(int i=1; i<s.length; ++i) { |
|---|
| 462 | | if (s[i]==']') numbrack--; |
|---|
| 463 | | if (s[i]=='[') { |
|---|
| 464 | | if (numbrack==0) ++r; |
|---|
| 465 | | numbrack++; |
|---|
| 466 | | } |
|---|
| 467 | | if (numbrack==0 && s[i]=='.' && s[i-1]=='.') { |
|---|
| 468 | | // if it's a slice, it does not increase the rank |
|---|
| 469 | | r--; |
|---|
| 470 | | } |
|---|
| 471 | | } |
|---|
| 472 | | return r; |
|---|
| 473 | | } |
|---|
| 474 | | |
|---|
| 475 | | |
|---|
| 476 | | RevisedExpression simplifyVectorExpression(char [] expr, int [] rank) |
|---|
| 477 | | { |
|---|
| 478 | | char [] s = exprSimplify(expr, rank, "", ""); |
|---|
| 479 | | if (s.length>1) s = s[1..$-1]; // strip off () |
|---|
| 480 | | char [][] comp; |
|---|
| 481 | | char [] used=""; |
|---|
| 482 | | for (int i=0; i<rank.length; ++i) used~="-"; |
|---|
| 483 | | int [] r; |
|---|
| 484 | | char next = cast(char)('A' + rank.length); |
|---|
| 485 | | char [] e = ""; |
|---|
| 486 | | for (int i=0; i<s.length; ++i) { |
|---|
| 487 | | if (s[i]==' ') { |
|---|
| 488 | | int k; |
|---|
| 489 | | for (k=i+1; s[k]!=' '; ++k) {} |
|---|
| 490 | | comp ~= s[i+2..k-1]; |
|---|
| 491 | | if (s[k-2]==']') { |
|---|
| 492 | | // it's a vector/matrix of some kind, with rank reduced |
|---|
| 493 | | // by indices. Can't just use exprRank, because the [] |
|---|
| 494 | | // aren't wrapped by (). |
|---|
| 495 | | r ~= rank[s[i+2]-'A'] - indexRank(s[i+2..k-1]); |
|---|
| 496 | | } else { |
|---|
| 497 | | // it's a scalar expression. Note that it could involve |
|---|
| 498 | | // a vector expression. |
|---|
| 499 | | r~=0; |
|---|
| 500 | | } |
|---|
| 501 | | e ~= next; |
|---|
| 502 | | ++next; |
|---|
| 503 | | i = k; |
|---|
| 504 | | } else { |
|---|
| 505 | | e ~= s[i]; |
|---|
| 506 | | if (s[i]>='A' && s[i]<='Z') used[s[i]-'A']=s[i]; |
|---|
| 507 | | } |
|---|
| 508 | | } |
|---|
| 509 | | return RevisedExpression(e, comp, r, used); |
|---|
| 510 | | } |
|---|
| 511 | | |
|---|
| 512 | | unittest { |
|---|
| 513 | | |
|---|
| 514 | | assert(exprSimplify("A+=(((D[E])*B)[E])", [1,0,3,3,0,0],"","")=="(A+=(B* {D[E][E]} ))"); |
|---|
| 515 | | assert(exprSimplify("A+=(B*((C[B])[B..E]))", [1,0,3,3,0,0],"","")=="(A+=(B* {C[B][B..E]} ))"); |
|---|
| 516 | | assert(exprSimplify("A*=(B*C)", [1,0,0],"","")== "(A*= {(B*C)} )"); |
|---|
| 517 | | assert(exprSimplify("A=((B*C)-D)", [1,1,0,1],"","")=="(A=((C*B)-D))"); |
|---|
| 518 | | |
|---|
| 519 | | RevisedExpression e = simplifyVectorExpression("A+=(((D[B])*C)[B])", [2,0,0,4]); |
|---|
| 520 | | assert(e.expr == "A+=(C*E)"); |
|---|
| 521 | | assert(e.rank[0]==2); |
|---|
| 522 | | assert(e.compounds[0]=="D[B][B]"); |
|---|
| 523 | | assert(e.used=="A-C-"); |
|---|
| 524 | | assert(removedUnusedSymbolsFromExpression(e)=="A+=(B*C)"); |
|---|
| 525 | | } |
|---|