summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
diff options
context:
space:
mode:
authordebashisdeb2014-06-20 15:42:42 +0530
committerdebashisdeb2014-06-20 15:42:42 +0530
commit83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (patch)
treef54eab21dd3d725d64a495fcd47c00d37abed004 /Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
parenta78126bbe4443e9526a64df9d8245c4af8843044 (diff)
downloadPython-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.gz
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.tar.bz2
Python-Textbook-Companions-83c1bfceb1b681b4bb7253b47491be2d8b2014a1.zip
removing problem statements
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb')
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb529
1 files changed, 502 insertions, 27 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
index 3ddc5480..34fb61dc 100644
--- a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "ch11"
+ "name": "",
+ "signature": "sha256:bdd5f9e441ccb7e3a87f4d8c5492d0aa1d228caba299fc1b0c667abb12780a1e"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,7 +11,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.1\nAdding an Assignment Operator to the Ratio Class\nHere is a class interface for the Ratio class, showing the default constructor, the copy constructor,\nand the assignment operator:\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n def equals(self):\n return self # retuns calling object.",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " if d==None:\n",
+ " self.num = n.num\n",
+ " self.den = n.den\n",
+ " elif n==None:\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " else:\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " \n",
+ " def equals(self):\n",
+ " return self # retuns calling object."
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -19,7 +36,15 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.2 The Preferred Prototype for an Overloaded Assignment Operator\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n pass\n \n def equals(self):\n pass\n",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " pass\n",
+ " \n",
+ " def equals(self):\n",
+ " pass\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -28,7 +53,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.3 Implementation of the Assignment Operator for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n\nz = Ratio(22,7)\ny = z\nx = z\n\nx = Ratio(22,7)\ny = Ratio(x)\nz = x\nw = x",
+ "input": [
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " if d==None:\n",
+ " self.num = n.num\n",
+ " self.den = n.den\n",
+ " elif n==None:\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " else:\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " \n",
+ "\n",
+ "z = Ratio(22,7)\n",
+ "y = z\n",
+ "x = z\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(x)\n",
+ "z = x\n",
+ "w = x"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -37,7 +85,16 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.4\nmultiplication operator overload\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __mul__(self,y):\n pass\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def __mul__(self,y):\n",
+ " pass\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -46,14 +103,68 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.5 The Ratio Class with Assignment and Multiplication Operators\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\nx = Ratio(22,7)\ny = Ratio(-3,8)\nz = x\nz.print_()\nx = y*z\nx.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(-3,8)\n",
+ "z = x\n",
+ "z.print_()\n",
+ "x = y*z\n",
+ "x.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "22 / 7\n-33 / 28\n"
+ "text": [
+ "22 / 7\n",
+ "-33 / 28\n"
+ ]
}
],
"prompt_number": 5
@@ -61,7 +172,30 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.6 The Ratio Class with an Overloaded *= Operator\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def print_(self):\n print self.num , '/', self.den",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __imul__(self,y):\n",
+ " self.num = self.num * y.num\n",
+ " self.den = self.den * y.den\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -70,7 +204,23 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.7 Overloading the Equality Operator == in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __imul__(self,y):\n",
+ " self.num = self.num * y.num\n",
+ " self.den = self.den * y.den\n",
+ " def __eq__(self,y):\n",
+ " return (x.num * y.den == y.num * x.den)\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -79,20 +229,44 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.8 \nPython does not use << operator for printing. So here we are just declaring function name as print_.\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n\nx = Ratio(22,7)\ny = Ratio(-3,8)\nx.print_() , y.print_()\n",
+ "input": [
+ "\n",
+ "\n",
+ "class Ratio:\n",
+ " def __init__(self,n=None,d=None):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " def __imul__(self,y):\n",
+ " self.num = self.num * y.num\n",
+ " self.den = self.den * y.den\n",
+ " def __eq__(self,y):\n",
+ " return (x.num * y.den == y.num * x.den)\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ "\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(-3,8)\n",
+ "x.print_() , y.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "22 / 7\n-3 / 8\n"
+ "text": [
+ "22 / 7\n",
+ "-3 / 8\n"
+ ]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
- "text": "(None, None)"
+ "text": [
+ "(None, None)"
+ ]
}
],
"prompt_number": 8
@@ -100,7 +274,62 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.9 \nPython does not have >> for input. so we will use input function.\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\n\nx = Ratio()\ny = Ratio()\nx.input()\ny.input()\nx.print_()\ny.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def input(self):\n",
+ " self.num = int(raw_input('Numerator : '))\n",
+ " self.den = int(raw_input('Denominator : '))\n",
+ " self.reduce()\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "\n",
+ "x = Ratio()\n",
+ "y = Ratio()\n",
+ "x.input()\n",
+ "y.input()\n",
+ "x.print_()\n",
+ "y.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -108,30 +337,41 @@
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Numerator : -10\n"
+ "text": [
+ "Numerator : -10\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Denominator : -24\n"
+ "text": [
+ "Denominator : -24\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Numerator : 36\n"
+ "text": [
+ "Numerator : 36\n"
+ ]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
- "text": "Denominator : -20\n"
+ "text": [
+ "Denominator : -20\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "5 / 12\n-9 / 5\n"
+ "text": [
+ "5 / 12\n",
+ "-9 / 5\n"
+ ]
}
],
"prompt_number": 1
@@ -139,14 +379,78 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.10\nAdding a Conversion Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def __float__(self):\n return float(self.num)/self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 15.py\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(-5,8)\nprint \"x = \" , \nx.print_() \nprint \", float(x) = \" , float(x) \nP = Ratio(22,7)\nPI = float(P)\nprint \"P = \" ,\nP.print_() \nprint \", PI = \" , PI\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def input(self):\n",
+ " self.num = int(raw_input('Numerator : '))\n",
+ " self.den = int(raw_input('Denominator : '))\n",
+ " self.reduce()\n",
+ " def __float__(self):\n",
+ " return float(self.num)/self.den\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 15.py\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(-5,8)\n",
+ "print \"x = \" , \n",
+ "x.print_() \n",
+ "print \", float(x) = \" , float(x) \n",
+ "P = Ratio(22,7)\n",
+ "PI = float(P)\n",
+ "print \"P = \" ,\n",
+ "P.print_() \n",
+ "print \", PI = \" , PI\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = -5 / 8\n, float(x) = -0.625\nP = 22 / 7\n, PI = 3.14285714286\n"
+ "text": [
+ "x = -5 / 8\n",
+ ", float(x) = -0.625\n",
+ "P = 22 / 7\n",
+ ", PI = 3.14285714286\n"
+ ]
}
],
"prompt_number": 3
@@ -154,14 +458,71 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.11 Adding a Pre-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nx += 1\ny = x\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def __iadd__(self,n):\n",
+ " self.num += self.den\n",
+ " return self\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "x += 1\n",
+ "y = x\n",
+ "print \"y = \" ,\n",
+ "y.print_()\n",
+ "print \", x = \",\n",
+ "x.print_()\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "y = 29 / 7\n, x = 29 / 7\n"
+ "text": [
+ "y = 29 / 7\n",
+ ", x = 29 / 7\n"
+ ]
}
],
"prompt_number": 5
@@ -169,14 +530,72 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.12 Adding a Post-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\ny = Ratio(x.num,x.den)\nx += 1\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def __iadd__(self,n):\n",
+ " self.num += self.den\n",
+ " return self\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "y = Ratio(x.num,x.den)\n",
+ "x += 1\n",
+ "print \"y = \" ,\n",
+ "y.print_()\n",
+ "print \", x = \",\n",
+ "x.print_()\n",
+ "\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "y = 22 / 7\n, x = 29 / 7\n"
+ "text": [
+ "y = 22 / 7\n",
+ ", x = 29 / 7\n"
+ ]
}
],
"prompt_number": 9
@@ -184,14 +603,70 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "'''\nEXAMPLE 11.13\nAdding a Subscript Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __getitem__(self,k):\n if k == 1:\n return self.num\n else:\n return self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nprint \"x = \" ,\nx.print_()\nprint \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n",
+ "input": [
+ "\n",
+ "def gcd(m,n):\n",
+ " # returns the greatest common divisor of m and n:\n",
+ " if (m<n):\n",
+ " m,n = n,m\n",
+ " while (n>0):\n",
+ " r = m % n\n",
+ " m = n\n",
+ " n = r\n",
+ " return m\n",
+ "class Ratio:\n",
+ " def __init__(self,n=0,d=1):\n",
+ " self.num = n\n",
+ " self.den = d\n",
+ " self.reduce()\n",
+ " def __mul__(self,y):\n",
+ " z = Ratio(self.num * y.num, self.den * y.den)\n",
+ " return z\n",
+ " def print_(self):\n",
+ " print self.num , '/', self.den\n",
+ " \n",
+ " def __getitem__(self,k):\n",
+ " if k == 1:\n",
+ " return self.num\n",
+ " else:\n",
+ " return self.den\n",
+ " def reduce(self):\n",
+ " # enforce invariant(den > 0):\n",
+ " if (self.num == 0 or self.den == 0):\n",
+ " self.num = 0\n",
+ " self.den = 1\n",
+ " return\n",
+ " if (self.den < 0):\n",
+ " self.den *= -1\n",
+ " self.num *= -1\n",
+ " # enforce invariant(gcd(num,den) == 1):\n",
+ " if (self.den == 1):\n",
+ " return\n",
+ " # it's already reduced\n",
+ " sgn = 0\n",
+ " if self.num < 0:\n",
+ " sgn = -1\n",
+ " else:\n",
+ " sgn = 1\n",
+ " g = gcd(sgn*self.num,self.den)\n",
+ " self.num /= g\n",
+ " self.den /= g\n",
+ "\n",
+ "x = Ratio(22,7)\n",
+ "print \"x = \" ,\n",
+ "x.print_()\n",
+ "print \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x = 22 / 7\nx[1] = 22 , x[2] = 7\n"
+ "text": [
+ "x = 22 / 7\n",
+ "x[1] = 22 , x[2] = 7\n"
+ ]
}
],
"prompt_number": 7
@@ -199,7 +674,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []