简体   繁体   English

如何将分数的立方根转换为分母中没有立方根的分数?

[英]How to transform a cube root of a fraction into an fraction without cube root in the denominator?

How do I write a python def function that can take the cube roots of fractions and return the answer as a fraction as a coefficient with a cube root in the numerator only?如何编写一个 python def 函数,它可以取分数的立方根并将答案作为分数作为系数返回,分子中只有立方根? I am new to coding, so I apologize if my question sounds vague.我是编码新手,所以如果我的问题听起来含糊不清,我深表歉意。 I am trying to write a program that can solve any cubic equation and return that answer in its exact form.我正在尝试编写一个可以解决任何三次方程并以确切形式返回该答案的程序。 Currently, I have a program that can do this, but it only gives the roots as a decimal.目前,我有一个程序可以做到这一点,但它只给出十进制的根。

For example, I want the answers of:例如,我想要以下答案:

This ∛10 / ∛56 = ∛490/14 Not ∛10 / ∛56 = 0.56312394∛10 / ∛56 = ∛​​490/14不是∛10 / ∛56 = 0.56312394

This ∛1 / ∛27 = 1/3 Not ∛1 / ∛27 = 0.333333333∛1 / ∛27 = 1/3不是∛1 / ∛27 = 0.333333333

This ∛48 / ∛9 = 6∛18/9 Not ∛48 / ∛9 = 1.74716092∛48 / ∛9 = 6∛18/9不是∛48 / ∛9 = 1.74716092

Basically, I just need -cubic_root(c) to be return as a fraction instead of a decimal基本上,我只需要 -cubic_root(c) 作为分数而不是小数返回

def cubic_root(x): 
        '''Compute cubic root of a number while maintaining its sign'''
        if x >= 0:
            return x**third #x**third means x^3
        else:
            return -(-x)**third

    if f == g == h == 0:
        x1 = -cubic_root(c) # I need this in fraction form

This is the entire code这是整个代码

'''
Cubic Equation Calculator (Exact Form)
By Patrick Thomas
'''

import time
import math
import fractions
goAgain = True

time.sleep(0.5)
print('')
print('Cubic Equation Calculator (Exact Form)')
time.sleep(0.5)
print('A Patman Inc. Program')

#While Loop---------------------------------------------------------------------
while goAgain == True:
    time.sleep(0.5)
    print('--------------------------')
    print('ax³ + bx² + cx + d = 0')
    print('--------------------------')
    
    a = int(input('a = '))
    
    b = int(input('b = '))
    
    c = int(input('c = '))
    
    d = int(input('d = '))
    
    print('')
    
