高斯消元法求解线性方程组(分数精确表示)

原理可以参考

https://zh.wikipedia.org/wiki/%E9%AB%98%E6%96%AF%E6%B6%88%E5%8E%BB%E6%B3%95

这里给出使用分数表示计算过程的高斯消元法写法

github地址:https://github.com/YIWANFENG/Algorithm-github

#!/usr/bin/env python3.5
# -*-coding: utf-8 -*-
"""
Created on 2018-6-9
@author: YWF

高斯消元法求解线性方程,
数字采用分数精确表示,无近似

"""
import numpy as np

def gcd(a,b):
    if a < b:
        c = a
        a = b
        b = c
    while a % b != 0:
        c = a % b
        a = b
        b = c
    return b

def lcm(a,b):
    return a * b / gcd(a,b)


class Fractional:

    def __init__(self,num,den):
        self.numerator = num
        if(den == 0):
            print("Error den")
        self.denominator = den
        pass
    def Simplify(self):
        if self.numerator == 0:
            return Fractional(0,1)
        gcd_ = gcd(self.denominator,self.numerator)
        if gcd_ != 1:
            self.denominator /= gcd_
            self.numerator /= gcd_
        return self
    def __sub__(self, other):
        lcm_ = lcm(self.denominator,other.denominator)
        self.numerator *= lcm_ / self.denominator
        num = self.numerator - other.numerator * lcm_ / other.denominator
        return Fractional(num,lcm_).Simplify()
    def __add__(self, other):
        lcm_ = 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值