Remove Redundant Parentheses from String Expression

String contains a long expression. Part of it is:
'...+(G0*(G1-G2+G3))+((G4*(G5+G6)))-(((G7+G8))*((G9+G10+G11)))+...'
The redundant parentheses could be removed and the expression should look like:
'...+G0*(G1-G2+G3)+G4*(G5+G6)-(G7+G8)*(G9+G10+G11)+...'
Is there a simple Matlab script to do this?

댓글 수: 5

dpb
dpb 2016년 6월 7일
" redundant parentheses must be removed"
Why is this a must?
Where did they come from; perhaps the place to look is in the tool that generated them instead of trying to fixup the result.
I suppose there's a parser somewhere, but it would have to be smart enough to determine which are and which aren't actually redundant which isn't totally trivial exercise in general.
S H
S H 2016년 6월 8일
The mparser link provided by isakson does not work. The link might be temporarily or permanently out of reach.
https://github.com/ewiger/decade/tree/master/lib/mparser
S H
S H 2016년 6월 10일
Thank you. I will look into https://github.com/ link.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2016년 6월 8일

0 개 추천

Recall that Azzi answered this, or something pretty much similar, and you accepted his answer in http://www.mathworks.com/matlabcentral/answers/287777#answer_224489

댓글 수: 4

S H
S H 2016년 6월 8일
The link you referred to is asking different question.
S H
S H 2016년 6월 8일
Image Analyst, you should post as comment instead of answer.
@S H —
Don’t get cheeky.
Revise the documentation for the Symbolic Math Toolbox simplify function.
S H
S H 2016년 6월 10일
편집: S H 2016년 6월 10일
Yes symbolic tool fixes this but it also shuffles all the terms and tries to sort them based on variable names. That is why I want to process this expression as string only.
This question takes finding matching parentheses one step further because repetition must also be accounted.
How to solve this includes the following steps:
1. write an algorithm to find matching parentheses for any existing pair
2. See if there is any pair with more than one repetition and then eliminate all redundants

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

S H
2016년 6월 7일

편집:

S H
2016년 6월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by