#Math---------------------------------------------------------------------------
    a0 = b/a
    b0 = c/a
    c0 = d/a
    #a0 Fraction--------------------------------------------------------------------
    def a0(b,a):
        if int(a) == 0:
            return int(b)
        return a0(int(a), int(b) % int(a))
    a0_GCD = a0(b,a)
    b_Simp = int(b) / a0_GCD
    a_Simp = int(a) / a0_GCD
    a0_frac = fractions.Fraction(int(b_Simp),int(a_Simp))
    print('a0 = {}'.format(a0_frac)) #remove when finished
    
    #b0 Fraction--------------------------------------------------------------------
    def b0(c,a):
        if int(a) == 0:
            return int(c)
        return b0(int(a), int(c) % int(a))
    b0_GCD = b0(c,a)
    c_Simp = int(c) / b0_GCD
    a_Simp = int(a) / b0_GCD
    b0_frac = fractions.Fraction(int(c_Simp),int(a_Simp))
    print('b0 = {}'.format(b0_frac)) #remove when finished
    
    #c0 Fraction--------------------------------------------------------------------
    def c0(d,a):
        if int(a) == 0:
            return int(d)
        return b0(int(a), int(d) % int(a))
    c0_GCD = c0(d,a)
    d_Simp = int(d) / b0_GCD
    a_Simp = int(a) / b0_GCD
    c0_frac = fractions.Fraction(int(d_Simp),int(a_Simp))
    print('c0 = {}'.format(c0_frac)) #remove when finished
    
    a0 = b/a
    b0 = c/a
    c0 = d/a
    
    a = a0
    b = b0
    c = c0
    
    # Some repeating constants and variables
    third = 1./3. #Cube root
    a13 = a*third #a is divided by 3
    a13_num = int(b_Simp) * 1 
    a13_den = int(a_Simp) * 3
    a13_frac = fractions.Fraction(a13_num,a13_den)
    print('') #remove when finished
    print('a13 (decimal) = {}'.format(a13)) #remove when finished
    print('a13 (fraction) = {}'.format(a13_frac)) #remove when finished
    
    a2 = a13*a13 #quotient of a*third times quotient of a*third
    a2_num = a13_num * a13_num
    a2_den = a13_den * a13_den
    a2_frac = fractions.Fraction(int(a2_num),int(a2_den))
    print('') #remove when finished
    print('a2 (decimal) = {}'.format(a2)) #remove when finished
    print('a2 (fraction) = {}'.format(a2_frac)) #remove when finished
    sqr3 = math.sqrt(3)
    
    # Additional intermediate variables
    
    #f--------------------------------------------------------------------------
    f = third * b - a2
    thirdb_num = 1 * int(c_Simp) 
    thirdb_den = 3 * int(a_Simp)
    thirdb = fractions.Fraction(thirdb_num,thirdb_den)
    f_frac = thirdb - a2_frac
    
    print('') #remove when finished
    print('f (decimal) = {}'.format(f)) #remove when finished
    print('f (fraction) = {}'.format(f_frac)) #remove when finished
    
    #g--------------------------------------------------------------------------
    g = a13 * (2*a2 - b) + c
    paren = 2*a2 - b
    two_a2_num = 2 * a2_num
    two_a2_den = 1 * a2_den
    two_a2_frac = fractions.Fraction(int(two_a2_num),int(two_a2_den))
    parenthesis = two_a2_frac - b0_frac
    g_frac = a13_frac * parenthesis + c0_frac
    
    print('') #remove when finished
    print('g (decimal) = {}'.format(g)) #remove when finished
    print('g (fraction) = {}'.format(g_frac)) #remove when finished
    
    #h--------------------------------------------------------------------------
    h = 0.25*g*g + f*f*f
    one_forth = fractions.Fraction(int(1),int(4))
    h_frac = one_forth * g_frac * g_frac + f_frac * f_frac * f_frac
    print('') #remove when finished
    print('h (decimal) = {}'.format(h)) #remove when finished
    print('h (fraction) = {}'.format(h_frac)) #remove when finished
    
    #cubic_root(x)--------------------------------------------------------------
    def cubic_root(x): 
        '''Compute cubic root of a number while maintaining its sign'''
        if x >= 0:
            return x**third #x**third means x^3
        else:
            return -(-x)**third

    if f == g == h == 0:
        x1 = -cubic_root(c) # I need this in fraction form
        print('x₁ = {:.5f}'.format(x1))
        print('x₂ = {:.5f}'.format(x1))
        print('x₃ = {:.5f}'.format(x1))

    elif h <= 0:
        j = math.sqrt(-f)
        k = math.acos(-0.5*g / (j*j*j))
        m = math.cos(third*k)
        n = sqr3 * math.sin(third*k)
        x1 = 2*j*m - a13
        x2 = -j * (m + n) - a13
        x3 = -j * (m - n) - a13
        print('x₁ = {:.5f}'.format(x1))
        print('x₂ = {:.5f}'.format(x2))
        print('x₃ = {:.5f}'.format(x3))

    else:
        sqrt_h = math.sqrt(h)
        S = cubic_root(-0.5*g + sqrt_h)
        U = cubic_root(-0.5*g - sqrt_h)
        S_plus_U = S + U
        S_minus_U = S - U
        x1 = S_plus_U - a13
        x2 = -0.5*S_plus_U - a13 + S_minus_U*sqr3*0.5j
        x3 = -0.5*S_plus_U - a13 - S_minus_U*sqr3*0.5j
        
    #x1 Section-----------------------------------------------------------------
        print('') #remove when finished
        print('x₁ = {:.5f}'.format(float(x1)))
        
    #x2 Section-----------------------------------------------------------------
    x2_str = str(x2)
    
    if '+' in x2_str:
        x2_part1, x2_part2 = x2_str.split('+')
        x2_part1 = x2_part1.replace('(','')
        x2_part2 = x2_part2.replace('j)','')
        x2_part1_num = float(x2_part1)
        x2_part2_num = float(x2_part2)
        if float(x2_part2_num) == 0:
            print('x₂ = {:.5f}'.format(x2_part1_num))
        else:
            print('x₂ = {:.5f} + {:.5f}i'.format(x2_part1_num,x2_part2_num))
        
    elif '-' in x2_str:
        if '(-' in x2_str:
            x2_str = x2_str.replace('(-','')
            x2_part1, x2_part2 = x2_str.split('-')
            x2_part1 = x2_part1.replace('','-',1)
            x2_part1 = x2_part1.replace('(','')
            x2_part2 = x2_part2.replace('j)','')
            x2_part1_num = float(x2_part1)
            x2_part2_num = float(x2_part2)
            if float(x2_part2_num) == 0:
                print('x₂ = {:.5f}'.format(x2_part1_num))
            else:
                print('x₂ = {:.5f} - {:.5f}i'.format(x2_part1_num,x2_part2_num))
        else:
            x2_part1, x2_part2 = x2_str.split('-')
            x2_part1 = x2_part1.replace('(','')
            x2_part2 = x2_part2.replace('j)','')
            x2_part1_num = float(x2_part1)
            x2_part2_num = float(x2_part2)
            if float(x2_part2_num) == 0:
                 print('x₂ = {:.5f}'.format(x2_part1_num))
            else:
                print('x₂ = {:.5f} - {:.5f}i'.format(x2_part1_num,x2_part2_num))
            
    #x3 Section-----------------------------------------------------------------
    x3_str = str(x3)
    
    if '+' in x3_str:
        x3_part1, x3_part2 = x3_str.split('+')
        x3_part1 = x3_part1.replace('(','')
        x3_part2 = x3_part2.replace('j)','')
        x3_part1_num = float(x3_part1)
        x3_part2_num = float(x3_part2)
        if float(x3_part2_num) == 0:
            print('x₃ = {:.5f}'.format(x3_part1_num))
        else:
            print('x₃ = {:.5f} + {:.5f}i'.format(x3_part1_num,x3_part2_num))
        
    elif '-' in x3_str:
        if '(-' in x3_str:
            x3_str = x3_str.replace('(-','')
            x3_part1, x3_part2 = x3_str.split('-')
            x3_part1 = x3_part1.replace('','-',1)
            x3_part1 = x3_part1.replace('(','')
            x3_part2 = x3_part2.replace('j)','')
            x3_part1_num = float(x3_part1)
            x3_part2_num = float(x3_part2)
            if float(x3_part2_num) == 0:
                print('x₃ = {:.5f}'.format(x3_part1_num))
            else:
                print('x₃ = {:.5f} - {:.5f}i'.format(x3_part1_num,x3_part2_num))
        else:
            x3_part1, x3_part2 = x3_str.split('-')
            x3_part1 = x3_part1.replace('(','')
            x3_part2 = x3_part2.replace('j)','')
            x3_part1_num = float(x3_part1)
            x3_part2_num = float(x3_part2)
            if float(x3_part2_num) == 0:
                print('x₃ = {:.5f}'.format(x3_part1_num))
            else:
                print('x₃ = {:.5f} - {:.5f}i'.format(x3_part1_num,x3_part2_num))
        

