필터 지우기
필터 지우기

How can I order the factors when using the sym and latex functions?

조회 수: 1 (최근 30일)
MortenJ
MortenJ 2016년 12월 26일
댓글: MortenJ 2016년 12월 30일
Hi,
is it possible to control the order of the factors when using the sym and latex-functions?
E.g.
str = (5/8)*x;
out = latex(sym(str));
% produces out='\frac{5\, x}{8}'
% corresponding to (5*x)/8 and not (5/8)*x (='\frac{5}{8}x') as desired
I know these are mathematically identical, but I use the output for educating students and for them the second expression is easier to understand. I have thousands of expressions so need to find a way to do this automatically.
Thank you in advance, Morten

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 29일
No, the Symbolic Toolbox does not offer an way of giving preferences on how the terms are to be formatted or ordered.
In this particular case the work-around would be
out = [latex(coeffs(str,x)),latex(x)]
coeffs() with two outputs is generally useful for formatting terms of a polynomial. For non-polynomial expressions, you might need to start using children() .
Unfortunately, in order to find out the basic operation that you are getting the children of, you need to pop into the symbolic engine:
feval(symengine, 'op', str, 0)
which in this case would get you sym('_mult'); you might also see _power or _plus or _not or _less or _leequal or _SYM_equal or _or

추가 답변 (1개)

Ankitha Kollegal Arjun
Ankitha Kollegal Arjun 2016년 12월 29일
Morten,
You could try the following:
1. Factorize the required symbolic expression using factor(). (result will be a symbolic vector)
2. Use the latex() function on each element of the above symbolic vector and concatenate the results to obtain the desired latex format.
>> syms x
>> str = (5/8)*x;
>> temp = factor(str);
>> out = [latex(temp(1)) latex(temp(2))]
out =
\frac{5}{8}x
  댓글 수: 1
MortenJ
MortenJ 2016년 12월 30일
Works great, thanks. I realized I also had to deal with a lot of other equation-formats, so picked the suggestion by Walter below, which was able to deal with those as well, but thanks for helping out.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by