区间数计算之Python实现
生活随笔
收集整理的這篇文章主要介紹了
区间数计算之Python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先粘代碼,空了再來整說明。
區間數的定義參考:
模糊數學筆記:八、模糊數及其運算性質
# -*- coding: utf-8 -*- """區間數計算.ipynbAutomatically generated by Colaboratory.Original file is located athttps://colab.research.google.com/drive/1VC_Px3l3pLg4f0RWQF3xz2wWdWM1Jynf """class IntervalNumber:def __init__(self,a,b):self.a = aself.b = bdef __str__(self):return '[{0},{1}]'.format(self.a,self.b)def __add__(self,other):return _add(self,other)def __sub__(self,other):return _sub(self,other)def __mul__(self,other):return _mul(self,other)def __truediv__(self,other):return _truediv(self,other)def _add(I,J):result = IntervalNumber(I.a+J.a,I.b+J.b)return resultdef _sub(I,J):result = IntervalNumber(I.a - J.b, I.b-J.a)return resultdef _mul(I,J):result = IntervalNumber(I.a*J.a,I.b*J.b)return resultdef _truediv(I,J):result = IntervalNumber(I.a/J.b, I.b/J.a)return result調用方法和結果測試:
I = IntervalNumber(1,3) J = IntervalNumber(2,5)print('I+J=',I+J) print('I-J=',I-J) print('I*J=',I*J) print('I/J=',I/J)運行結果:
I+J= [3,8] I-J= [-4,1] I*J= [2,15] I/J= [0.2,1.5]總結
以上是生活随笔為你收集整理的区间数计算之Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: autograd库测试笔记-(一个基于N
- 下一篇: 用numpy autograd 实现牛顿