You really need to be more clear about what you want to achieve but here is an answer based on what I could understand from your question:您确实需要更清楚地了解您想要实现的目标,但这是基于我从您的问题中可以理解的答案:

You need to look into the Fractions module.您需要查看分数模块。 Here is script that does what you explained in your second example.这是执行您在第二个示例中解释的脚本。

from fractions import Fraction

def cuberoot(number: float) -> float:
    if number < 0:
        number = abs(number)
        cube_root = number**(1/3)*(-1)
    else:
        cube_root = number**(1/3)
    return cube_root

def cube_root_fraction(initial_fraction: Fraction) -> Fraction:
    numerator = int(cuberoot(initial_fraction.numerator))
    denominator =  int(cuberoot(initial_fraction.denominator))
    return Fraction(numerator, denominator)
 
initial_fraction = Fraction(1, 27)
result = cube_root_fraction(initial_fraction)
print(result)

Input:输入:

Fraction(1, 27)

Output:输出:

1/3

To get rid of the cube root in the denominator, we can multiply both numerator and denominator with the square of that cube root.为了消除分母中的立方根,我们可以将分子和分母都与立方根的平方相乘。 For example:例如:

∛48 / ∛9 = ∛48∛9² / ∛9∛9² = ∛(48⋅9²) / 9 ∛48 / ∛9 = ∛48∛9² / ∛9∛9² = ∛(48⋅9²) / 9

