summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch10.ipynb
blob: 4b6ac8a8dac59ccb90c20309d7231b4965d3ca54 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
{
 "metadata": {
  "name": "ch10"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.1 Implementing the Ratio Class\n'''\nclass Ratio:\n    def __init__(self):\n        self.num = 0\n        self.den = 0\n    def assign(self,n,d):\n        self.num = n\n        self.den = d\n    def convert(self):\n        return float(self.num)/self.den\n    def invert(self):\n        self.num,self.den = self.den,self.num\n    def print_(self):\n        print self.num , '/' , self.den ,\n    \nx = Ratio()\nx.assign(22,7)\nprint \"x = \",\nx.print_()\nprint \" = \" , x.convert() \nx.invert()\nprint  \"1/x = \",\nx.print_()\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x =  22 / 7  =  3.14285714286\n1/x =  7 / 22\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.2 Class example\n'''\nclass Ratio:\n    def __init__(self):\n        self.num = 0\n        self.den = 0\n    def assign(self,n,d):\n        self.num = n\n        self.den = d\n    def convert(self):\n        return float(self.num)/self.den\n    def invert(self):\n        self.num,self.den = self.den,self.num\n    def print_(self):\n        print self.num , '/' , self.den ,\n",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.3\nA Constructor Function for the Ratio Class\n'''\nclass Ratio:\n    def __init__(self,n,d):\n        self.num = n\n        self.den = d\n    def print_(self):\n        print self.num , '/' , self.den ,\n\nx = Ratio(-1,3)\ny = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \" and y = \",\ny.print_()\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x =  -1 / 3  and y =  22 / 7\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.4 Constructors to the Ratio Class\n'''\nclass Ratio:\n    def __init__(self,n=None,d=None):\n        if n==None:\n            self.num = 0\n            self.den = 1\n        elif d==None:\n            self.num = n\n            self.den = 1\n        else:\n            self.num = n\n            self.den = d\n    def print_(self):\n        print self.num , '/' , self.den ,\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \"\\ny = \",\ny.print_()\nprint \"\\nz = \",\nz.print_()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x =  0 / 1 \ny =  4 / 1 \nz =  22 / 7\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.5\n'''\nclass Ratio:\n    def __init__(self,n=None,d=None):\n        if n==None:\n            self.num = 0\n            self.den = 1\n        elif d==None:\n            self.num = n\n            self.den = 1\n        else:\n            self.num = n\n            self.den = d\n    def print_(self):\n        print self.num , '/' , self.den ,\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.6 Using Default Parameter Values in the Ratio Class Constructor\n'''\n\nclass Ratio:\n    def __init__(self,n=0,d=1):\n        self.num = n\n        self.den = d\n    def print_(self):\n        print self.num , '/' , self.den ,\n\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.7 Access Functions in the Ratio Class\n'''\n\nclass Ratio:\n    def __init__(self,n=0,d=1):\n        self.num = n\n        self.den = d\n    def numerator(self):\n        return self.num\n    def denominator(self):\n        return self.den\n    def print_(self):\n        print self.num , '/' , self.den ,\n\n\nx = Ratio(22,7)\nprint x.numerator() , '/' , x.denominator() ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "22 / 7\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.8\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\nclass Ratio:\n    def __init__(self,n=0,d=1):\n        self.num = n\n        self.den = d\n        self.reduce()\n    def numerator(self):\n        return self.num\n    def denominator(self):\n        return self.den\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\nx = Ratio(100,-360)\nx.print_()\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "-5 / 18\n"
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.9 Adding a Copy Constructor 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\n\nclass Ratio:\n    def __init__(self,n=0,d=None):\n        if d == None:\n            self.num = n.num\n            self.den = n.den\n        else:   \n            self.num = n\n            self.den = d\n        self.reduce()\n    def numerator(self):\n        return self.num\n    def denominator(self):\n        return self.den\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\nx = Ratio(100,360)\ny = Ratio(x)\nprint \"x = \",\nx.print_()\nprint \"y = \",\ny.print_()\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "x =  5 / 18 y =  5 / 18\n"
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.10 Tracing Calls to the Copy Constructor\n'''\n'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\n'''\nEXAMPLE 10.8\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\nclass Ratio:\n    def __init__(self,n=0,d=None):\n        if d == None:\n            print \"COPY CONSTRUCTOR CALLED\"\n            self.num = n.num\n            self.den = n.den\n        else:   \n            self.num = n\n            self.den = d\n        self.reduce()\n    def numerator(self):\n        return self.num\n    def denominator(self):\n        return self.den\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\ndef f(r):\n    s = Ratio(r)\n\nx = Ratio(22,7)\ny = Ratio(x) #calls the copy constructor, copying x to y\nf(y)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "COPY CONSTRUCTOR CALLED\nCOPY CONSTRUCTOR CALLED\n"
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.11 Including a Destructor in the Ratio Class\nNote : Python objects die when program gets exit.\n'''\n\nclass Ratio:\n    def __init__(self):\n        print \"OBJECT IS BORN.\"\n    def __del__(self):\n        print \"OBJECT DIES.\"\n\nx =  Ratio()\nprint \"Now x is alive.\"\nprint \"Now between blocks.\"\ny = Ratio()\nprint \"Now y is alive.\"\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "OBJECT IS BORN.\nNow x is alive.\nNow between blocks.\nOBJECT IS BORN.\nNow y is alive.\n"
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.12\n'''\nclass X:\n    def __init(self):\n        data = 0\n\np = X()\np.data = 22\nprint \"p.data = \" , p.data , \" = \" , p.data\np.data = 44\nprint \" p.data = \" , p.data , \" = \" , p.data ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "p.data =  22  =  22\n p.data =  44  =  44\n"
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.13 A Node Class for Linked Lists\n'''\nclass Node:\n    def __init__(self,d,q=None):\n        self.data = d\n        self.next = q\n\nn = int(raw_input())\nq = Node(n)\nwhile True:\n    n = int(raw_input())\n    if n<=0:\n        break\n    p = Node(n, q)\n    q = p\nk = p\nwhile k != None:\n    print k.data , '->' , \n    k = k.next\nprint '*'\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "22\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "33\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "44\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "55\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "66\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "77\n"
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": "0\n"
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n"
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.14 \nPython does not support static data type.\nPython automatically handles local variable so we need not to delete it.\n'''\ncount = 0\nclass Widget:\n    def __init__(self):\n        global count\n        count += 1\n        \nw = Widget()\nx = Widget()\nprint  \"Now there are \" , count , 'widgets'\nif True:\n    w = Widget()\n    x = Widget()\n    y = Widget()\n    z = Widget()\n    print \"Now there are\" , count , 'widgets' \nprint \"Now there are \" , count , 'widgets'\ny = Widget()\nprint \"Now there are \" , count , 'widgets'\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "OBJECT DIES.\nNow there are  2 widgets\nOBJECT DIES.\nNow there are 6 widgets\nNow there are  6 widgets\nNow there are  7 widgets\n"
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.15 \n'''\ncount = 0\nclass Widget:\n    def __init__(self):\n        global count\n        count += 1\n    def numWidgets(self):\n        global count\n        return count\n        \nw = Widget()\nx = Widget()\nprint  \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n    w = Widget()\n    x = Widget()\n    y = Widget()\n    z = Widget()\n    print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Now there are  2 widgets\nNow there are 6 widgets\nNow there are  6 widgets\nNow there are  7 widgets\n"
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nEXAMPLE 10.16\n'''\ncount = 0\nclass Widget:\n    def __init__(self):\n        global count\n        count += 1\n    def numWidgets(self):\n        global count\n        return count\n        \nw = Widget()\nx = Widget()\nprint  \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n    w = Widget()\n    x = Widget()\n    y = Widget()\n    z = Widget()\n    print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Now there are  2 widgets\nNow there are 6 widgets\nNow there are  6 widgets\nNow there are  7 widgets\n"
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}