Sympy-分数操作 [英] Sympy - fraction manipulation

查看:944
本文介绍了Sympy-分数操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上希望Sympy生成乳胶代码\ frac {x-1} {3} = y,但是每当我问它生成东西的Tex组件时,Sympy总是返回\ frac {x} {3}-\ frac {1 } {3}.

I basically want Sympy to generate latex code \frac{x-1}{3} = y but whenever I ask it generate the Tex component of things Sympy always returns \frac{x}{3} - \frac{1}{3} .

如何避免拆分等式,并为另一个变量分配一个equals运算符.

How do I avoid splitting up the equations, and assign an equals operator to another variable.

我还没有尝试在代码中添加"y ="部分,因为我想先阐明小数的情况,但是由于我不得不加紧手来进行堆栈交换,所以我想我会问两个问题.我遍历了所有可以找到的教程页面,但无济于事.

I have not attempted to add the "y =" part to the code yet as I wanted to clarify the fraction situation first, but since I have had to come cap in hand to stack exchange I thought I would ask both questions. I have been through every tutorial page I could find but to no avail.

尽管我会要求您保持相对简单,但任何帮助将不胜感激!!!

Any help would be much appreciated although I would ask you keep it relatively simple !!!

谢谢.

import sympy
from sympy import *
x = Symbol("x")
a = (x-Integer(1))
b = (3)
c = a/b
print(latex(c))

推荐答案

问题来自sympy自动将(x-1)/3扩展为x/3-1/3.因此,一种解决方案是让sympy将此因素分解:

The issue comes from sympy automatically expanding (x-1)/3 into x/3-1/3. So one solution is to ask sympy to factor this back:

In [18]: c = a/b; c
Out[18]: x/3 - 1/3

In [19]: d = c.factor(); d
Out[19]: (x - 1)/3

In [20]: print(latex(d))
\frac{1}{3} \left(x - 1\right)

这篇关于Sympy-分数操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