** 연산자에 의존하려면 Steve의 대답이 잘 작동해야합니다. 그러나 해당 연산자가 공식적으로 지원되지 않으며 해당 솔루션이 OP 질문에 직접 응답하지 않기 때문에 여기에 지수 식으로 지수를 확장 할 수있는 함수가 있습니다.
function [ text ] = remove_powers(exp)
%Cleans the powers from T, and return expanded text representation
% Define functions
enclose [email protected](t) ['(' t ')'];
[email protected](b,p) strjoin(strcat(cell(1,p),enclose(char(b))),'*');
[email protected](s) arrayfun(@(k) count(char(s(k)),'^'), 1:length(s));
sym2str = @(s) strrep(char(s), ' ', '');
% Check for fractions
[N,D]=numden(exp);
if ~isequal(D,sym(1))
% pass up the num and den
text = [remove_powers(N) '/' enclose(remove_powers(D))];
else
% Split a into subterms
Ts = children(exp);
% Clean children
has_pow=count_pow(Ts)>0;
text = sym2str(exp);
if sum(count_pow(Ts))<count_pow(exp)
% We have removed a power, clean it, expand it, and pass up
text = expand_pow(remove_powers(Ts(1)),Ts(2));
else
% Just clean the unclean children and pass up
for t=Ts(has_pow)
text = strrep(text,sym2str(t),remove_powers(t));
end
end
end
end
함수 재귀 부모 각 서브 표현식을 청소 (텍스트)를 대체 매트랩 children
함수를 사용한다. 스티브 (Steve)가 cos(x^2+2*y)^2
에 대한 주석에서 언급했듯이이 방법은 구문 분석 문제를 피하기 때문에 regex를 사용하는 것보다 낫습니다.
syms x y real
exp = cos(x^2+2*y)^2;
cleaned_exp = remove_powers(exp)
출력 : 좋은 예있게
(cos(2*y+(x)*(x)))*(cos(2*y+(x)*(x)))
매트랩 구문 분석을하고 있기 때문에, '^'사업자에 대한 우선 순위를 분석 할 필요가 없었다 공지 것을 , 정규 표현식으로 달성하기가 어려울 수 있습니다.
예상대로
syms ddth1t dth1t th1t ddth2t dth2t th2t w11 w12 w13 J11 J31 real
exp = (J31*(2*ddth2t*cos(th1t) - 2*w12*w13 - 4*dth1t*dth2t*sin(th1t) - 4*dth2t*w13*sin(th1t) + dth1t^2*sin(2*th2t)*cos(th1t) - w12^2*sin(2*th2t)*cos(th1t) + w13^2*sin(2*th2t)*cos(th1t) + 2*dth2t*w11*sin(2*th2t) + 2*w12*w13*cos(th2t)^2 + ddth1t*sin(2*th2t)*sin(th1t) + 4*dth1t*dth2t*cos(th2t)^2*sin(th1t) + 2*dth1t*w13*sin(2*th2t)*cos(th1t) + 4*dth2t*w13*cos(th2t)^2*sin(th1t) + w11*w12*sin(2*th2t)*sin(th1t) + 4*dth1t*w12*cos(th1t)^2*cos(th2t)^2 - 2*dth1t*w11*sin(2*th1t)*cos(th2t)^2 - 2*dth2t*w11*sin(2*th2t)*cos(th1t)^2 + 2*w12*w13*cos(th1t)^2*cos(th2t)^2 - w11*w13*sin(2*th1t)*cos(th2t)^2 - 4*dth2t*w12*cos(th1t)*cos(th2t)*sin(th1t)*sin(th2t)))/(2*(J11 + J31));
cleaned_exp = remove_powers(exp);
isequal(sym(cleaned_exp),exp) % This should be 1
count(cleaned_exp,'^') % This should be 0
, 새로운 표현이 원본과 동일하지만 더 '^'문자가 없습니다 :
은 영업 이익의 예제를 테스트합니다.
''^'을 (를) 검색하기 위해 ['regexp'] (http://mathworks.com/help/matlab/ref/regexp.html)를 작성하고 그 앞에있는 식으로 여러 번 사용하십시오. (나는 정규 표현할 수 없기 때문에 거기에서 당신을 도울 수 없다). – Adriaan
@Adriaan이 작동 할 수도 있습니다. 나는 정규 표현식을 가지고 있지 않기 때문에, 변경 내용을 하드 코딩하는 것이 텍스트 편집기일지도 모른다. ... –
'syms th2t;''factor (cos (th2t)^2)' –