summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch11.ipynb
blob: 3ddc548005f80dace204e92e5bad12c8a04b8ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
 "metadata": {
  "name": "ch11"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "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.",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "22 / 7\n-33 / 28\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "22 / 7\n-3 / 8\n"
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": "(None, None)"
      }
     ],
     "prompt_number": 8
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "Numerator : -10\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "Denominator : -24\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "Numerator : 36\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "Denominator : -20\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "5 / 12\n-9 / 5\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "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",
     "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"
      }
     ],
     "prompt_number": 3
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "y =  29 / 7\n, x =  29 / 7\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "y =  22 / 7\n, x =  29 / 7\n"
      }
     ],
     "prompt_number": 9
    },
    {
     "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",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x =  22 / 7\nx[1] =  22 , x[2] =  7\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}