Function rationalize #

将可理化表达式转换为有理分数。如果是有理分数,则将分子和分母转换为规范形式,按指数递减排序,并返回分子系数。

Syntax #

math.rationalize(expr)
math.rationalize(expr, detailed)
math.rationalize(expr, scope)
math.rationalize(expr, scope, detailed)

Parameters #

Parameter Type Description
expr Node | string 用于检查表达式是否为多项式的表达式
可选 Object | boolean 表达式的作用域,或对于已在输入中评估过的有理表达式为 true
detailed 布尔值 (Boolean) 可选,如果返回一个对象则为 True,如果返回表达式节点则为 False (默认)

Returns #

Type Description
Object | Node expr 的有理多项式,或者一个对象 {expression, numerator, denominator, variables, coefficients},其中 expression 是一个 Node,表示简化后的表达式节点;numerator 是一个 Node,表示简化后的分子;denominator 是一个 Nodeboolean,表示简化后的分母,如果不存在分母则为 falsevariables 是一个包含变量名的数组;coefficients 是一个按指数递增排序的分子系数数组 {Expression Node} 简化后的表达式节点

Throws #

Type | Description —- | ———–

Examples #

math.rationalize('sin(x)+y')
              //  Error: There is an unsolved function call
math.rationalize('2x/y - y/(x+1)')
              // (2*x^2-y^2+2*x)/(x*y+y)
math.rationalize('(2x+1)^6')
              // 64*x^6+192*x^5+240*x^4+160*x^3+60*x^2+12*x+1
math.rationalize('2x/( (2x-1) / (3x+2) ) - 5x/ ( (3x+4) / (2x^2-5) ) + 3')
              // -20*x^4+28*x^3+104*x^2+6*x-12)/(6*x^2+5*x-4)
math.rationalize('x/(1-x)/(x-2)/(x-3)/(x-4) + 2x/ ( (1-2x)/(2-3x) )/ ((3-4x)/(4-5x) )') =
              // (-30*x^7+344*x^6-1506*x^5+3200*x^4-3472*x^3+1846*x^2-381*x)/
              //     (-8*x^6+90*x^5-383*x^4+780*x^3-797*x^2+390*x-72)

math.rationalize('x+x+x+y',{y:1}) // 3*x+1
math.rationalize('x+x+x+y',{})    // 3*x+y

const ret = math.rationalize('x+x+x+y',{},true)
              // ret.expression=3*x+y, ret.variables = ["x","y"]
const ret = math.rationalize('-2+5x^2',{},true)
              // ret.expression=5*x^2-2, ret.variables = ["x"], ret.coefficients=[-2,0,5]

另请参阅 #

simplify

Fork me on GitHub