Then a factor should be extracted from the cube root that is a cube.然后应该从立方体根中提取一个因子。 For that purpose we could find all factors and then identify those that have a power of 3 or more.为此,我们可以找到所有因素,然后确定那些具有 3 或更多幂的因素。 The powers of 3 can be extracted out of the cube root.可以从立方根中提取 3 的幂。 We can either code this ourselves, or make use of sympy.ntheory.factor_.core with t=3 , which extracts the cube-free factor, so that we can derive what the cube factor is.我们可以自己编写代码,也可以使用t=3sympy.ntheory.factor_.core提取无立方体因子,这样我们就可以推导出立方体因子是什么。

In the example, we can write the fraction as:在示例中,我们可以将分数写为:

∛(48⋅9²) / 9 = ∛(6³18) / 9 = 6∛18 / 9 ∛(48⋅9²) / 9 = ∛(6³18) / 9 = 6∛18 / 9

The core function will give us the 18 from 48⋅9², and from the remaining factor 6³ (which always is a perfect cube) we can derive 6. core函数将给我们 48⋅9² 中的 18,从剩余的因子 6³(它总是一个完美的立方体)我们可以推导出 6。

And then finally we can divide both the coefficient part of the numerator and the denominator by their greatest common divisor (which Fraction will do for us), and so we get:最后我们可以将分子和分母的系数部分除以它们的最大公约数( Fraction将为我们做),所以我们得到:

6∛18 / 9 = 2∛18 / 3 6∛18 / 9 = 2∛18 / 3

from fractions import Fraction
from sympy.ntheory.factor_ import core  # for getting the cube free factor
from sympy import integer_nthroot  # for applying the integer cube-root

def rationalize(root3numerator, root3denominator):
    root3numerator *= root3denominator * root3denominator
    cubefree = core(root3numerator, t=3)
    cube,_ = integer_nthroot(root3numerator // cubefree, 3)
    return (Fraction(cube, root3denominator), cubefree)

# Helper function for formatting output
def format(fraction, root3):
    strdenominator = "" if fraction.denominator == 1 else f"/{fraction.denominator}"
    strroot3 = "" if root3 == 1 else f"∛{root3}"
    strcoefficient = "" if fraction.numerator == 1 and strroot3 else str(fraction.numerator)
    return f"{(fraction, root3)}: {strcoefficient}{strroot3}{strdenominator}"

# Example runs
for num, denom in [(10, 56), (1, 27), (48, 9), (20, 2)]:
    print((num, denom), "=>", format(*rationalize(num, denom)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM