Errata File "OOP via F90"

Chia sẻ: Tan Giang | Ngày: | Loại File: PDF | Số trang:0

0
64
lượt xem
2
download

Errata File "OOP via F90"

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Errata File "OOP via F90" This is only the beginning of an errata for the text based on comments from readers. As of June 2004 I have not yet verified the suggested errors. I hope to review them when I start the summer break in July 2004. Please feel free to send your comments. Prof. Akin The Errata will cite pages in the published book, which will differ from the PDF drafts here, especially for Chapter 9. ------------------------------- 1 -------------------------------------One reviewer suggests that the source code line numbers should be moved from the left margin to the right margin. This...

Chủ đề:
Lưu

Nội dung Text: Errata File "OOP via F90"

  1. Errata File "OOP via F90" This is only the beginning of an errata for the text based on comments from readers. As of June 2004 I have not yet verified the suggested errors. I hope to review them when I start the summer break in July 2004. Please feel free to send your comments. Prof. Akin The Errata will cite pages in the published book, which will differ from the PDF drafts here, especially for Chapter 9. ------------------------------- 1 -------------------------------------- One reviewer suggests that the source code line numbers should be moved from the left margin to the right margin. This will be done in any future release. ------------------------------- 2 -------------------------------------- Date: Fri, 4 Jul 2003 19:40:51 +0100 From: Alistair Mills To: akin@rice.edu Subject: Object oriented Fortran 90 Professor Akin I have just come across your very interesting book. I think that this is a book which has been waiting to be written! It is very interesting. I hope that you will not be offended by what I have to say. It is intended to be constructive. Your book is a very readable account of the subject, and is much more comprehensible than other things which I have read on the same matter! I do not know how you can find time to be a professor at a major school of engineering, do research, and write such things also! I have also found your web site, and I have found that you have indexes to the figures and the files. This is very helpful, as it was taking me time to work out which source code files correspond to the text. I found that the page numbers are not correct, but the figure numbers are approximately correct. For example, I have changed the page numbers for chapter 7, and a couple of the figure numbers. I will send you a complete updated index, if you are interested. That would then agree with the published page numbers. class_Stack.f90 7.2 160 *** See expanded list below *** stack_check.f90 7.3 161 class_Queue.f90 7.5 163 queue_check.f90 7.6 165 singly_linked_list.f90 7.10 169 Test_SLL_Integers.f90 7.11 171 Integer_Objects.f90 7.12 172 doubly_linked_list.f90 7.14 173 Test_DLL_Integers.f90 7.15 175 random_access.f90 7.16 176 interface_Queue Section_7.3 exceptions.f90 none object_type.f90 none I have also observed a couple of coding errors. There is quite a serious one in chapter 7. Test_DLL_Integers.f90 7.15 175
  2. Line 156 should read as follows: point_to_Obj_3 => Get_Ptr_to_Obj (container, Obj_3) ! There is also an error on the printed version on this line [20] and [21] Rather than: point_to_Obj_3 = Get_Ptr_to_Obj (container, Obj_3) The program fails at run time when using both DVF 6.6 and Intel 7.0. The error is a common one. The assignment statement on the original attempts to copy the contents of the object at the end of the pointer to the contents of the object at the end of point_to_Obj_3. As there is nothing yet on the end of point_to_Obj_3, the run time system fails with an access violation. There is also an error in chapter 4. Line 19 of passing_types.f90 is "call by value". Fortran only does call by address. If you change line 19 to eliminate the additional parentheses, then the results are different. The parentheses force the creation of a temporary location containing a copy of Input_val. The value in the temporary location is changed, but this is not copied back to the source ie Input_Val. So the effect is the same, although the reasons are different. ! pass by value call No_Change ( Input_Val ) ! Use but do not change print *, "After No_Change it is ", Input_Val If you find what I have to say constructive, then I may have more when I have completed reading the book! With best wishes Alistair Mills ------------------------------- 3 te: Tue, 8 Jul 2003 20:10:48 +0100 From: Alistair Mills To: 'Ed Akin 221 Cox x4879' Subject: RE: Object oriented Fortran 90 Prof Akin Thanks for pointing out the link, and thank you for including my comments. Here is my version of the source code index. Alistair Source Figure Page hello.c 1.3 09 hello.cpp 1.3 09 hello.f90 1.3 09 hello.m 1.3 09 Math_Constants.f90 2.1 29 Fibonacci.f90 2.6 34 create_a_type.f90 Section_2.2 29 use_a_type.f90 Section_2.2 30 Geometric_Classes.f90 3.3,3.4 39
  3. Test_Geometry.f90 3.3,3.4 40 class_Date.f90 3.6 42 Test_Date.f90 3.7 43 class_Person.f90 3.9 44 Test_Date_Person.f90 3.10 45 class_Student.f90 3.12 46 Test_Student.f90 3.13 47 class_Rational.f90 3.15 49 Test_Rational.f90 3.16 52 generic_geometry.f90 none generic_geometry_2.f90 none arithmetic.f90 4.1 61 do_for.f90 4.2 65 array_index.f90 4.3 65 more_or_less.f90 4.4 70 if_else.f90 4.5 70 and_or_not.f90 4.6 71 clip.f90 4.7 79 maximum.f90 4.8 80 cpu_time.f90 4.10 82 exceptions.f90 4.11 83 passing_types.f90 4.12 86 string_use.f90 4.13 89 string_or_integer.f90 4.14 90 upper_lower.f90 4.15,4.16 91 struct_access.f90 4.17 96 fractions.f90 4.18 97 test_overload.f90 4.19 98 pt_expression.f90 4.20 101 linear_fit.f90 4.21 106 sort_reals.f90 4.23 109 sort_string.f90 4.24 110 integer_sort.f90 4.25 111 test_bubble.f90 4.27 113 cases.f90 none vector_norm.f90 none array_pointer.f90 none interp_vs_compil.f90 none interface 5.2 122 class_Drill.f90 5.3 123 test_Drill.f90 5.4 124 class_Angle.f90 5.6 126 class_Position_Angle.f90 5.8 130 class_Global_Position.f90 5.10 132 class_Great_Arc.f90 5.12 139 GPS_library.f90 5.13 135 non_poly_pos_ang.f90 none 6.5 139 6.6 140 6.7 141 6.8 143 6.9 143 6.10 144 6.11 145
  4. 6.12 146 6.13 147 6.14 148 6.15 149 6.16 150 Test_Is_A_Member.f90 6.17 153 member_1_class.f90 6.18 153 member_2_class.f90 6.19 154 is_a_member_class.f90 6.20 155 class_Stack.f90 7.1 160 stack_check.f90 7.3 161 class_Queue.f90 7.5 163 queue_check.f90 7.6 165 singly_linked_list.f90 7.10 169 Test_SLL_Integers.f90 7.11 171 Integer_Objects.f90 7.12 172 doubly_linked_list.f90 7.14 173 Test_DLL_Integers.f90 7.15 175 random_access.f90 7.16 176 interface_Queue Section_7.3 exceptions.f90 none object_type.f90 none trans_opt.f90 8.1 191 Matrix_Operators.f90 none array_demo.f90 none display_real.f90 none elem_type_data_class.f90 9.1 210 memory_leak.f90 9.2 212 No_Copy_Reallocate.f90 9.4 214 9.6 219 System_Constants.f90 none ------------------------------- 4 --------------------------------- Date: Sun, 20 Jun 2004 03:56:44 -0700 (PDT) From: Wayne B'Rells To: akin@rice.edu Subject: Errata for "OO Programming via Fortran 90/95"? Dear Dr. Akin: I recently picked up your book on OO programming with Fortran 90/95. I have only read up to section 2.4, but have discovered a few errors in the text and in the sample programs. Specifically, line [19] of the Fibonacci example (Figure 2.6 ) references 'num%exists', which does not exist in the definition of the 'Fibonacci_Number' type. This new logical variable is also mentioned in the text, but not seem to be USED in the code. Would you, perhaps, have an Errata listing for your book? (Such a listing of corrections would make it a bit easier to follow some of your examples...) BTW, I will be converting many of your examples so they can be compiled with the 'F' compiler. This compiler seems to include all the new F90/95 features, but leaves out those F77 constructs which are now redundant. Hoping to hear from you,
  5. Wayne B'Rells Schenectady, NY ------------------------------- 5 --------------------------------- Date: Mon, 21 Jun 2004 05:16:37 -0700 (PDT) From: Wayne B'Rells To: Ed Akin 221 Cox x4879 Subject: Re: Errata for "OO Programming via Fortran 90/95"? Dr. Akin: I was able able to look at the 'errata', but did not notice my specific problem. On the other hand, the code on the CD was correct in that it did NOT include any reference to "num%exists". FYI, the homepage for the F compiler is: http://www.swcp.com/~walt/ As I see it, the major advantages of the F compiler are: 1) It is free. 2) It is available for both the PC and Unix. 3) It FORCES users to strictly adhere to Fortran 90/95 coding practices and to explicitly declare all variables, access privileges, etc. This seems particularly desirable from a pedagogical point of view. My "conversion" of the Fibonaccci example to F did suggest a few places where the code could be "cleaned up" a bit: 1) The "implicit none" statements in the contained functions of the "class_Fibonacci_Number" module were flagged as errors by the F compiler. According to the Metcalf & Reid book ("Fortran 90/95 Explained"): "...and if there is an implicit none statement there must be no other implicit statement in the scoping unit." In other words, the implicit none statement at the module level is inherited by the contained functions via host association. (I suspect that many F90 compilers just treat the extra implicit none statements as redundant and ignore them...) 2) In F, all contained functions must be explicitly given the 'public' or 'private' access attribute. Therefore, I had to add 'new_Fibonacci_Number' to the list of public module functions. 3) In F, the 'intent' of all dummy arguments must be specified. Therefore, I had to add "intent(in)" to the list of attributes for the 'max' argument in the 'new_Fibonacci_Number' function. These were the major modifications that I found necessary. The minor changes included: 1) Putting only one statement per line 2) Changing ' to ". 3) Putting output formats directly in the write statements Best wishes, Wayne------------------------------- 6 ---------------------------------
  6. Object Oriented Programming via Fortran 90/95 Ed Akin Rice University Mechanical Engineering and Materials Science Department Houston, Texas May 29, 2001 ­ Draft # 4.2, Copyright c 2001, All rights reserved.
  7. ii
  8. Contents Preface vii 1 Program Design 1 1.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2. Problem Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3. Modular Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4. Program Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.4.1. Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.4.2. Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.4.3. Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.4.4. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4.5. Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4.6. Dynamic Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.5. Program evaluation and testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6. Program documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.7. Object Oriented Formulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.8. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 Data Types 23 2.1. Intrinsic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.2. User Defined Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3. Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.4. Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.5. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3 Object Oriented Programming Concepts 33 3.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2. Encapsulation, Inheritance, and Polymorphism . . . . . . . . . . . . . . . . . . . . . 34 3.2.1. Example Date, Person, and Student Classes . . . . . . . . . . . . . . . . . . . 37 3.3. Object Oriented Numerical Calculations . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.3.1. A Rational Number Class and Operator Overloading . . . . . . . . . . . . . . 39 3.4. Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.5. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4 Features of Programming Languages 51 4.1. Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.2. Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.3. Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.3.1. Explicit Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.3.2. Implied Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.3.3. Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.4. Subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 ­2001 J.E. Akin c iii
  9. 4.4.1. Functions and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4.4.2. Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.4.3. Bit Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.4.4. Exception Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.5. Interface Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4.6. Characters and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4.7. User Defined Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4.7.1. Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 4.7.2. User Defined Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4.8. Pointers and Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4.8.1. Pointer Type Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4.8.2. Pointer Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.8.3. Using Pointers in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.8.4. Pointers and Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.9. Accessing External Source Files and Functions . . . . . . . . . . . . . . . . . . . . . 89 4.10. Procedural Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 4.10.1. Fitting Curves to Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 4.10.2. Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 4.11. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5 Object Oriented Methods 103 5.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 5.2. The Drill Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 5.3. Global Positioning Satellite Distances . . . . . . . . . . . . . . . . . . . . . . . . . . 106 5.4. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 6 Inheritance and Polymorphism 119 6.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6.2. Example Applications of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.2.1. The Professor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.2.2. The Employee and Manager Classes . . . . . . . . . . . . . . . . . . . . . . . 121 6.3. Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6.3.1. Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 6.3.2. Subtyping Objects (Dynamic Dispatching) . . . . . . . . . . . . . . . . . . . 130 6.4. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 7 OO Data Structures 135 7.1. Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 7.2. Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 7.3. Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 7.4. Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 7.4.1. Singly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 7.4.2. Doubly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 7.5. Direct (Random) Access Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 7.6. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 8 Arrays and Matrices 155 8.1. Subscripted Variables: Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 8.1.1. Initializing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 8.1.2. Intrinsic Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 8.1.3. Colon Operations on Arrays (Subscript Triplet) . . . . . . . . . . . . . . . . . 159 8.1.4. Array Logical Mask Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 163 8.1.5. User Defined Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 8.1.6. Connectivity Lists and Vector Subscripts . . . . . . . . . . . . . . . . . . . . 166 ­2001 J.E. Akin c iv
  10. 8.1.7. Component Gather and Scatter . . . . . . . . . . . . . . . . . . . . . . . . . . 168 8.2. Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 8.2.1. Matrix Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 8.2.2. Inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 8.2.3. Factorizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 8.2.4. Determinant of a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 8.2.5. Matrix Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 8.2.6. Computation with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 8.3. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 9 Advanced Topics 181 9.1. Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 9.2. Subtyping Objects (Dynamic Dispatching) . . . . . . . . . . . . . . . . . . . . . . . . 183 9.3. Non-standard Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 A Bibliography 187 B Fortran 90 Overview 191 B.1. List of Language Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 B.2. Alphabetical Table of Fortran 90 Intrinsic Routines . . . . . . . . . . . . . . . . . . . 17 B.3. Syntax of Fortran 90 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 C Selected Exercise Solutions 47 C.1. Problem 1.8.1 : Checking trigonometric identities . . . . . . . . . . . . . . . . . . . 47 C.2. Problem 1.8.2 : Newton-Raphson algorithm . . . . . . . . . . . . . . . . . . . . . . 47 C.3. Problem 1.8.3 : Game of life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 C.4. Problem 2.5.1 : Conversion factors . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 C.5. Problem 3.5.3 : Creating a vector class . . . . . . . . . . . . . . . . . . . . . . . . . 50 C.6. Problem 3.5.4 : Creating a sparse vector class . . . . . . . . . . . . . . . . . . . . . 56 C.7. Problem 4.11.1 : Count the lines in an external file . . . . . . . . . . . . . . . . . . . 61 C.8. Problem 4.11.3 : Computing CPU time useage . . . . . . . . . . . . . . . . . . . . . 62 C.9. Problem 4.11.4 : Converting a string to upper case . . . . . . . . . . . . . . . . . . . 62 C.10. Problem 4.11.8 : Read two values from each line of an external file . . . . . . . . . . 63 C.11. Problem 4.11.14 : Two line least square fits . . . . . . . . . . . . . . . . . . . . . . . 63 C.12. Problem 4.11.15 : Find the next available file unit . . . . . . . . . . . . . . . . . . . 65 C.13. Problem 5.4.4 : Polymorphic interface for the class ‘Position Angle’ . . . . . . . . . 66 C.14. Problem 6.4.1 : Using a function with the same name in two classes . . . . . . . . . . 67 C.15. Problem 6.4.3 : Revising the employee-manager classes . . . . . . . . . . . . . . . . 67 D Companion C++ Examples 69 D.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 E Glossary of Object Oriented Terms 77 F Subject Index 0 G Program Index 1 ­2001 J.E. Akin c v
  11. Preface There has been an explosion of interest in, and books on object-oriented programming (OOP). Why have yet another book on the subject? In the past a basic education was said to master the three r’s: reading, ’riting, and ’rithmetic. Today a sound education in engineering programming leads to producing code that satisfy the four r’s: readability, reusability, reliability, and really-efficient. While some object-oriented programming languages have some of these abilities Fortran 90/95 offers all of them for engineering applications. Thus this book is intended to take a different tack by using the Fortran 90/95 language as its main OOP tool. With more than one hundred pure and hybrid object-oriented languages available, one must be selective in deciding which ones merit the effort of learning to utilize them. There are millions of Fortran programmers, so it is logical to present the hybrid object-oriented features of Fortran 90/95 to them to update and expand their programming skills. This work provides an introduction to Fortran 90 as well as to object-oriented programming concepts. Even with the current release (Fortran 95) we will demonstrate that Fortran offers essentially all of the tools recommended for object-oriented programming techniques. It is expected that Fortran 200X will offer additional object-oriented capabilities, such as declaring ”extensible” (or virtual) functions. Thus, it is expected that the tools learned here will be of value far into the future. It is commonly agreed that the two decades old F77 standard for the language was missing several useful and important concepts of computer science that evolved and were made popular after its release, but it also had a large number of powerful and useful features. The following F90 standard included a large number of improvements that have often been overlooked by many programmers. It is fully compatible with all old F77 standard code, but it declared several features of that standard as obsolete. That was done to encourage programmers to learn better methods, even though the standard still supports those now obsolete language constructs. The F90 standards committee brought into the language most of the best features of other more recent languages like Ada, C, C++, Eiffel, etc. Those additions included in part: structures, dynamic memory management, recursion, pointers (references), and abstract data types along with their supporting tools of encapsulation, inheritance, and the overloading of operators and routines. Equally important for those involved in numerical analysis the F90 standard added several new features for efficient array operations that are very similar to those of the popular M ATLAB environment. Most of those features include additional options to employ logical filters on arrays. All of the new array features were intended for use on vector or parallel computers and allow programmers to avoid the bad habit of writing numerous serial loops. The current standard, F95, went on to add more specific parallel array tools, provided “pure” routines for general parallel operations, simplified the use of pointers, and made a few user friendly refinements of some F90 features. Indeed, at this time one can view F90/95 as the only cross-platform international standard language for parallel computing. Thus Fortran continues to be an important programming language that richly rewards the effort of learning to take advantage of its power, clarity, and user friendliness. We begin that learning process in Chapter 1 with an overview of general programming techniques. Primarily the older “procedural” approach is discussed there, but the chapter is closed with an outline of the newer “object” approach to programming. An experienced programmer may want to skip directly to the last section of Chapter 1 where we outline some object-oriented methods. In Chapter 2, we introduce the concept of the abstract data types and their extension to classes. Chapter 3 provides a fairly detailed introduction to the concepts and terminology of object-oriented programming. A much larger supporting glossary is provided as an appendix. For the sake of completeness Chapter 4 introduces language specific details of the topics discussed in ­2002 c J.E. Akin i
  12. the first chapter. The Fortran 90/95 syntax is used there, but in several cases cross-references are made to similar constructs in the C++ language and the M ATLAB environment. While some readers may want to skip Chapter 4, it will help others learn the Fortran 90/95 syntax and/or to read related publications that use C++ or M ATLAB. All of the syntax of Fortran 90 is also given in an appendix. Since many Fortran applications relate to manipulating arrays or doing numerical matrix analysis, Chapter 5 presents a very detailed coverage of the powerful intrinsic features Fortran 90 has added to provide for more efficient operations with arrays. It has been demonstrated in the literature that object- oriented implementations of scientific projects requiring intensive operations with arrays execute much faster in Fortran 90 than in C++. Since Fortran 90 was designed for operations on vector and parallel machines that chapter encourages the programmer to avoid unneeded serial loops and to replace them with more efficient intrinsic array functions. Readers not needing to use numerical matrix analysis may skip Chapter 5. Chapter 6 returns to object-oriented methods with a more detailed coverage of using object-oriented analysis and object-oriented design to create classes and demonstrates how to implement them as an OOP in Fortran 90. Additional Fortran 90 examples of inheritance and polymorphism are given in Chapter 7. Object-oriented programs often require the objects to be stored in some type of “container” or data structure such as a stack or linked-list. Fortran 90 object-oriented examples of typical containers are given in Chapter 8. Some specialized topics for more advanced users are given in Chapter 9, so beginning programmers could skip it. To summarize the two optional uses of this text; it is recommended that experienced Fortran program- mers wishing to learn to use OOP cover Chapters 2, 3, 6, 7, 8, and 9, while persons studying Fortran for the first time should cover Chapters 1, 2, 3, and. Anyone needing to use numerical matrix analysis should also include Chapter 5. A OO glossary is included in an appendix to aid in reading this text and the current literature on OOP. Another appendix on Fortran 90 gives an alphabetical listing on its intrinsic routines, a subject based list of them, a detailed syntax of all the F90 statements, and a set of example uses of every statement. Selected solutions for most of the assignments are included in another appendix along with comments on those solutions. The final appendix gives the C++ versions of several of the F90 examples in the text. They are provided as an aid to understanding other OOP literature. Since F90 and M ATLAB are so similar the corresponding M ATLAB versions often directly follow the F90 examples in the text. Ed Akin, Rice University, 2002 Acknowledgements We are all indebted to the hundreds of programmers that labor on various standards committees to con- tinually improve all programming languages. Chapter 1 is a modification of introductory programming notes developed jointly with Prof. Don Johnson at Rice University. I would like to thank Prof. Tinsley Oden and the Texas Institute for Computational Mathematics for generously hosting my sabbatical leave where most of this work was developed, and Rice University for financing the sabbatical. Special thanks go to my wife, Kimberly, without whose support and infinite patience this book would not have been completed. Source Codes All of the example programs and selected solutions are included on the CD-ROM provide with the book. To be readable on various platforms they have been written with the ISO9660 standard format. Additional files are provided to relate the ISO standard short filenames to the full length program names used in the book. Of course, the source files will have to be processed through a Fortran 90 or 95 or 2000 compiler to form executables. All of the figures are also provided as encapsulated Postscript (tm) files. ­2002 c J.E. Akin ii
  13. Index  , 53, 56 allocate, 42 =, 53 ALLOCATE statement, 15, 74, 92, 181, 183 =, 53 ALLOCATED function, 15, 181, 183 Ò, 122 allocation status, 74, 181, 258 *, 10, 56 AND operand, 42, 63, 104 **, 56 AND operator, 53 +, 53, 56 ANINT function, 162 /, 10, 56 ANY function, 162, 181 ::, 25, 53 any mask element true, 162 =, 10 arc cosine, 56 = , 143 arc sine, 56 %, 51, 143 arc tangent, 56 &, 10, 34, 37, 42 arccosine, 162 /=, 53 arcsine, 162 ==, 53 arctangent, 162 =>, 121 arctangent for complex number, 162 area, 34 ABS function, 56, 162, 250 argument, 285 absolute value, 56, 162 inout, 69 abstract class, 285 input, 69 abstract data type, 15, 23, 27, 285 interface, 75 abstraction, 19, 27, 285 none, 69 access, 36 access operation, 142 number of, 75 access restriction, 19 optional, 75, 76 accessibility, 19 order, 75 accessor, 18, 285 output, 69 ACHAR function, 77, 80 rank, 75 ACOS function, 56, 162 returned value, 75 actual argument, 56 type, 75 Ada, 15, 33 array, 26, 60, 66, 82, 135, 149, 285 addition, 56 allocatable, 156 ADJUSTL function, 77 assumed shape, 76 ADJUSTR function, 77 automatic, 89, 156 ADT, see abstract data type Boolean, 164 ADVANCE specifier, 42, 102 constant, 156 agent, 18 dummy dimension, 156 AIMAG function, 56, 162 flip, 166 AINT function, 56, 162 mask, 164, 179 algorithm, 51 of pointers, 135 ALL function, 162, 255 rank, 76, 155, 157, 166 all mask elements true, 162 rectangular, 166 allocatable array, 156, 157, 285 reshape, 155 ALLOCATABLE attribute, 183 shape, 155 ALLOCATABLE statement, 15 shift, 168 1
  14. size, 155 ISHFTC, 74 total, 162 MVBITS, 74 unknown size, 76 NOT, 74 variable rank, 156 TRANSFER, 74 array operations, 159 bit manipulation, 74 array pointer, 285 blanks array shape vector, 162 all, 77 ASCII character set, 23, 76, 77, 98, 159 leading, 77 ASIN function, 56, 162 trailing, 77 assembly language, 15 Boolean type, 53 assignment operator, 10, 39, 189, 285 Boolean value, 23 assignment statement, 285 bottom-up, 4 ASSOCIATED function, 15, 75, 88, 130, 132, boundary condition, 192 181 bounds, 155 association, 285 bubble sort, 92, 94 associative, 172, 173 ordered, 95 asterisk (*), 58 bug, 9 ATAN function, 56, 162 ATAN2 function, 13, 56, 162 C, 1, 33, 52 attribute, 103, 104, 107, 119, 123, 192, 285 C++, 1, 10, 14, 24, 33, 52, 58, 59, 76, 81, 102, name, 19 121 private, 27, 123 call by reference, 286 public, 27 call by value, 286 terminator, 25 CALL statement, 42, 76, 86, 89, 92, 97, 121, attribute terminator, 25 123, 124, 131, 137, 140, 142, 143, attributes, 19, 27 149 automatic array, 89, 156, 157, 285 CASE DEFAULT statement, 63, 188 automatic deallocation, 29 CASE statement, 63, 188, 272 cases, 62 BACKSPACE statement, 75 CEILING function, 56, 162 bad style, 158 central processor unit, 72 base 10 logarithm, 56, 162 CHAR function, 77 base class, 119, 286 character, 81 behavior, 104, 107 case change, 80 binary file, 159 control, 76 binary operator, 286 from number, 80 binary read, 268 functions, 77 binary write, 183 non-print, 76, 102 bit strings, 76 clear, 74 to number, 80 extract, 74 character set, 23 set, 74 CHARACTER type, 23, 26, 53 shift, 74 chemical element, 25 test, 74 chemical element, 128 bit function circuits, 166 BIT SIZE, 74 circular shift, 168 BTEST, 74 circular-linked list, 185 IAND, 74 class, 15, 19, 33, 286 IBCLR, 74 base, 18 IBITS, 74 Date, 118, 121 IBSET, 74 derived, 18 IEOR, 74 Drill, 103 IOR, 74 Employee, 123 ISHFT, 74 Geometric, 118 ­2002 c J.E. Akin 2
  15. Global Position, 112 column major order, 177 Great Arc, 112 column matrix, 170 hierarchy, 33 column order, 158 instance, 33 comma, 98 iterator, 192 comment, 1, 2, 7, 9, 12, 51, 52 Manager, 123, 133 commutative, 100, 172, 173 Person, 118, 121 compiler, 10, 15, 90 polymorphic, 131 complex, 10, 81, 161 Position Angle, 107, 112 complex conjugate, 56 Professor, 121 COMPLEX type, 23, 24, 53 sparse vector, 258 component Student, 118, 121 assignment, 82 class attribute, 286 declaring, 82 class code initializing, 82 class Angle, 112 interpretation, 82 class Circle, 34 referencing, 82 class Date, 37 syntax, 82 class Employee 1, 122 component selector, 34, 37, 42 class Employee 2, 123 composition, 34, 36, 190, 194 class Employee 3, 124 concatanate, 122 class Fibonacci Number, 29 conditional, 7–9, 11, 51, 58 class Manager 1, 123 conformable, 172 class Manager 2, 123 CONJG function, 56, 162 class Manager 3, 124 conjugate of complex number, 162 class Object, 143 connectivity, 166 class Person, 37 constant array, 156 constructor, 18, 29, 34, 123, 132, 133, 136, 149, class Position Angle, 270 255, 286 class Professor, 121 default, 18 class Queue, 140 intrinsic, 18, 26, 34, 39 class Rational, 42 manual, 36 class Rectangle, 34 public, 37 class sparse Vector, 258 structure, 26 class Stack, 137 container, 135 class Student, 37 container class, 286 class Vector, 257 CONTAINS statement, 29, 33, 34, 72, 75, 85 Drill, 104 continuation marker, 10 elem type data class, 181 control key, 78 Global Position, 112 conversion factors, 29 Great Arc, 112 convert real to complex, 162 Is A Member Class, 131 convert to integer, 162 Member 1 Class, 131 convert to real, 162 Member 2 Class, 131 COS function, 56, 162, 249 Position Angle, 112 COSH function, 56, 162 class descriptor, 286 cosine, 56, 162 class inheritance, 286 COUNT function, 162, 259, 263 clipping function, 14, 69 count-controlled DO, 12, 13 CLOSE statement, 74, 92, 97, 271 CPU, see central processor unit CMPLX function, 162 curve fit, 90 Coad/Yourdon method, 18 CYCLE statement, 65, 66, 260, 263 code reuse, 194 colon operator, 56, 60, 61, 77, 156, 159, 163, data abstraction, 19 166, 267 data hiding, 36, 286 syntax, 56 data structure, 135 ­2002 c J.E. Akin 3
  16. data types, 10 dynamic data structures, 38 intrinsic, 23 dynamic dispatching, 130 user defined, 23 dynamic memory, 74, 181 date, 99, 265 allocation, 15 DATE AND TIME intrinsic, 265 de-allocation, 15 deallocate, 18, 42, 181 management, 15 DEALLOCATE statement, 15, 74, 183 dynamic memory management, 88 deallocation, 287 debugger, 17, 287 e, 25 debugging, 16 EBCDIC character set, 23, 76 declaration statement, 287 efficiency, 194 default case, 63 Eiffel, 18 default constructor, 287 electric drill, 103 default value, 29 ELSE statement, 42, 63, 66 defined operator, 287 encapsulate, 15 dereference, 58 encapsulation, 27, 33, 192, 194, 287 dereferencing, 287 end off shift, 168 derived class, 119 end-of-file, 75 derived type, 15, 23, 287 end-of-record, 75 component, 82 end-of-transmission, 77 EOF, see end-of-file nested, 82 EOR, see end-of-record print, 84 EOT, see end of transmission read, 84 EPSILON function, 162 destructor, 29, 34, 41, 48, 254, 287 equation determinant, 175 number, 169 diagonal matrix, 170 EQV operator, 53 dimension error checking, 18 constant, 157 exception, 74, 287 extent, 155 exception handler, 74 lower bound, 155 exception handling, 18 upper bound, 155 exercises, 21, 31, 48, 99, 118, 132, 154, 178, distributive, 173 195 division, 56 EXIT statement, 65, 66, 251, 260, 262, 263, division remainder, 56 265, 269, 272, 273 DO statement, 29, 58, 61 EXP function, 56, 162, 250 DO WHILE statement, 66 explicit interface, 288 DO-EXIT pair, 67, 68 explicit loop, 11 documentation, 17 exponent range, 24 domain, 19 exponential, 56, 162 dot product, 162 exponentiation, 56 dot product, 12 expression, 10, 51, 52, 88 DOT PRODUCT intrinsic, 12, 162 external double, 24 file, 89 DOUBLE PRECISION type, 23, 24, 53 subprogram, 89 doubly linked list, 149 external file, 288 drop fraction, 56 external procedure, 288 dummy argument, 57, 72, 287 external subprogram, 76 dummy array, 287 dummy dimension, 157 factorization, 174, 175, 179 dummy dimension array, 156 FALSE result, 62 dummy pointer, 287 Fibonacci number, 29 dummy variable, 72 file, 74 dynamic binding, 18, 287 access, 151 ­2002 c J.E. Akin 4
  17. binary, 183 dot Vector, 255, 259 column count, 99 Drill , 104, 106 direct access, 150 D L new, 149 I/O, 151 el by el Mult, 259 internal, 80 equality operator point, 188 line count, 99 equal to Object, 143 modify, 151 gcd, 42, 101 random, 151 getEmployee, 123, 124 random access, 150 getName, 123 read status, 99 getNameE, 122, 124 record number, 150 getNameM, 123, 124 scratch, 183 getRate, 122, 124 unit number, 100 GetX, 188 FILE= specifier, 271 GetY, 188 finite difference method, 179 get Arc, 112 finite element, 43 Get Capacity of Q, 140 finite element analysis, 181 get Denominator, 42 flip, 163, 166 get element, 260 float, 53 Get Front of Q, 140 floating point, see real, 23, 24, 179 get item cost, 264 FLOOR function, 56, 162 get item count, 264 flow control, 11, 51, 58 get item delay, 264 forever loop, see infinite loop get item name, 264 FORM= specifier, 271 get Latitude, 112 FORMAT statement, 34, 112 Get Length of Q, 140, 142 function, 7, 9, 51, 68 get Longitude, 112 argument, 13, 15 get menu, 273 extensible, 130 get mr rate, 104 generic, 183 get next io unit, 102, 269 INTEGER, 140 Get Next Unit, 98 LOGICAL, 137, 140 get Numerator, 42 recursive, 42, 101 Get Obj at Ptr, 149 result, 69 get Person, 37 return, 13 get person, 37 TYPE, 137, 140 Get Ptr to Obj, 149 variable, 15 get torque, 104 function code Global Position , 112 Add, 29 Great Arc , 112 add Rational, 42 initialize item, 264 add Real to Vector, 253 inputCount, 92, 265 add Vector, 253 Int deg, 112 Angle , 112 Int deg min, 112 assign, 253 Int deg min sec, 112 circle area, 34 is equal to, 42, 255, 260 clip, 69 is item empty, 264 convert, 42 Is Q Empty, 140 copy Rational, 42 is Q Empty, 142 copy Vector, 254 Is Q Full, 140 Create Q, 140 is Q Full, 142 Date , 37 is Stack Empty, 137 Decimal min, 112 is Stack Full, 137 Decimal sec, 112 is S L empty, 143 Default Angle, 112 largest index, 260 ­2002 c J.E. Akin 5
  18. length, 260 values of, 263 lengthnormalize Vector, 255 Vector , 255 less than Object, 143 Vector max value, 255, 263 make Person, 37 Vector min value, 255, 263 make Professor, 121 Vector mult real, 255 make Rational, 42 Vector To Sparse, 263 make Rectangle, 36 zero sparse, 263 make Stack, 137 function definition, 288 make Student, 37 FUNCTION statement, 29 make Vector, 253 Manager , 123, 124 Game of Life, 4 maximum, 70 Gamma, 25 mid value, 69 gather-scatter, 168 mult Fraction, 86 gcd, see greatest common divisor mult Rational, 42 generic function, 33, 34, 183, 288 new Fibonacci Number, 29 generic interface, 132 next generation, 251 generic linked list, 149 norm, 262 generic name, 34 normalize Vecto, 262 generic object, 42 pay, 123 generic operator, 288 payE, 122, 124 generic routine, 121 payM, 123, 124 generic subprogram, 76 Person, 121 geometric shape, 34 Person , 37 global positioning satellite, 106 pop from Stack, 137 global variable, 14, 72 GO TO statement, 64, 65 print, 121 GPS, see global positioning satellite Professor, 121 Graham method, 18 Rational, 42 graphical representation, 27, 118 Rational , 42 greatest common divisor, 42, 101 real mult Sparse, 262 greatest integer, 162 real mult Vector, 255 grid, 190 rectangle area, 34 rows of, 262 Has-A, 107, 194 setDataE, 122, 124 header file, 129 setDataM, 123, 124 heat transfer, 185 set Date, 37 Hello world, 7 set Lat and Long at, 112 hello world, 52, 100 size of, 262 hierarchie size Vector, 255 kind of, 18 Sparse mult real, 262 part of, 18 Student, 37, 121 High Performance Fortran, 195 Student , 37 horizontal tab, 77 subtract Real, 255 host association, 288 subtract Vector, 255 Hubbard, J.R., 36 Sub Sparse Vectors, 263 HUGE function, 162 Sum Sparse Vectors, 263 hyperbolic cosine, 56, 162 S L new, 143 hyperbolic sine, 56, 162 toc, 72 hyperbolic tangent, 56, 102, 162 to Decimal Degrees, 112 to lower, 80 I/O, see Input-Output to Radians, 112 IACHAR function, 77, 80 to upper, 80, 100, 266 ICHAR function, 77 values, 255 identity matrix, 178 ­2002 c J.E. Akin 6
  19. IF, 62 Add to Q, 140 nested, 62 assign, 131 if, 12 Create Q, 140 IF ELSE statement, 62 display, 131 IF statement, 29, 37, 42, 62 getName, 124 if-else, 12 Get Capacity of Q, 140 IF-ELSE pair, 63 Get Front of Q, 140 IF-ELSEIF, 130 Get Length of Q, 140 imaginary part, 56, 162 Init, 188, 190 IMPLICIT COMPLEX, 53 Is Q Empty, 140 IMPLICIT DOUBLE PRECISION, 53 Is Q Full, 140 IMPLICIT INTEGER, 52 is Stack Empty, 136 implicit loop, 12 is Stack Full, 136 IMPLICIT NONE, 26, 29 make Stack, 136 IMPLICIT REAL, 52 MyPrint, 188 implied loop, 60, 61, 156, 166 new, 131 INCLUDE line, 37, 42, 89 orthonormal basis, 257 INDEX function, 77, 80, 266, 273 pop from Stack, 136 indexed loop, 11 Position Angle , 270 infinite loop, 9, 68, 269 PrintPay, 123, 124 information hiding, 288 push on Stack, 136 inheritance, 18, 33, 34, 72, 119, 190, 193, 194, Remove from Q, 140 288 Set, 188 rename, 119 swap, 127 selective, 119 testing basis, 257 inherited, 37 interface operator, 188, 258 initialize random number, 162 interface operator ( ), 143 inner loop, 61 interface operator (*), 39 INQUIRE intrinsic, 92, 97, 102, 268, 269 interface operator (==), 143 INTERFACE OPERATOR block, 85, 86 INQUIRE statement, 75 INTERFACE OPERATOR statement, 166 instance, 33, 122, 288 interface prototype, 103, 104, 123 INT function, 162 INTERFACE statement, 34 integer, 10, 81, 161 internal file, 80, 289 integer nearest to real, 162 internal sub-programs, 72 INTEGER type, 23, 24, 53 internal subprogram, 251, 289 intent, 289 interpreter, 10, 15 in, 29, 100 intrinsic, 166 inout, 29 intrinsic constructor, 85, 98, 106, 136, 289 out, 100 intrinsic function, 12, 68 statement, 29 inverse, 178 INTENT attribute, 142 IOLENGTH result, 268 INTENT statement, 29, 58, 69, 93 IOSTAT= variable, 74, 75, 271 interface, 2, 6, 9, 13, 15, 27, 34, 75, 92, 104, Is-A, 106, 107, 124, 194 107, 121, 136, 189, 258, 289 ISO VARIABLE LENGTH STRING, 23 general form, 76 iterator, 143, 149, 191, 192, 289 human, 18 input/output, 18 keyword, 121, 289 prototype, 18 KIND intrinsic, 24 interface assignment, 258 Kind-Of, 107, 123 INTERFACE ASSIGNMENT (=) block, 86 interface block, 34, 76 largest integer, 56 interface body, 76 largest number, 162 interface code latitude, 106 ­2002 c J.E. Akin 7
  20. least integer, 162 abort, 66, 67 least squares, 90, 266, 267 breakout, 65 LEN function, 77, 80 counter, 59 LEN intrinsic, 77, 80 cycle, 65, 66 length exit, 59, 65, 66 line, 52 explicit, 58 name, 52 implied, 60 LEN TRIM function, 77 index, 100 LEN TRIM intrinsic, 77 infinite, 60, 67, 68 lexical operator, 94 nested, 61, 65 lexically pseudocode, 58 greater than, 77 skip, 65 less than, 77 until, 66, 67 less than or equal, 77 variable, 60 LGE function, 77 while, 66 LGT function, 77 loop construct, 59 library function, 16 loop control, 60, 158 line continuation, 100 loop index, 100 linear equations, 173, 174, 179, 184 loop variable, 11 linked list, 38, 87, 88, 142, 149, 289 lower triangle, 171, 174 doubly, 149 manual constructor, 85, 104 linked-list, 191 manual page, 17 linker, 16, 89, 289 mask, 161, 164, 165, 179, 259 list masks, 61 circular, 139, 185, 190 Mathematica, 51 doubly-linked, 88 mathematical constants, 25 empty, 149 mathematical functions, 56 length, 139 Matlab, 1, 10, 14, 52, 60, 68, 99, 102 singly-linked, 88 MATMUL intrinsic, 162, 173 LLE function, 77 matrix, 155, 170, 289 LLT function, 77 addition, 172 local name, 119 algebra, 155 LOG function, 56, 162 column, 170 LOG10 function, 56, 162 compatible, 172 logarithm, 68, 91, 162 determinant, 175 logical, 81 diagonal, 170 AND, 63 factorization, 174 equal to, 63 flip, 163 EQV, 63 identity, 174 greater than, 63 inverse, 89, 174 less than, 63 multiplication, 159, 172 NEQV, 63 non-singular, 174 NOT, 63 null, 170 operator, 63 skew symmetric, 171 OR, 63 solve, 89 logical expression, 11 sparse, 192 logical mask, 61 square, 170, 171 LOGICAL type, 23, 42, 137 symmetric, 171 long, 24 Toeplitz, 171 long double, 24 transpose, 159, 171 long int, 24 triangular, 171, 174 longitude, 106 tridiagonal, 179 loop, 5, 7–9, 11, 51, 58, 179 matrix addition, 177, 178 ­2002 c J.E. Akin 8
Đồng bộ tài khoản