Simplify some generalized math formulas:
Now the 'sin^2+cos^2=1' and the 'sinh^2-cosh^2=-1' code can handle, for example: sin^4+cos^4=1, sinh^10-cosh^10=-1. Also, the arguments can be also multitokens, so that it's possible to simplify, for example: 'sin^2(k())+cos^2(k())=1'.
This commit is contained in:
parent
5485e6866f
commit
1c0c0471df
|
@ -9350,14 +9350,37 @@ void Tokenizer::simplifyMathExpressions()
|
||||||
tok->str("0");
|
tok->str("0");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok,"pow ( sin ( %any% ) , 2 ) + pow ( cos ( %any% ) , 2 )") && tok->strAt(4) == tok->strAt(14)) {
|
//Pythagorean trigonometric identity: pow(sin(x),2n)+pow(cos(x),2n) = 1
|
||||||
tok->deleteNext(18);
|
//Hyperbolic identity: pow(sinh(x),2n)-pow(cosh(x),2n) = -1
|
||||||
tok->str("1");
|
//2n = any number which is multiple of 2
|
||||||
}
|
if (Token::simpleMatch(tok, "pow (")) {
|
||||||
|
if (Token::simpleMatch(tok->tokAt(2), "sin (")) {
|
||||||
if (Token::Match(tok,"pow ( sinh ( %any% ) , 2 ) - pow ( cosh ( %any% ) , 2 )") && tok->strAt(4) == tok->strAt(14)) {
|
Token *tok2 = tok->linkAt(3);
|
||||||
tok->deleteNext(18);
|
if (!Token::Match(tok2, ") , %num% ) + pow ( cos (") ||
|
||||||
tok->str("-1");
|
MathLib::toLongNumber(tok2->strAt(2)) % 2 != 0)
|
||||||
|
continue;
|
||||||
|
Token *tok3 = tok2->tokAt(8);
|
||||||
|
if (!Token::Match(tok3->link(), ") , %num% )") ||
|
||||||
|
tok3->link()->strAt(2) != tok2->strAt(2))
|
||||||
|
continue;
|
||||||
|
if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok3->link()->next())) {
|
||||||
|
Token::eraseTokens(tok, tok3->link()->tokAt(4));
|
||||||
|
tok->str("1");
|
||||||
|
}
|
||||||
|
} else if (Token::simpleMatch(tok->tokAt(2), "sinh (")) {
|
||||||
|
Token *tok2 = tok->linkAt(3);
|
||||||
|
if (!Token::Match(tok2, ") , %num% ) - pow ( cosh (") ||
|
||||||
|
MathLib::toLongNumber(tok2->strAt(2)) % 2 != 0)
|
||||||
|
continue;
|
||||||
|
Token *tok3 = tok2->tokAt(8);
|
||||||
|
if (!Token::Match(tok3->link(), ") , %num% )") ||
|
||||||
|
tok3->link()->strAt(2) != tok2->strAt(2))
|
||||||
|
continue;
|
||||||
|
if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok3->link()->next())) {
|
||||||
|
Token::eraseTokens(tok, tok3->link()->tokAt(4));
|
||||||
|
tok->str("-1");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7725,6 +7725,10 @@ private:
|
||||||
" std::cout<<ln(1);\n"
|
" std::cout<<ln(1);\n"
|
||||||
" std::cout<<pow(sin(x),2)+pow(cos(x),2);\n"
|
" std::cout<<pow(sin(x),2)+pow(cos(x),2);\n"
|
||||||
" std::cout<<pow(sinh(x),2)-pow(cosh(x),2);\n"
|
" std::cout<<pow(sinh(x),2)-pow(cosh(x),2);\n"
|
||||||
|
" std::cout<<pow(sin(x*y+z),2)+pow(cos(x*y+z),2);\n"
|
||||||
|
" std::cout<<pow(sinh(x*y+z),2)-pow(cosh(x*y+z),2);\n"
|
||||||
|
" std::cout<<pow(sin(x),4)+pow(cos(x),4);\n"
|
||||||
|
" std::cout<<pow(sinh(x),10)-pow(cosh(x),10);\n"
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
const char expected1[] = "void foo ( ) {\n"
|
const char expected1[] = "void foo ( ) {\n"
|
||||||
|
@ -7738,6 +7742,10 @@ private:
|
||||||
"std :: cout << 0 ;\n"
|
"std :: cout << 0 ;\n"
|
||||||
"std :: cout << 1 ;\n"
|
"std :: cout << 1 ;\n"
|
||||||
"std :: cout << -1 ;\n"
|
"std :: cout << -1 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << -1 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << -1 ;\n"
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));
|
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue